Beruflich Dokumente
Kultur Dokumente
int main()
{
char arr[] = "geeksforgeeks";
printf("%d", sizeof(arr));
return 0;
}
Output
Output: 14
Output: 1
int main()
{
printf(" \"GEEKS %% FOR %% GEEKS\"");
getchar();
return 0;
}
Output
int main()
{
char *str = "geeksforgeeks";
printf("%d", fun(str));
getchar();
return 0;
}
Output
Output: 13
int main()
{
int r = 20;
int *p = &r;
fun(p);
printf("%d", *p);
getchar();
return 0;
Output
Output: 20
int main()
{
int r = 20;
int *p = &r;
fun(&p);
printf("%d", *p);
getchar();
return 0;
Output
Output 10
Note that we are passing address of p to fun(). p in fun() is actually
a pointer to p in main() and we are changing value at p in fun(). So
p of main is changed to point q of fun(). To understand it better, let
us rename p in fun() to p_ref or ptr_to_p
void fun(int **ptr_to_p)
{
static int q = 10;
*ptr_to_p = &q; /*Now p of main is pointing to q*/
}
Also, note that the program wont cause any problem because q is
a static variable. Static variables exist in memory even after
functions return. For an auto variable, we might have seen some
weird output because auto variable may not exist in memory after
functions return.
Question 7
Predict the output of below program
int main()
{
int f = 0, g = 1;
int i;
for(i = 0; i < 15; i++)
{
printf("%d \n", f);
f = f + g;
g = f - g;
}
getchar();
return 0;
}
Output
Answer: The function prints first 15 Fibonacci Numbers.
Question 8
Predict the output of below program.
#include<stdio.h>
int main(void)
{
int a = 1;
int b = 0;
b = ++a + ++a;
printf("%d %d",a,b);
getchar();
return 0;
}
Output
Output : 3 6
Question 9
Predict the output of below program.
#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
printf("%p\t%p", p, a);
}
Output
Output:
Same address is printed
Question 10
Predict the output of below program.
Output:
Same address is printed
Question 11
Predict the output of below program.
int main()
{
int ary[4] = {1, 2, 3, 4};
printf("\n%ld",ary);
printf("\n%ld",ary+3);
int *p = ary + 3;
printf("\n%d\n", p[-2]);
getch();
}
Output
Output:
Base address of ary
Address of element 4
2
Question 12
Predict the output of below
program(Recursion).
void main()
{
m();
void m()
{
printf("hi");
}
}
Output
Output:
Compile Time error
Question 13
Predict the output of below program.
void main()
{
m();
void m()
{
printf("hi");
}
}
Output
Output:
Compile Time error
Question 14
Predict the output of below program.
#include <stdio.h>
void main()
{
m();
}
void m()
{
printf("hi");
m();
}
Output
Output:
Infinite hi
Question 15
Predict the output of below program.
#include <stdio.h>
void main()
{
static int x = 3;
x++;
if (x <= 5)
{
printf("hi");
main();
}
}
Output
Output:
hi hi
Question 16
Predict the output of below program.
#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int p[4];
p = ary;
printf("%d\n", p[1]);
}
Output
Output:
Compile Time error
Question 17
Predict the output of below program.
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97, .f = 3, .k = 1};
printf("%f\n", x.f);
}
Output
Output:
3.00000
Question 18
Predict the output of below program.
#include <stdio.h>
struct temp
{
int a;
int b;
int c;
} p[] = {0};
main()
{
printf("%d", sizeof(p));
}
Output
Output:
12
Question 19
Predict the output of below program.
#include <stdio.h>
struct student
{
char *name;
};
struct student s[2];
void main()
{
s[0].name = "alan";
s[1] = s[0];
printf("%s%s", s[0].name, s[1].name);
s[1].name = "turing";
printf("%s%s", s[0].name, s[1].name);
}
Output
Output:
alan alan alan turing
Question 20
Predict the output of below program.
#include <stdio.h>
struct student
{
char *name;
};
struct student s[2], r[2];
void main()
{
s[0].name = "alan";
s[1] = s[0];
r = s;
printf("%s%s", r[0].name, r[1].name);
}
Output
Output:
Compile Time error
Question 21
Predict the output of below program.
#include <stdio.h>
struct student
{
};
void main()
{
struct student s[2];
printf("%d", sizeof(s));
}
Output
Output:
0
Question 22
Predict the output of below program.
#include <stdio.h>
int main(){
static char a;
static long b;
int c;
printf("%d,%d,%d",a,b,c);
return 0;
}
Output
Output:
0 0 Garbage value
Question 23
Predict the output of below program.
int main()
{
int i;
for (i = 0; i<5; i++)
{
int i;
i = 10;
printf("%d ", i) ;
}
return 0;
}
Output
Output:
10 10 10 10 10
Question 23
Predict the output of below program.
#include <stdio.h>
#include <string.h>
int main()
{
printf("GEEKS size = %d \n\n", sizeof("GEEKS"));
printf("GEEKS length = %d \n", strlen("GEEKS"));
return 0;
}
Output
Output:
GEEKS size = 6
GEEKS length = 5
#include <stdio.h>
int extern i;
int main()
{
printf("%d", i);
}
int i = 10;
Output
Output:
10
Output:
False
Question 26
Predict the output of below program.
#include <stdio.h>
int main()
{
enum channel {star, sony, zee};
int i = 0;
for(i = star; i <= zee; i++)
{
printf("%d ", i);
}
return 0;
}
Output
Output:
012
Question 27
Predict the output of below program.
#include<stdio.h>
int main()
{
int i, j;
int p = 0, q = 2;
return 0;
}
Output
Output:
GeeksforGeeks
GeeksforGeeks
Question 28
Predict the output of below program.
#include <stdio.h>
#define R 4
#define C 4
int main()
{
int mat[R][C] = { {1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};
int i,j;
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
printf("%3d ", mat[i][j]);
printf("\n");
}
Output
Output:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Question 29
Predict the output of below program.
int main()
{
int i, j;
int arr[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", j[i[arr]] );
printf("\n");
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", i[j[arr]] );
}
Output
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
Question 30
Predict the output of below program.
int main()
{
int i, j;
int arr[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", j[i[arr]] );
printf("\n");
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
printf("%d ", i[j[arr]] );
}
Output
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
Question 31
Predict the output of below program.
int main()
{
int i = 10;
static int x = i;
if (x==i)
printf("x and i are Equal\n");
return 0;
}
Output
Output:
Here, it would seem that, as x and i are equal, so the
output will be x and i are Equal. But the output is quite
unexpected.
As, static variables are load time entity while auto
variables are run time entity an We can not initialize any
load time variable by the run time variable. So,the output
the c code above will show Compile error.
Question 32
Predict the output of below program.
int main()
{
int a = 10;
printf("%o %x", a, a);
return 0;
}
Output
Output:
As, %o is used to print the number in octal number
format and %x is used to print the number in
hexadecimal number format. And if we convert decimal
10 to octal , we get 12 and in hexadecimal it will give a
Question 33
Predict the output of below program.
int main()
{
int a = 10;
printf("%o %x", a, a);
return 0;
}
Output
Output:
As, %o is used to print the number in octal number
format and %x is used to print the number in
hexadecimal number format. And if we convert decimal
10 to octal , we get 12 and in hexadecimal it will give a
Question 34
Predict the output of below program.
int fun()
{
static int num = 40;
return num--;
}
int main()
{
for(fun(); fun(); fun())
{
printf("%d ", fun());
}
getchar();
return 0;
}
Output
Output:
38 35 32 29 26 23 20 17 14 11 8 5 2
getchar();
return 0;
}
Output
Output:
nowledge nowledge s
#include<stdio.h>
#define R 10
#define C 20
int main()
{
int (*p)[R][C];
printf("%d", sizeof(*p));
getchar();
return 0;
}
Output
Output:
10*20*sizeof(int) which is 800 for compilers with
integer size as 4 bytes.
int main()
{
char arr[] = {1, 2, 3};
char *p = arr;
if(&p == &arr)
printf("Same");
else
printf("Not same");
getchar();
}
Output
Output:
Not Same