Set - 1

Question 11 :

What is the difference between printf() and sprintf() ?

Answer :

sprintf() writes data to the character array whereas printf(...) writes data to the standard output device.


Question 12 :

How to reduce a final size of executable ?

Answer :

Size of the final executable can be reduced using dynamic linking for libraries.


Question 13 :

Can you tell me how to check whether a linked list is circular ?

Answer :

Create two pointers, and set both to the start of the list. Update each as follows:

while (pointer1) {
	pointer1 = pointer1->next;
	pointer2 = pointer2->next; 
	if (pointer2) pointer2=pointer2->next;
	if (pointer1 == pointer2) {
		print ("circular");
	}
}

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.


Question 14 :

Advantages of a macro over a function ?

Answer :

Macro gets to see the Compilation environment, so it can expand __ __TIME__ __FILE__ #defines. It is expanded by the preprocessor. 

For example, you can't do this without macros

#define PRINT(EXPR) printf( #EXPR "=%d\n", EXPR) 

PRINT( 5+6*7 ) // expands into printf("5+6*7=%d", 5+6*7 ); 

You can define your mini language with macros:

#define strequal(A,B) (!strcmp(A,B)) 

Macros are a necessary evils of life. The purists don't like them, but without it no real work gets done.


Question 15 :

What is the difference between strings and character arrays ?

Answer :

A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword. 

Actually, a string is a character array with following properties: 
* the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character. 
* it not specified what happens if this array, i.e., string, is modified. 
* Two strings of same value[1] may share same memory area. For example, in the following declarations:

char *s1 = "Calvin and Hobbes";
char *s2 = "Calvin and Hobbes";

the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following: 

char ca1[] = "Calvin and Hobbes";
char ca2[] = "Calvin and Hobbes";

[1] The value of a string is the sequence of the values of the contained characters, in order.