C Programing Interview Question
C Programing Interview Question
Set 3
Q.1 Write down the smallest executable code? Output
void main()
printf{"hello"}
For Example:
Pointers which are used in devices, running program,
simple pointers, which we
i.e to attack on other computers via this far pointers.
normally studied in C and C++
Q.6 Why does “type demotion” does not exist instead of “type
promotion”? Also, it would consume less space resource than by doing it
from type promotion.?
Ans. Let’s take an example to understand it.
Suppose
double a=1.5; int b=10 and we want to calculate a+b
By type demotion, float type a will convert to int. Therefore a=1 and
a+b=1+10=11 but we know that correct answer is 11.5 which will only get by type
promotion. So the conclusion is that by type demotion we will not get the correct
answer.
Q.7 What are stack and heap areas?
1. Heap Area:It is used for the objects allocated dynamically (Using malloc()
and calloc()).
2. Stack Area:It is used to store local variables and arguments of a method.
This stays in memory only till the termination of that particular method.
Please refer stack vs heap memory for details.
Q.8 Difference between #include in C and import in Java?
Ans.
#include import
It is processed by pre-
It is processed by compiler.
processor software.
struct test
{
char *str;
};
int main()
{
struct test st1, st2;
st1.str = (char *)malloc(sizeof(char) * 20);
strcpy(st1.str, "GeeksforGeeks");
st2 = st1;
st1.str[0] = 'X';
st1.str[1] = 'Y';
return 0;
}
Output
st1's str = XYeksforGeeks
st2's str = XYeksforGeeks
To do Deep Copy, we allocate new memory for dynamically allocated members
and explicitly copy them.
// C program to demonstrate deep copy
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
struct test
{
char *str;
};
int main()
{
struct test st1, st2;
st1.str = (char *)malloc(sizeof(char) * 20);
strcpy(st1.str, "GeeksforGeeks");
st2 = st1;
return 0;
}
Output
st1's str = XYeksforGeeks
st2's str = GeeksforGeeks