10_-_specifying_comments_in_c.doc

Upload: bianca-jane-maaliw

Post on 04-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    1/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 singleline 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.

    xample0

    1. Char month12' /* de"ines a character array */

    where0

    [10]indicates that the maximum number o" characters the stringwill hold. The length o" the string is always the number o"characters up to but not including the null 3ero.

    !. char month12 & 45anuary6'

    /* de"ines a character array at the same time storing a string

    value in the array */

    month 2 12 !2 #2 72 )2 82 92 :2 ;2

    J a n u a r y tring terminator

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    2/7

    where0

    Subscripts are numbers speci"ied inside the brac?ets that re"er to

    each o" the array elements.

    Elementsare the individual piece o" an array.

    String Terminator - C adds automatically the null 3ero at the endo" all strings.

    INITIALIZING STRINGS

    month & 4pril6' /* @AT BBAD */

    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, 4pril6+' /* 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

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    3/7

    hat is your "irst name= Jo$

    hat 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

    eight0 !)

    ge0 71

    )EFINING CONSTANTS

    Constants are data value that does not change.#definepreprocessor directive de"ines constants.

    Eormat0Fde"ine CA@>T@T constant de"inition

    xamples0

    1. Fde"ine IB%J%T !1

    !. Fde"ine JK@J 4Laula -olt6

    #. Fde"ine L% #.171);

    E*$rci%$ 1+

    rite 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

    ,

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    4/7

    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% A%%ociati.ity

    $ + $post"ix+ $post"ix+ le"t to right

    $unary+ $unary+ $pre"ix+ $pre"ix+ right to le"t

    * / H le"t to right

    le"t to right

    & & & *& /& etc. right to le"t

    xamples0

    ans & ) ! * 7 / ! H # 1 N #' /* hat is the answer= */

    avg & ! # 7 ) / 7' /* will @AT compute the averageO */

    ans & $) !+ * #' /* puts !1 in ans */

    avg & $! # 7 )+ / 7' /* puts !1 in ans */

    &

    Example

    a+ ! * ) ) * # 7 * 1& 1 ) * # 7 * 1

    & 1 1) 7 * 1& 1 1) 7

    & !) 7

    & 8)

    b+ 7 H ! # 7 * )

    & # 7 * )

    & # !& 19

    c+ 9 a * bwhere a & ! and b & 7

    & 9 $!+ * b& 9 $!+ * $)+

    & 9 $1+

    & 9 1

    & 19

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    5/7

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    6/7

    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' /* adds 1 to x */

    sales & sales * 1.!)' /* increases sales by !)H */

    x & 1' /* adds 1 to x */

    sales 4 15#(6 7increases sales by !)H */

    Compoun# $ssignment %perators

    Compound xample Puivalent >tatement

    Aperator

    *& total *& 1.!)' total & total * 1.!)'

    /& amt /& "actor' amt & amt / "actor'

    H& days H& #' days & days H #'

    & count & 1' count & count 1'

    N & adjust N & .)' adjust & adjust N .)'

    8

  • 8/14/2019 10_-_Specifying_Comments_in_C.doc

    7/7