Test Your C Skills
 

Tips 1

 

Tips 2

 

Tips 3

 

Tips 4


Tips 5


Tips 6

 

Tips 7

 

Tips 8

 

Tips 9  

 

Tips 10

 

Tips 11

 

Tips 12

 

Tips 13

 

Tips 14

 

Tips 15

 

Tips 16

 

Tips 17

 

Tips 18

 

Tips 19 

 

Tips 20

  

Tips 21

 

 

Q1.1

What would be the output of the following program?

main( )

{

char far *s1, *s2 ;

printf ( "%d %d", sizeof ( s1 ), sizeof ( s2 ) ) ;

}

Q1.2

What would be the output of the following program?

int x = 40 ;

main( )

{

int x = 20 ;

printf ( "\n%d", x ) ;

}

Q1.3

What would be the output of the following program?

main( )

{

int x = 40 ;

{

int x= 20 ;

printf ( "\n%d ", x ) ;

}

printf ( "%d", x ) ;

}

Q1.4

Is the following statement a declaration or a definition?

extern int i ;

Q1.5

What would be the output of the following program?

main( )

{

extern int i ;

i = 20 ;

printf ( "%d", sizeof ( i ) ) ;

}

A. 2

B. 4

C. Would vary from compiler to compiler

D. Error, i undefined

Q1.6

Is it true that a global variable may have several declarations, but only one definition? <Yes/No>

Q1.7

Is it true that a function may have several declarations, but only one definition? <Yes/No>

Q1.8

In the following program where is the variable a getting defined and where is it getting declared?

main( )

{

extern int a ;

printf ( "%d", a ) ;

}

int a = 20 ;

Q1.9

What would be the output of the following program?

main( )

{

extern int a ;

printf ( "\n%d", a ) ;

}

int a = 20 ;

A. 20

B. 0

C. Garbage value

D. Error

Q1.10

What's the difference between a defintion and declaration of a variable?

Q1.11

If the definition of an external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function. <True/False>

Q1.12

Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file f1 but used in the files f2 and f3. In such a case would we need the external declaration for the variables in the files f2 and f3? <Yes/No>

Q1.13

When we mention the prototype of a function are we defining the function or declaring it?

Q1.14

What's the difference between the following declarations?

extern int fun( ) ;

int fun( ) ;

Q1.15

Why does the following program report a redeclaration error of function display( )?

main( )

{

display( ) ;

}

void display( )

{

printf ( "\nCliffhanger" ) ;

}

Q1.16

What would be the output of the following program?

main( )

{

extern int fun ( float ) ;

int a ;

a = fun ( 3.14 ) ;

printf ( "%d", a ) ;

}

int fun ( aa )

float aa ;

{

return ( ( int ) aa ) ;

}

A. 3

B. 3.14

C. 0

D. Error

Q1.17

Point out the error, if any, in the following program.

struct emp

{

char name[20] ;

int age ;

}

/* some more code may go here */

fun ( int aa )

{

int bb ;

bb = aa * aa ;

return ( bb ) ;

}

main( )

{

int a ;

a = fun ( 20 ) ;

printf ( "\n%d", a ) ;

}

Q1.18

If you are to share the variables or functions across several source files how would you ensure that all definitions and declarations are consistent?

Q1.19

How would you rectify the error in the following program?

f ( struct emp ) ;

/* any other prototypes may go here */

struct emp

{

char name[20] ;

int age ;

} ;

main( )

{

struct emp e = { "Soicher", 34 } ;

f ( e ) ;

}

f ( struct emp ee )

{

printf ( "\n%s %d", ee.name, ee.age ) ;

}

Q1.20

Global variables are available to all functions. Does there exist a mechanism by way of which I can make it available to some and not to others.

Q1.21

What do you mean by a translation unit?

Q1.22

What would be the output of the following program?

main( )

{

int a[5] = { 2, 3 } ;

printf ( "\n%d %d %d", a[2], a[3], a[4] ) ;

}

A. Garbage values

B. 233

C. 322

D. 000

Q1.23

What would be the output of the following program?

main( )

{

struct emp

{

char name[20] ;

int age ;

float sal ;

} ;

struct emp e = { "Tiger" } ;

printf ( "\n%d %f", e.age, e.sal ) ;

}

