Module 2-Question Bank
1) Write a note on Preprocessor directives in C.
Before a C program is compiled in a compiler, source code is processed by a program
called preprocessor. The commands used inn preprocessor are called preprocessor directives and they
begin with ‘#’ symbol.
Preprocessor Syntax/Description
Syntax: #define
Macro This macro defines constant value and can be any of the
basic data types.
Syntax: #include <file_name>
Header file inclusion The source code of the file “file_name” is included in the
main program at the specified place.
Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source
Conditional compilation
program before compilation with respect to the
condition.
Syntax: #undef, #pragma
#undef is used to undefine a defined macro variable.
Other directives
#Pragma is used to call a function before and after main
function in a C program.
2) Write a note on various operators in C.
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of
operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20 then −
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer B%A=0
division.
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, (A == B)
then the condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If the (A != B)
values are not equal, then the condition becomes true. is true.
> Checks if the value of left operand is greater than the value of (A > B) is
right operand. If yes, then the condition becomes true. not true.
< Checks if the value of left operand is less than the value of (A < B) is
right operand. If yes, then the condition becomes true. true.
>= Checks if the value of left operand is greater than or equal to (A >= B)
the value of right operand. If yes, then the condition becomes is not
true. true.
<= Checks if the value of left operand is less than or equal to the (A <= B)
value of right operand. If yes, then the condition becomes is true.
true.
Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then −
Operator Description Example
&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then the condition becomes true.
|| Called Logical OR Operator. If any of the two operands (A || B) is true.
is non-zero, then the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and
^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100 and B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and
variable 'B' holds 13, then −
Operator Description Example
& Binary AND Operator copies a bit to the result (A & B) = 12, i.e., 0000
if it exists in both operands. 1100
| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e., 0011
either operand. 1101
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e., 0011
in one operand but not both. 0001
~ Binary Ones Complement Operator is unary (~A ) = -61, i.e,. 1100
and has the effect of 'flipping' bits. 0011 in 2's complement
form.
<< Binary Left Shift Operator. The left operands A << 2 = 240 i.e., 1111
value is moved left by the number of bits 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands A >> 2 = 15 i.e., 0000
value is moved right by the number of bits 1111
specified by the right operand.
Assignment Operators
The following table lists the assignment operators supported by the C language −
Operator Description Example
= Simple assignment operator. Assigns values C = A + B will assign
from right side operands to left side operand the value of A + B to C
+= Add AND assignment operator. It adds the right C += A is equivalent to
operand to the left operand and assign the result C=C+A
to the left operand.
-= Subtract AND assignment operator. It subtracts C -= A is equivalent to
the right operand from the left operand and C=C-A
assigns the result to the left operand.
*= Multiply AND assignment operator. It multiplies C *= A is equivalent to
the right operand with the left operand and C=C*A
assigns the result to the left operand.
/= Divide AND assignment operator. It divides the C /= A is equivalent to
left operand with the right operand and assigns C=C/A
the result to the left operand.
%= Modulus AND assignment operator. It takes C %= A is equivalent
modulus using two operands and assigns the to C = C % A
result to the left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C =
C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C =
C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C =
C&2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C
^2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C
|2
Misc Operators : sizeof & ternary
Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.
Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will
return 4.
& Returns the address of a variable. &a; returns the actual address of
the variable.
* Pointer to a variable. *a;
?: Conditional Expression. If Condition is true ? then value X :
otherwise value Y
3) Differentiate keywords and identifiers.
Keywords Identifiers
Reserved words User defined words
Lower case letters only Lower case or upper case letters
Finite number of keywords (32) Infinite
E. g. int , auto, break Hello, area
4) What are escape characters in C.
C uses combination of characters such as \t, \b, \n to represent special conditions
such as horizontal tabs, blank space, newline respectively. These character
combinations are called escape characters.
5) Write down the rules for identifiers.
a. Consists of letters and digits, but the first character must be a letter or
underscore.
b. Both upper case and lower case letters are permitted
c. No blank space between two words.
d. E. g. circle_area, STUD_NAME
6) Define expressions in C and write about its variants
An expression is a valid combination of operators and operands arranged as per the
syntax of the language that computes a value. Three types of expressions in C.
a. Arithmetic expressions
b. Logical expressions
c. Relational expressions
7) Difference between while and do while loop.
While loop is an entry controlled loop whereas do while is an exit controlled loop.
Entry controlled means the statements will be executed only if the condition is true.
Exit controlled means the statements will be executed at least once. The next
execution of statements depends on the condition. Give an example for each along
with syntax
8) Write a C program to check whether a number is perfect or not.
#include<stdio.h>
main()
{
int n, i, s;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for(i=1;i<=n/2;i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
printf(“%d is a perfect number\n”,n);
else
printf(“%d is not a perfect number\n”,n);
}
9) Accept two integers for a coordinate point and determine its quadrant.
#include<stdio.h>
main( )
{
int x, y;
printf(“Enter x and y\n”);
scanf(“%d%d”,&x, &y);
if(x>0 &&y>0)
printf(“ (%d,%d) lies in the first quadrant\n”,x, y);
else if(x<0 &&y>0)
printf(“ (%d,%d) lies in the second quadrant\n”,x, y);
else if(x<0 &&y<0)
printf(“ (%d,%d) lies in the third quadrant\n”,x, y);
else
printf(“ (%d,%d) lies in the fourth quadrant\n”,x, y);
}
10)Write a C program to convert one case to another
#include<stdio.h>
main( )
{
char c, cc;
printf( “ Enter the character\n”);
c=getchar();
if((c>=’a’)&&(c<=’z’))
{
cc=c-32;
printf(“Upper case of %c is %c\n, c, cc);
}
else if((c>=’A’ )&&(c<=’Z’))
{
cc=c+32;
printf(“Upper case of %c is %c\n, c, cc);
}
}
11) Write a C program to display the Floyd’s triangle
1
2 3
4 5 6
7 8 9 10
#include<stdio.h>
main( )
{
int n, j, i, k=1;
printf(“Enter the number of lines\n”);
sccanf(“%d”, &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf(“%d\t”, k);
k++;
}
printf(“\n”);
}
}
12) Write a C program to check whether a number is prime or not.
#include<stdio.h>
main( )
{
int n, flag=0, j;
printf(“Enter the number\n”);
scanf(“%d”, &n);
for(j=2; j<=n/2; j++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf(“%d is a prime number\n”, n);
else if (flag ==1)
printf(“%d is not a prime number\n”, n);
}