74 Qs · since 2011 · 111 marks · 4.3 marks/paperHigh yield
In GATE CS, Programming in C is tested extensively through rigorous code tracing, pointer mechanics, and fundamental semantics rather than high-level syntax trivia. Questions empha… Guide
#include <stdio.h>
void func(int i, int j) {
if(i < j) {
int i = 0;
while (i < 10) {
j += 2;
i++;
}
}
printf("%d", i);
}
int main() {
int i = 9, j = 10;
func(i, j);
return 0;
}
The output of…head.
struct node{
int elt;
struct node *next;
};
int getListSize (struct node *head)
{
if( E1 ) return 1;
return E2;
}…#include <stdio.h>
int main(){
int *ptr, a, b, c;
a=5; b=11; c=20;
ptr=&a; *ptr=c; ptr=&c;
a=*(&b); c=*ptr-a;
printf("%d",c);
return(0);
}
The output of this program is ________. (answer in integer)
Note: Assume that the program compiles…#include <stdio.h>
void foo(int *p, int x){
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}
The output of the given C program is ________. (Answer in integer)LIST be a datatype for an implementation of linked list defined as follows:
typedef struct list {
int data;
struct list *next;
} LIST;
Suppose a program has created two linked lists, L1 and L2, whose contents are given in the figure below (code for creating L1 and L2 is not…#include <stdio.h>
int gate (int n) {
int d, t, newnum, turn;
newnum = turn = 0; t=1;
while (n>=t) t *= 10;
t /=10;
while (t>0) {
d = n/t;
n = n%t;
t /= 10;
if (turn) newnum = 10*newnum + d;
turn = (turn + 1) % 2;
}
return newnum;
}
int main () {
printf ("%d", gate(14362));
return 0;
}…#include <stdio.h>
void stringcopy(char *, char *);
int main() {
char a[30] = "@#Hello World!";
stringcopy(a, a + 2);
printf("%s
", a);
return 0;
}
void stringcopy(char *s, char *t) {
while(*t)
*s++ = *t++;
}
Which ONE of the following…int x=126, y=105;do { if(x>y) x=x-y; else y=y-x;} while(x!=y);printf("%d",x);
The output of the given C code segment is ________ . (Answer in integer)#include <stdio.h>
int main() {
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}
The output of the above program is ________. (Answer in integer)#include <stdio.h>
int g(int n) {
return (n+10);
}
int f(int n) {
return g(n*2);
}
int main() {
int sum, n;
sum=0;
for (n=1; n<3; n++)
sum += g(f(n));
printf ("%d", sum);
return 0;
}
The output of the given C program is ________.…#include <stdio.h>
int main() {
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;
}
printf("%d", a);
return 0;
}
Which one of the following statements is CORRECT?int f(int x, int y) {
for (int i=0; i<y; i++) {
x=x+x+y;
}
return x;
}
Which of the following statements is/are TRUE about the above function?#include <stdio.h>
int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }
void f(int x, int y) {
g(x);
h(y);
}
int main() {
f(g(10),h(20));
}
Which one of…int fX(char *a) {
char *b = a;
while(*b)
b++;
return b - a;
}
Which of the following statements is/are TRUE?#include <stdio.h>
int main() {
double a[2]={20.0, 25.0}, *p, *q;
p = a;
q = p + 1;
printf("%d,%d", (int)(q - p), (int)(*q - *p));
return 0;
}#include<stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d
", (x+y));
return 0;
}#include<stdio.h>
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d
", x, z[0], z[1]);
return 0;
}#include<stdio.h>
int main(int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++ )
printf("%d ", a[i][j][k]);
printf("
");
}
return 0;
}#include<stdio.h>
int main(int argc, char *argv[]){
char a = 'P';
char b = 'x';
char c = (a & b) + '*';
char d = (a | b) - '-';
char e = (a ^ b) + '+';
printf("%c %c %c
", c, d, e);
return 0;
}
ASCII encoding for…#include <stdio.h>
int main()
{
int i, j, count;
count = 0;
i = 0;
for (j = -3; j <= 3; j++)
{
if ((j >= 0) && (i++))
count = count + j;
}
count = count + i;
printf("%d", count);
return 0;
}
Which one of the…int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex = 1; loopIndex <= n - 1; loopIndex++)
total = x * total + Y[loopIndex];
return total;
}
Let be an array of 10 elements with , for all …#include <stdio.h>
int main(){
int arr[4][5];
int i, j;
for (i=0; i<4; i++){
for (j=0; j<5; j++){
arr[i][j] = 10*i + j;
}
}
printf("%d", *(arr[1] + 9));
return 0;
}
What is the output of the above program?int SomeFunction(int x, int y)
{
if ((x == 1) || (y == 1)) return 1;
if (x == y) return x;
if (x > y) return SomeFunction(x - y, y);
if (y > x) return SomeFunction(x, y - x);
}
The value returned by SomeFunction(15, 255) is ________.#include <stdio.h>
#include <stdlib.h>
struct Node{
int value;
struct Node *next;};
int main(){
struct Node *boxE, *head, *boxN; int index = 0;
boxE = head = (struct Node *) malloc(sizeof(struct Node));
head->value = index;
for (index = 1; index <= 3; index++){
boxN = (struct Node *) malloc(sizeof(struct Node));
boxE->next = boxN;
boxN->value = index;
boxE = boxN; }
for (index = 0; index <= 3; index++) {
printf("Value at index %d is %d
", index, head->value);
head = head->next;
printf("Value at index %d is %d
", index+1, head->value); } }…#include <stdio.h>
int main() {
int a[4][5]={{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18 , 19, 20}};
printf("%d
", *(*(a+**a+2)+3));
return(0);
}
The output of the program is…int fun1(int n) {
static int i = 0;
if (n > 0) {
++i;
fun1(n-1);
}
return(i);
}
int fun2(int n) {
static int i = 0;
if (n > 0) {
i = i + fun1(n);
fun2(n-1);
}
return(i);
}
The return value of fun2(5)…int tob(int b, int* arr){
int i;
for(i=0; b>0; i++){
if(b%2) arr[i]=1;
else arr[i]=0;
b = b/2;
}
return(i);
}
int pp(int a,int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob(b,arr);
for(i=0; i<len; i++){
if(arr[i]==1)
tot = tot * ex;
ex = ex * ex;
}
return(tot);
}…#include <stdio.h>
int jumble(int x, int y){
x=2*x+y;
return x;
}
int main(){
int x=2, y=5;
y=jumble(y,x);
x=jumble(y,x);
printf("%d
", x);
return 0;
}
The value printed by the program is _______.#include <stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip=arr+4;
printf("%d
", ip[1]);
return 0;
}
The number that will be displayed on execution of the program is _______.#include <stdio.h>
int r(){
static int num=7;
return num--;
}
int main(){
for (r();r();r())
printf("%d",r());
return 0;
}
Which one of the following values will be displayed on execution of the programs?#include<stdio.h>
struct Ournode{
char x,y,z;
};
int main() {
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf ("%c, %c", *((char*)q+1), *((char*)q+2));
return 0;
}
The output of this program is:#include <stdio.h>
int counter = 0;
int calc (int a, int b) {
int c;
counter++;
if (b==3) return (a*a*a);
else {
c = calc(a, b/3);
return (c*c*c);
}
}
int main () {
calc(4, 81);
printf ("%d", counter);
}
The output of this…typedef struct node {
int data;
node* next;
} node;
void join(node* m, node* n){
node* p = n;
while(p->next != NULL) {
p = p->next;
}
p->next = m;
}Assuming that \(m\) and \(n\) point to valid NULL-terminated linked lists, invocation ofjoin…#include <stdio.h>
int *assignval(int *x, int val) {
*x = val;
return x;
}
void main () {
int *x = malloc(sizeof(int));
if(NULL == x) return;
x = assignval(x, 0);
if(x) {
x = (int *)malloc(sizeof(int));
if(NULL == x) return;
x = assignval(x, 10);
}
printf("%d
", *x);
free(x);
}…#include <stdio.h>
#include <string.h>
void printlength(char *s, char *t) {
unsigned int c = 0;
int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t);
printf("%d
", len);
}
void main() {
char *x = "abc";
char *y = "defgh";
printlength(x,y);
}…#include <stdio.h>
int total(int v) {
static int count = 0;
while(v) {
count += v&1;
v >>= 1;
}
return count;
}
void main() {
static int x = 0;
int i = 5;
for(; i > 0; i--) {
x = x + total(i);
}
printf("%d
", x);
}void printxy(int x, int y) {
int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;
printf("%d,%d",x,y);
}
The output of invoking printxy(1,1) isx by y using repeated subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) {
r = r - y;
q = q + 1;
}
Which of the…swap (&x, &y) exchanges the contents of x and y.
int main() {
int array[] = {3, 5, 1, 4, 6, 2};
int done = 0;
int i;
while (done == 0) {
done = 1;
for (i=0; i<=4; i++) {
if (array[i] < array[i+1]) {
swap(&array[i], &array[i+1]);
done = 0;
}
}
for (i=5; i>=1; i--) {
if (array[i] > array[i-1]) {
swap(&array[i], &array[i-1]);
done = 0;
}
}
}
printf("%d", array[3]);
}…#include<stdio.h>
int main() {
int m = 10;
int n, n1;
n = ++m;
n1 = m++;
n--;
--n1;
n -= n1;
printf("%d", n);
return 0;
}
The output of the program is _________.#include<stdio.h>
#include<string.h>
int main() {
char* c = "GATECSIT2017";
char* p = c;
printf("%d", (int)strlen(c+2[p]-6[p]-1));
return 0;
}
The output of the program is _________.void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}
Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?#include<stdio.h>
void mystery(int *ptra, int *ptrb) {
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main() {
int a=2016, b=0, c=4, d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%d
", a);
}…p[] of size ().
int max(int *p, int n) {
int a=0, b=n-1;
while (__________) {
if (p[a] <= p[b]) { a = a+1; }
else { b = b-1; }
}
return p[a];
}
The…void f(int* p, int m) {
m = m + 5;
*p = *p + m;
return;
}
void main() {
int i=5, j=10;
f(&i, j);
printf("%d", i+j);
}int exp(int X, int Y) {
int res = 1, a = X, b = Y;
while ( b != 0 ){
if ( b%2 == 0) { a = a*a; b = b/2; }
else { res = res*a; b = b-1; }
}
return res;
}
Which one of the following conditions…int f(int *p, int n)
{
if (n <= 1) return 0;
else return max(f(p+1,n-1),p[0]-p[1]);
}
int main()
{
int a[] = {3,5,2,6,4};
printf("%d", f(a,5));
}
Note: returns the maximum of and .
The value printed by this program is _______.void f1(int a, int b) {
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b) {
int c;
c=*a; *a=*b; *b=c;
}
int main(){
int a=4, b=5, c=6;
f1(a,b);
f2(&b, &c);
printf("%d",c-a-b);
}begin
q := 0
r := x
while r >= y do
begin
r := r - y
q := q + 1
end
end
The post condition that needs to be satisfied after the program terminates isint main () {
unsigned int x[4][3] =
{{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}while(first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
found = TRUE;
else last = middle - 1;
middle = (first + last)/2;
}
if (first > last) notPresent = TRUE;
The cyclomatic…#include <stdio.h>
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '\0';
printf("%s", s1);
}
What will be printed by the program?int main( )
{
static int a[ ] = {10, 20, 30, 40, 50};
static int *p[ ] = {a, a+3, a+4, a+1, a+2};
int ptr = p;
ptr++;
printf("%d%d", ptr-p,ptr);}</code>
The output of the program is ____________.#include<stdio.h>
int main( )
{
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k -= --j;
for(i = 0; i < 5; i++)
{
switch(i + k)
{
case 1:
case 2: printf("
%d", i+k);
case 3: printf("
%d", i+k);
default: printf("
%d", i+k);
}
}
return 0;
}…#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main( )
{
int x = 1;
x += f1( ) + f2( ) + f3( ) + f2( );
printf("%d", x);
return 0;
}
int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;}…#include <stdio.h>
main()
{
int i;
int *pi = &i;
scanf("%d",pi);
printf("%d
", i+5);
}
Which one of the following statements is TRUE?size is the number of elements in the array E:
int MyX(int *E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;
for(i = 0; i < size; i++)
Y = Y + E[i];
for(i = 0; i < size; i++)
for(j = i; j < size; j++)
{
Z = 0;
for(k = i; k <= j; k++)
Z = Z + E[k];
if (Z > Y)
Y = Z;
}
return Y;
}…func shown below:
int func(int num) {
int count = 0;
while (num) {
count++;
num>>= 1;
}
return (count);
}
The value returned by func(435) is ________.n and p are unsigned int variables in a C program. We wish to set p to . If n is large, which one of the following statements is most likely to set p correctly?C = 100;
for i = 1 to n do
for j = 1 to n do
{
Temp = A[i][j] + C;
A[i][j]= A[j][i];
A[j][i] = Temp - C;
}
for i = 1 to n do
for j = 1 to n do
output (A[i][j]);listA contains n () elements, sorted in ascending order.
int ProcessArray(int *listA, int x, int n)
{
int i, j, k;
i = 0;
j = n-1;
do {
k = (i+j)/2;
if (x <= listA[k])
j = k-1;
if (listA[k] <= x)
i = k+1;
}while (i <= j);
if (listA[k] == x)
return(k);
else
return -1;
}…A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in…A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in…char inChar = 'A' ;
switch ( inChar ) {
case 'A' : printf ("Choice A\ n") ;
case 'B' :
case 'C' : printf ("Choice B") ;
case 'D' :
case 'E' :
default : printf ( " No Choice" ) ; }int a, b, c = 0;
void prtFun(void);
main( )
{ static int a = 1; /* Line 1 */
prtFun( );
a += 1;
prtFun( );
printf("
%d %d ", a, b);
}
void prtFun(void)
{ static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf("
%d %d ", a, b);
}…int a, b, c = 0;
void prtFun(void);
main( )
{ static int a = 1; /* Line 1 */
prtFun( );
a += 1;
prtFun( );
printf("
%d %d ", a, b);
}
void prtFun(void)
{ static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf("
%d %d ", a, b);
}…Char c[] = "GATE2011";
char *p = c;
printf("%s", p + p[3] - p[1]);Topic guide
In GATE CS, Programming in C is tested extensively through rigorous code tracing, pointer mechanics, and fundamental semantics rather than high-level syntax trivia. Questions emphasize pointer arithmetic and multidimensional array decay, storage classes (especially static local variables), evaluation order, side effects of increment/decrement operators, parameter passing mechanisms (pass-by-value vs pass-by-reference), and recursive program state tracking. The topic features a nearly balanced split between NAT and MCQ items across 1-mark and 2-mark categories, demanding precise, bug-free execution traces without a compiler.
Pointer Arithmetic and Multidimensional Array Indexing
common · mixed · 2 marks · 2026, 2025, 2024, 2022
Evaluating complex pointer expressions involving 1D/2D/3D arrays, array decaying, pointer subtraction, array subscript commutativity (), and double dereferencing ().
Storage Classes, Variable Scope, and Shadowing
common · NAT · 2 marks · 2026, 2023, 2020, 2019
Tracing code containing static local variables retaining state across multiple recursive or iterative function calls, global vs local variable shadowing, and lifetime vs scope distinctions.
Parameter Passing and Pointer Swapping Idioms
common · mixed · 1 marks · 2026, 2025, 2024, 2019
Testing the distinction between pass-by-value and pass-by-reference using pointers, including classic traps where pointer formal parameters are reassigned locally without dereferencing.
Short-Circuit Evaluation, Precedence, and Side Effects
common · mixed · 1 marks · 2024, 2022, 2021, 2017
Evaluating expressions involving logical operators (&&, ||) where side-effects like pre/post increment (++i, i++) or assignment are skipped due to short-circuiting, or custom operator precedence tables.
Loop Invariants, Algorithm Semantics, and Verification
occasional · MCQ · 2 marks · 2025, 2024, 2017, 2016
Formulating correct pre/post-conditions, identifying loop invariants (e.g., division by repeated subtraction, binary exponentiation), or filling missing loop termination/update guards.
String Manipulation and Null Termination Pitfalls
occasional · mixed · 1 marks · 2025, 2024, 2017, 2015
Tracing pointer-based string copies or scans that miss copying the null terminator '\0', unsigned size_t differences producing underflow, or ASCII arithmetic offsets.
Dynamic Memory Allocation and Memory Layout
occasional · MCQ · 1 marks · 2026, 2021, 2017
Identifying memory leaks, dangling pointers, uninitialized heap memory accesses, or matching C declarations with memory segments (Stack, Heap, Data/BSS).
Mathematical Algorithm Trace / Output Evaluation
common · NAT · 1 marks · 2021
A C recursive function implementing a fundamental mathematical operation (e.g., Euclidean GCD via repeated subtraction) is given with multiple base conditions and recursive steps. Candidates must trace the execution or identify the closed-form computation to determine the return value for given arguments.
Pointer Subtraction
Used when computing the numerical result of subtracting two pointers pointing to elements of the same array.
2D Array Element Address Calculation
Used for mapping multi-dimensional array indices to flat memory offsets in row-major layout.
Array Subscript Equivalence
Used to resolve commutative and unorthodox array indexing expressions in C snippets.
Cyclomatic Complexity
Used to calculate McCabe's cyclomatic complexity where is the count of predicate/decision conditions in the control flow.
Integer Division Loop Invariant
Used to verify formal loop invariants in repeated subtraction integer division routines.
Euclidean Subtraction GCD
Used when analyzing recursive functions that reduce two numbers by repeated subtraction until equality or 1 is reached.
Shift towards Numerical Answer Type (NAT) questions requiring exact printed output calculations rather than multiple-choice guessing.
2026, 2025, 2023, 2021, 2020, 2019, 2018, 2017, 2015
Introduction of non-standard operator precedence tables where candidates must compute expression values based on customized grammar rules rather than default C precedence.
2024
Inclusion of Multi-Select Questions (MSQ) examining algorithmic invariants, asymptotic function bounds, and string pointer validity across multiple scenarios.
2024
Increased focus on subtle C standard specifications such as unsigned arithmetic wrap-around with strlen(), uninitialized struct next pointer dereferencing, and block-scope variable shadowing.
2026, 2025, 2024, 2021, 2017
Focus on concise ANSI C code implementing arithmetic algorithms via recursion, testing understanding of control flow and reduction to base cases within NAT format.
2021
Easy questions typically test direct syntax rules, single function calls with pass-by-value/pass-by-pointer, basic switch fall-through, or standard 1D pointer dereferencing. Medium questions combine multiple concepts: nested loops with complex pointer arithmetic on 2D/3D arrays, static variables in mutual/deep recursion, short-circuit boolean expressions with post-increment side effects, or unsigned arithmetic traps. Hard/complex questions require formulating algebraic invariants for loops, identifying obscure memory leak conditions, or tracing customized operator precedence grammars.