C Programming Questions
C Programming Questions
Thara. R. J
04 – 08 – 2018
GATE-2000
#include <stdio.h>
int main()
{
int a = 67;
printf("%o\n", a);
return 0;
}
Unsigned Hexadecimal for integer : %x, %X
#include <stdio.h>
int main()
{
int a = 15;
printf("%x\n", a);
return 0;
}
Predict output ?
int main() {
int x = 032;
printf("%d", x);
return 0;
}
Predict output ?
#include <stdio.h>
int main(){
int a,b,c;
a=0x10; b=010;
c=a+b;
printf("\nAddition is= %d",c);
return 0;
}
%d Vs %i
%d specifies signed decimal integer while %i specifies integer.
There is no difference between the %i and %d format specifiers for printf.
%d and %i behavior is different in scanf
%d assume base 10 while %i auto detects the base.
So with scanf, 012 would be 10 with %i but 12 with %d.
%d takes integer value as signed decimal integer i.e. it takes negative values
along with positive values but values should be in decimal otherwise it will
print garbage value.
%i takes integer value as integer value with decimal, hexadecimal or octal type.
#include <stdio.h>
int main( ) {
int a;
printf("Enter number: ");
scanf("%5d", &a); //123456
printf("Formatted number is: %7d", a);
return 0;
}
Predict output ?
#include <stdio.h>
int main() {
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f",
(9/5)*c + 32);
return 0;
}
Predict output ?
#include <stdio.h>
int main() {
int data = 29;
printf("%x\n", data);
int data printf("%0x\n", data);
printf("%8x\n", data);
printf("%08x\n", data);
}
Consider the following C program
If entered data items are,
9876 5432 1
What will be the output?
#include<stdio.h>
void main()
{ int a, b, c;
printf ("Enter the numbers");
scanf("%3d%3d%3d",&a,&b,&c);
printf("%d%d%d", a, b, c);
}
Predict output ?
#include <stdio.h>
void main(){
unsigned char c=290;
printf("%d",c);
}
Predict output ?
#include <stdio.h>
void main()
{ unsigned char c=325;
printf("%c",c);
}
Predict output ?
#include<stdio.h>
int main() {
typedef int i;
i a = 0;
printf("%d", a);
return 0;
}
Predict output ?
#include <stdio.h>
enum numbers {
zero, one, two, three ,
four=3,five,six,seven=0,eight
};
void main() {
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d",zero,
one,two,three,four,five,six,seven,eight);
}
Arrange the operators according to
their precedence: +, %, ->, =
A. ->, %, +, =
B. =, +, %, ->
C. %, +, =, ->
D. %, ->, =, +
Predict output ?
#include <stdio.h>
void main()
{
printf("value is = %d",(10++));
}
Predict output ?
What will be the output of following program?
#include <stdio.h>
void main() {
unsigned short var='B';
var+=2;
var++;
printf("var : %c , %d ", var,var);
}
Answer = var : E,69
ASCII Code of
Var=‘B’ • A=65
Var = 66 in decimal • B=66
Var+2 = 68 (represents • C=67
‘D’ in ASCII)
• ……..
Var++ = 69(represents
• ……..
‘E’ in ASCII)
• a = 97
Ans = var : E,69
• b = 98
• c = 99
GATE 2017 – Set 2
Consider the following C program.
#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 ______
Answer = 0
• m = 10
• n = ++m
(m = 11 , n = 11)
• n1 = m++
(n1 = 11 , m = 12)
• n-- = 10
• --n1 = 10
• n -= n1
(n=0)
Predict output ?
#include <stdio.h>
void main()
{
int x=(20 || 40 ) && (10);
printf("x= %d",x);
}
Answer: x = 1
• X = (20 logical OR 40) logical AND 10
• X= (TRUE) AND 10
• X= TRUE
• Which is equivalent to 1
Predict output ?
#include <stdio.h>
void main() {
int x;
x= (printf("AA")||printf("BB"));
printf("%d",x);
printf("\n");
x= (printf("AA")&&printf("BB"));
printf("%d",x);
}
Answer: AA1
AABB1
• Prints AA, then the condition is logical OR. Since
AA is 1, due to optimisation – x is predicted as 1.
• Prints AA, but AA = 1. Since next condition is
logical AND, it prints BB. BB = 1. and finally
prints 1
Predict output ?
#include <stdio.h>
void main()
{
int a=3,b=2;
a=a==b==0;
printf("%d,%d",a,b);
}
Answer: 1,2
Compare from left then assign value
A= (A==B) ==0
A= (0==0)
A=1
But B= 2
Predict output ?
#include <stdio.h>
void main(){
int intVar=20,x;
x= ++intVar,intVar++,++intVar;
printf("Value of intVar=%d, x=%d",intVar,x);
}
Answer: Value of intVar= 23, 21
intVar = 20
step by step execution because of coma
step 1: ++intVar =21, x=21
step 2: intVar++ =22
step 3: ++intVar =23
Predict output ?
#include <stdio.h>
int main() {
int x,y;
x=(100,200);
y=100,200;
printf("x=%d,y=%d",x,y);
return 0;
}
Answer: 200,100
x=(100,200);
selects x=200 (because of bracket)
y=100,200;
selects y=100
Predict output ?
#include <stdio.h>
int main(){
int x;
x=100,30,50;
printf("x=%d\n",x);
x=(100,30,50);
printf("x=%d\n",x);
return 0;
}
Answer: 100,50
x=100,30,50;
selects x=100
x=(100,30,50);
selects x=50 (because of bracket)
Predict output ?
#include <stdio.h>
int main(){
char val=250;
int ans;
ans= val+ !val + ~val + ++val;
printf("%d",ans);
return 0;
}
Answer: -6
val = 250 (which is unsigned int)
val = 250-128= 122 = 1111010 (which is
2’s complement of 6)
therefore val =-6
ans = val+ !val + ~val + ++val;
-6 = -5 + 0 + 4 + -5
Predict output ?
#include <stdio.h>
int main(){
int var;
var= - -10;
printf("value of var= %d\n",var);
var= + +10;
printf("value of var= %d\n",var);
return 0;
}
Answer: value of var = 10
value of var = 10
case 1:
var = - ( - 10) =10
case 2:
var = + ( + 10) =10
ISRO 2017
What does the following program do when the input is unsigned 16 bit integer?
#include<stdio.h>
main(){
unsigned int num;
int i;
scanf("%u", &num);
for(i=0;i<16;i++){
printf("%d", (num<<i&1<<15)?1:0);
}
}
A. It prints all even bits from num B. It prints all odd bits from num
C. It prints binary equivalent of num D. None of above
Answer: c)It prints binary equivalent of num
x = 1, y = 2, z = 3
x = 10, y = 20.000000, z = 3
x = 10, y = 20.000000, z = 100
#include <stdio.h>
extern int var = 0;
int main() {
var = 10;
printf("%d ", var);
return 0;
}
Answer:10
var = 10
Reason: Check the scopes of each variable assignment.
#include <stdio.h>
int var = 20;
int main() {
int var = var;
printf("%d ", var);
return 0;
}
Answer: Garbage Value
Let us divide ‘I’ as Iin (for loop) and Iout (inside for
loop) based on scope.
Loop 1: Iin = 0, Iout=10, prints Iout, increments Iout.
Loop 2: Iin = 1, Iout=10, prints Iout, increments Iout.
Loop 3: Iin = 2, Iout=10, prints Iout, increments Iout.
Loop 4: Iin = 3, Iout=10, prints Iout, increments Iout.
Loop 5: Iin = 4, Iout=10, prints Iout, increments Iout.
Predict output ?
#include <stdio.h>
int main() {
static int i=5;
if (--i) {
printf("%d ",i);
main();
}
}
Predict output ?
#include <stdio.h>
int main() {
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
What is the output of the program?
int main() {
printf("%d", f(5));
}
GATE CS 2004
Consider the following C function
int f(int n) {
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
GATE CS 2012 - Predict output ?
#include <stdio.h>
int a, b, c = 0;
void prtFun (void);
int main () {
static int a = 1;
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void) {
static int a = 2;
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
GATE CS 2012 - Predict output ?
#include <stdio.h>
int a, b, c = 0;
void prtFun (void);
int main () {
auto int a = 1;
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void) {
register int a = 2;
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
GATE 2017 - Set 1
#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\n", x);
}
For a given integer n, what does the
function compute?
(A) Gate (B) gatecse (C) 2018 (D) Compile Time Error
Predict output ?
Predict output ?
#include "stdio.h"
int * gPtr;
int main() {
int * lPtr = NULL;
if(*gPtr == *lPtr) {
printf("Equal!");
}
Else {
printf("Not Equal");
}
return 0;
}
GATE 2014 - Set 1
Consider the following program in C language:
#include <stdio.h>
main() {
int i;
int *pi = &i;
scanf("%d", pi);
printf("%d\n", i+5);
}
Which one of the following statements is TRUE?
(A) Compilation fails.
(B) Execution results in a run-time error.
(C) On execution, the value printed is 5 more than the address of variable i.
(D) On execution, the value printed is 5 more than the integer value entered.
Give output with call by value/
reference scheme
int i ;
program main () {
int j = 60;
i = 50;
call f (i, j);
print i, j;
}
procedure f (x, y) {
i = 100;
x = 10;
y=y+i;
}
Predict output ?
int main() {
char st1[10] = “India”;
char st2[10] = “India”;
if(st1 == st2)
printf(“Two string are equal”);
else
printf(“Both are unequal”);
}