Saturday, October 8, 2011

Output of C program (printf)

What would be the output of the following C program? (Is it a valid C program?)
#include <stdio.h>
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}

Output : 4321

Explanation:This will produce output “4321”. The return type of printf is “int” which is the number of characters written to stdout.
printf("%d\n",printf("%d",printf("%d",i)));
printf("%d\n",printf("%d",printf("%d",43))); => 43
printf("%d\n",printf("%d",2)); => 2
printf("%d\n",1); => 1


Sunday, September 25, 2011

To find the length of string litteral or char array, We can use sizeof than strlen

sizeof is evaluated by the compiler where as strlen is evaluated at runtime.
Examples 1. int len = sizeof(“akamai”) - 1;

2. Char a[] = “akamai”; int len = sizeof(a) - 1;

3. Char *a = “akamai”; don’t use sizeof, please use strlen(a);