You are on page 1of 120

Test Your C Skills

B.Bhuvaneswaran
Assistant Professor (SS)
Department of Computer Science & Engineering
Rajalakshmi Engineering College
Thandalam
Chennai 602 105
bhuvaneswaran@rajalakshmi.edu.in

01. Declarations and Initializations

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

Whenever there is a conflict between a


local variable and a global variable it is
local variable gets a priority.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-01
#include <stdio.h>
int x = 40;
int main()
{
int x = 20;
printf("%d", x);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-01

20

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02
In case of a conflict between two local
variables that one that is more local gets
the priority.
More local means the one nearer to the
point of usage.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-02
#include <stdio.h>
int main()
{
int x = 40;
{
int x = 20;
printf("%d ", x);
}
printf("%d", x);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-02

20 40

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

When an automatic structure is partially


initialized, the remaining elements of the
structure are initialized to 0.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-03
#include <stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d %f", e.age, e.sal);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-03

0 0.000000

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

When an automatic array is partially


initialized, the remaining array elements
are initialized to 0.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-04
#include <stdio.h>
int main()
{
int a[5] = {2, 3};
printf("%d %d %d", a[2], a[3], a[4]);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-04

000

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

Enum elements always take values like 0,


1, 2, etc.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-06
#include <stdio.h>
int main()
{
enum status {pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d %d %d", stud1, stud2, stud3);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-05

021

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

Sizes of short integer and long integer


would vary from one platform to another.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

Sizes of short integer and long integer can


be verified using the sizeof() operator.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

A float is 4 bytes wide, whereas a double


is 8 bytes wide.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-09

A long double should be used if range of a


double is not enough to accommodate a
real number.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-10

The modulus operator cannot be used with


a long double.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-11

A char variable can store either an ASCII


character or a Unicode character.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-12

A short integer is as least 16 bits wide and


a long integer is at least 32 bits wide.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-13

By default, the data type of a constant


without a decimal point is int, whereas the
one with a decimal point is a double.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-14
The values stored in variables depends
upon where they are defined. If they are
defined outside all functions, they would
be treated as external storage class
variables and hence would contain default
values 0 and 0.0 respectively.
However, if they are defined inside a
function then they would be treated as
automatic storage class variables. In this
case the variables would contain garbage
values.

B.Bhuvaneswaran / AP (SS) / CSE / REC

02. Control Instructions

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

goto cannot take control to a function


other than the one in which it is being
used. In other words, goto cannot perform
a non-local jump.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

Constant expression required in the case,


we cannot use variables.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

Constant expression like 1 * 2 + 4 are


acceptable in case of a switch.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

Though never required, there can exist a


switch, which has no cases.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

All statements in a switch have to belong


to some case or the other.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

continue can work only with loops and not


with switch.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

When 0.7 is stored in variable what get stored is


a 32-bit binary equivalent of 0.7. The binary
equivalent of 0.7 turns out to be a recurring
number. This recurring number is terminated at
32 bits when it is stored in a. As against this,
when 0.7 is treated as a double the recurring
binary equivalent of 0.7 is terminated at 64 bits.
When the 64-bit recurring binary equivalent of
0.7 is compared with 32-bit recurring binary
equivalent of 0.7, the 64-bit value turns out to be
greater than the 32-bit value.
B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-07
#include <stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi");
else
printf("Hello");
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-07

Hi

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

switch is useful when we wish to check the


value of a variable against a particular set
of values.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-09

Compiler implements a jump table for


cases used in a switch.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-10

It is not compulsory to use a break in


every switch statement.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-11

During comparison int x and float y, x


would get promoted to a float and then
two float values would be compared.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-11
#include <stdio.h>
int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-11

x and y are equal

B.Bhuvaneswaran / AP (SS) / CSE / REC

03. Expressions

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

The order of operators get evaluate:

Arithmetic
Relational
Logical
Assignment

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02
Expression on the right hand side of &&
and || operators does not get evaluated if
the left hand side determines the
outcome.
For example:

If a is non-zero then b will not be evaluated in


the expression a || b.
Likewise, if a is 0 then b will not be evaluated
in the expression a && b.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

The order of evaluation of the arguments


passed to a function call is unspecified.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-03
#include <stdio.h>
int main()
{
int i = 2;
printf("%d %d", ++i, ++i);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-03

Output may vary from compiler to


compiler.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

The comma operator has left-to-right


associativity. The left operand is always
evaluated first, and the result of
evaluation is discarded before the right
operand is evaluated.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-04
#include <stdio.h>
int main()
{
int i = 2;
int j = i + (1, 2, 3, 4, 5);
printf("%d", j);
return 0;
}
B.Bhuvaneswaran / AP (SS) / CSE / REC

A-04

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

Associativity of an operator is either Left


to Right or Right to Left.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

Every operator has an Associativity.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

Associativity has no role to play unless the


precedence of operators is same.

B.Bhuvaneswaran / AP (SS) / CSE / REC

04. Floating Point Issues

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

To treat the constant 3.14 as a float use


3.14f.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

To treat the constant 3.14 as a long


double use 3.14L or 3.14l.

B.Bhuvaneswaran / AP (SS) / CSE / REC

05. Functions

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

return statement cannot be used with


conditional operators:

a>20 ? return(10) : return(20)

Instead the following statement can be


used:

return (a>20 ? 10 : 20);

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

A function CANNOT be defined inside


another function.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

Functions can be called either by value or


by reference.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

Functions cannot return more than one


value at a time.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

Names of the functions in two different


files linked together must be unique.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

A function may have any number of return


statements each returning different
values.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

If return type of a function is not


specified, it defaults to int.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

While defining a function it is necessary to


collect the values passed to it.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

It is not compulsory to collect the value


returned from a function.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-09

Actual arguments used in a function can


be variables, constants or expressions.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-10

Redeclaration of a function is not an error.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-11

Redefinition of a function is an error.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-12

In a function two return statements


should never occur successively.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-13

Usually recursion works slower than loops.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-14

Too many recursive calls may result into


stack overflow.

B.Bhuvaneswaran / AP (SS) / CSE / REC

06. The C Preprocessor

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

If the file to be included doesnt exist, the


preprocessor flashed an error message.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

A header file contains macros, structure


declarations and function prototypes.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

A preprocessor directive is a message


from programmer to the preprocessor.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

Once preprocessing is over and the


program is sent for the compilation the
macros are removed from the expanded
source code.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

Preprocessor directive #undef can be used


only on a macro that has been #defined
earlier.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

Macros with arguments are allowed.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

Preprocessor directive #ifdef #else


#endif is used for conditional compilation.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

There exist ways to prevent the same file


from getting #included twice in the same
program.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-09

To define the SQR macro such that it gives


the correct result:

#define SQR(x) (x * x)

As

#define SQR(x) ((x) * (x))

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-10

Header files contains Preprocessor


directives like #define, structure, union
and enum declarations, typedef
declarations, global variable declarations
and external function declarations. You
should not write actual code (i.e. function
bodies) or global variable definition (that
is defining or initializing instances) in
header files. The #include directive should
be used to pull in header files, not other
.c files.
B.Bhuvaneswaran / AP (SS) / CSE / REC

E-11

Header files are searched:

If included using < > the file get searched in


the predefined include path. It is possible to
change the predefined include path.
If included with the syntax in addition to
the predefined include path the file also get
searched in the current directory (usually the
directory from which you invoked the
compiler).

B.Bhuvaneswaran / AP (SS) / CSE / REC

07. Pointers

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01
*ptr++ increments the pointer and not
the value pointed by it, whereas ++*ptr
increments the value being pointed to by
ptr.
++*ptr works like *ptr = *ptr + 1.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

Another expression which does the same


job as ++*ptr is:

(*ptr)++

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

The equivalent pointer expression for


referring the array element a[i][j][k][l] is:

*(*(*(*(a + i) + j) + k) + l)

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

Compiler never detects error if bounds of


an array are exceeded.

B.Bhuvaneswaran / AP (SS) / CSE / REC

Q-04
#include <stdio.h>
int main()
{
char str[5] = "fast enough";
return 0;
}

B.Bhuvaneswaran / AP (SS) / CSE / REC

A-05

No error.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

You can think of using pointer at lot of


places, some of which are:

Accessing array or string elements


In passing big objects like arrays, strings and
structures to functions
Dynamic memory allocation
Call by reference
Implementing linked lists, trees, graphs and
many other data structures

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

To declare an array of three function


pointers where each function receives two
ints and returns a float:

Float (*arr[3]) (int, int);

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

The NULL macro is defined in the header


files:

stdio.h
Stddef.h

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-09

For each pointer type (like say a char


pointer) C defines a special pointer value,
which is guaranteed not to point to any
object or function of that type. Usually,
the null pointer constant used for
representing a null pointer is the integer
0.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-10
A null pointer is a pointer, which doesnt
point anywhere.
A NULL macro is used to represent the null
pointer in source code. It has a value 0
associated with it.
The ASCII NULL character has all its bits
as 0 but doesnt have any relationship
with the null pointer.
The null string is just another name for an
empty string .

B.Bhuvaneswaran / AP (SS) / CSE / REC

08. Arrays

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-01

While initializing a 2-D array, either both


dimension should be mentioned or the
column dimension should be mentioned.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-02

The array int num[26]; can store twentysix elements.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-03

The expression num[27] designates the


twenty-eighth element in the array.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-04

The declaration num[SIZE] is allowed if


SIZE is a macro.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-05

In the following definition:

int arr[3][4][5];

arr is a collection of 3 2-D arrays, each


containing 4 rows and 5 columns of
integers.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-06

A pointer to a block of memory is


effectively same as an array.

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-07

If you pass an array as an argument to a


function, it passes:

Base address of the array

B.Bhuvaneswaran / AP (SS) / CSE / REC

E-08

The following declaration:

int (*ptr) [10];

Means ptr is a pointer to an array of 10


integers.

B.Bhuvaneswaran / AP (SS) / CSE / REC

09. Strings

B.Bhuvaneswaran / AP (SS) / CSE / REC

10. Structures, Unions and Enums

B.Bhuvaneswaran / AP (SS) / CSE / REC

11. Input/Output

B.Bhuvaneswaran / AP (SS) / CSE / REC

12. Command Line Arguments

B.Bhuvaneswaran / AP (SS) / CSE / REC

13. Bitwise Operators

B.Bhuvaneswaran / AP (SS) / CSE / REC

14. Subtleties of typedef

B.Bhuvaneswaran / AP (SS) / CSE / REC

15. The const Phenomenon

B.Bhuvaneswaran / AP (SS) / CSE / REC

16. Memory Allocation

B.Bhuvaneswaran / AP (SS) / CSE / REC

17. Variable Number of Arguments

B.Bhuvaneswaran / AP (SS) / CSE / REC

18. Complicated Declarations

B.Bhuvaneswaran / AP (SS) / CSE / REC

19. Library Functions

B.Bhuvaneswaran / AP (SS) / CSE / REC

B.Bhuvaneswaran / AP (SS) / CSE / REC

References

Yashavant Kanetkar, Test Your C Skills,


5th Edition, BPB Publications, 2013.

B.Bhuvaneswaran / AP (SS) / CSE / REC

B.Bhuvaneswaran / AP (SS) / CSE / REC

You might also like