CS 120 Variable Declaration
CS 120 Variable Declaration
Variables are simply names used to refer to some location in memory-allocation that holds a value with which we are working. 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 of main program or a function.
In standard C, there are four basic data types: int : uses integer numbers (e.g. int a=3, b=-2;) float : uses floating point numbers (e.g. float d=3.141159, e=-5.01;) double : uses big floating numbers (e.g. double g=6.002e23;) char : uses single character (e.g. char x=a; )
4.
Variables in C can be given any name made from numbers, letters and underscores which is not a keyword and does not begin with a number. int 2mark; char for; illegal
l l
A good name for your variables is important foo_bar _foo42 BAZ valid but not descriptive (lots of comments are needed)
Variable Declaration in C
l
Multiple variables can be declared with one statement. int a, b, c, d; Initializing: declare and assign some content to a variable at the same time. int a=3; 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;
/* Variable Declaration */ /* prompt : tells the user to take a specific action */ /* read an integer : to obtain a value from the user*/
%d : int %f : float %c : char & : address operator integer1 : variable name The combination tells scanf the location in memory in which the variable integer1 is stored
Note
The following two statements can be written in one statement:
Examples : printf
#include<stdio.h> 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; }
Arithmetic in C
remainder division % X mod Y / X Y X/Y multiplication * 5X subtraction X-5 addition + X+5 operator Algebraic expression C expression
X%Y
5*X
X-5
X+5
Arithmetic in C
() is the 1st precedence, if there are several pairs of parentheses 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.
l
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); printf(%d, X%Y);
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--; y=--x;
Arithmetic assignment statement i+=10; i-=10; i*=10; is equivalent to i=i+10; is equivalent to i=i-10; is equivalent to i=i*10;
Relational operators(1)
less or equal greater or less equal greater not equal equal
XY
XY
X<Y
X>Y
XY
X=Y
Algebra expression
X<=Y
X>=Y
X<Y
X>Y
X!=Y X==Y
C expression
Relational operators(2)
> greater than < less than >= greater than or equal <= less than or equal == equal to != not equal to
5 > 4 is TRUE 4 < 5 is TRUE 4 >= 4 is TRUE 3 <= 4 is TRUE 5 == 5 is TRUE 5 != 4 is TRUE
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 (==, !=, >=,<=) are separated by spaces. 2- if they are reversed as in =!, =>, =<
Logical operators:
if ( x < y && y < z ) printf( "x is less than z\n" ); printf( "%d" , (x == w || x == y || x == z) );
Exercise
1- Write a C program that reads two positive integers x and y and calculate the following equation:
Exercise
Write a statement (or comment) to accomplish each of the following: - State that a program will calculate the product of three integers. - Declare the variables x, y, z and result to be of type int. - Prompt the user to enter three integers. - Reads three integers from the keyboard and store them in the variables x, y, and z. - Compute the product of the three integers contained in variables x, y and z, and assign the results to the variable result. - Print The product is followed by the value of the variable result.
Exercise
Write a C program to perform the following: A. Read the distance in miles. B. Convert the distance to kilometers. ( 1 mile = 1.609 kilometers ) C. Display the distance in kilometers. Write a C program to perform the following: A. Read the temperature in degree Celsius B. Convert the degree Celsius to degree Fahrenheit. (F=(9/5)C+32)