Thursday, October 25, 2012

C puzzles

#include "stdio.h"
Can we include stdio.h in double quotes instead of angle brackets?

Answer: Yes

If the filename is enclosed within angle brackets, the file is searched in the path where compiler includes standard header files. If the filename is enclosed within double quotes, we are telling the compiler that search for this header file in current source directory too.

========================================================================

#include<stdio.h>
int main()
{
char *p=NULL;
int *a =NULL;
p++;
a++;
printf("%u %u",p,a);
return 0;
}
Output: 1 4

The Address of Null pointer is 0. So the increment will end up in 1 for char pointer as char takes 1 byte and 4 for int pointer as int takes 4 bytes.



========================================================================

#include<stdio.h>
int main()
{
char *p="abcd";
p[2]='C';
printf("%s",p);
return 0;
}
Output: Segmentation fault

“abcd” will be stored in read only section of memory, so we cannot change the value.



========================================================================

#include<stdio.h>
#include<string.h>
int main()
{
char *str;
str=(char *)malloc(sizeof(char)*100);
strcpy(str,"abcd");
printf("%s",str);
str[2]='C';
printf(" %s",str);
return 0;
}
Output: abcd abCd

We have allocated memory to "abcd". So we can modify it.



========================================================================

int main()
{
char a='a';
char *p;
printf("%d",sizeof(p));
return 0;
}
Output: 4

Pointer always stores address of a variable. Address is an integer value. Hence it takes 4 bytes.



========================================================================

int main()
{
int a=1;
printf("%d %d %d %d %d %d",++a,a++,++a,a,a++,++a);
return 0;
}
Output: 6 4 4 3 2 2

Processing happens from right to left. Start from right and increment variable ‘a’.



========================================================================

int main()
{
int a=1;
int b=0;
int c=0;
c=a++ + a++;
b=a++ + ++a;
printf("%d %d %d\n",c,b,a);
printf("%d %d %d %d %d %d\n",a++,a++,a++,a++,a++,a++);
printf("%d %d %d %d %d %d",++a,a++,++a,a,a++,++a);
return 0;
}
Output: 2 8 5
10 9 8 7 6 5
16 14 14 13 12 12



========================================================================

#include<stdio.h>
int main()
{
char *p="abc";
*p++;

printf("%s",p);
return 0;
}
Output: bc

Pointer will be pointing to abc’s first address which is the address of a. When pointer is incremented, it will be pointing to ‘b’. So when we print it will show string from ‘b’ that is “bc”.



========================================================================

#include<stdio.h>
int main()
{
char *p="abc";
char a;
a=*++p;

printf("%c %s",a,p);
return 0;
}
Output: b bc



========================================================================

#include<stdio.h>
int main()
{
char *p="abcdefgh";

printf("%d %d %d %c %c",p,p++,++p,*p++,*++p);
return 0;
}
 Output: 134513788  134513787  134513787  b b



========================================================================

#include<stdio.h>
int main()
{
char *p="abc";
*p++;

printf("%s",p);
p--;
printf(" %s",p);
return 0;
}
Output: bc abc



========================================================================

int main()
{
char a='a';
char *p;
p=&a;
printf("%u %u",p,&a);
p++;
printf(" %u %u",p,&a);
return 0;
}
Output: 3220097815 3220097815 3220097816 3220097815



========================================================================

Write a C program to call by itself 5 times (recursively) without using any global variables.

#include<stdio.h>
int call();
int main()
{
call();
return 0;
}

int call()
{
static int count = 0;
count++;
if(count == 6)
{
        exit(0);
}
else
{
        printf("count = %d\n",count);
        call();
}
}

Using static variable to retain value between various function calls.



========================================================================

Why is volatile keyword used?
Answer: To tell compiler not to optimize the code.
Example:
static int noChange = 0;

void vol()
{
while (noChange!= 5)
{
                Printf(“hi”);
         }
}

Compiler will think noChange cannot become 5 and replace noChange!= 5 with true value.

static int noChange = 0;

void vol()
{
while (1)
{
                Printf(“hi”);
         }
}
So to avoid this type of optimization we make static int noChange as static volatile int noChange.



========================================================================

Can static variables be declared outside the main function?
Answer: Yes.
Variables declared as static outside any function definitions are visible throughout that file.



========================================================================

Can we use extern to a static variable?
Answer: No.
The purpose of static variable is to make that a variable private to the source file where it is declared.



========================================================================

How do you avoid structure padding?
Answer: You can do it using pragma



========================================================================

What is the difference between scope and lifetime of a variable?
Scope: It is the place where you can access the variable.
Lifetime: Time for which the variable will be alive.
Example
Void Abc();
int main()
{
Abc();
return 0;
}
void Abc()
{
                int count = 0;
                printf(“%d”,count);
}

Let us take count variable
Scope of count variable: You can access count variable only inside abc() function. So scope is local to function block.
Lifetime of count variable: From the beginning of function abc() till end of function abc() we can use variable count. So lifetime becomes “till control remain within the block”.



========================================================================

What is the difference between function definition and declaration?

Consider below example
Void Abc();
int main()
{
Abc();
return 0;
}
void Abc()
{
                int count = 0;
                printf(“%d”,count);
}
Function definition: Where the logic for the function is written.
void Abc()
{
                int count = 0;
                printf(“%d”,count);
}

Function declaration: It is like a signature to let compiler know that there is a function with name Abc();
Void Abc();

Function call: Calling the function to execute the function definition
Abc();


No comments:

Post a Comment