0% found this document useful (0 votes)
2 views11 pages

selfstudy(c programming)

Chapter 1 of the document provides an introduction to programming in C, covering fundamental concepts such as variables, data types, constants, and control statements. It explains the syntax for declaring variables, the use of the const keyword, and the importance of type conversion. Additionally, it outlines the structure of a C program, including the documentation section, preprocessing, and control flow statements.

Uploaded by

tia641246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views11 pages

selfstudy(c programming)

Chapter 1 of the document provides an introduction to programming in C, covering fundamental concepts such as variables, data types, constants, and control statements. It explains the syntax for declaring variables, the use of the const keyword, and the importance of type conversion. Additionally, it outlines the structure of a C program, including the documentation section, preprocessing, and control flow statements.

Uploaded by

tia641246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Chapter 1

Programming in C

LEARNING OBJECTIVES

• Basic concepts • Precedence decreases as we move from top to bottom


• Character set • Type conversion
• Identifier • Documentation section
• Declaring a variable • Preprocessing
• Visualization of declaration • Global declaration
• Constants • Control statements
• Single character constants • Selection/Decision making statement
• String constants • Looping statements
• Using const keyword • Unconditional jump statements

BasiC COnCepts Variable


The name itself represents value, is not constant. Variable is a
Character Set data name whose value varies/changes during program execution.
Variable name is a name given to memory cell (may be one or
A character refers to an alphabet, digit or a special symbol. multiple bytes).
Alphabets: A – Z, a – z
Digits: 0 -9
Special symbols:
Data types
∼ ! # % ∧ and * ( ) - + { } [ ] - < > , . | ? \ | : ; ” ’ White space Represents type of data and set of operations to perform on data .
Data Type
Identifier Primitive/Basic Derived User defined Valueless
Identifier is a user-defined name used for naming a variable or a – Char – Array – Structure
function. – float – pointer – union – void
Rules for naming an identifier
– double Enumeration
• Consists only letters, digits and underscore – integer
• Starts only with an alphabet or underscore
• Keywords cannot be used.
Type Keyword Number of Bytes
• Can be as long as you like, first 31 characters are significant.
Integer int 2

Example: Valid identifiers: RollNo, Roll_No, _Roll_No Floating float 4


rollno, Name2; Double double 8
Invalid: 2name, Roll No. Character char 1
3.4 | Unit 3 • Programming and Data Structures

Declaring a Variable Constants


