Data Types & Basic Constructs (If, If-Else, Switch-Case)
Data Types & Basic Constructs (If, If-Else, Switch-Case)
return 0;
}
%c is for displaying characters and %d is for displaying integers. First we have displayed the
equivalent integer of the character. Then we have displayed the equivalent character of the
integer.
Character, integer (all their variants) and enumerated data type are called integral data type.
There internal representation is the same.
2. Operators available in C:
There are three types of operators available in C:
unary – that requires only one operand. For example: &a, ++a, *a
binary - that requires two operands. For example: a + b, a * b
ternary - that requires three operands. For example: (a>b) ? a : b [ ?:]
Priority and associativity: The higher priority operator is operated first. If the priority or
precedence is same then associativity is applied.
Following is the operators available in C. Their precedence and associativity are shown.
Associativity Operators Description
1. left-to-right . () [] Highest priority operators
2. right-to-left ++, --, !, ~, +, -, *, &, sizeof( ) all unary operators
3. left-to-right * / % arithmetic operators
4. left-to-right + - arithmetic operators
5. left-to-right << >> bit shift operators
6. left-to-right <, <=, >, >=, relational operator
7. left-to-right = =, != relational operator
8. left-to-right & ampersand
9. left-to-right ^ caret bit wise operator
10. left-to-right | pipeline
11. left-to-right &&
Logical operator
12. left-to-right ||
13. right-to-left ? : ternary/conditional operator
14. =, +=, -=, *=, /=, %=, &=, ^=, |
right-to-left assignment operator
=, <<=, >>=
15. left-to-right , sequencing operator
Examples:
Note: Each expression generates a resultant numeric value. This resultant numeric value
has a data type. The data type of the result is the data type of the operand with largest
capacity, participating in the expression. That is why C is called strongly typed language.
int $ int will produce int
char $ int will produce int
int $ float will produce float
op1 / op2
Data type of op1 Data type of op2 Data type of result
int Int int (fractional part truncated)
Float Int float
int Float float
Float Float float
Token:
In a C source program, the basic element recognized by the compiler is the “token.” A token is
source-program text that the compiler does not break down into component elements.
token: keyword, identifier, constant, string-literal, operator, punctuator
c = ++a + b; sum = x + pow(3,6)
Note See the introduction to Appendix A, C Language Syntax Summary, for an explanation
of the ANSI syntax conventions.
The keywords, identifiers, constants, string literals, and operators described in this chapter are
examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses
( ( ) ), and commas (,) are also tokens.
Modulus operator
It is represented by %. It gives the remainder after division:
int a, b, c;
/*incr4.c*/ /*incr5.c*/
void main( ){ void main( ){
int a, b; int a, b, c;
a = 10; a = 10;
b = ++a; b = 20;
printf(“\n%d %d”, a, b); c = (++a) + (b++);
} printf(“\n%d %d %d”, a, b, c);
}
Note: The expressions in which a variable is incremented and decremented and used more
than once, must be strictly avoided. These are confusing. Moreover, the compiler may
behave in unpredictable manner.
There are some more examples; there result may not be predicted easily, and the results will be
environment dependent also. /*incr6.c*/
void main( ) {
int a, b;
a = 10;
b = (++a) + (a++);
printf(“\n%d %d”, a, b);
}
Page 4 of 16 [Chapter 2] [Modified – 2]
/*incr8.c*/ /*incr7.c*/
void main( ){ void main( ) {
int a, b; int a, b;
a = 10; a = 10;
b = a * 2 + ( ++a); b = (++a * 2 ) + ( a++) * 3;
printf(“\n%d %d”, a, b); printf(“\n%d %d”, a, b);
} }
Explain examples incr9.c, 10, 10A, 12, 13, 14, 15
Assignment Operators:
An assignment statement is used to assign the value of an expression to a single variable. The
syntax is:
Explain a = b = c = 10;
lvalue = rvalue
/* ass1.c */ /* ass2..c */
void main( ){ void main( )
int a, b, c; {
a = 10; b = 20; c= 30; int a, b, c;
b += a; /* b = b + a a = 10; b = 20; c= 30;
*/ c *= b += a;
c - = a; /* c = c – a */ printf(“\n%d %d %d”, a, b, c);
printf(“\n%d %d %d”, a, b, c); }
}
printf( ): Writes to the standard output (stdout) a sequence of data formatted as the format
argument specifies. After the format parameter, the function expects at least as many additional
arguments as specified in format.
format is a string that contains the text to be written to stdout.
It can optionally contain embedded format tags that are substituted by the values specified in
subsequent argument(s) and formatted as requested. The number of arguments following the
format parameters should at least be as much as the number of format tags.
The format tags follow this prototype:
%[flags][width][.precision][length]specifier
Where specifier is the most significant one and defines the type and the interpretation of the
value of the corresponding argument:
specifie
Output Example
r
C Character a
d or i Signed decimal integer 392
e Scientific notation (mantissa/exponent) using e character 3.9265e+2
E Scientific notation (mantissa/exponent) using E character 3.9265E+2
f Decimal floating point 392.65
g Use the shorter of %e or %f 392.65
G Use the shorter of %E or %f 392.65
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are
optional and follow these specifications:
Flags description
Left-justify within the given field width; Right justification is the default (see width sub-
-
specifier).
Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers.
+
By default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
Used with o, x or X specifies the value is preceded with 0, 0x or 0X respectively for
values different than zero.
# Used with e, E and f, it forces the written output to contain a decimal point even if no
digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see
0
width sub-specifier).
width description
Minimum number of characters to be printed. If the value to be printed is shorter than this
(number) number, the result is padded with blank spaces. The value is not truncated even if the
result is larger.
The width is not specified in the format string, but as an additional integer value argument
*
preceding the argument that has to be formatted.
.precision description
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits
to be written. If the value to be written is shorter than this number, the result is padded
with leading zeros. The value is not truncated even if the result is longer. A precision of 0
means that no character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
.number For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters
are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an
explicit value for precision, 0 is assumed.
The precision is not specified in the format string, but as an additional integer value
.*
argument preceding the argument thas has to be formatted.
length description
The argument is interpreted as a short int or unsigned short int (only applies to integer
h
specifiers: i, d, o, u, x and X).
The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o,
l
u, x and X), and as a wide character or wide character string for specifiers c and s.
The argument is interpreted as a long double (only applies to floating point specifiers: e,
L
E, f, g and G).
Return Value
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string
//printf4.c
int main( ){ Note: We should not change the specifier of
int a=10, b=10; a data type in printf() or scanf() statements.
float c = 3.14; Otherwise, the result may be garbage.
C !C
F T
T F
Explain the output of the code:
/*expr0.c*/ /*expr1.c*/
int main( ) { int main ( ) {
int a= 10, int a = 10, b= 5;
b=20;
printf(“%d ”, a < b); printf(“%d %d %d” b, a<b, b=a+20);
printf(“%d ”, a > b); return 0;
} }
c = a == b; d = a < b = = c;
printf(“%d %d %d”, a, b, c); printf(“ %d ”, d);
return 0; return 0;
} }
/*expr5.c*/ /*expr6.c*/
int main { int main( ){
float y = 2.56; x = 2.5; floot a = - 1.0, b = 0.5, c = 1.0;
int a, b; int d;
a = (y= = 2.56);
b = (x = = 2.5); d=a< b < c;
printf (“%d, %d” a, b); printf(“%d”, d);
return 0; return 0;
} }
4. The ‘if’ Statement – It is used to selectively execute a set of statements. The set of
statements is executed, if the condition is true. The syntax is:(Draw the flowchart
corresponding to the code)
Example : Design program to calculate money accumulated after one years, if the interest rates
are as follows:
initial amt 10,000 rate = 5%
initial amt > 10,000 rate = 10%
c = (float)a / b;
printf(“\n%f”, c);
return 0;
}
During type casting the system creates a temporary memory location for target data type. The
value of casted variable is copied into it. Now this temporary variable takes part in the
calculations of this expression only. After the calculation of expression the temporary memory
location is destroyed automatically.
a: 2 bytes b: 2 bytes c: 4 bytes
11 2
case itemn:
statementn;
break;
default:
statement;
break;
}
In each case the value of itemi must be a constant, variables are not allowed. The break is
needed if you want to terminate the switch after execution of one choice. Otherwise the next
case would get evaluated. Note: This is unlike most other languages.
We can also have null statements by just including a ; or let the switch statement fall through
by omitting any statements (see e.g. below).
The default case is optional and catches any other cases.
For example:-
switch (letter)
{
case `A':
case `E':
case `I':
case `O':
case `U':
numberofvowels++;
break;
//sizeof1.c //sizeof2.c
#include <stdio.h> #include <stdio.h>
int main( )
int main ( ) {
{ int a, b; int a, b, c;
b= 10;
a= sizeof( ++ b ); b=10, c = 20;
printf( “%d%d”, a, b); a = sizeof(b + c);
return 0; printf( “%d”, a);
} Page 14 of 16 [Chapter 2] return 0; – 2]
[Modified
}
9. Low Level Operators and Bit Fields
Many programs (e.g. systems type applications) must actually operate at a low level where
individual bytes must be operated on.
Bitwise Operators
The bitwise operators of C a summarised in the following table:
XOR
One's Compliment
<< Left shift
>> Right Shift
The is a unary operator -- it only operates on one argument to right of the operator.
The shift operators perform appropriate shift by operator on the right to the operator on the left.
The right operator must be positive. The vacated bits are filled with zero (i.e. There is NO
wrap around).
For example: x << 2 shifts the bits in x by 2 places to the left.
So:
if x = 00000010 (binary) or 2 (decimal)
then:
or 0 (decimal)
Also: if x = 00000010 (binary) or 2 (decimal)
or 8 (decimal)
Therefore a shift left is equivalent to a multiplication by 2.
Similarly a shift right is equal to division by 2
NOTE: Shifting is much faster than actual multiplication (*) or division (/) by 2. So if you
want fast multiplications or division by 2 use shifts.
//bit1.c
#include <stdio.h>
int main() {
char x = 12, y=10, a, b, c, d;
clrscr();
a = x | y;
printf("\n%d", a);
b = x & y;
printf("\n%d", b);
c = x ^ y;
printf("\n%d", c);
d = ~x;
printf("\n%d", d);
return 0;
}
Page 15 of 16 [Chapter 2] [Modified – 2]
// Discuss exercises asked on campus & CDEC