selfstudy(c programming)
selfstudy(c programming)
Programming in C
LEARNING OBJECTIVES
}
executes as
on its left. ++ i
++ i pre-increments
Precedence Decreases as We Move from ++ i
Top to Bottom X = i + i + i + i + i
Examples:
i ++ ; }
i ++ ; post-increments
}
executes as Notes: ‘C’ allows both implicit and explicit type conversion.
- - j; Type conversion is of two types:
- - j; pre decrements
1. Narrowing: Conversion of ‘higher’ type to ‘lower’
- - j;
type.
y = j + j + j + j + j ;
2. Widening: Conversion of ‘lower’ type to ‘higher’ type.
j - -;
}
j - -; post decrements.
y = 35; j = 5
Widening
Char – int ¬– long – float – double – long double
%f floating point
Implicit
%h short int
C automatically converts any intermediate values to proper
type so that the expression can be evaluated without losing %o octal integer
{ {
Body of main; Statement2(s);
} }
User defined function area; else
{
Documentation section/comments Ignored by com- Statement3(s);
piler, provides additional information to user to improve }
readability.
Nested if:
Preprocessing Tells the compiler to do pre-processing if (expression1)
before doing compilation. For example {
Statement(s)1;
#include < stdio.h > tells to include stdio header file.
if (expression(s)2)
else
Global declaration It contains variable declarations, these
Statement(s)3;
are accessible in more than one function.
}
Function Functions are main building blocks of ‘C’ pro- else
gram. Every ‘C’ program contains one or more functions. A Statement(s)4;
mandatory function called ’main( )’ instructs the compiler Note: If the expression evaluates to true then the statements
to start execution from here. of if block gets executed otherwise else block statements
will execute.
User defined area Here user can define his own functions.
Example 2: Consider the following program segment:
if (a > b) printf (“a > b”);
Control Statements else
printf (“else part”);
The statement that controls the execution sequence of a pro-
printf (“a < = b”);
gram is called “control statement”.
a < = b will be printed if
The control statements are classified as: (A) a > b (B) a < b
1. Selection statement: if, switch (C) a = b (D) all of these
2. Iterative/looping statement: While, do-while, for Ans: (D)
3. Unconditional jump statements: break, continue, Because the statement, printf(“a < = b”); is not the part of
return, goto either if block or else block.
The switch statement Switch is a multi-way (n-way)
Selection/Decision-making Statement selection statement.
Makes a decision to select and execute statement(s) based
on the condition. ‘C’ supports if and switch selection Syntax:
statements. switch (var_name/exp)
{
The if statement “if ” is called two-way selection statement. case const1: stmts1;
Syntax: break;
if (expression) // simple-if case const2: stmts2;
statement(s); break;
if (expression) // if-else .
{ .
statement1(s); .
} case constn: stmts n;
else break;
{ default: statements;
statements(s); }
} Notes:
if (expression) // ladder else-if. •• For switch only the integral (integer/char) type variables
{ or expression evaluates to integral allowed.
Statement1(s); •• Absence of break after case statements leads continua-
} tion execution of all case statements followed by match-
else if (expression2) ing case block.
3.8 | Unit 3 • Programming and Data Structures
Exercises
Practice Problems 1 printf ( “%d \n”, clrscr());
Directions for questions 1 to 15: Select the correct alterna- }
tive from the given choices. Output of the above program will be?
(A) error (B) No output
1. What will be the output of the following program? (C) 1000 (D) 1
void main()
{ 6. Output of the following program is
int i; main( )
char a[ ] =” \0 ”; {
if (printf(“%s\n”, a)) int i = -2;
printf (“ok \n”); +i;
else printf(“i = %d, +i = %d\n”, i, +i);
printf(“program error \n”); (A) error (B) -2, +2
} (C) -2, -2 (D) -2, 2
(A) ok (B) progam error 7. main( )
(C) no output (D) compilation error {
2. Output of the following will be int n;
# define FALSE-1 printf(“%d”, scanf (“%d”, & n));
# define TRUE 1 }
# define NULL 0 For the above program if input is given as 20. What will
main( ) be the output?
{ (A) 20 (B) 1
if(NULL) (C) 2 (D) 0
puts(“NULL”); 8. How many times will the following code be executed?
else if(FALSE) {
puts(“TRUE”); x = 10;
else while (x = 1)
puts(“FALSE”); x ++;
} }
(A) NULL (B) TRUE (A) Never
(C) FALSE (D) 1 (B) Once
3. main( ) (C) 15 times
{ (D) Infinite number of times
printf(“%x”,-1 << 4) ; 9. The following statement
} printf(“%d”, 9%5); prints
For the above program output will be (A) 1.8 (B) 1.0
(A) FFF0 (B) FF00 (C) 4 (D) 2
(C) 00FF (D) 0FFF 10. int a;
4. For the following program printf(“%d”, a);
# define sqr (a) a*a What is the output of the above code fragment?
main( ) (A) 0 (B) 2
{ (C) Garbage value (D) 3
int i; 11. printf(“%d”, printf(“time”));
i = 64 / sqr(4); (A) syntax error
printf( “%d”, i); (B) outputs time 4
} (C) outputs garbage
output will be (D) prints time and terminates abruptly
(A) 4 (B) 16 12. The following program
(C) 64 (D) compilation error main( )
5. #define clrscr ( ) 1000 {
main ( ) int i = 2;
{ {
clrscr(); int i = 4, j = 5;
Chapter 1 • Programming in C | 3.11
printf (“%d%d”,i,j); 14. What is the output of the following program segment?
} int a = 4, b = 6;
printf (“%d%d”,i,j); printf(“%d”, a = b);
} (A) Outputs an error message
(A) Compiler error: unrecognised symbol j; (B) Prints 0
(B) Prints 2545 (C) Prints 1
(C) Print 4525 (D) None of these
(D) None of the above
15. The statements:
13. What is the output of the following program fragment? a = 7;
for (i = 3; i < 15; i + = 3); printf(“%d”, (a++));
printf (“%d”, i); prints
(A) a syntax error (B) an execution error (A) Value of 8 (B) Value of 7
(C) prints 12 (D) prints 15 (C) Value of 0 (D) None of the above
Answer Keys
Exercises
Practice Problems 1
1. A 2. B 3. A 4. C 5. C 6. C 7. B 8. D 9. C 10. C
11. B 12. A 13. D 14. D 15. B
Practice Problems 2
1. B 2. D 3. C 4. D 5. C 6. D 7. C 8. C 9. C 10. C
11. C 12. C