A constant value is one which does not change during the
•• Before using a variable, you must give some information
execution of a program.
to compiler about the variable. i.e., you must declare it.
C supports several types of constants:
•• Declaration statement includes the type and variable
name. 1. Integer constants
2. Real constants
Syntax:
3. Single character constants
Datatype Var_name;
4. Strings constants
Example:
int roll_no;
char ch; Integer constants
float age;
An integer constant is a sequence of digits. It consists of
•• When we declare a variable a set of digits 0 to 9 preceded by an optional + or - sign
•• memory space is allocated to hold a value of specified spaces, commas, and non-digit characters are not permitted
type. between digits.
•• space is associated with variable name Examples for valid decimal integer constants are
•• space is associated with a unique Address. 123
-31
Table 1 Visualization of declaration 0
roll no 562321
int roll no; garbage
+78
Examples for invalid integer constants are
2002
20,000
marks `1000
int marks = 10; 10
3008 Real constants
diameter Real constants consist of a fractional part in their represen-
float diameter = 5.9 5.9 tation. Integer constants are inadequate to represent quanti-
4252 ties that vary continuously.
ch → variable name Examples of real constants are
char ch : ‘A’ A → value
0.0026
-0.97
2820 → address
435.29
Note: The default value is garbage, i.e., an unknown value +487.0
is assigned randomly.
Renaming data types with typedef Typedef is a keyword,
Single character constants
which can form complex types from the basic type, and will A single character constant represents a single character
assign some simpler names for such combinations. This is which is enclosed in a pair of quotation symbols.
more helpful when some declaration is very tough, confus- Examples for character constants are
ing or varies from one implementation to another. ‘5’
For example, the data type unsigned long int is redefined ‘x’
as LONG as follows: ‘;’
typedef unsigned long int LONG;
String constants
Uses of enumerated data types Enumerated data types are
A string constant is a set of characters enclosed in dou-
most useful when one is working over small, discrete set
ble quotation marks. The characters in a string constant
of values, in which each is having a meaning and it is not
sequence may be alphabet, number, special character and
a number.
blank space.
A best example can be given on months jan, feb, mar, …,
dec, which are 12 in number, with assigning consecutive num- Examples of string constants are
bers for it. “VISHAL”
The main advantages are storage efficiency, the c-code “1234”
can become readable “C language”
“!….?”
Chapter 1 • Programming in C | 3.5

Naming constants 2. int a, b = 55, c = 10;


A name given to a constant value. Value of name does not initializes ‘b’ with 55 and ‘c’ with ‘10’.
change during program execution. b + c = a; // Invalid
Only variable is allowed on left side of assignment.
Using const keyword 3. int a = 15, b = 20, c = 2, d = 5, e = 10, f, g, h, i;
When we use ‘const’ with data type, memory will be allo- f = a << c;
cated to variable and the initialized value does not change. ‘a’ is left shifted for ‘c’ times and result stored in ‘f’
const int x = 10; i.e.,
const float pi = 3.141; a = 15 = (1 1 1 1)2
↓ ↓ ↓ ↓
Using # define 1 1 1 1 0 (After first shift)
# define x 10 ↓↓↓↓
# define pi 3.141 1 1 1 1 0 0 (After second shift)
Where ‘# define’ is instruction to preprocessor so memory One left shift multiplies 15 by 2 = 30
is allocated. The preprocessor replace each occurrence of Again the 2nd left shift multiplies 30 by 2 = 60
name with value in program before execution. Thus 15 × 22 = 60, where the power of 2 is the number
of times shift is made. Value of ‘f ’ becomes 60.
Operator Note: Left shift multiplies the value by 2. Right shift divides
An operator is a symbol which performs operations on the value by 2.
given data elements. g = a and b;
a-01111
Table 2 Precedence and Associativity b-10100
( ) Parenthesis ______________
[ ] Index    0 0 1 0 0 = 4
L-R ______________
→ Member of ‘&’ performs bitwise AND. So ‘g’ value is ‘4’.
• Member of h = a|b; a - 0 1 1 1 1
Pre ++, - - b-10000
(unary) - , &(address of ) R-L _______________
* (Indirection )    1 1 1 1 1 = 31
_______________
Arithmetic * , /, % L-R “I ” performs bitwise ‘OR’. R value is ‘31’.
Arithmetic: +, - L-R i=a^d: a-1111
Bitwise shift : ,  L-R b - 0 1 0 1
_____________
Relational: <, >, =. >, > = = =, ! = L-R
   1 0 1 0 = 10
Bitwise ex -OR : ∧ L-R _____________
‘^’ performs bit-wise ex - OR. i value is ‘10’.
Logical AND : && L-R
4. int a = 100 , b = 200, c = 300, x;
Logical OR : || L-R
x = (a > b)?((a > c)? a:c):((b > c)? b:c);
Conditional: ? : R-L    false     c
Assignment & compound Assignment
R-L x = c
=, + =, - =, * =, / = ; % = so, x = 300
Separation operator: , (comma) L-R 5. int i = 10, j = 10, x, y;
x = i+++++i+i+++++i+++i
Note: For Assignment operator, only a variable is allowed

}
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

1. int a,b,c; so x = 65, i = 15.


a = b = c = 0; y = j - - + - - j + j - - + - - j +
Assigns ‘0’ to a,b,c; - - j
3.6 | Unit 3 • Programming and Data Structures

}
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

6. int i = 10; Narrowing

printf(“%d%d%d%d%d”, i++, ++i, ++i, Note: Narrowing causes loss of data.


i++, ++i); Input/output Functions
evaluates the values in printf from Function Purpose
right to left.
printf prints formatted string
So
i++, ++i, ++i, i++, ++i scanf reads formatted string

getchar reads character


Prints 14 14 13 11 11 putchar displays a character
Printf (“%d”, i) gets reads a string
Prints 15:
puts displays a string

Type Conversion Format Specifier Purpose


‘C’ allows mixed mode operations, i.e., variables of differ- %c single character
ent type may appear in same expression. To perform the
operation, the data need to convert into compatible type. %d decimal integer

The conversion takes place in two ways: %e floating point

%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

any significance. %x hexa decimal


For mixed mode operations, generally the ‘lower’ type is
%s string
automatically converted to ‘higher’ type before the opera-
tion proceeds. %u unsigned decimal integer

Note: scanf(“%s”, string_var); does not read string which


Explicit
contains white space. Hence to read multi word string use
‘C’ allows programmer to use type conversion operator to gets(string_var);
convert a data value to the required type.
Example 1: Which of following comment regarding the
Syntax: reading of a string using scanf( ) and gets ( ) is true?
V1 = (type) V2; (A) Both can be used interchangeably
Type in parenthesis represents the destination type. (B) scanf is delimited by end of line, gets is delimited by
Example: int a = 3, b = 2, float x, y; blank space
(C) scanf is delimited by blank, gets is delimited by end of
Case I: x = a/b;
line
   results x = 1.000000
(D) None of these
Case II: y = (float) a/b; Ans: (C)
results y = 1.500000.
Because, in case 1, the integer division is performed and
so returns an integer by division operator. While assigning the
Program Structure
integer value implicitly converted to 1.000000, then assigns /* Documentation section */
to float variable x where as in case 2, (float)a converts value Preprocessor commands;
of ‘a’ to float, the second variable ‘b’ is integer. The compiler Global declaration;
implicitly converts integer to float. Then it performs float main ()
division. So 1.500000 is stored into floating variables.
Chapter 1 • Programming in C | 3.7

{ {
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

Example 3: do-while is same as ‘while; except that the statement(s) will


main () execute for at least once.
{ Notes:
int i = 10; •• The condition will not be evaluate to execute the block
switch(i) for first time.
{ •• ‘do-while’ is called exit-control loop.
case 10 : printf (“case 10”);
Example 5:
case 15 : printf (“case 15”);
main ( )
case 20 : printf (“case 20”);
{
default : printf (“default case”);
int i = 0;
}
while (i! = 0)
}
{
Output: Case 10 case 15 case 20 default case printf(“%d”, i);
Reason: Missing break after each case, leads to execution i++;
of all the cases from matching case. }
Example 4: }
main ( ) No output, because the condition is false for the first time.
{ main ( )
int i = 10; {
switch (i) int i = 0;
{ do
case 10 : printf(“case 10”); {
break ; printf(“%d”, i);
case 8 + 2 : printf(“case 8+2”); i++;
break; } while (i! = 0);
default : printf(“ No matching case”); }
} Output: Displays 0 to 32767 and-32768 to-1
}
The for loop ‘for’ provides more concise loop control
Program raises an error called ‘Duplicate case’ while com-
structure.
piling because the expression ‘8 + 2’ evaluates to ‘10’.
Syntax:
Looping Statements for(exp1; exp2; exp3)
{
Sometimes, there is a situation to execute statement(s) repeat-
Statement(s);
edly for a number of times or until the condition satisfies. C’
}
supports following looping statements: while, do-while, for.
Expression 1: Initialization expression may contain mul-
While Statement tiple initializations. It executes only once before executing
the loop for first time.
Syntax: while (condition)
{ Expression 2: Condition expression. Only one condition
Statement(s); expression is allowed. That may be single or compound
} condition, evaluates before every execution.
If the condition is true the block of statements will execute Expression 3: Modification statement may contain multi-
and control returns to condition, i.e., the statement(s) exe- ple statements. It executes on completion of loop body for
cutes till the condition becomes false. every iteration.
Notes: Note: All the expressions in parenthesis are optional. Two
•• ‘While’ executes the block either ‘0’ or more times. semi-colons (;) are compulsory even though there are no
•• ‘While’ is called entry control loop. expressions.
Odd loops In the for loop, while loop, the condition speci-
Do-while Statement. fies the number of times a loop can be executed. Sometimes
Syntax: a user may not know, about the number of times a loop is
do to be executed. If we want to execute a loop for unknown
{ number of times, then the concept of odd loops should be
Statement(s); implemented, these can be done using the for, while (or)
} while (condition); do-while loops. Let us illustrate odd-loop with a program
Chapter 1 • Programming in C | 3.9

# include <stdio.h> Reverse jump, executes the statements repeatedly where as


main() in forward jump, the statements are skipped from execution.
{
int num, x; Example 6:
num = 1; main( )
while (num = = 1) {
{ int i ;
printf (“enter a number“); for (i=1; i<=10; i++)
scanf (“%d”, & x); {
if((x % 2) = = 0) if (i = = 5)
printf(“number is even”); break;
else printf(“%d” , i);
printf(“number is odd”); }
printf(“do u want to test any num.”); }
printf(“for yes-enter ‘1’, No-enter ‘0’”); output:   1 2 3 4
Scanf(“%d”,& num); if i = 5, then the loop will break.
}
} Example 7:
main( )
Unconditional Jump Statements {
int i ;
•• “C” language permits to jump from one statement to for (i = 1; i<=10; i++)
another. {
•• ‘C’ supports break, continue, return and goto jump if (i = = 5)
statements. continue;
printf(“%d” , i);
Break statement Breaks the execution sequence. That is
}
when the break statement executes in a block (loop) it’ll
}
come out from block (loop).
o/p: 1 2 3 4 6 7 8 9 10
Syntax: if i = 5, the loop statements skipped for that iteration. So it
break; does not print ‘5’.
Continue statement Used to skip a part of the loop under Example 8:
certain conditions. Output for the following program segment
Syntax: for (i = 1, j = 10 ; i < 6; ++i, --j)
continue; printf(“\n %d %d”, i, j);

Return statement Terminates the execution of a function Output:


and returns the control to the calling function. 1 10
Syntax: 2 9
return [exp/value];
3 8
Goto statement Jumps from one point to another with in a
4 7
function.
5 6
Syntax:
label1: goto label2:
Statement(s); Statement(s); Note: Since for statement allows multiple initialization and
goto label1; label2; multiple update statements, expression 1 and expression 3,
reverse jump forward jump does not raise any error.
3.10 | 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

Practice Problems 2 9. An unrestricted use of ‘goto’ statement is harmful because


Directions for questions 1 to 12: Select the correct alterna- (A) it results in increasing the executing time of the
tive from the given choices. program
(B) it increases the memory of the program
1. If the condition is missing in a FOR loop of a C pro- (C) it decreases the readability and testing of program
gram then (D) None of the above
(A) It is assumed to be present and taken to be false
(B) It is assumed to be present and taken to be true 10. What will be the output?
(C) It results in syntax error main()
(D) Execution will be terminated abruptly {
int i = 0, j = 0;
2. Which of the following operators in ‘C’ does not asso-
if(i && j ++)
ciate from the right?
printf(“%d..%d”, i++, j);
(A) = (B) + =
printf(“%d..%d”, i, j);
(C) postfix++ (D) >
}
3. In a C programming language x - = y + 1 means (A) 1..1 (B) 2..2
(A) x = -x - y - 1 (B) x = x - y + 1 (C) 0..0 (D) 1..1, 1..1
(C) x = x - y - 1 (D) x = -x + y + 1
11. What is the output?
4. Minimum number of temporary variables needed to main ()
swap two variables is {
(A) 1 (B) 2 int a = 0;
(C) 3 (D) 0 int b = 20;
5. A preprocessor command char x = 1;
(A) need not start on a new line char y = 10;
(B) need not start on the first column if(a, b, x, y);
(C) has # as the first character printf(“hello”);
(D) comes after the first executable statement }
6. printf (“%d”, printf (“%d”, printf(“time4kids”))); (A) logical error (B) Garbage value
(A) Outputs time (B) Syntax error (C) hello (D) 20
(C) Outputs 9 (D) None of the above 12. What will be the value of count after executing the
7. for (i = 1; i < 5; i++) below program:
if (i!=3) main ( ) {
int count = 10, digit = 0;
printf(“%d”, i); while (digit < = 9) {
Outputs: printf (“%d\n”, ++count);
(A) 12345 (B) Error ++digit;
(C) 1245 (D) 0000 }
8. Which operand in ‘C’ takes only integer operands? }
(A) * (B) / (A) 10 (B) 11
(C) % (D) + (C) 20 (D) 21
3.12 | Unit 3 • Programming and Data Structures

Previous Years’ Questions


1. Which one of the following are essential features of 4. Suppose n and p are unsigned int variables in a C pro-
an object-oriented programming language? gram. We wish to set p to nC3. If n is large, which one of
(i) Abstraction and encapsulation the following statements is most likely to set p correctly?
(ii) Strictly-typedness [2014]
(iii) Type-safe property coupled with sub-type rule (A) p = n * (n - 1) * (n - 2)/6;
(iv) Polymorphism in the presence of inheritance (B) p = n * (n - 1) /2* (n - 2)/3;
[2005] (C) p = n * (n - 1) /3 * (n - 2)/2;
(A) (i) and (ii) only (D) p = n * (n - 1) * (n - 2)/6.0;
(B) (i) and (iv) only 5. The secant method is used to find the root of an equa-
(C) (i), (ii) and (iv) only tion f (x) = 0. It is started from two distinct estimates
(D) (i), (iii) and (iv) only xa and xb for the root. It is an iterative procedure
involving linear interpolation to a root. The iteration
2. Which of the following are true? stops if f (xb) is very small and then xb is the solution.
(i) A programming language which does not permit The procedure is given below. Observe that there is
global variables of any kind and has no nesting of an expression which is missing and is marked by ?.
procedures/functions, but permits recursion can be Which is the suitable expression that is to put in place
implemented with static storage allocation of ? so that it follows all steps of the secant method?
[2015]
(ii) Multi-level access link (or display) arrangement is
needed to arrange activation records only if the pro- Secant
gramming language being implemented has nesting Initialize: xa, xb, ε, N // ε = convergence indicator
of procedures/functions // N = maximum no. of
iterations
(iii) Recursion in programming languages cannot be
fb = f (xb)
implemented with dynamic storage allocation
i = 0
 (iv) Nesting procedures/functions and recursion require while (i < N and | fb | > ε) do
a dynamic heap allocation scheme and cannot be i = i + 1 // update counter
implemented with a stack-based allocation scheme xt = ? // missing expression for
for activation records // intermediate value
(v) Programming languages which permit a function to xa = xb // reset xa
return a function as its result cannot be implemented xb = xt // reset xb
with a stack-based storage allocation scheme for acti- fb = f (xb) // function value at new xb
vation records end while
[2008] if | fb | > ε then // loop is terminated with i = N
(A) (ii) and (v) only    (B) (i), (iii) and (iv) only write “Non-convergence”
(C) (i), (ii) and (v) only (D) (ii), (iii) and (v) only else
write “return xb”
3. What will be the output of the following C program end if
segment? (A) xb – ( fb – f (xa) ) fb / (xb – xa)
char inChar = ‘A’; (B) xa – (fa – f(xa) ) fa / (xb – xa)
switch(inChar) { (C) xb – (xb – xa) fb / ( fb – f (xa))
case ‘A’: printf(“choice A\n”): (D) xa – (xb – xa) fa / ( fb – f (xa))
case ‘B’: 6. Consider the following C program:
case ‘C’: printf(“choice B”); #include<stdio.h>
case ‘D’ int main( )
case ‘E’: {
default: printf(“No Choice”);}[2012] int i, j, k = 0;
(A) No choice j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
(B) Choice A k -= --j;
(C) Choice A for (i = 0; i < 5; i ++)
Choice B No choice {
(D) Program gives no output as it is erroneous switch(i + k)
Chapter 1 • Programming in C | 3.13

{ ensure that the loop terminates in a state satisfying the


case 1: condition x == (y*q + r)? [2017]
case 2: printf(“\n%d”, i + k); (A) (q == r) && (r == 0)
case 3: printf(“\n%d”, i + k); (B) (x > 0) && (r == x) &&(y > 0)
default: printf((“\n%d”, i + k); (C) (q == 0) && (r == x) && (y > 0)
} (D) (q == 0) && (y > 0)
}
8. Consider the following C Program.
return 0;
#include<stdio.h>
}
int main () {
The number of times printf statement is executed is int m = 10;
_______.[2015] int n, nl ;
7. Consider the C program fragment below which is n = ++m;
meant to divide x by y using repeated subtractions. nl = m++;
The variables x, y, q and r are all unsigned int. n−−;
while (r >= y) { −−nl;
r = r − y; n −= nl;
q = q + 1; printf (“%d”, n) ;
} return 0;
Which of the following conditions on the variables x, }
y, q and r before the execution of the fragment will The output of the program is ___________. [2017]

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

Previous Years’ Questions


1. B 2. D 3. C 4. B 5. C 6. 10 7. C 8. 0

You might also like