A. 00.000000

B. Garbage values

C. Error

D. None of the above

Q1.24

Some books suggest that the following definitions should be preceded by the word static. Is it correct?

int a[ ] = { 2, 3, 4, 12, 32 } ;

struct emp e = { "sandy", 23 } ;

Q1.25

Point out the error, if any, in the following program.

main( )

{

int ( *p )( ) = fun ;

( *p )( ) ;

}

fun( )

{

printf ( "\nLoud and clear" ) ;

}

Q1.26

Point out the error, if any, in the following program.

main( )

{

union a

{

int i ;

char ch[2] ;

} ;

union a z = 512 ;

printf ( "%d %d", z.ch[0], z.ch[1] ) ;

}

Q1.27

What do you mean by scope of a variable? What are the 4 different types of scopes that a variable can have?

Q1.28

What are the different types of linkages?

Answers

A1.1

42

A1.2

20. Whenever there is a conflict between a local variable and a global variable it is the local variable which gets a priority.

A1.3

2040. In case of a conflict between local variables, the one which is more local that gets the priority.

A1.4

Declaration

A1.5

D. extern int i is a declaration and not a definition, hence the error.

A1.6

Yes

A1.7

Yes

A1.8

extern int a is the declaration whereas int a = 20 is the definition.

A1.9

A

A1.10

In the defintion of a variable space is reserved for the variable and some initial value is given to it, whereas a declaration only identifies the type of the variable for a function. Thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.

A1.11

True

A1.12

Yes

A1.13

We are declaring it. When the function alongwith the statements belonging to it are mentioned we are defining the function.

A1.14

There is no difference except for the fact that the first one gives a hint that the function fun() is probably in another source file.

A1.15

Here display( ) is called before it is defined. In such cases the compiler assumes that the function display() is declared as

int display( ) ;

That is, an undeclared function is assumed to return an int and accept an unspecified number of arguments. Then when we define the function the compiler finds that it is returning void hence the compiler reports the discrepancy.

A1.16

D. The error occurs because we have mixed the ANSI prototype with K & R style of function definition. When we use ANSI protptype for a function and pass a float to the function it is promoted to a double. When the function accepts this double into a float a type mismatch occurs hence the error.The remedy for this error could be to define the function as:

int fun ( float aa )

{

..

}

A1.17

Because of the missing semicolon at the end of the structure declaration (the intervening comment further obscures it) the compiler believes that fun() would return something of the the type struct emp, whereas in actuality it is attempting to return an int. This causes a mismatch, hence an error results.

A1.18

The best arrangement is to place each definition in a relevant .c file. Then, put an external declaration in a header file (.h file) and use #include to bring in the declaration wherever needed.

The .c file which contains the definition should also include the header file, so that the compiler can check that the definition matches the declaration.

A1.19

Declare the structure before the prototype of f( ).

A1.20

No. The only way this can be achieved is to define the variable locally in main( ) instead of defining it globally and then passing it to the functions which need it.

A1.21

A translation unit is a set of source files seen by the compiler and translated as a unit: generally one .c file, plus all header files mentioned in #include directives.

A1.22

D. When an automatic array is partially initialised, the remaining array elements are initialised to 0.

A1.23

A. When an automatic structure is partially initialised, the remaining elements of the structure are initialised to 0.

A1.24

Pre-ANSI C compilers had such a requirement. Compilers which conform to ANSI C standard do not have such a requirement.

A1.25

Here we are initialising the function pointer p to the address of the function fun(). But during this initialisation the function has not been defined. Hence an error.

To eliminate this error add the prototype of the fun() before declaration of p, as shown below:

extern int fun( ) ;

or simply

int fun( ) ;

A1.26

In a pre-ANSI compiler a union variable cannot be initialised. However, ANSI C permits initialisation of first member of the union.

A1.27

Scope indicates the region over which the variable's declaration has an effect. The four kinds of scopes are: file, function, block and prototype.

A1.28

There are three different types of linkages: external, internal and none. External linkage means global, non-static variables and functions, internal linkage means static variables and functions with file scope, and no linkage means local variables.