Professional Documents
Culture Documents
ALGORITHMS
composed of a finite set of steps, each of which may
require one or more operations.
Each operation must be definite meaning that it must
be perfectly clear what should be done.
Each operation should be effective, in the sense that
it can at least in principle be done by a person in a
finite amount of time.
It produces one or more outputs and may have zero
or more inputs which are internally supplied.
Algorithm should terminate after a finite number of
operations.
Algorithms are also called as computational
procedure.
Examples:
(1) Write an algorithm for determining the largest of
three numbers:
Step 1: Start
Step 2: Read A, B.
Step 3: if(A>B) then if(A>C) then print ‘A’;
stop
else print ‘C’; stop
else if(B>C) then print ‘B’; stop
else
print ‘C’; stop;
1
C and Data Structures – Unit I
2
C and Data Structures – Unit I
3
C and Data Structures – Unit I
4
C and Data Structures – Unit I
FLOW CHARTS
When a problem is programmed for a computer, it is
essential to work out in detail all the decisions to be
taken by the machine and the actions to be performed
and the sequence in which these are to be performed.
Flow charts and decision tables aid this planning.
A flow chart depicts in a pictorial form the sequence
in which conditions are to be tested and calculations
performed for solving a problem.
5
C and Data Structures – Unit I
SYMBOL NAME
Oval
Parallelogram
Rectangle
Diamond
Circle
Polygon
6
C and Data Structures – Unit I
Read A,B,C
is
Yes A>B No
Yes is No Yes is No
A>C B>C
Stop
7
C and Data Structures – Unit I
Read A
X=3*A
T = Sin X
M = 3 * Sin A
W=M-N
No is
T=W
Stop
8
C and Data Structures – Unit I
(3) Draw a flow chart for printing the sum of first 100
natural numbers.
Start
Sum ←0
N ←0
N ←N+1
Sum ← Sum + N
is
N≥ 100
Print Sum
Stop
9
C and Data Structures – Unit I
Start
Sum ←0
I ←1
Term ← XI * YI
I ←I+1
is
I>N
Print
Sum
Stop
10
C and Data Structures – Unit I
11
C and Data Structures – Unit I
12
C and Data Structures – Unit I
Debugging
Concerned with the location and correction of all
errors that cause the program to produce incorrect
results or perform improper actions.
This is the most frustrating, agonizing and time-
consuming step in the overall programming process.
The reason for this could be the lack of time spent on
careful planning, organising and structuring the
solution.
One thing to remember about debugging is the very
best technique is to avoid making mistakes in the first
place.
Testing and verification
Involves demonstrating that the program is
theoretically correct and will work properly on all
data, even those that have not been explicitly tested.
With testing, we select a large number of test cases
and run the program using these data. If the program
produces correct results for this collection of test
data, we can say that it will work properly on all test
data.
With verification techniques, we argue in a more
formal, mathematical or logical sense to prove that
the program P will produce the correct output O, for
all input data I. Verification is much more complex
than simple testing, but it allows us to make much
stronger claims about the correctness of the program.
13
C and Data Structures – Unit I
Documentation phase
Involves developing and writing the supporting
manuals and on-line assistance, which the user will
need to understand and use the finished program
effectively.
Program maintenance
Operation of adapting a program to keep it correct
and current with changing specifications and near
equipment.
If the program has been well organised and well
planned, has been carefully designed as a set of
independent modules, has been well coded, and is
clearly documented, then program maintenance will
be a less difficult task.
TOP DOWN DESIGN METHODOLOGY
Top down programming (design) is a program design
technique that describes a high level problem in
technique that describes a high level problem in
terms of more elementary subtasks.
Through the technique of stepwise refinement we
then expand and define each of these separate
subtasks until the problem is solved.
Each subtask is tested, verified before it is expanded
further.
The principles of top-down modular programming
are based on decomposing a problem into a hierarchy
of tasks, and then developing the solution from the
highest level down to the lowest.
14
C and Data Structures – Unit I
P111 o o o o o o P121o o
P112 P113 P121 P122 P131 P132
In top down programming, the original problem is
described in terms of a reasonable small number of
subtasks, which if they existed and were performed in
the correct order, would solve the original problem.
This operation of tasking a problem step and defining
it in terms of more elementary sub problems is called
stepwise refinement.
The stepwise refinement process terminates when a
subtask is so simple it need not be expressed in terms
of any new subtasks but can be coded directly in the
primitives of the language.
The problem solution is developing from the highest
levels downward, so that we are always “on top” of
the problem.
We always know, where we are heading and what
needs to be done.
15
C and Data Structures – Unit I
16
C and Data Structures – Unit I
17
C and Data Structures – Unit I
White spaces
Blank space
Horizontal Tab
Carriage return
Newline
Form feed.
In C-language uppercase and lowercase letters are
distinct.
C-Forms
Individual words of the text are called as forms.
There are six types of tokens.
The programs are written using these tokens and the
system of the language.
Forms
(i) Keywords (ii) Identifiers
(iii) Constants (iv) Strings
(v) Operators (vi) Special symbols.
Identifiers and keywords
Every word of ‘C’ is known as a keyword or an
Identifier.
Every keyword has fixed meaning and these
meanings cannot be changed.
Keywords act as basic building blocks of program
statements.
The rule for writing a keyword is that it should be
written in lowercase.
18
C and Data Structures – Unit I
ANSI C - Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers are defined as the manes of the variables
and other program elements like function names,
arrays etc., basically user defined names using the
combination of sequence of letters, and digits with a
letter as first character.
Both uppercase and lowercase letters can be used for
identifiers.
The underscore character “_” is also permitted in
identifiers.
Usually it is used to form a link between two words.
Note: Keywords are reserved words and hence
they should not be used as identifiers.
Data Types
C-language has a quite a few data types.
The variety of data types available allows the
programmer to select the type appropriate to the
needs of the application.
19
C and Data Structures – Unit I
20
C and Data Structures – Unit I
21
C and Data Structures – Unit I
Constants
Constants in ‘C’ are fixed values that do not get
change during the execution of a program.
Numeric constants are again divided into integer
constants and floating-point constants.
Integer constants
Do not contain decimal points.
Consists of sequence of digits.
Integer constants can be decimal, octal, or
hexadecimal.
Decimal integer constants consist of digits 0 through
9.
22
C and Data Structures – Unit I
23
C and Data Structures – Unit I
24
C and Data Structures – Unit I
25
C and Data Structures – Unit I
26
C and Data Structures – Unit I
Declaration of variables
All variable s must be declared before use, although
certain declarations can be made implicitly by
content.
A declaration specifies a data type and contains one
or more variables of the type.
The general form is
data-type V1, V2, V3....Vn;
27
C and Data Structures – Unit I
28
C and Data Structures – Unit I
29
C and Data Structures – Unit I
30
C and Data Structures – Unit I
Expressions
An expression is a combination of variables,
constants and operands following the syntactic rules
of the language.
The impressions can be again classified according to
the operators present in them as arithmetic
expressions or logical expressions etc.
Every valid expression yields a value, which can be
assigned to a variable.
Precedence of arithmetic operators
An arithmetic expression without parenthesis will be
evaluated from left to right using the precedence
rules of operators.
The priority of arithmetic operators is high priority is
given to *, /, % and then low priority is given to +, -.
High priority operators are evaluated first followed
by low priority operators.
Mixed-Mode Arithmetic
Generally in an expression, same type of operands
like integer or real (float) is used and hence type of
the expression will be accordingly real or integer.
But sometimes, we can have one operand as real and
the other as integer in an expression. Such an
expression is called a mixed-mode expression.
31
C and Data Structures – Unit I
32
C and Data Structures – Unit I
33
C and Data Structures – Unit I
34
C and Data Structures – Unit I
First C Program
main ()
{
printf (“Welcome to C programming”);
}
Every program should have only main () function.
This is the place where the program execution begins.
The opening brace ‘{‘ indicated the beginning of
main program and the closing brace indicates the end
of the program. All the statements between these two
braces from the body of the function, which contains
a set of instructions to perform the given task.
This program contains only one statement printf ()
with a message to be printed.
The lines beginning with /* and ending with */ are
comment lines, which are non-executable.
When you run this program, the message “Welcome
to C programming” will be displayed on the screen.
Example 2
main ()
{
int a;
float b;
a = 10;
b = 5.5;
printf(“%d %d”, a, b);
}
35
C and Data Structures – Unit I
36
C and Data Structures – Unit I
Exercises
(1) An identifier in ‘C’
(a) is a name of an entity such as variable and
function.
(b) is made up of letters, numerals and the
underscore character.
(c) an contain both uppercase and lowercase
letters.
(d) all of the above.
(2) Which are the following are the valid character
constants.
(a) ‘\n’ (b) ‘\\’ (c) ‘\0’ (d) all the above
(3) An expression
(a) is a collection of data objects and operators that
can be evaluated to a single value.
(b) is a name that substitutes for a sequence of a
characters.
(c) causes the computer to carry out some action
(d) all the above.
(4) The statement I*3; is equivalent to
(a) I=3*; (b) I=3; (c) I=*3; (d) i=i*3;
(5) A character variable can at a time store
(a) 1 character (b) 8 characters
(c) 254 characters (d) none of the above
(6) A ‘C’ variable cannot start with
(a) An alphabet (b) A number
(c) A special symbol (d) both (b) & (c)
37
C and Data Structures – Unit I
38
C and Data Structures – Unit I
39
C and Data Structures – Unit I
40
C and Data Structures – Unit I
6 5 4 3
41
C and Data Structures – Unit I
42
C and Data Structures – Unit I
Printing strings
The format specification for printing strings is similar
to that of real numbers.
%m, n, s
Mixing all the specifications
It is allowed to mix the data in one printf statement.
Example: printf (“%d %f %c %s”, x, y, z, t);
printf uses its control string to decide how many
variables to be printed and what their types are.
In summary, the format specifications used in printf()
function for the various data variables are:
Code Format
%c a single character
%d decimal numbers (integers)
%f floating values
%e floating with e-notation
%o Octal
%x hexadecimal
%s string
%u unsigned decimal
%ld long integer
%p print memory address value.
43
C and Data Structures – Unit I
Example 1:
main ()
{
int x = 100, z = 200;
float y = 205.75;
char c= ‘d’;
printf(“x=%d y = %f c=%c”, x, y, c);
printf(“%d + %d = %d”, x, z, x+z);
}
The first printf would print the values of x, y & c.
The second printf would print the sum of x & z.
The first %d will be for x, the second will be for and
the this will be for x+z.
Note that the format specifies could be used as
arguments to the binary operators.
Example 2:
main( )
{
int x = 78;
printf (“\n x in decimal is %d”, x)’
printf (“\n x in Octal is %o”, x);
printf (“\n x in Hen is %x”, x);
}
The first printf() would print he value 78.
The second one would print on the newline because
we have a ‘\n’ the value of x is octal.
The third statement would print the value of x in
hexadecimal form on the newline.
44
C and Data Structures – Unit I
45
C and Data Structures – Unit I
46
C and Data Structures – Unit I
47
C and Data Structures – Unit I
48
C and Data Structures – Unit I
49
C and Data Structures – Unit I
50
C and Data Structures – Unit I
51
C and Data Structures – Unit I
52
C and Data Structures – Unit I
53
C and Data Structures – Unit I
54
C and Data Structures – Unit I
55
C and Data Structures – Unit I
Start
Yes errors
any
No
link file.o
No Correct
output
Yes
stop
56
C and Data Structures – Unit I
57
C and Data Structures – Unit I
58
C and Data Structures – Unit I
59
C and Data Structures – Unit I
Exercises
(1) Which function would you use if a single key
were to be received
(a) scanf (b) gets (c) getchar (d) none
(2) If an integer were to be entered through a
keyword, which input function would you use?
(a) scanf (b) gets (c) getchar (d) printf( )
(3) A field width specifier in a printf() function
(a) controls the margins of the program listing.
(b) specifies the maximum value of a number.
(c) controls the size of types used to print numbers.
(d) specifies how many columns will be used to
print the number.
(4) If a character string s to be received through the
keyboard which function would you use.
(a) scanf( ) (b) gets( )
(c) getchar( ) (d) all the above can be used
(5) #directives may be present (recommended)
(a) before the main function
(b) after the main function
(c) at the end of the program
(d) anywhere in the program.
(6) The single character I/O functions are
(a) scanf( ) & printf( )
(b) getchar & putchar( )
(c) scanf( ) & putchar( )
(d) getchar( ) & printf( )
60
C and Data Structures – Unit I
61
C and Data Structures – Unit I
62
C and Data Structures – Unit I
63
C and Data Structures – Unit I
65
C and Data Structures – Unit I
67
C and Data Structures – Unit I
68
C and Data Structures – Unit I
Example: x = 10
x << 3 ( 1010 << 0011)
1010
1010
0100
1000
∴ The result will be 1000 (8).
Right shift
Denoted by >> and the contents will be shifted right.
Example: y = 6 ( 0110 )
y >> 2
0011
0001
∴ The result will be 0001 (1)
Conditional operator
Also known as ternary operator.
Denoted by ?.
The general format of this is
Expression1 ? Expression2 : Expression3
which results in expression 2 or expression 3 being
evaluated.
If expression 1 is true, then expression 2 is evaluated,
otherwise expression 3 is evaluated.
69
C and Data Structures – Unit I
70
C and Data Structures – Unit I
Example:
suppose i = 7, then if we have a statement x = ++i;
first the value of i is incremented to 8 and then it is
assigned to x.
∴ After execution, the value of x & i will be 8.
Where as if we have x = i++; with the present value
of i = 7, then the present value of i is assigned to x
and then the value of i is incremented to 8.
∴ After execution x =7 and i = 8.
Decrement operator
Denoted by -- (double minus).
The functionality of it is same as that of increment
operator, except that it subtracts the value by 1.
--i, and i-- is same as i = i - 1;
Like increment operator, we have pre decrement and
post decrement operators.
Example:
say x = 8;
z = -- x;
After execution i = 7 and x = 7
say y = 10;
z = y --;
After execution z = 10 and y = 9;
71
C and Data Structures – Unit I
Special operators
The comma operator
The first use of a comma is as a separator in the
variable declaration like int a, b, c;
Another use of comma operator is to link the related
expressions together.
A comma list of expressions is evaluated left to right
and the value of the combined expression.
Example: value = (t=10, b=18, t+b+5);
First assigns the value 10 to t, then 18 to b and finally
t+b+5 i.e. 33 to value.
Comma operator has the lowest precedence, of all
operators, therefore parentheses are necessary.
The sizeof operator
It is compile time operator, it results the number of
bytes the operand of it requires. The operand can be a
variable, a constant or a data type qualifier.
Example: m = sizeof (number);
n = sizeof (longint);
k = sizeof (FF);
Uses of size of
• An determining the lengths of arrays, structures
etc., when their sizes are not known.
• To allocate memory space dynamically to
variables during execution of a program.
72
C and Data Structures – Unit I
73
C and Data Structures – Unit I
Type casting
The cast operator is used to convert from one type to
another.
The cast operator syntax is
(cast-type) expression;
Example: result = (int) (29.2/4);
The (int) operator casts only (29.2) & the result will
be 7.
char ch;
int x, y, z;
x = (int) ch;
forces to convert the character to integer type.
SUMMARY OF C OPERATORS
Operator Meaning Associativity
() Function call Left to Right
[] Array element reference Left to Right
→ Pointer to structure Left to Right
Member reference Left to Right
Structure member reference Left to Right
− Unary minus Right to Left
++ Increment Right to Left
−− Decrement Right to Left
! Not Right to Left
∼ One’s complement Right to Left
* Pointer reference Right to Left
& Address Right to Left
sizeof Size of object Right to Left
type Type conversion Right to Left
* Multiplication Left to right
74
C and Data Structures – Unit I
75
C and Data Structures – Unit I
LIBRARY FUNCTIONS
The standard libraries are used to perform some
predefined operations on characters and strings etc.
There are many library functions for performing
different functions.
Most of the C compilers support the following
standard library facilities.
• Operations on characters
• Operations on strings
• Mathematical functions
• Input / Output functions
• Storage allocation functions
The declarations (prototypes) are placed in the header
files.
math.h
abs(x) Gets the absolute value of its argument
acos(x) Determines the Arc cos of x.
asin(x) Determines the Arc sin of x
atan(x) Determines the Arc tan of x.
ceil(x) Rounds its argument up to an integer.
cos(x) cos value of x , x in radians.
cosh(x) Hyperbolic cosine.
exp(x) Calculates ex
floor(x) Truncates x to an integer.
log (x) Determines Natural logarithm of x.
log10x Log to base 10 of x.
pow (x, y) calculates x raised to y.
sqrt (x) Calculates the square root of x.
76
C and Data Structures – Unit I
Functions of string.h
strcat(dest, source) Appends the contents of the
source string to the
destination string.
strcpy(dest, source) Copy the contents of the
source string to the
destination string
strcmp(dest, source) Compares source and
destination strings.
If dest < source returns -1
if dest = source returns 0
if dest > source returns 1
strncpy(dest, source, n) Copy exactly ‘n’ characters
from source to destination.
Functions of ctype.h
isalnum(ch) Checks whether ‘ch’ is an alphanumeric
character.
isalpha(ch) Checks whether ‘ch’ is an alphabet
iscntrl(ch) Checks whether ‘ch’ is a control
character.
isdigit(ch) Checks whether ‘ch’ is a digit
islower(ch) Checks whether a given character
belongs to a lower case alphabetic
character
isprint(ch) Checks whether a given character is a
printable (visible) character.
isspace(ch) Checks for white space character.
isupper(ch) Checks whether ‘ch’ is upper case
character.
77
C and Data Structures – Unit I
78
C and Data Structures – Unit I
EXERCISES
(1) Evaluate the following expressions:
a. g = b/2 + b * 4/b - b + a/3 ( a=1.5, b=3)
b. s = q*a/4-6/2+2/3*6/g; (q=2 , a =4 , g = 3 )
(2) Convert the following equations to C statements
z=9.8(a+b+c)z/t - 1.5+4b/(q + t)/(a+b)*(b-c)
t = -b + √b2-4ac / 2a
R = 2v + 1.6 g / (2a) / g + v.
(3) Give the output of the following program
segment:
int i = 2, j = 3,k,l;
float a, b;
k = i / j * j;
l = j / j *i;
a = i / j * j;
b = j / i * I;
printf (“ %d %d %f %f “ ,k, l, a, b);
(4) Which of the following shows the correct
hierarchy of arithmetic operations in C.
a. ( ) , ** ,* , / ,+ or - b. ( ) , * or | , + or -
c. ( ) , ** , | , * ,+ , - d. NONE
79
C and Data Structures – Unit I
80
C and Data Structures – Unit I
81
C and Data Structures – Unit I
CONTROL STATEMENTS
Decision making and Branching: If-else, switch,
break & continue
Normally the set of statements are executed
sequentially in a C program. But in practice, we
come across situations; where we may have to
change the order of execution of statements based
on certain conditions, i.e. break the normal flow of
sequential control.
This involves a kind of decision making to see
whether a particular condition has occurred or not
and then direct the control to execute certain
statements accordingly.
C supports such decision-making by providing the
following decision-making control statements:
• If statement
• Switch statement
These two are conditional branching statements.
There are some unconditional branching constructs
like goto, break and continue.
If statement
It is used to control the flow of execution of
statements.
It is a two-way decision statement, which is used in
conjunction with an expression.
82
C and Data Structures – Unit I
True
Expression
Next-statement
83
C and Data Structures – Unit I
84
C and Data Structures – Unit I
False true
Expression
?
Next Statement
85
C and Data Structures – Unit I
86
C and Data Structures – Unit I
87
C and Data Structures – Unit I
88
C and Data Structures – Unit I
89
C and Data Structures – Unit I
90
C and Data Structures – Unit I
91
C and Data Structures – Unit I
92
C and Data Structures – Unit I
3. if (expr) {
if (expr) {
….
….
}
else {
……
____
}
}
else {
if (expr) {
_____
_____
}
else {
____
____
}
}
93
C and Data Structures – Unit I
94
C and Data Structures – Unit I
95
C and Data Structures – Unit I
96
C and Data Structures – Unit I
97
C and Data Structures – Unit I
98
C and Data Structures – Unit I
Exercises:
1. Write a program to determine whether a number
is odd or even.
2. Write a program to find the largest of four
numbers.
3. Write a program to solve the quadratic equation to
check all possibilities of their coefficients
ax2+bx+c=0
Note: In the case labels of the switch statements if
the break statement is missing, then the
subsequent statements present in the next case-
label if any will be executed.
Goto Statement
So far we have seen the branching using if-else or
switch statements based on some conditions, hence
these statements are also called as conditional
statements.
Another way of branching to break the sequential
flow of control is unconditional branching i.e.
without testing any condition we branch to a
statement.
This is achieved by using the goto statement of C.
Although it may not be essential to use the goto
statement in a highly structured language like C,
there may be occasions when the use of goto might
be desirable.
The syntax of goto is
goto label;
99
C and Data Structures – Unit I
100
C and Data Structures – Unit I
}
The program used two goto statements, one at the
end, after printing the results to transfer the control
back to the input statement and the other to skip any
computation if the number is negative.
This program puts the computer in a permanent loop
known as an infinite loop i.e., it continuously
executes the statements until we take some special
steps to terminate the loop.
While programming, care should be taken to avoid
such loops.
Examples: 1. Program to calculate the sum of
squares of all integers between 1 and 100.
main ( ) {
int sum, n;
sum=0, n=1;
loop: sum = sum + n * n;
if (n == 10)
goto print;
else
{
n++;
goto loop;
}
print: printf ("sum = %d", sum);
}
101
C and Data Structures – Unit I
2. Program to print all the numbers less then 200 that are
divisible by 10.
main ( ) {
int number;
number =1;
loop: if ((number %10) == 0))
printf ("%d\t", number);
number ++;
if (number != 200)
goto loop;
}
Decision making and looping: While, do-while, for
statements and continue statement
We have seen that sometimes it becomes essential to
repeatedly execute a segment of a program. This was
done with a goto statement, by initializing a counter and
later testing it, using the if statement.
Consider the program to calculate the sum of squares of
100 integers.
sum = 0;
n = 1;
loop: sum + = n * n;
if (n == 100) goto print;
else
{
n++;
goto loop;
}
print: printf ("%d", sum);
102
C and Data Structures – Unit I
103
C and Data Structures – Unit I
While statement
The basic format of the while loop is
while (testing condition)
{
body of the loop;
}
While loop is an entry-controlled loop statement.
The test condition is evaluated and if the condition
is true, then the body of the loop is executed. After
execution of the body, the test condition is once
again evaluated and if it is true, the body is executed
once again.
This process of execution of the body is continued
until the test condition is false. Then the control is
transferred out of the loop.
104
C and Data Structures – Unit I
105
C and Data Structures – Unit I
106
C and Data Structures – Unit I
107
C and Data Structures – Unit I
108
C and Data Structures – Unit I
109
C and Data Structures – Unit I
110
C and Data Structures – Unit I
111
C and Data Structures – Unit I
1
2 2
3 3 3
: : : :
n n n n n
main ( ) {
int n, i, j;
printf ("Enter the value of n");
scanf ("%d", &n);
for (i=1; i<=n; ++i) {
for (j=1; j<=i; ++i) {
printf ("%d", i)
}
printf ("\n");
}
}
The three sections of the for loop are optional. We
can ignore some of the sections depending on the
requirement.
We can ignore the initialization section and have a
for loop like this for( ; j <= 100; ++j). Similarly we
can have a for loop without increment section,
which case we assume that we increment the loop
control variable within the body of the for loop.
We can even have a for loop like for (; ;) without
any sections, which will create an infinite loop.
Note: you should not have a semicolon immediately
after the for-loop (unless you desire to create
a delay within the program), otherwise the
following statements will not be repeated.
112
C and Data Structures – Unit I
113
C and Data Structures – Unit I
114
C and Data Structures – Unit I
115
C and Data Structures – Unit I
116
C and Data Structures – Unit I
Exercises:
1. Write a program to generate the sum and product
of the digits of the number.
2. Write a program to read a number and count the
occurance of a particular digit in it. (Ex: In the
number 1257653 the digit 5 is occurring 2 times)
3. Write a program to generate the following series:
(a) 1, 2, 3, 4, ….., n
(b) 0, 2, 4, 6, ….., n
(c) 1, 22, 32, 42, …., n2
(d) 1, 23, 33, 43, …., n3
4. Write a program to calculate ncr and npr
ncr = n! npr = n!
r!(n-r)! (n-r)!
5. The 'break' statement may be used to exit from
(a) a do-while loop (b) a for loop
(c) a switch statement (d) all of the above
6. In which statements, does a 'continue' statement
cause the control to go directly to the test
condition and then continue the loop process.
(a) do-while and while
(b) while and 'if-else'
(c) 'do-while' and 'if-else'
(d) none of the above
117
C and Data Structures – Unit I
int a = 20, b, c;
if (a >= 40)
b = 30;
c = 20;
printf("%d %d", b, c);
}
(b) main( ) {
int x=10, y=30;
if(x ! = y);
printf("%d%d%d", x, y);
}
(c) main() {
int x=3, y=10;
if(x==3)
printf ("%d", x);
else;
printf ("%d",y);
}
(d) main() {
int val = 35, k = 20;
printf("%d %d %d", k == 35, k = 50, k >
100);
}
118
C and Data Structures – Unit I
(b) main() {
char x;
for(x = 0; x <= 255; ++x) {
printf ("%d %c", x, x)
}
}
(c) main( ) {
int i = 100;
while(I = 100);
printf("Infinite loop");
}
(d) main( ) {
int i;
for(i=1; i<=10);
printf("%d", i);
i++;
}
119
C and Data Structures – Unit I
(e) main( ) {
int i = 1, j = 1;
for ( ; ;) {
if (I > 5)
break;
else
j += i;
printf ("%d", j);
i + = j;
}
}
9. Give the output of the following:
(a) main( ) {
int i=0;
switch(i)
{
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
}
(b) x = 1;
y = 1;
if (n > 0)
x += 2;
y -= 2;
printf ("%d %d", x, y);
10. Determine how many times the body of the
loop will be executed.
120
C and Data Structures – Unit I
m++;
n += 2;
}
(b) int i;
int n = 7;
{
…
…
}
11. What is the output of the following program?
main ( ) {
int x, y, z;
x = y = 0;
while (y < 10) ++y; x + = y;
printf ("%d %d", x, y);
for (y = 1; y < 10; y++) x = y;
printf ("%d %d", x, y);
}
121