Ee1101 Workshop03
Ee1101 Workshop03
Building Statements
3.1 Expressions
Program is a set of statements executed sequentially.
– constants
– variables
combined by using
– operators
Expressions are the basic building blocks of a program.
Expression Type
3 Constant
0x04A Constant
myAge Variable
x Variable
3+87 Constants combined via operator
x + 48.09 Constant and variable combined via operator
(5.0/9.0) * (tFar -32.0) Constant and variable combined via operator
3.2 Constants
The C has two types of constants.
Literal Constants
Symbolic Constants
1
Literal Constant Meaning
1.23 1.23
0.019 0.019
100. 100.0
1.23E2 123
1.23e2 123
1.23e-2 0.0123
1.23e2 123
0123 83, A constant starting with zero is treated as a octal integer
0x123 0X123 291, A constant starting with zero ans x or X is treated as a hex-
adecimal constant
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
# define RESISTANCE 86.45 co
columns
columns co
columns
columns
int main ( void ) co
columns
columns
{ float voltage ; co
columns
columns
const float current =7.23; co
columns
columns co
columns
columns
voltage = current * RESISTANCE ; co
columns
columns co
columns
columns
return 0; co
columns
columns
} co
3.3 Variables
A variable is a named data storage location in computer’s Random Access Memory (RAM).
typename varname;
Eg. In the Listing 3.2, a variable named x of type int has been declared by int x;.
The keyword int defines the type to be integer,
– that is, the x can contain an whole number (not fractional numbers)
– 4 bytes are allocated for int type variables
2
– and therefore the values that can be assigned to x is limited to the range -2,147,483,648 to -
2,147,483,647
The name of the variable x is then used in the program to access the variable. Examples
– x=1; the x is assigned the value 1.
– x=x+6; the 6 is added to x and the result (7) is then assigned back to x.
– y=x; the y is assigned the value of x
C’s numeric variable types are summarized in Table 3.1
Integer variables hold values that have no fractional part (i.e. whole numbers). Integer numbers come in
two flavors:
– signed integer variables can hold positive or negative values, whereas
– unsigned integer variables can hold only positive values (and zero).
Floating-point variables hold values that have a fractional part (i.e. real Numbers)
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
int main ( void ) co
{ int x ; /* declaration : int type variable x */
columns
columns co
columns
columns
int y ; /* declaration : int type variable y */ co
columns
columns co
columns
columns
x = 1; /* write : Assign 1 to x */ co
columns
columns
x = x + 6; /* Assigne x +6 to x */ co
columns
columns
y = x; /* read : from variable x and write to y */ co
columns
columns co
columns
columns
return 0; co
columns
columns
} co
scanf("%d", &x);
where,
– &x is the address of x
– "%d" indicates that the user input characters must be formatted as an integer (specified by the letter
d) and then store in x.
– The letter d is one of the format specifiers in C
– Format specifier should match with the type of the variable (x)
– The list of format specifies is listed in Table 3.3.
– How to read some of other input types are given in the Listing 3.3.
printf()
The following example shows how to use printf() to write an integer stored in int type variable x on
the Console.
printf("%d", x);
where,
– printf() prints characters of the text string given within " and ".
– However, in place of the format specifier \%d, it prints the value of the variable given after the ,
(that is value of x) after formating it as indicated by the format specifier (that is as an integer)
– The letter d is one of the format specifiers in C
– Format specifier should match with the type of the variable (x)
– The list of format specifies is listed in Table 3.3.
– How to print some of other variable types are given in the Listing 3.3.
the
The first character of the name must be a letter. Underscore is also legal though is not recommended to
use.
Case matters (i.e. upper and lower case letters). C is case sensitive, thus, the names count and Count
refer to two different variables.
C keywords (see Table 3.2 for the complete list) can not be used as variable names. A keyword is a word
that is part of the C language.
For many compilers, a C variable name can be up to 31 characters long.
It is recommended to create variable names that reflect the meaning of data being stored. For example,
interest_rate, Interest_Rate, intrestRate, accFixed, accSavings, accCurrent
4
Table 3.3: C format specifiers
3.4 Operators
An operator is a symbol that instructs C to perform some operation, or action on one or more operands.
5
Anary Mathematical Operators
++x; same as x=x+1;
Can be placed
Briefly: ++i adds one to the stored value of i and ”returns” the new, incremented value to the surrounding
expression; i++ adds one to i but returns the prior, unincremented value.
An expression containing a relational operator evaluates to either true or false having numerical values 1
or 0, respectively.
Evaluate:
5==1 5>1 5 !=1 (5+10) ==(3*5)
x = (5==5) x =1 x =(5!=5) x = 0
x = (12==12) + (5!=3) x = 2
exp1 || exp2 true if either exp1 or exp2 is true; false only if both are false.
E.g. (5==5) && (6!=2), (5>5) && (6<1), (2==1) && (5==5), (5==4)
C’s relational expressions are evaluate to 0 to represent false and to 1 to represent true.
Any numerical value is interpreted as either true or false when it is used in a C expression or statement
that is expecting a logical value. The rules for this are as follows.
– A value of zero represents false.
– Any nonzero value represents true.
6
3.4.5 Operator Precedence and Parentheses
In an expression that contains more than one operator, what is the order in which operations are performed?
x = 4 + 5 *3
If an expression contains more than one operator with the same precedence level, the operators are
performed from left to right. E.g. y=12 % 5 * 2;, x = 4 + 5 *3
C uses parentheses to modify the evaluation order. A sub-expression enclosed in parentheses is evaluated
first, without regard to operator precedence. E.g. x = (4 + 5) *3
Multiple and nested parentheses are possible. Evaluation proceeds from the innermost parentheses on-
wards. E.g. y = 25 - (2 * (10 + (8/2)));
Do use parentheses to make the order of expression evaluation clear.
Less than < Is left operand less than right operand? x<y
Less than or <= Is left operand less than or equal to right x<=y
equal to operand?
Logical AND && Logical AND between operands (expres- exp1 && exp2
sions)
7
Logical NOT ! Logical NOT between operands (expres- !exp1
sions)
Level Operators
1 () (function operator ), [] (array operator ) -¿ .
2 ! ~ ++ -- * (indirection), & (address of ) (Type) sizeof + - unary
3 * (multiplication) / %
4 + -
5 << >>
6 < <= > >=
7 == !=
8 & (bitwise AND)
9 ^
10 |
11 &&
12 ||
13 ?:
14 = += .= *= /= %= &= ^= |= <<= >>=
15 ,
—-
8
Listing 3.3: Use of scanf() and printf() with some variable types
columns
columns
/* scanf () and printf () with some variable types co
*/
columns
columns co
columns
columns
# include < stdio .h > co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
unsigned char mGender ;
columns
columns co
columns
columns
int mAge ; co
float mMarksGeo , mMarksMth ;
columns
columns co
columns
columns
double mWeight , mHeight ; co
columns
columns co
columns
columns
printf ( " What is your gender ( M / F ) : " ) ; co
columns
columns
scanf ( " % c " ,& mGender ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your age ( in years ) : " ) ; co
scanf ( " % d " ,& mAge ) ;
columns
columns co
columns
columns co
columns
columns
printf ( " \ nWhat is your marks for Mathematics (0 -100) : " ) ; co
columns
columns
scanf ( " % f " ,& mMarksMth ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your marks for Geography (0 -100) : " ) ; co
columns
columns
scanf ( " % f " ,& mMarksGeo ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your Weight ( in kg ) : " ) ; co
columns
columns
scanf ( " % lf " ,& mWeight ) ; co
columns
columns co
columns
columns
printf ( " \ ntWhat is your Height ( in m ) : " ) ; co
columns
columns
scanf ( " % lf " ,& mHeight ) ; co
columns
columns co
columns
columns
printf ( " \n - - - - - - - - - - - - - " ) ; co
columns
columns
printf ( " \n - Your Info -" ) ; co
columns
columns
printf ( " \n - - - - - - - - - - - - - " ) ; co
columns
columns co
columns
columns
printf ( " \ n Gender = % c \ t Age : % d " , mGender , mAge ) ; co
printf ( " \ n Marks for Geography = % f and for Mathamatics = % f " ,
columns
columns co
columns
columns
mMarksGeo , mMarksMth ) ; co
columns
columns
printf ( " \ n Avarage Marks = % f " , ( mMarksGeo + mMarksMth ) /2.0) ; co
columns
columns
printf ( " \ n Weight = % lf , Height = % lf " , mWeight , mHeight ) ; co
columns
columns
printf ( " \ n Ratio Weight / Height = % lf " , mWeight / mHeight ) ; co
columns
columns co
columns
columns co
return 0;
columns
columns co
columns
columns
} co
9
Listing 3.4: Demonstrating the modulus operator
10