Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Code Newbie > Submit Tutorials > C
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 02-24-2003, 01:06 PM   #1 (permalink)
anon
 
Posts: n/a
Starting out with C

This is a basic C tutorial. First
off lets discuss what is C. C is a low-level programming language. It was
created in around 1970 by Dennis M. Ritchie. You should have a compiler for this tutorial. I use cc and gcc but if you are on Windows
you can use Borland's C compiler. First off lets look at the basic syntax of C.

Code:
#include<stdio.h> //That calls a the stdio.h library for C. main() //starts the main function. { //opens the function printf("This is C!"); //prints This is C! } //closes the main function
First off see the ; after printf("This is C!"). Well there needs to be ; at the end of most lines code. Or else the C code will not work.
If you don;t already know // stands for a comment. It is good practice to comment your code. /* */ is also used for comments for
example

Code:
printf("This is C!"); /*Prints this is C! printf("it is!"); */
would only print This is C! Because printf("it is!"); is between the /* and the */. All C files that include C code that haven't been
compiled should be named something.c where something is the filename that you want. For some compilers if it's not a *.c file it wont
compile it! Now if you compiled a program with

Code:
printf("This is the first line!"); printf("This the second line!");
would write This is the first line!This is the second line!. Why? Just because you put it on different it doesn't move it over one line.
You need to write:

Code:
printf("This is the first line!\n"); printf("This the second line!");
the \n moves the next thing that will be printed over a line. In C \*something* can do something. Like \n moved text over a line well
there are other things that \*something* can do. so if you want to write something like:

Code:
printf("I love \!");
Well, Its going to think you mean \! not just \ as text! so it will try to find out what \! means and does instead of printing \! So what do
we do to write a \ to screen? \\

Code:
print("I love \\!");
\\ stands for \.
\? stands for ?
' stands for '
\" stands for "

Now, lets get into strings! First of all in programming there are 3 main types of data. Boolean, true or false,
number/variable, number, and string, which means a string of letters. But just "a" can be a string too. But all numbers can be a variable
or a string. Because you can have 902 as a variable. Or you can have 902 as a string by itself or as a part of a string like "Once there
was a great boy called Vlad who went by v902." the 902 in there isn't a variabiable. So lets make a very basic program where we declare a
string called myFirstString.

Code:
#include<stdio.h> main() { char myFirstString[100]="I love Strings!" printf("%s",myFirstString) }
Here we DECLARE myFirstString. We declare it by saying what it is. It is a string. As you can see it says char myFirsString. The char
stands for character. And when you say char something you make something a string. The [100] stands for how long the string can be. Here I
put 100 but it could be smaller or larger. Then we write ="I love Strings!". Which tells us what myFirstString is equal to. Then we print
myFirstString to the screen by doing printf("%s",myFirstString). The %s is there to reserve space for where myFirstString will be. For
example lets say we wanted to print to screen "The contents of myFirstString are: " and then here were the contents of myFirstString. We
would write

Code:
printf("The contents of myFirstString are: %s",myFirstString)
This will show The contents of myFirstString are: I love Strings!. Because we reserved a place where the contents of
myFirstString would go. Lets try something with reserving places

Code:
#include<stdio.h> main() { char myFirstString[100]="First String!"; char mySecondString[100]="Second String!"; printf("myFirstString: %s\nmySecondString: %s\n",myFirstString,mySecondString); }
I believe thats pretty self-explanatory. Now a string in C is actually an array of characters. First lets discuss what an array is. In an
array there can be infinite amount of characters. Imagine this. You have to make a program. The program has to hold every single person in
a bussiness names. Now how are we gonna do that? Make 3000 different strings?!?! No. We will make an array. Lets say we have an array
called myArray. And it holds the values, J, S, C and X. We could access those values easily by doing myArray[0] for J myArray[1] for S,
myArray[2] for C, etc.. Because computers start counting at 0 not 1. So here is what myArray looks like

myArray

0 1 2 3
J S C X

and strings are arrays, arrays of eltters, so myFirstString is actually

1 2 3 4 5 6 7 8 9 10 11 12 13
F i r s t S t r i n g !

Where myFirstString[0]=F and myFirstString[1]=i. So a string is actually an array of letters.

Now lets go onto numbers! While it was simple for strings because we only had char in numbers we have more ways to declare variables. We
have:

int-for small numbers, usually between 33,000 to -33,000 (much larger on current compiler, only old compilers are like this now.)
short-for short integers
long-for long intergers
float-floating point number. It can have a decimal in it and they are usually between 10^38(10 to the 38th power) and -10^38
double-double float, huge!

Lets try something now

Code:
#include<stdio.h> main() { int myVar, myVar2; myVar=20; myVar2=30; myVar=myVar+myVar2; printf("%i",myVar); }
Now it should print 50. We declared myVar and myVar2 integers because of the int. Then we said that myVar=20, and myVar=30. Then we said
myVar=20+30. and then printed myVar. As %s stood for string, %i is for integer. Now here is something you haven't learned before that you
should learn now as to save you some time debugging. You need to declare all your variables and strings in the very start of the code.
Like this wouldn't work:

Code:
#include<stdio.h> main() { int myVar; myVar=20; int myVar2; myVar2=30; myVar=myVar+myVar2; printf("%i",myVar); }
Because you declared myVar, then had a line of code, then declared another integer. You can't do this. ALL intergers and strings have to
be declared first then code can be written. This would work though:

Code:
#include<stdio.h> main() { int myVar=20; int myVar2=30; myVar=myVar+myVar2l; print("%i",myVar); }
Here are the very basics. I plan to write another tutorial, you may want to check that one out, it will cover loops, if then, functions,
and other more advanced things.
__________________
  Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 09:48 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 05:54 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle