Basic C Language
Basic C Language
i=5
5 is a constant which is being stored in a location which has been
given a name i. So, i is a variable just as a, x, note or str are. They
all designate memory locations that will hold different values assigned
to these variables from time to time.
Depending on the purpose for which you want to utilize memory, C
allows you to decide how much memory to allocate to a variable.
Accordingly memory comes in three convenient versions: char, int
and float, which occupy 1,2 and 4 locations ( bytes ) respectively. A
char can store values in the range -128 to +127, whereas an int can
store values in the range -32768 to +32767. When do we use float?
When we want to store numbers like 3.1415, or 0.0005672 in
memory. A float can store values ranging from -3.4e+38to +3.4e+38.
Now that is certainly a very impressive range!
The memory occupied by each datatype can be found out using an
operator called sizeof. For example, sizeof (int) would yield 2.
Similarly, sizeof ( float ) would yield 4. The essence of these
datatypes has been captured in Figure 1.1.
The chars and ints can also be expressed in hexadecimal and octal
notations. For example, 16 can be expressed in hex as 0x10, and as
020 in octal. Similarly, 22 can be expressed in hex as 0x16, and as
026 in octal. A hex number is always preceded by Ox or OX whereas
an octal is preceded by 0 ( zero, not o or O ). C doesn't accept floats
in hex or octal.
With constants out of the way, let us now take a look at the variables.
Certain rules have been framed to create variable names. A variable
4 Exploring C
name can be any combination of alphabets, digits and an underscore,
Figure 1.1 Integer, Character and Float ranges
with a proviso that the first character is an alphabet. The length of the
variable is a compiler dependent feature. Turbo C accepts a variable
name upto 32 characters.
The ABC Of C 5
Operations on Data
C programs are concise partly because of the large number of
operators available with it. There are as many as 45 operators available
in C. For starters we will concentrate only on arithmetic and
assignment operators..These are shown in the Figure 1.2. Operators
are classified as unary, binary or ternary depending on the number of
operands they operate upon. For example, + is a binary operator since
it operates on two operands that it adds.
Figure 1.2 Arithmetic and Assignment Operators
Except for % the other operators are as usual. % is read as 'modulus'
operator, and it returns the remainder on dividing an int with another.
As against this, a / returns the quotient. While evaluating an expression,C gives priorities to some operators above the others. A unary
minus has the highest priority, followed by arithmetic operators and
lastly the assignment operator. Within arithmetic operators, *, / and
% enjoy a higher priority than + and -.
6 Exploring C
Integer and Float Conversions
It is important to understand the rules that govern the conversion of
standard library function used to display the output on the screen. The
general form of printf() looks like this...
printf ("Format string", list of variables);
And here are a few examples...
printf ("%c %d %f, name, age, sal);
printf ("name = %c age = %d salary = %f, name, age, sal);
printf ("name = %c\nage = %d\nsalary = %f, name, age, sal);
Assuming the value of name as 'A', that of age as 23, and that of sal
as 1500.00, the output of the above printf( )s would be
A 23 1500.000000
name = A age = 23 salary = 1500.000000
name=A
age = 23
salary = 1500.000000
The first printf() prints the values of the variables name, age and
sal. As against this, the second would print messages against each
value displayed. In the third printf(), the \n is used to send the cursor
to the next line, hence the three values are printed on three different
lines.
Sometimes the list of variables may be dropped, as in,
printf ("Enter values of a and b");
In this case everything enclosed within the pair of double quotes
would be printed as it is on the screen.
8 Exploring C
Having imbibed those details, now let us switch over to scanf().
scanf( ) is once again a standard library function. It is used to receive
avg.
thereisagirlinmysoup
[C] Point out which of the following C constants are invalid:
124.567
0001
0xbc40
0Xbc40
0x12.45
Oxfgff
(5) C variables are case ( sensitive / insensitive ).
10 Exploring/C
.001
-12e-12
[D] What will be the output of the following programs:
(1) main()
{
printf ("%d %d %d %d", 72,072,0x72,0X72);
}
(2) main()
.{
printf("%d%o%x",72,72,72);
}
(3) main()
{
char ch;
inta;
float b;
printf ("bytes occupied by ch = %d\n", sizeof (ch));
printf ("bytes occupied by a = %d\n", sizeof (a));
printf ("bytes occupied by b = %d\n", sizeof (b));
}
(4) main()
{
printf ("char occupies %d bytes\n", sizeof (char));
printf ("int occupies %d bytes\n", sizeof (int));
printf ("float occupies %d bytes\n"sizeof (float));
}
main()
printf ("bytes occupied by 7' = %d", sizeof (T)) ;
printf ("bytes occupied by 7 =i%d", sizeof (7));
The ABC Of C 11
printf ("bytes occupied by 7.0 = %d", sizeof (7.0));
I
(6) main()
{
char ch = 291;
printf ("%d %d %c", 32770, ch, ch);
}
(7) main()
{
printf ("%d%c\n");
printf ("%d%c\n");
}
(8) main()
{
int a = 33000;
float b = 3.4e100;
printf("a = %db = %f\n",a,b);
printf ("%d %d", sizeof (a), sizeof (b));
}
(9) main()
{
int a, b;
a = -3--3;
b = -3--(-3);
printf("a = %db = %d",a,b);
}
(10) main()
{
intx;
x = 3*4%5 ;
printf ("x = %d",x);
12 Exploring C
}
(11) main()
{
intx;
x = 3 + 4-7*8/5%10;
The ABC Of C 13
(16) main()
{
printf ("%d", 4% 3);
printf ("%d ",4%-3);
printf ("%dM
, -4 % 3);
printf ("%d ",-4%-3);
(17) main()
{
f loat a = 5, b = 2;
int c;
c = a%b;
printf ("%d", c);
(18) main()
{
int x;
x = 3**4-7 A
8;
printf ("x = %d",x);
(19) main()
{
int g = 300 * 300/300;
printf ("g = %d",g);
}
(20) main()
{
floata=1.5;
int b = 3;
a = b/2 + b*8/b-b + a/3;
14 Exploring C
printf ( u
a = %f,
I
a);
}
(21) main()
{
int i = 3, a = 4, n;
float t = 4.2.;
n = a*a/i + i/2*t + 2 + t;
printf ("n = %d", n);
}
(22) main()
{
int q = 2, d = 3, st;
st = q*d/4-12/12+ 12/3*16/d
printf ("st = %d", st);
}
(23) main()
{
int a, b;
a = 5.999999;
b = 5.000001;
printf {"a = %d b = %d", a, b);
}
(24) main()
{
float a;
a=4/2 ;
printf("%f%f,a,4/2);
}
(25) main()
{
printf ("%d %An", 4,4);
printf ("%d%f\n", 4.0,4.0);
}
(26) main()
{
float a = 4;
inti = 2;
printf ("%f %d", i/a, i/a);
printf ("%d%f,i/a,i /a);
(27) main()
{
printf ("%d Vsizeof (4) / sizeof (2.0));
printf ("%d", sizeof (2.0) / sizeof (4));
}
(28) main()
{
printf ("nn \n\n nn\n");
printf ("nn /n/n nn/n");
}
(29) main()
{
int p, q;
scanf ("Enter values of p and q %d %d", &p, &q);
printf ("p = %d q = %d", p,q);
}
(30) main()
{
int a, b;
printf ("Enter values of a and b");
scanf (" %d%d ",&a,&b);
printf ("a = %d b = %d",
16 Exploring C
}
(31) main()
{
int p, q;
printf ("Enter values of p and q");
scant ("%d\n\n%d", &p,&q);
printf ("p = %d q = %d", p, q);
}
(32) main()
{
int p, q;
printf ("Enter values of p and q");
scant ("%d %d\p,q);
printf ("p = %dq = %d", p,q);
}
(33) main()
{
/* This program attempts to find what happens when
integer range I* -32768 to +32767 */ is exceeded */
int a = 330000;
float b = 3.4e100;
printf("a = %db = %f\n"Ia,b);
}
(34) main()
{
printf ("Menu is a list of options you have at a \
particular point in a program. It is just \
like a restaurant menu - everything has a \
misleading name and what you want is never \
available.");
}
fE] Attempt the following:
(1) Write a program to round off an integer i to the next largest
multiple of another integer j . For example, 256 days when
and 16 respectively for octal and hex. Also note that octal
numbering system is a base 8 numbering system, and hence
any number in this system is built as a combination of digits 0
to 7. Similarly, hex is a base 16 numbering system, and any
number in hex is constructed using 16 digits 0 to 9 and A to F
( A being 10 and F being 15 ).
Once we have obtained the binary equivalent there is a simpler
and faster way of obtaining the octal and hexadecimal
equivalent. Let us first convert 1011111010 to octal. All that
we need to do is pick up 3 digits at a time ( from right to left)
and then go on writing their octal equivalents as shown below.
1 011 111 010
1372
For example, octal equivalent of 010 is 2, whereas octal
equivalent of 111 is 7 and so on.
The ABC Of C 19
The same method can be used to convert 011111010 to its
hex equivalent. Only difference being this time we would take
4 digits at a time. This is shown below.
101111 1010
2FA
2 762 8 762 16 762
2 381 0 8 95 2 16 47 A
2 190 1 8 11 7 16 2 F
2 95 0 8 1 3 0 2
2 47 1 0 1
2 23 1
2 11 1
251
221
210
01
Figure 1.4
(3) 32 characters
This maximum allowable width changes from compiler to
compiler. Some compilers like Turbo C allow the user to set
this maximum width through its Integrated Development Environment
( IDE ) by selecting items from the menu in the
order: Options, Compiler, Source, Identifier length.
(4) alphabet
(5) sensitive. This means that variables sal, Sal and SAL would
be treated as different variables in C.
20 Exploring C
(6) only one character. In fact, if we execute the following statements,
what gets stored in variable ch is not really the character
constant, but the ascii value of 'A', which is 65.
char ch;
ch =
,A
1
;
Answers to [B]
Explanation
32770 falls outside the integer range ( -32768 to +32767 )
hence goes to the other side and becomes an appropriate
negative number (-32766) which gets printed.
Same thing happens to 291, which also falls outside the range
of char ( -128 to +127 ), hence goes to the other side and
becomes 35, which is then printed. The third output is the hash
character. This is nothing but the character corresponding to
ascii value 35. %c always prints the character corresponding
to the ascii value.
(7) Output
-22A
-22A
Explanation
Here, even though we have not supplied any variables to
printf( ), still some garbage integers ( for %d ) and characters
( for %c ) get printed. This is however true only of characters
and integers. For example try the following statement:
24 Exploring Q
printf ("%f%f);
This statement gets compiled, but during run-time it flashes
the error message: printf: floating point formats not linked,
Abnormal program termination.
(8) Output
a = -32536b = +INF
24
Explanation
33000 exceeds the valid integer range, whereas 3.4el00 exceeds
the valid float range. In the first case the number from
the other side of the range is picked up, which in this case turns
out to be -32536. However, in case of 3.4el00 the printf()
simply prints out +INF, which should be read as 'plus infinity',
telling us that we are going beyond the valid float range on
positive side. If we attempt to print out - 3.4el00, printf( )
would output -INF.
Whether the range is exceeded or not, the number of bytes
occupied by variables a and b in memory remains unchanged
- 2 for an int and 4 for a float.
(9) Output
a = 0b = -6
Explanation
'Minus of minus is plus'. This is true in usual arithmetic, so
also in C. Hence -3 - -3 becomes -3 + 3, which is equal to 0,
and is assigned to variable a. Similarly, while evaluating the
The ABC Of C 25
next statement, -(-3) becomes +3 and therefore the statement
gets reduced to -3 - +3, which is equal to -6. This -6 is assigned
to variable b. The values of a and b are then outputted through
printf().
(10) Output
x=2
Explanation
Explanation
On dividing two integers we always get an integer. If you
imbibe this fact, then the above program is a breeze. 4/ 3 will
resultinto 1, and not 1.333333. Whenever either the numerator
or denominator is negative, the answer will be negative. If both
are negative, then they cancel out, and the answer would be
positive.
(16) Output
minuses
x = 12%-6/- 5
x = 0 / -5
x=0
operation: *
operation: %
operation: /
11-1-1
28 Exploring C
Explanation
% operator always returns the remainder on dividing the first
integer by the second. If one of them is negative, then the result
takes the sign of the numerator. Once these facts sink in, the
above output is fairly straight-forward.
(17) Output
Error message: Illegal use of floating point in function main.
Explanation
The % operator works only on integers, never on floats. Here
6.500000
Explanation
Steps involved in the evaluation of the expression are given
below. Note the following points carefully:
(a) An operation between an int and an int would result into
an int. Thus, 3/ 2 would give 1 and not 1.5.
(b) An operation between a float and an int would result
into a float.
Let us first replace the variables in the expression by their
values and then proceed with evaluation.
a
a
a
a
a
a
a
a
3 / 2 + 3 *8/3- 3 + 1.5/3
1 + 3*8/3- 3 + 1.5/3
1 + 24/3- 3 + 1.5/3
1 + 8- 3 + 1.5/3
.1 + 8- 3 + 0.5
9 - 3 + 0.5
6 + 0.5
6.5
operation: /
operation: *
operation: /
operation: /
operation: +
operation: operation: +
30 Exploring C
(21) Output
n = 15
Explanation
As in the previous example, let us first replace the variables
with their actual values and then evaluate the expression step
by step.
n = 4*4/ 3 + 3/2*4. 2 + 2 + 4.2
n = 16/ 3 + 3/2*4. 2 + 2 + 4.2 operation:
n = 5 + 3/2*4. 2 + 2 + 4.2 operation: /
n = 5 + 1 * 4.2 + 2 + 4.2 operation: /
n = 5 + 4.2 + 2 + 4.2 operation:
n = 9.2 + 2 + 4.2 operation: +
n = 11.2 + 4.2 operation: +
n = 15.4 operation: +
When 15.4 is to be assigned to n, it is demoted to 15 before
assignment, because n is an int, and hence can hold only
integer values.
(22) Output
st = 21
Explanation
Replacing the values of variables in the expression, we get,
St = 2*3/4-12/12+12/3*16/3
The ABC Of C 31
Let us now evaluate the expression, keeping in mind the
priorities of operators and integer and float conversion rules.
st = 6/4-12/12+12/3 * 16/ 3 operation: *
st = 1-12/1 2 + 12/3 * 16/ 3 operation: /
st = 1-1 + 12/3 * 16/ 3 operation: /
st = 1-1+4 * 16/ 3 operation: /
st = 1 - 1 + 64/ 3 operation: *
st = 1 - 1 + 21 operation : /
st = 0 + 21 operation: st = 21 operation: +
(23) Output
a=5b=5
Explanation
Whenever a floating point value is assigned to an integer
variable, the fractional part is always truncated, and never
rounded off to the next integer. Thus, 5.999999 as well as
5.000001, both are truncated to 5 when assigned to integer
variables, a and b, which are then printed out.
(24) Output
2.000000-1.4746701 e+308
Explanation
nn
nn
nn /n/n nn/n
Explanation
34 Exploring C
The output highlights the difference between a \n and a In. A
\n, when used in printf(), ensures that the subsequent output
goes to the next line. As against this, a /n is treated as two
ordinary characters, and if they occur in printf( ), they are
outputted as they are on the screen. Once this difference is
understood, the above output would appear quite logical.
(29) Output
p = 1562 q = 1686
Explanation
We got the above result when we entered 10 and 20 as values
of p and q. Surprising? This so happens because of the message
'Enter val.... q' written inside the scanf( ), due to which it
behaves erratically. To get rid of the problem, either we should
drop the message from scanf(), since it is serving no meaningful
purpose, or while entering the values, before the values
we must type the message through the keyboard as it is. For
example, if we supply through the keyboard the following to
scanf( ), it would match the message with the message in
scanf( ) and ignore it, whereas it would store 10 and 20 in p
and q.
Enter values of p and q 10 20
(30) Output
No output
Explanation
The scanf() just doesn't seem to work. Once again the reason
is the spaces immediately after the " " and the spaces immediThe ABC Of C 35
ately before the "". scanf( ) works in a peculiar way if you
write anything other than format specifications like %d %c
%f within the " ". If it finds anything other than format
specfications ( even spaces ), then it expects the same characters
to be typed through the keyboard, such that it can match
them with the ones in the format string, and ignore them. Thus,
if we supply the input as follows, the scanf() would work
correctly.
<space><space>10 20<spacexspace>
Since this seems to be quite unnecessary, we can make a
ground rule that in the format string of scanf(), do not write
anything except the format specifications. However, spaces, if
they occur between two format specfications, do not create any
kind of problem.
(31) Output
Enter values of p and q 10 20
p = 10 q = 20
Explanation
If between two format specifications of scanf(), anything other
than spaces, \ns or \ts occurs, then the scanf() doesn't work as
Explanation
printf() can be split over multiple lines. However, at the end
of each line, it is necessary to give a 'V, which tells the compiler
that what follows on the next line is the continuation of the
previous line.
Solutions to [E]
(1) Program
The ABC Of C 37
main()
{
int i, j, k;
printf ("\nEnter values of i and j");
scant ("%d%d*,&i,&j);
k = i + j-i%j;
printf ("\nNext largest multiple = %d", k);
}
Sample run
Enter values of i and j 256 7
Next largest multiple = 259
Explanation
Suppose value of i and j are entered as 256 and 7, then k
evaluates to 259 which is the next largest multiple of 7 after
256.
(2) Program
main()
{
float f, c;
printf ("Enter temperature in farenheit degrees");
scant ("%f,&f);
c = 5/9.0*(f-32);
printf ("Temp, in centigrade degrees = %f, c);
\
38 Exploring C
Sample run
Enter temperature in farenheit degrees 212
Temp, in centigrade degrees = 100.000000
Explanation
Once the temperature in farenheit degrees is entered through
the keyboard, all that we have to do is apply the standard
formula to get the temperature in centigrade degrees. The only
catch here is, while using the formula is it necessary to use 9.0,
or 9 would do? If we use 9, then irrespective of what is the
value of f, c would always turn out to be 0. This is because 5
and 9 both are integers, and hence must always return an
integer. Therefore 5/ 9 would yield 0. To avoid this integer
division we use 9.0, such that before division 5 would get
promoted to 5.0 and then 5.0 / 9.0 would be evaluated, which
would return a float value. This float value is then multiplied
by the result of ( f - 32 ), and the answer is stored in c, which
is ultimately printed out.
(3) Program
main()
{
int a = 10, b = 20;
printf ("Before interchanging^");
prinrf("a = %db = %d",a,b);
a = a+ b;
b = a-b;
a = a-b;
printf ("\nAfter interchanging^");
The ABC OfC 39
printf ("a = %d b = %dB
, a, b) ;
}
Sample run
Before interchanging
a=10b = 20
After interchanging
a = 20b = 10
Explanation
To begin with, we initialise variables a and b to values 10 and
20. The printf() then prints out these valuesvThen come the
three arithmetic statements, a = a + b stores 30 in a. Thus, the
next statement b = a - b becomes b = 30 - 20, yielding 10,
which is assigned to b. Then a = a - b is executed, which is
equivalent to a = 30 -10. Therefore 20 is assigned to a. Having
thus interchanged the contents, the interchanged values are
displayed using printf().
(4) Program
main()
{
int days, weeks, leftoverdays;
days = 31 +29 + 31 +30 + 31
weeks = days / 7;
leftoverdays = days % 7;
printf ("days = %d", days);
printf ("weeks = %d", weeks);
printf ("left over days = %d", leftoverdays);
40 oring C
Sample run
days = 152
weeks = 21
left over days = 5
Explanation
Calculation of days is straightforward. To calculate weeks, we
divide days by 7, whereas to calculate leftoverdays, we use
the modulus operator. This is because while calculating leftoverdays,
we are not interested in the quotient, but in the
remainder.
Steering the Control
E
veryone of us is called upon to take decisions in the face of
changing circumstances. If there is a good movie on TV I
would stay at home; if I get a visa I would fly next month; if
you do it so would I. Put all these statements in spotlight and you will
notice that the decisions depend on certain conditions being met.
C, too, must be able to perform different sets of actions depending
on the circumstances. C has three major decision making media:
(a) if-else statement
(b) switch statement
(c) Conditional operators
In this chapter we will examine the if-else and the conditional
operators. Figure 2.1 shows the if-else at work. From the figure one
can observe that if the condition after if is satisfied one set of
statements gets executed, otherwise a different set of instructions gets
executed.
But how do we express the condition itself in C? And how do we
evaluate its truth or falsity? As a general rule, we express a condition
using C's Relational operators. The relational operators allow us to
compare two values to see whether they are equal to each other,
unequal, or whether one is greater than the other. The various relational
operators are shown in Figure 2.2.
Steering the Control 43
false
statements
4 and 5
true
statements
1, 2 and 3
if (condition)
{
statement 1;
statement 2;
statement3;
else
statement 4;
statements;
Figure 2.1 Working of if-else
this expression is true if
x==y x is equal to y
x != y x is not equal to y
x < y x is less than y
x > y x is greater than y
x <= y x is less than or equal to y
x >= y . x is greater than or equal to y
Figure 2.2 Relational operators
44 Exploring C
Points to Note
A few tips are in order:
(a) Whenever a condition is evaluated in C, it is given a value 1 if
the condition is satisfied, and 0 if it is not satisfied.
(b) Any non-zero number is always treated as truth, whereas a zero
is treated as falsity.
(c) The group of statements after the if, upto and not including the
else is known as the "if block". Similary, the statements after
the else form the "else block".
(d) If there is only one statement in the if block then the pair of
braces can be dropped.
(e) If there is only one statement in the else block then the pair of
braces can be dropped.
(f) If there is no statement to be executed in the else block then
the keyword else can also be dropped.
(g) It is perfectly valid to write an entire if-else construct within
an if block or an else block. This is called' nesting'.
(h) More than one condition can be combined together using
logical operators && ( AND ), || ( OR ),! ( NOT ). For example,
if t (per >= 50) && (per < 60))
printf ("Second division");
On execution, 'Second division' would be printed if both
conditions, ( per >= 50 ) as well as ( per < 60 ) evaluate to
Steering the Control 45
true. Even if one of the conditions evaluates to false, then the
printf() would not be executed.
Let us look at another example:
if ((code >= i &&key==5) || (code < 5 && val == 1))
printf ("Whisky is Risky");
Here there are four conditions being checked. They have been
split into two groups by the || operator. "Whisky is Risky"
would be printed if any one group evaluates to true. In each
group there are two conditions separated by && operator. For
each group to evaluate to true, both the conditions in the group
must be satisfied.
(2) main()
{
int a = 30, b = 40, x;
x = (a!=10)&&(b = 50);
printf ("x = %d",x);
(3) main()
{
int a = 100, b = 200, c;
c = (a==100||b>200);
printf ("c = %d",c);
(4) main()
{
intx = 11,y = 6,z;
z = x == 51| y != 4 ;
printf ("z = %d",z );
}
(5) main()
{
50 Exploring C
int a = 300, b=10, c = 20;
if(!(a>=400))
b = 300;
c = 200;
printf ("b = %dc = %d", b,c);
(6) main()
{
{
if (13.14)
printf ("I have robbed and killed...");
else
printf ("Until my evil purse was filled");
}
(11) main()
{
int x = 3, y = 4, z = 4;
printf("ans = %d",z>=y&&y>=x?1:0);
}
(12) main()
{
int x = 3, y = 4, z = 4;
printf("ans = %d", (z>=y>=x?100:200));
}
(13) main()
{
float a = 12.25, b= 13.65;
if (a = b)
printf ("a and b are equal");
else
printf ("a and b are not equal");
}
(14) mainQ
{
if('Z'<'z')
printf ("Pilots are on strike...");
52 Exploring C
else
printf ("for absolutely outlandish demands")
}
(15) main()
{
int x = 10;
if x >= 2
printf ("%d\n",x);
}
(16) main()
{
int i = 10, j = 40;
if((j-i)%10)
printf ("man sees your actions..");
else
printf ("god sees your motives..");
(17) main()
{
inti = -4, j, num = 10;
j = i%-3;
j = (j ? 0: num * num);
printf ("j = %d",j); i /
(18) main()
{
float a = 0.7;
if (a< 0.7)
printf ("Stoned");
else
printf ("Avenged");
Steering the Control 53
(19) main()
{
inti = 400 * 400/400;
if (i == 400)
printf ("Filibusters");
else
printf ("Sea gherkins");
(20) main()
{
intk = 12,n = 30;
k = (k>5&&n = 4?100:200);
printf ("k = %d*,k);
(21) main()
{
int c = 0, d = 5, e = 10, a;
a = c> 1 ? d > 11| e > 1 ? 100:200:300;
printf ("a = %d", a);
(22) main()
{
(26) MAIN()
{
INTX = 100;
IF (!!X)
PRINTF ("X = %D", !X);
ELSE
PRINTF ("X = %D",X);
}
[B] Improve the following programs by reorganising the statements:
INT I = 10;
IF(I>10)
i
ELSE
(1) MAIN()
{
Steering the Control 55
printf ("Hello Cocaine!");
}
(2) main()
{
int i = 5, j = 30, k = 5;
if (i < 30)
{
if (j < 20 > ;
{
if (k == 40)
printf ("Hi Computerist!");
else
t
}
else
}
}
else
i
}
[Q Attempt the following:
(1) A semiconductor manufacturer sells three types of
microprocessors: 8-bit, 16-bit and 32-bit. It differentiates between
three types of customers: industry, government, and
university. It has the following discount policy that depends on
the type of microprocessor, the amount of order, and the type
of customer:
For 32-bit microprocessor, if the order is for less .han Rs.
50,000, allow 5 % discount to industrial customers and 6.5 %
discount to the government agencies. If the order is Rs. 50,000
or more, a discount of 7.5 % and 8.5 % respectively is given
to the industrial customers and the government agencies. A
56 Exploring C
discount of 10 % is given to both industrial customers and
Answers to [A]
(1) Output
p=1q=1
Explanation
Since x is greater than 9, the condition evaluates to true. In C,
while checking a condition if it evaluates to true, the result of
the test is treated as 1 otherwise it is treated as 0. Hence p
contains, value 1.
In the statement,
q = x>3&&y !=3;
the first condition evaluates to true, hence is replaced by 1.
Similarly, second condition also evaluates to true and is
replaced by 1. Since the conditions are combined using &&
and since both are true, the result of the entire expression
becomes 1, which is assigned to q.
(2) Output
x=1
Explanation
a != 10 evaluates to true and is replaced by 1. b = 50 uses an
assignment operator ( = ), hence 50 is assigned to b. Therefore
the condition becomes,