C Interview questions
C Interview questions
int main(void)
{
if (printf("Hello World"))
}
// EXAMPLE 1
int *ptr = (int *)malloc(sizeof(int));
.............
.............
free(ptr);
// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10; // or printf("%d", *ptr);
// EXAMPLE 2
int *ptr = NULL
{
int x = 10;
ptr = &x;
}
// x goes out of scope and memory allocated to x is free now.
// So ptr is a dangling pointer now.
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
#include <stdio.h>
void fun()
{
// static variables get the default value as 0.
static int x;
printf("%d ", x);
x = x + 1;
}
int main()
{
fun();
fun();
return 0;
}
// Output: 0 1
// PROGRAM 1
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
++*p;
printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p);
return 0;
}
// PROGRAM 2
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*p++;
printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p);
return 0;
}
// PROGRAM 3
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*++p;
printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p);
return 0;
}
The output of above programs and all such programs can be easily guessed by remembering
following simple rules about postfix ++, prefix ++ and * (dereference) operators
1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is
left to right.
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity.
Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore
the output of first program is “arr[0] = 11, arr[1] = 20, *p = 11“.
The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *.
Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
The expression *++p has two operators of same precedence, so compiler looks for assoiativity.
Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore
the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
What is l-value?
l-value or location value refers to an expression that can be used on left side of assignment operator. For
example in expression “a = 3”, a is l-value and 3 is r-value.
l-values are of two types:
“nonmodifiable l-value” represent a l-value that can not be modified. const variables are “nonmodifiable
l-value”.
“modifiable l-value” represent a l-value that can be modified.
#include<stdio.h>
if(n > 0)
printNos(n-1);
int main()
int n=100;
printNos(100);
return 0;
int main(void)
{
const volatile int local = 10;
int *ptr = (int*) &local;
printf("Initial value of local : %d \n", local);
*ptr = 100;
printf("Modified value of local: %d \n", local);
return 0;
}
http://www.geeksforgeeks.org/quiz-corner-gq/#C%20Programming%20Mock%20Tests