CS 120 Variable Declaration

Embed Size (px)

Citation preview

  • 7/31/2019 CS 120 Variable Declaration

    1/21

    Variable Declaration in C

    Variables are simply names used to refer to some location inmemory-allocation that holds a value with which we areworking.

    We must declare every variable before we use. Every variable has a type and a name. Variables should be declared in three basic places:

    Inside functions local variables In the definition of function parameters formal parameters

    Outside of all functions global variables

    Declarations of types should always be together at the top ofmain program or a function.

  • 7/31/2019 CS 120 Variable Declaration

    2/21

    Variable Declaration in C (type)

    l In standard C, there are four basic data types:

    1. int : uses integer numbers (e.g. int a=3, b=-2;)

    1. float : uses floating point numbers (e.g. float d=3.141159, e=-5.01;)

    1. double : uses big floating numbers (e.g. double g=6.002e23;)

    4. char: uses single character (e.g. char x=a; )

    Other types are signed, unsigned, long, short and const.

  • 7/31/2019 CS 120 Variable Declaration

    3/21

    Variable Declaration in C (name)

    l Variables in C can be given any name made from numbers, letters andunderscores which is not a keyword and does not begin with a number.

    l int 2mark; char for; illegal

    l A good name for your variables is important

    start_time foo_bar

    student_no _foo42

    course_mark BAZ

    Very descriptive valid but not descriptive

    (fewer comments are needed) (lots of comments are needed)

  • 7/31/2019 CS 120 Variable Declaration

    4/21

    Variable Declaration in C

    l Multiple variables can be declared with one statement.

    int a, b, c, d;

    l Initializing: declare and assign some content to a variable at the same time.int a=3;

    l Assigning: after declaring variables, you can assign a value to a variable later on.

    int a;

    a=6;

    You can also assign a variable the value of another variable.a=b;

    You can assign multiple variables the same value with one statement.

    a=b=c=d=7;

  • 7/31/2019 CS 120 Variable Declaration

    5/21

    Input / Output statements

    #include

    /* addition program */

    int main ()

    {

    int integer1, integer2, sum; /* Variable Declaration */

    printf( Enter first integer \n); /* prompt : tells the user to take a specific action */

    scanf( %d, &integer1); /* read an integer : to obtain a value from the user*/

    printf( Enter second integer \n);

    scanf( %d, &integer2);

    sum=integer1 + integer2;

    printf( sum is %d \n, sum);

    return 0;

    }

    Enter first integer

    45

    Enter second integer

    72

    Sum is 117

  • 7/31/2019 CS 120 Variable Declaration

    6/21

    Input / Output statements

    scanf( %d, &integer1); /* scanf has 2 arquments */

    %d : format control string which indicates the type of data that

    should be input by the user. d stands for decimal integer %d : int

    %f : float

    %c : char

    & integer1 : consists of :-

    & : address operator integer1 : variable name

    The combination tells scanf the location in memory in which thevariable integer1 is stored

  • 7/31/2019 CS 120 Variable Declaration

    7/21

    Note

    The following two statements can be written in one statement:

    sum=integer1 + integer2;

    printf( sum is %d \n, sum);

    as

    printf( sum is %d \n,integer1 + integer2);

    l Common programming errorinteger1 + integer2 = sum /* calculation on the left side */

  • 7/31/2019 CS 120 Variable Declaration

    8/21

    Examples : printf

    #include

    int main ()

    {

    int x = 6;float y = 5.127, b=5;

    char z = 'A';

    printf(" Welcome to C course ");

    printf(" \n %d \n %3.2f \n %2.1f \n %c", x, y, b, z);

    /* % 3.2f means floating point number with a minimum of 3 characters wide and 2 decimal places*/

    return 0;

    }

    Welcome to C course65.135.0A

  • 7/31/2019 CS 120 Variable Declaration

    9/21

    Arithmetic in C

    remainder division multiplication subtraction addition

    % / * - + operator

    X mod Y X

    Y

    5X X-5 X+5 Algebraic

    expression

    X % Y X/Y 5*X X-5 X+5 C expression

  • 7/31/2019 CS 120 Variable Declaration

    10/21

    Arithmetic in C

    l () is the 1st precedence, if there are several pairs ofparentheses on same level and not nested, they are

    evaluated left to right.(5+3) * (6/2)

    (5+3*(6/2))

    *, /, % are the 2nd precedence, if they are several,

    they are evaluated left to right. +, - are the 3rd precedence, if they are several, they

    are evaluated left to right.

  • 7/31/2019 CS 120 Variable Declaration

    11/21

    Arithmetic in C (examples)

    Algebra : y= mx+b

    C : y=m*x+b;

    Algebra : z=pr mod q+w/x-y

    C : z=p*r%q+w/x-y

    int X, Y;

    X=5;

    Y=2;printf(%d, X/Y); /* will display 2 */

    printf(%d, X%Y); /* will display 1 */

  • 7/31/2019 CS 120 Variable Declaration

    12/21

    (1) Arithmetic operators

    l Increment operator (++)

    l Decrement operator (--)

    B++ or++B is equivalent to B=B+1;

    B-- or--B is equivalent to B=B-1;

    Example:

    int x=8, y;

    y=x--; /* x=7 & y=8*/

    y=--x; /* x=7 & y=7*/

    Arithmetic assignment statement

    i+=10; is equivalent to i=i+10;

    i-=10; is equivalent to i=i-10;

    i*=10; is equivalent to i=i*10;

  • 7/31/2019 CS 120 Variable Declaration

    13/21

    Arithmetic operators (2)

    #include

    int main()

    {int i = 7 , j = 5 ;

    float x , y = 1.3 ;

    x = --j + i / 2.0 ;

    printf("j=%d x=%.2f \n", j, x) ;

    y += 7 % j * 1.1 ;printf("y=%.2f \n", y) ;

    return 0 ;

    }

  • 7/31/2019 CS 120 Variable Declaration

    14/21

    Arithmetic operators (3)

    #include

    int main()

    {int i = 3 , j = 5 ;

    float x , y = 1.5 ;

    x = --j + i / 2.0 ;

    printf("j=%d x=%.2f \n", j, x) ;

    y += i++ - i % 4 * 2 ;printf("y=%.2f \n", y) ;

    return 0 ;

    }

  • 7/31/2019 CS 120 Variable Declaration

    15/21

    Arithmetic operators (4)

    #include

    intmain() {

    int i = 7 , j = 5;float x , y = 1.5;

    x = --j/3.0 + i++ ;

    printf("j=%d x=%.2f \n", j, x);

    y += i-- % 4 + ++i * 2;printf("i=%d y=%.2f \n", i, y);

    return 0;

    }

  • 7/31/2019 CS 120 Variable Declaration

    16/21

    Relational operators(1)

    less orequal

    greater orequal

    less greater not equal equal

    XY XY XY XY X=Y Algebraexpression

    X=Y XY X!=Y X==YC expression

  • 7/31/2019 CS 120 Variable Declaration

    17/21

    Relational operators(2)

    > greater than 5 > 4 is TRUE

    < less than 4 < 5 is TRUE>= greater than or equal 4 >= 4 is TRUE

  • 7/31/2019 CS 120 Variable Declaration

    18/21

    Relational operators(3)

    l Relational operators are lower in precedence than the arithmetic operators.

    example:

    10> 1+12 evaluated as 10>(1+12)

    Common programming errors:1- if the two symbols in any of the operators (==, !=, >=,, =