C Quiz
C Quiz
return 0;
#include<stdio.h>
int main()
{
int a = 12;
void *ptr = (int *)&a;
printf("%d", *ptr);
getchar();
return 0;
}
#include<stdio.h>
int main()
{
int a = 12;
void *ptr = (int *)&a;
printf("%d", *(int *)ptr);
getchar();
return 0;
}
NOTE:
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.
For ternary operator, both 2nd and 3rd operands are necessary.
#include <stdio.h>
#define get(s) #s
int main()
{
char str[] = get(GeeksQuiz);
printf("%s", str);
return 0;
}
GeeksQuiz
NOTE:
The preprocessing operator '#' is used to convert a string argument into a string constant.
#define INC1(a) ((a)+1)
#define INC3( a ) (( a ) + 1)
#define INC4 ( a ) (( a ) + 1)
The translator which performs macro calls expansion is called Macro pre -
processor.
A Macro processor is a program that copies a stream of text from one place to
another, making a systematic set of replacements as it does so.
A Dynamic linker is the part of an operating system that loads and links the shared
libraries.
The C pre processor is a micro processor that is used by compiler to transform your
code before compilation. It is called micro pre-processor because it allows us to add
macros.
NOTE: In C, when we initialize less no of elements in an array all uninitialized elements become ‘\0′
in case of char and 0 in case of integers.
#include <stdio.h>
int main()
{
char *str1 = "GeeksQuiz";
char str2[] = "GeeksQuiz";
return 0;
}
sizeof(str1) = 4, sizeof(str2) = 10
int main()
{
char *s1 = (char *)malloc(50);
char *s2 = (char *)malloc(50);
strcpy(s1, "Geeks");
strcpy(s2, "Quiz");
strcat(s1, s2);
printf("%s", s1);
return 0;
}
GeeksQuiz
strcpy puts \0 at the end. strcat starts from \0, concatenates string and puts \0 at the end.
int main() {
struct Ournode p = {'1', '0', 'a' + 2};
struct Ournode *q = &p;
printf("%c, %c", *((char *)q + 1), *((char *)q + 2));
return 0;
}
'a' + 2 will be 'c', so Ournode p = {'1', '0', 'c'} and output will be 0, c.
char *getString()
{
char *str = "GfG"; /* Stored in read only part of shared segment */
int main()
{
printf("%s", getString());
getchar();
return 0;
}
Works Fine because str is stored in read only part of shared memory.
NOTE
static variables can only be initialized using constant literals, NOT SOME FUNCTION RETURNS.
“break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can
be used in “switch” body.
In "switch" body, two "case" can't result in same value. Though having only "case" or only "default"
is okay. In fact, "switch" body can be empty also.
#include <stdio.h>
int main()
{
int i;
for ( i=0; i<5; i++ )
{
int i = 10;
printf ( "%d ", i );
i++;
}
return 0;
}
-- 10 10 10 10 10
#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
In C, if a macro is not defined, the pre-processor assigns 0 to it by default. Hence, the control goes to
the conditional else part and 5 is printed.
printf("Quiz");
int main()
{
return 0;
}
The above code produces error because printf() is called outside main
#include <stdio.h>
#define a 10
int main()
{
printf("%d ",a);
#define a 50
printf("%d ",a);
return 0;
}
10 50
After the pre-processing of a C program, a .i file is generated which is passed to the compiler for
compilation.
#include <stdio.h>
#define get(s) #s
int main()
{
char str[] = get(GeeksQuiz);
printf("%s", str);
return 0;
}
GeeksQuiz
The preprocessing operator '#' is used to convert a string argument into a string constant
The translator which performs macro calls expansion is called Macro pre -
processor.
A Macro processor is a program that copies a stream of text from one place to
another, making a systematic set of replacements as it does so.
A Dynamic linker is the part of an operating system that loads and links the shared
libraries.
The C preprocessor is a micro processor that is used by compiler to transform your
code before compilation. It is called micro pre-processor because it allows us to add
macros.
#include<stdio.h>
struct st
{
int x;
static int y;
};
int main()
{
printf("%d", sizeof(struct st));
return 0;
}
Compile error
In C, struct and union types cannot have static members. In C++, struct types are allowed to have
static members, but union cannot have static members in C++ also.
A structure variable can be assigned to other using =, but cannot be compared with other using ==
/* First declaration */
typedef struct node
{
int data;
struct node *nextPtr;
}* NODEPTR;
/* Second declaration */
struct node
{
int data;
struct node * nextPtr;
};
typedef struct node * NODEPTR;
int main()
{
struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b =
2};
printf("%d %d %d and",arr[0].a[0],arr[0].a[1],arr[0].b);
printf("%d %d %dn",arr[1].a[0],arr[1].a[1],arr[1].b);
return 0;
}
- there is no error, all initializations ae correct.
struct addr {
char city[10];
char street[30];
int pin ;
};
struct {
char name[30];
int gender;
struct addr locate;
} person , *kd = &person ;
Then *(kd -> name +2) can be used instead of
*(kd -> name +2) = *((*kd).name + 2 )
# include <stdio.h>
int main()
{
char str1[] = "GeeksQuiz";
char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}
n1 = 10, n2 = 9
#include <stdio.h>
int main()
{
char str[] = "geeksskeeg";
fun(str);
puts(str);
return 0;
}
int main()
{
int r = 20;
int *p = &r;
fun(&p);
printf("%d", *p);
return 0;
}
1. #include<stdio.h>
2. void main ()
3. {
4. int a[10] = {100, 206, 300, 409, 509, 601}; //Line 1
5. int *p[] = {a, a+1, a+2, a+3, a+4, a+5}; //Line 2
6. int **pp = p; //Line 3
7. pp++; // Line 4
8. printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 5
9. *pp++; // Line 6
10. printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 7
11. ++*pp; // Line 8
12. printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 9
13. ++**pp; // Line 10
14. printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 11
15. }
1 1 100
2 2 300
2 3 409
2 3 410
INCORRECT:a>b ? c=10 : d=10; is valid statement. Based on the value of a and b, either c
or d gets assigned the value of 10.
CORRECT: a>b ? (c=10,d=20) : (c=20,d=10); is valid statement. Based on the value of a and
b, either c=10,d=20 gets executed or c=20,d=10 gets executed.
Name of array in C gives the address(except in sizeof operator) of the first element. Adding 1 to this
address gives the address plus the sizeof type the array has. Applying the Address-of operator
before the array name gives the address of the whole array. Adding 1 to this address gives the
address plus the sizeof whole array.
There is compilation error in the declaration " int a[][] = {{1,2},{3,4}};". Except the first dimension,
every other dimension must be specified. int arr[] = {5, 6, 7, 8} //valid int arr[][5] = {}; //valid int arr[]
[] = {}; //invalid int arr[][10][5] = {}; //valid int arr[][][5] = {}; //invalid
int (*p)[5];
p is a pointer to an array of 5 integers
Here p is basically a pointer to integer array of 5 integers. In case of “int *p[5]”, p is array of 5
pointers to integers.
#include "stdio.h"
int * arrPtr[5];
int main()
{
if(*(arrPtr+2) == *(arrPtr+4))
{
printf("Equal!");
}
else
{
printf("Not Equal");
}
return 0;
}
Here arrPtr is a global array of pointers to int. It should be noted that global variables such arrPtr are
initialized to ZERO. That’s why all are elements of arrPtr are initialized implicitly to ZERO i.e. correct
answer is “it will always print equal”
In a C file (say sourcefile1.c), an array is defined as follows. Here, we don’t need to mention
array arr size explicitly in [] because the size would be determined by the number of elements
used in the initialization.
int arr[] = {1,2,3,4,5};
In another C file (say sourcefile2.c), the same array is declared for usage as follows:
extern int arr[];
In sourcefile2.c, we can use sizeof() on arr to find out the actual size of arr.?
FALSE
#include "stdio.h"
void fun(int n)
{
int idx;
int arr1[n] = {0};
int arr2[n];
int main()
{
fun(4);
return 0;
}
Initialization of arr1 is incorrect. arr1 can’t be initialized due to its size being specified as
variable. That’s why compile error.
#include "stdio.h"
int size = 4;
int arr[size];
int main()
{
if(arr[0])
printf("Initialized to ZERO");
else
printf("Not initialized to ZERO");
return 0;
}
Compile error because size of arr has been defined using variable outside any function.
An array whose size is specified as variable can’t be defined out any function. It can be defined only
inside a function. So putting arr[size] outside main() would result in compile error.
int ( * f) (int * ) ;
A pointer to a function that takes an integer pointer as argument and returns an integer.