
By : Nanda Kishor K N                                      Mail ID : nandakishorkn@rediffmail.com
_________________________________________________________________________________________________

                                       C For Swimmers
                                       ______________

      If u don't know how to swim, learn it now. Otherwise participate in the competition.

_________________________________________________________________________________________________

		                 Topic : Exploring C (Part I)
                		 _____   ___________

*************************************************************************************************
* NOTE : All the programs are tested under Turbo C/C++ compilers.  				*
* It is assumed that,										*
*			*=> Programs run under DOS environment,					*
*			*=> The underlying machine is an x86 system,				*
*			*=> Necessary header files are included.				*
*			*=> Program is compiled using Turbo C/C++ compiler.			*
* The program output may depend on the information based on this assumptions. 			*
* (For example sizeof(int) = 2 bytes may be assumed). 						*
*************************************************************************************************

[Q001]. What will be the output of the following program :
	#define func(a,b) b-##--a
	void main()
	{
	  int a=5,b=3,c;
	  c=func(a,b);
	  printf("%d %d %d",a,b,c);
	}

(a)Compile-Time Error	(b)5 3 2			(c)4 3 -1		(d)5 2 -2
Ans. (d) When using macros with argument lists, you can merge (or paste) two tokens by separating
them with ## (plus optional whitespace on either side), i.e. Token pasting with ##.		
_________________________________________________________________________________________________
	
[Q002]. What will be the output of the following program :
	void main()
	{
	  #define b 100
	  int a=5,c;
	  c=a+b;
	  printf("%d %d %d",a,b,c);
	}

(a)Compile-Time Error	(b)Run-Time Error		(c)5 100 105		(d)None of these
Ans. (c) Preprocessor directives can be defined anywhere in the program but just before its use.	
_________________________________________________________________________________________________

[Q003]. What will be the output of the following program :
	void main()
	{
	  int a=25.0;
	  #include<math.h>
	  printf("%.2lf %.2lf",sqrt(a),pow(sqrt(a)*5.0,2.0));
	}
(a)Compile-Time Error	(b)5 625			(c)5.00 125.00		(d)5.00 625.00
Ans. (d) Preprocessor directives can be defined anywhere in the program but just before its use.
_________________________________________________________________________________________________
	
[Q004]. What will be the output of the following program :
	void main()
	int c;
	{
	  int a=5,b=10;
	  c=a+=b-=a;
	  printf("%d %d %d",a,b,c);
	}
(a)Compile-Time error	(b)5 10 5			(c)10 5 10		(d)10 10 5
Ans. (a) Consider any function definitions (here it is 'main') in which DECLARATION OF VARIABLES
between the () and {} are LEGAL for specifying data types for FORMAL PARAMETERS ONLY. Otherwise
it is ILLEGAL or INVALID. Ex:- int c; in the above program is an ILLEGAL statement in C.
_________________________________________________________________________________________________

[Q005]. What will be the output of the following program :
	#define func(x,y) { func(x,y) }
	void main()
	{
	  int a=5,b=6;
	  c=func(x,y);
	  printf("%d %d %d",c);
	}
(a)Compile-Time Error	(b)Linker Error			(c)5 6 11		(d)Infinite Loop
Ans. (b) A macro cannot call itself recursively.	
_________________________________________________________________________________________________

[Q006]. What will be the output of the following program :
	void main() 
	{
	  const val=57;
	  printf("%d ",val);
	  *(int *)&val=75;
	  printf("%d",val);
	}
(a)Compile-Time Error	(b)75 57			(c)57 75		(d)None of these
Ans. (c) A const variable can be indirectly modified by a pointer as shown in the above program.
_________________________________________________________________________________________________
	
[Q007]. What will be the output of the following program :
	#define print(msg) printf("Message:" #msg)
	void main()
	{
	  print("Hi Friends");
	}
(a)Compile-Time Error					(b)Message:Hi Friends
(c)Message:"Hi Friends"					(d)"Message:Hi Friends"
Ans. (c) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. Any special characters such as '," and \, will be replaced by their
corresponding escape sequences, Ex: \',\" and \\. In addition, the resulting string will
automatically be concatenated (combined) with any adjacent strings.
_________________________________________________________________________________________________

[Q008]. What will be the output of the following program :
	#define print(val) printf("x" #val "=%.2f ",x##val)
	void main()
	{
	  float x1=5.74,x2=23.78;
	  print(1);
	  print(2);
	}
(a)Compile-Time error	(b)x1=5.74 x2=23.78		(c)Linker Error		(d)None of these
Ans. (b) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. The "token-pasting" operator ## causes individual items within a macro
definition to be concatenated, thus forming a single item.	
_________________________________________________________________________________________________
	
[Q009]. What will be the output of the following program :
	#define print(msg) printf(#msg)
	void main()
	{
	  print(C   FOR   SWIMMERS);
	}
(a)Compile-Time error	(b)C FOR SWIMMERS		(c)C   FOR   SWIMMERS	(d)No Output
Ans. (b) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. Consecutive whitespace characters inside the actual argument will be
replaced by a single blank space character. 	
_________________________________________________________________________________________________

[Q010]. What will be the output of the following program :
	#define int float
	void main()
	{
	  int a=5.75,b=9.63,c;
	  c=a+b;
	  printf("%.2f %.2f %.2f",a,b,c);
	}

(a)Compile-Time Error	(b)5.75 9.63 15.38		(c)Linker Error		(d)None of these
Ans. (b) It is legal but ill-advised to use Turbo C++ keywords as macro identifiers.
In the above program : #define int float is legal, but possibly catastrophic.
_________________________________________________________________________________________________

