Industrial Training




C Tips Tricks-2



I have an array declared in file 'F1.C' as,

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

and used in the file 'F2.C' as,

extern int a[ ] ;



In the file F2.C, why sizeof doesn't work on the array a[ ]?

Ans: An extern array of unspecified size is an incomplete type. You cannot apply sizeof to it, because sizeof operates during compile time and it is unable to learn the size of an array that is defined in another file. You have three ways to resolve this problem:

1. In file 'F1.C' define as,

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

int size_a = sizeof ( a ) ;

and in file F2.C declare as,

extern int a[ ] ;

extern int size_a ;

2. In file 'F1.H' define,

#define ARR_SIZ 6

In file F1.C declare as,

#include "F1.H"

int a[ ARR_SIZ ] ;

and in file F2.C declare as,

#include "F1.H"

extern int a[ ARR_SIZ ] ;

3. In file 'F1.C' define as,

int a[ ] = { 1, 2, 3, 4, 5, 6, -1 } ;

and in file 'F2.C' declare as,

extern int a[ ] ;

Here the element -1 is used as a sentinel value, so the code can understand the end without any explicit size.



How to write to multiline macro in C?

Ans: To write a multiline macro we have to use C's line splitting character, the '\'.

Here is a macro definition of LINE and the program which uses it:

#define LINE for ( i = 0 ; i < 80 ; i++ )\

printf ( "_" ) ;\

printf ( "\n" ) ;

main( )

{

int i ;

LINE ;

printf ( "S.No Roll No. Name Phy Math Chem\n" ) ;

LINE ;

}

The macro LINE draws a horizontal line on the screen. The above program attempts to print the name of the fields of the students record in between the two lines. In the LINE macro we have used i as a variable hence, it has to be declared before we use the macro. Since we are using the macro in main( ) function, we have declared i before using the macro.



Would the following program compile?

struct syntax

{

int i ;

float g ;

char c ;

}

main( )

{

printf ( "I won't give you any error" ) ;

}

Ans: The above program successfully compiles and on execution prints the message given in printf( ). In the above program the structure syntax is declared but not terminated with the statement terminator, the semicolon. The compiler does not give any error message for it. This is because the compiler assumes that main( ) function has a return type of struct syntax and hence it successfully compiles and executes the program.

What will be the output of the following program?

main( )

{

printf ( "%c", "abcdefgh"[ 3 ] ) ;

}

Ans: The above program prints character 'd' on the screen. This is because the string "abcdefgh" gives the base address of the same string. Hence, "abcdefgh"[3] is equivalent to * ( "abcdefgh" + 3 ) i.e. the fourth character in the string which is 'd'.



What will the output of the following program?

main( )

{

int i ;

char a[ ] = "Hello" ;

while ( a != '\0' )

{

printf ( "%c", *a ) ;

a++ ;

}

}

Ans. The above program on compilation flashes an error message, Lvalue Required on the line number 8. This is because we are incrementing a which is nothing but a constant pointer to zeroth element of the array. We know that increment operator ++ cannot operate on a constant object. It is equivalent to saying that 5++ which is not permitted since it is same as

5 = 5 + 1 ;

Since on the left hand side of the assignment operator something other than waht is required (a variable) the error Lvalue Required results.



Hi I am Pluto.