Beruflich Dokumente
Kultur Dokumente
The Basics
The C code you write is called the SYNTAX. Syntax is a mixture of: C keywords like int, for and return. Constants and varia les ! more on these later. "#erators like + $arithmetic %addition%&, || $lo'ical %or%& and & $the %address of% o#erator&. Note that C is CAS( S(NS)T)*(+ )n other words, writin' in ca#itals as o##osed to lowercase makes a difference. Also, the amount of %white s#ace% you use in a C #ro'ram does not affect the way it,s com#iled. -ike .T/-, you can #ut as many s#aces in to make your #ro'rams more reada le.
Hello World
-et,s write and com#ile your first #ro'ram+ Ty#e the code shown in the screenshot into Note#ad $or emacs for 2N)X users& and save it with the .c extension.
Header Files
.eader files have the extension .h and the full filename follows from the #include directive. .eader files contain declarations to certain functions that you may or may not have used in your #ro'ram. 6or exam#le, the stdio.h file is re7uired if you have used functions like printf and scanf in your #ro'ram. /ore a out these two functions in the STAN8A48 )N32T AN8 "2T32T section. There are two ways to include a header file:
#include "stdio.h"
and
Co#yri'ht -o'ic "#tion 3vt. -td.
#include <stdio.h>
)f you use the dou le 7uote marks, it means that the directory you,re currently in, will e searched for first for the header file, efore any other directories are searched. )f you use the s7uare rackets, directories other than the one you,re currently in, will e searched for the header file.
9. You have to write the source code in C. You declare which header files you want to include within the source code. The source code must e saved with the extension .c. ?. Then you run the com#iler, which translates the source code into machine, or inary code, which the com#uter can understand. Com#uters do N"T understand C+ @. Sometimes the source code is still lackin' some #arts, so after 'oin' throu'h the com#iler, the code is #assed throu'h a -)NA(4. This asically %links% the source code to other li rary or o Bect files so that the final inary code is #roduced.
A statement is formed if you take an ex#ression and stick a semi!colon, ; , at the end of it+ 0asically, statements are instructions. .ere are some exam#les of statements:
x $ " + %; printf("&e 'ill learn about functions later()n"); int x* +* ,; <= more on %int% later =<
Statement locks, on the other hand, can contain a 'rou# of statements. The C com#iler com#iles the statement lock as if it was Bust one statement. To declare a statement lock you use the o#enin' race, - ut don,t for'et to close it with a corres#ondin' closin' race, . ++
Co#yri'ht -o'ic "#tion 3vt. -td.
Data T$&es
There are four main data ty#es: char, int, float and double.
Characters
%A%, % %, %0% and %a%, alon' with all the other characters from the key oard, are classed as char s. The com#uter refers to the char s numerically and uni7uely and each character has a numerical value. Fust for fun, co#y this exam#le and save it as a source file. Then com#ile it and the #ro'ram should dis#lay the character set. 8on,t worry if you can,t 7uite understand the code.
#include <stdio.h> int main() int i*0; for(i$0 ; i</1 ; i++) for(0$0 ; 0<"0 ; 0++) printf("2d$2c "* "0!i+0* "0!i+0); printf(")n"); . return 0; .
)t is assumed that the % locks% occur when there is no character with that numerical value. )ncidentally, the newline character has the value of 9G. )t is also assumed that H is the acks#ace character and 'oodness knows what has ha##ened to the characters with value 99 throu'h to 9@+ The maximum numerical value of a character is ?II ! after that, the #attern re#eats itself. 0ut if you do use a character data ty#e $like in character arrays&, when you assi'n a varia le with a character, make sure you surround the individual characters with sin'le 7uote marks. The dou le 7uote marks are used for strin's, which will e ex#lained much later+
#include <stdio.h> int main() Co#yri'ht -o'ic "#tion 3vt. -td.
char a*b*c*d*e; a $ 343; <= correct way =< <= a K %.% is wron' =< K ,e,L c K ,l,L d K ,l,L e K ,o,L char strin'9MN K %world%L <= more on strin's later =< #rintf$%OcOcOcOcOc OsPn%, a, , c, d, e, strin'9&L return GL Q
'ntegers
5hole num ers are inte'ers, as you could,ve 'uessed.
Coersion
)f you did want to divide two inte'ers to'ether to #roduce a decimal, you can use the a #rocess called C"(4S)"N. Take a look at this exam#le:
#include <stdio.h> int main() int a*b; float c; a $ 1; b $ 5; c $ (float)a b;
Co#yri'ht -o'ic "#tion 3vt. -td.
This exam#le mi'ht seem a little confusin' at first ! if that,s the case, i'nore the printf line for now. Anyway, the line that does the coersion usiness is:
c $ (float)a b; *aria les a and b are oth declared as inte'ers and have J and R assi'ned to them. c is a floatin' #oint num er. The (float) #art of the line, mentioned a ove, forces the floatin' #oint value of a b into c, which is already of the ty#e float )t is #ossi le to denote num ers in scientific notation, with the letter e ein' used to re#resent the
%ex#% utton on some calculators. )n 'eneral, xe+ means %x times $9G to the #ower of y&% where y can e #ositive or ne'ative. So ?IGG can e re#resented y /7e/ And G.GGC?H is 8/%e97 To learn a out the declaration of varia les and the in#ut and out#ut of the various data ty#es, see STAN8A48 )N32T AN8 "2T32T 0ut first, let,s move onto the asic arithmetic o#erators...
)rithmetic *&erators
The )ssignment *&erator
5hen you declare a varia le, you reserve a slot in the com#uter,s memory for value to e assi'ned to that varia le name, which is done y usin' the assi'nment o#erator. 6or exam#le, x $ "; takes the slot reserved for x and #uts the value " into it. 3retty o vious huh1+ Too o vious, that ) for'ot to mention anythin' a out the assi'nment o#erator in an earlier version of this site+ The assi'nment o#erator asically stores what ever value into the memory s#ace reserved for the varia le.
The less o vious one is the modulus o#erator. This is also known as the remainder o#erator and the conce#t in usin' it can e a little confusin' at first. No need to worry thou'h+ Su##ose you had x $ a 2 b; . This is asically tellin' the com#uter to work out how many times b 'oes into a and assi'n the 4(/A)N8(4 to x. So x $ 8 2 5; assi'ns " to x, since 5 'oes into 8 twice with " left over Not that confusin' after all then1+ The forthcomin' 7ui; should e a doddle+ 0e careful when mixin' o#erators. /ulti#lication, division and modulus are #erformed first, T.(N addition and su traction. 2se your common sense when it comes it the inclusion of rackets+ The stuff enclosed in rackets is worked out first, efore multi#lyin' etc.
+elational *&erators
6or us to make decisions, we act accordin' to a condition. )n the #revious exam#le, %it is rainin'% is a condition, where as %) will stay indoors% and %) will walk the do'% are results ased on the condition. 3ro'rammin' uses a similar conce#t. 5e can say:
Co#yri'ht -o'ic "#tion 3vt. -td.
%)f x #lus 9 is e7ual to I, then x e7uals J, else it is not e7ual to J%. The ta le elow sums u# the six relational o#erators used in C. (7ual to Not e7ual to -ess than -ess than or e7ual to Sreater than Sreater than or e7ual to
$ $ ( $ < < $ > > $
Note that the relation o#erators consist of a maximum of two characters with no s#ace etween them. Also, take care when usin' the %e7ual to% relational o#erator as #uttin' the assi'nment o#erator in is a common mistake.
,ogical *&erators
-o'ical o#erators allow you to com ine relation o#erators when it comes to writin' ex#ressions $or conditions& in if and else statements or when it comes to creatin' loo#s. 2sin' lo'ic o#erators allows you to %test% more than one condition. .ere are the three lo'ical o#erators and their 'eneral form: N"T AN8 "4
((expression) (expression") && (expression/) (expression") || (expression/)
And their truth ta les: $note that if ;ero is returned, the ex#ression can e thou'ht of as %false% and if one is returned, it,s %true%.& value returned from value returned from ex#ression N"T ex#ression 9 G G 9 value returned from value returned ex#ression9 ex#ression? 9 9 9 G G 9 G G
Co#yri'ht -o'ic "#tion 3vt. -td.
value returned from value returned ex#ression9 ex#ression? 9 9 9 G G 9 G G You,ll see some exam#les in the followin' sections.
T$&e -odifiers
The signed and unsigned #e$%ords
5hen you declare a varia le of the ty#e, int, y default, its value is S)SN(8. )n other words, the varia le could e #ositive or ne'ative. "n my machine, the minimum value of a si:ned int is !@?ERH and the maximum is @?ERH $K ?9I!9&. .owever, an unsi:ned int has the ran'e from G to RII@I $K?9R!9&.
Note that: ?9I K @?ERH ?9R K RII@R ?@9 K ?9JEJH@RJH ?@? K J?CJCRE?CR
#include <stdio.h> int main() printf(";i,e of int is 2d b+tes)n"* si,eof(int)); printf(";i,e of short int is 2d b+tes)n"* si,eof(short int)); printf(";i,e of lon: int is 2d b+tes)n)n"* si,eof(lon: int)); printf(";i,e of si:ned int is 2d b+tes)n"* si,eof(si:ned int)); printf(";i,e of si:ned short int is 2d b+tes)n"* si,eof(si:ned short int)); printf(";i,e of si:ned lon: int is 2d b+tes)n)n"* si,eof(si:ned lon: int)); printf(";i,e of unsi:ned int is 2d b+tes)n"* si,eof(si:ned int)); printf(";i,e of unsi:ned short int is 2d b+tes)n"* si,eof(unsi:ned short int)); printf(";i,e of unsi:ned lon: int is 2d b+tes)n)n"* si,eof(unsi:ned lon: int)); printf(";i,e of char is 2d b+tes)n"* si,eof(char)); printf(";i,e of float is 2d b+tes)n"* si,eof(float)); printf(";i,e of double is 2d b+tes)n"* si,eof(double)); return 0; .
5hen usin' the si,eof o#erator, all you have to do is #lace the data ty#e etween the rackets and it will return the num er of ytes a varia le of that ty#e will occu#y. Alternatively you can #lace a varia le name inside the rackets and it,ll tell you how much s#ace is reserved for that varia le. )sn,t that 'reat111+++
9G