[Q011]. What will be the output of the following program :
	void main()
	{
	  int i=5;
	  float j=56.78;
	  char str[20];
	  scanf("%s %*d %f",str,&i,&j);
	  printf("%s %d %.2f",str,i,j);
	}
[Assume the INPUT values entered by the user are :testing 12345 2.25]
(a)Compile-Time Error						(b)testing 12345 2.25
(c)testing 5 2.25						(d)testing 12345 56.78 
Ans. (c) To skip over a data item without assigning it to the designated variable or array, the
% sign within the appropriate control group should be followed by an asterisk (*). This feature
is referred to as ASSIGNMENT SUPPRESSION.	
_________________________________________________________________________________________________

[Q012]. What will be the output of the following program :
	main()
	{
	  static int val=7;
	  int data;
	  if (--val)
	     {
	       data=main()+val;
	       printf("%d ",data);
	     }
	  return 0;
	}
(a)Compile-Time Error	(b)INFINITE LOOP		(c)1 2 3 4 5 6		(d)0 0 0 0 0 0
Ans. (d) The variable 'val' is declared as static, hence memory for 'val' will be allocated for
only once, as it encounters the statement. The function main() will be called recursively unless
'val' becomes equal to 0, and since main() is recursively called, so the value of static 'val'
ie., 0 will be printed every time the control is returned.	
_________________________________________________________________________________________________	

[Q013]. What will be the output of the following program :
	void main()
	{
	  int i=5;
	  {
	    int j=10;
            printf("%d %d",i++,++j);
	  }
	  printf("\n%d %d",i,j);
	}
(a)Compile-Time Error	(b)5 11				(c)5 11			(d)No Output
			   6 11				   6 Garbage value here
Ans. (a) The variable j is a block level variable and the visibility is inside that block only.	
_________________________________________________________________________________________________	

[Q014]. What will be the output of the following program :
	#define val 100
	void main()
	{
	  printf("%d ",val);
	  #define val 1000
	  printf("%d",val*10);
	}
(a)Compile-Time Error	(b)100 1000			(c)100 10000		(d)None of these
Ans. (c) The preprocessor directives can be redefined anywhere in the program. So the most
recently assigned value will be taken.	
_________________________________________________________________________________________________	

[Q015]. What will be the output of the following program :
	#define a 50
	#define b 60
	void main()
	{
	  int sum;
	  sum=++a + --b;
	  printf("%d %d %d",a,b,sum);
	}
(a)Compile-Time Error	(b)50 60 110			(c)51 59 110		(d)None of these
Ans. (a) #define statement defines symbolic constants in a C program. So the assigned value
cannot be altered using any operators instead it can be redefined anywhere in the program.
_________________________________________________________________________________________________	

[Q016]. What will be the output of the following program :
	void main()
	{
	   int a[5];
	   printf("%d",9-*a-3+*a);
	}
(a)Compile-Time Error	(b)6				(c)Garbage Value	(d)None of these
Ans. (b) *a and -*a cancels out. The result is as simple as 9 - 3 = 6 	
_________________________________________________________________________________________________	

[Q017]. What will be the output of the following program :
	void main()
	{
	   static int i;
	   while (i <= 10)
		 (i > 2) ? i++ : i--;
	   printf("%d",i);
	}
(a)Compile-Time Error	(b)11				(c)32767		(d)None of these
Ans. (c) Since i is static it is initialized to 0. Inside the while loop the conditional operator
evaluates to false, executing i--. Thus the integer value rotates to positive value (32767). Then
while condition becomes false and hence, comes out of the while loop, printing the i value.	
_________________________________________________________________________________________________	

[Q018]. What will be the output of the following program :
	void main()
	{
	  int a=5,b=6,c;
	  c=(b++ == 6) || (--a < 2);
	  printf("%d %d %d",a,b,c);
	}
(a)Compile-Time Error	(b)4 7 1			(c)4 7 6		(d)5 7 1
Ans. (d) The boolean expression needs to be evaluated only till the truth value of the expression
is not known. Here <expr1>'s truth value is 1 or true. Because true || <expr2> where (expr2)
will not be evaluated. So the value of a remains the same. Thus a=5, b=7 & c=1
_________________________________________________________________________________________________	

[Q019]. What will be the output of the following program :
	void main()
	{
	   int *ptr;
	   ptr=0x1fa;
	   *ptr++=5000;
	   *ptr=6000;
	   printf("%d ",++*ptr--);
	   printf("%d",(*ptr)++);
	}
(a)Compile-Time Error	(b)6000 6000			(c)6001 5000		(d)6001 5001
Ans. (c) The pointer ptr will point to the integer value in the memory location 0x1fa.
Ex: The value 5000 stored at 0x1fa and 0x1fb since size of integer value is 2 bytes. Similarly,
the value 6000 stored at 0x1fc and 0x1fd.
_________________________________________________________________________________________________	

[Q020]. What will be the output of the following program :
	void main()
	{
	  char *str="ABCDEFGHI";
	  printf("%c %c %c ",str[7]+1,3[++str],++*(str+5));
	  printf("%c %c %c",++*str,*++str,++*str++);
	}
(a)Compile-Time Error	(b)J D G E D B 			(c)J E G E D C		(d)J E G D C B
Ans. (c) str[i] is same as i[str], str[i] is same as *(str+i), str[i+1] is same as *++(str+i)
	 Careful observation is necessary. 	
_________________________________________________________________________________________________	

				Thanx for using "C For Swimmers".
Regarding this material, you can send Bug Reports, Suggestions, Comments,etc. to nandakishorkn@rediffmail.com