Sie sind auf Seite 1von 7

SPECIFYING COMMENTS IN C

Comments are messages that explains whats going on. C comments beign with /* and end with */. Comments can span several lines, and they can go just about anywhere in a program.

Examples of Comments in C: 1. /* This is a single line comment */ !. /* This is a comment that happens to span two lines be"ore coming to an end */ #. "or $% & 1' % ( !)' %**+ /* Counts "rom , to !) */

CHARACTER ARRAY

-olds strings in memory .n array is a list o" variables o" the same data type. /* de"ines a character array */

/xample0 1. Char month11,2'

where0 [10] indicates that the maximum number o" characters the string will hold. The length o" the string is always the number o" characters up to but not including the null 3ero. !. char month11,2 & 45anuary6' /* de"ines a character array at the same time storing a string value in the array */

subscripts

month 1,2 112 1!2 1#2 172 1)2 182 192 1:2 1;2 J a n u a r y <, = =
>tring terminator

where0 Subscripts are numbers speci"ied inside the brac?ets that re"er to each o" the array elements. Elements are the individual piece o" an array. String Terminator - C adds automatically the null 3ero at the end o" all strings. INITIALIZING STRINGS month & 4.pril6' /* @AT .BBAC/D */

String Copy Function STRCPY !" assigns a new string to a character array Eormat0 strcpy$char array, 4string6+ /xample0 Finclude (stdio.hG F include (string.hG 0 strcpy$month, 4.pril6+' /* puts new string in month array */ 0 PRINTING STRINGS Hs "ormat speci"ier "or printing string array

Example: 1. print"$4The month is Hs6, month+' !. print"$4<n/nter the month0 6+' scan"$4Hs6, month+' /* no ampersand on character arrays */
#

)EFINING CONSTANTS Constants are data value that does not change. #define preprocessor directive de"ines constants. Eormat0 Fde"ine CA@>T.@T constant de"inition /xamples0 1. Fde"ine .I/B%J%T !1 !. Fde"ine JK@.J/ 4Laula -olt6 #. Fde"ine L% #.171); E*$rci%$ 1+ Crite a program that will prompt the user to enter his "irstname, lastname, age and weight. Then the program will display the name $"irstname and lastname combined, age and weight+. Melow is the sample execution o" the program0 Chat is your "irst name= Jo$ Chat is your last name= Harri%on -ow old are you= &1 -ow much do you weigh= #'( -ere is the in"ormation you entered0 @ame0 5oe -arrison Ceight0 !,) .ge0 71
,

Pr$c$-$nc$ an- A%%ociati.ity o/ Op$rator% ans & ) * ! * #' /* puts 11 in ans */

The "ollowing summari3es the rules o" precedence and associativity0 Op$rator% $ + ** $post"ix+ $post"ix+ * $unary+ $unary+ ** $pre"ix+ * / H * & *& & *& /& etc. /xamples0 ans & ) * ! * 7 / ! H # *1, N #' avg & ! * # * 7 * ) / 7' ans & $) * !+ * #' avg & $! * # * 7 * )+ / 7' Example a+ ! * ) * ) * # * 7 * 1, & 1, * ) * # * 7 * 1, & 1, * 1) * 7 * 1, & 1, * 1) * 7, & !) * 7, & 8) b+ 7 H ! * # 7 * ) & ,*# 7*) & , * # !, & 19 /* Chat is the answer= */ /* puts !1 in ans */ /* puts !1 in ans */ /* will @AT compute the averageO */ A%%ociati.ity le"t to right $pre"ix+ right to le"t le"t to right le"t to right right to le"t

c+ 9 a * **b where a & ! and b & 7 & 9 $ !+ * **b & 9 $ !+ * $)+ & 9 $ 1,+ & 9 * 1, & 19

&

Parentheses Larentheses change the rules o" precedence and associativity. /xpressions in parentheses have the highest precedence. /xamples0 1. ) * # * 7 * ! & ) * 1! * ! & 19 * ! & 1; !. ) * # * $7 * !+ & ) * # * 8 & ) * 1: & !# #. $) * #+ * $7 * !+ & : * $7 * !+ & :*8 & 7: E*$rci%$ #+ /nter the price and Puantity o" an item bought, and the percentage discount rate. .pply :H sales tax be"ore applying the discount rate on the total price. Display the computed value. Ot0$r Op$rator% Incr$1$nt Op$rator ! 2 2 " The increment operator $**+ is a unary operator that increments the contents o" a variable by 1. Eor example, the statement0 ** age' /* the same as age & age * 1 */

)$cr$1$nt Op$rator ! 3 3 " The decrement operator $ + is a unary operator that decrements the contents o" a variable by 1. age--; /* is the same age = age 1 *

Lre"ix vs. Lost"ix The post"ix and pre"ix notations o" the increment and decrement operators di""er when used in expressions.
(

a = ++age; .ssume age = !. ."ter execution o" this statement, a = " and age = ". a = age++; .ssume age = !. ."ter execution o" this statement, a = ! and age = ". CAJLAQ@D .>>%I@J/@T x & x * 1' sales & sales * 1.!)' x *& 1' sales 4 15#(6 /* adds 1 to x */ /* increases sales by !)H */ /* adds 1 to x */ 7 increases sales by !)H */

Compoun# $ssignment %perators Compound Aperator *& /& H& *& N& /xample total *& 1.!)' amt /& "actor' days H& #' count *& 1' adjust N & ,.)' /Puivalent >tatement total & total * 1.!)' amt & amt / "actor' days & days H #' count & count * 1' adjust & adjust N ,.)'

Typ$ca%ting Typecast temporarily changes the data type o" one variable to another with the "ormat0 /xample0 1. %nt age' .ge & 8' $"loat+ age' !. /* converts 8 to 8., */ &#ata'(pe) *alue

salaryMonus & salary * "loat$age+ / 1),.,'

E*$rci%$ ,+ /nter a persons age and compute its ePuivalent dog age. $. year to a dog is seven years to a person+.

Das könnte Ihnen auch gefallen