ch03 Expressions
ch03 Expressions
Chapter Contents
Expressions
Operators:
◦ Arithmetic operators
◦ Assignment operator and its compressed form
◦ Increment and decrement operators
◦ sizeof operator
◦ Relational operators
◦ Logical operators
Some standard libraries
I/O - getchar(), putchar()
Random numbers
Summary
Exercises
Expressions
An expression consists of a combination of operators and operands.
Operand may be a variable, constant value.
The simplest expression is a lone operand.
Here are some expressions:
6
-45
4+54
D * ( b / a + d)
A=2+b
A>b
Expressions – cont’d
An important property of C is that every expression has :
◦ a type
◦ a value:
x=x+1;
is perfectly legal. First x+1 is evaluated,
and then the result is assigned to x.
The value and the type of the expression are the value and the type of the lvalue (left
hand side).
int x,y,z;
That is why x=y=z=5; is perfectly legal.
The statement x = x + 2;
may be written in the form x += 2;
◦ Which is identical to
variable = variable op expression;
◦ op may be one of the arithmetic or bitwise
operators.
Increment and Decrement Operators
C provides two unusual unary operators for incrementing and
decrementing variables by one (1).
Prefix Increment and Decrement Operators: ++, --
unary-expression :
++ unary-expression
-- unary-expression
When the prefix operators are being used, the operand is incremented or
decremented and its new value is the result of the expression.
Example:
i = 5;
printf(“i ==> %d\n”, ++i); /* i ==> 6 */
printf(“i ==> %d\n”, --i); /* i ==> 5 */
printf(“i ==> %d\n”, i); /* i ==> 5 */
Increment and Decrement Operators –
cont’d
Postfix Increment and Decrement Operators: ++, --
postfix-expression :
postfix-expression ++
postfix-expression --
The result of the postfix increment or decrement operation is the value of
the postfix-expression before the increment or decrement operator is
applied.
Example:
i = 5;
printf(“i ==> %d\n”, i++); /* i ==> 5
*/
printf(“i ==> %d\n”, i--); /* i ==> 6
*/
printf(“i ==> %d\n”, i); /* i ==> 5
*/
sizeof()
sizeof() is an operator (unary operator - not a
function) that evaluates the size (in bytes) of:
◦ Type.
◦ Constant.
◦ Variable.
◦ (i < j ) + 1
17 or2 = ( x != 2 || x = y < 2 );
18 printf("x = %d y = %d or2 = %d\n", x, y, or2);
19 }
Precedence Table
== Equal Left to
!= Not equal right
Precedence Table – cont’d
& Bitwise AND Left to
right
^ Bitwise exclusive OR – Left to
(XOR) right
| Bitwise OR Left to
right
&& Logical AND Left to
right
|| Logical OR Left to
right
?: Conditional (ternary) Right to
left
= Assignment Right to
left
Precedence Table – cont’d
*=
/=
%=
+=
-
= Compound assignment
<<=
>>=
&=
^=
,|= Comma Left to
right
Standard Libraries
What is a Library?
◦ The libraries are supplied with the compiler.
◦ C is small and compact.
libraries
Object file
Object file
link of
Executable Library
module function
printf()
Math Library
Mathematical library:
Provides functions for mathematical calculations.
For example : sqrt(), sin() etc.
#include <stdio.h>
#include <math.h>
void main(void)
{
float a,b;
scanf("%f", &a);
b = sqrt(a);
printf("The sqrt of %f is %f\n", a,b);
}
Standard Libraries – Explanation
Topass compilation with no warning messages we
need to include header files, where functions are
declared.
cos(a)
sin(a) cosine, sine and tangent of double argument a.
tan(a)
exp(a) Exponent, natural logarithm, and logarithm to base
log(a) 10 of double argument a.
log10(a)
Mathematical Functions – cont’d
sqrt(a) Square root of double argument a.
Second Example:
printf("\n"); and putchar('\n'); are equivalent.
◦ c = getchar();
c is assigned the ASCII of the next character from the
standard input. If it is a digit, its value is between 48
and 57.
◦ i = c-’0’;
‘0’ is the ASCII of the digit 0, which is 48. c-’0’ is the
“distance” between the typed character and the
character ‘0’.
Here, if the first string contains two words or more, only the first word is entered. The
remaining word stays in the buffer, and is retrieved by the next scanf.
I/O Streaming Functions – fflush
We would obviously like to prevent the second
word staying in the buffer – to clean the buffer
of remaining data.
The fflush() function provides a convenient
answer.
int fflush (FILE *stream);