What will be the output of the program?
#include<stdio.h>
int main()
{
char ch;
if(ch = printf(""))
printf("It matters\n");
else
printf("It doesn't matters\n");
return 0;
}
printf() returns the number of charecters printed on the console.
Step 1: if(ch = printf("")) here printf() does not print anything, so it returns
'0'(zero).
Step 2: if(ch = 0) here variable ch has the value '0'(zero).
Step 3: if(0) Hence the if condition is not satisfied. So it prints
the else statements.
Hence the output is "It doesn't matters".
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("This is c program.");
case 1:
printf("Case1");
break;
case 2:
printf("Case2");
break;
}
return 0;
}
switch(i) becomes switch(1), then the case 1: block is get executed. Hence it
prints "Case1".
printf("This is c program."); is ignored by the compiler.
Hence there is no error and prints "Case1".
What will be the output of the program?
#include<stdio.h>
#include<math.h>
int main()
{
float n=1.54;
printf("%f, %f\n", ceil(n), floor(n));
return 0;
}
ceil(x) round up the given value. It finds the smallest integer not < x.
floor(x) round down the given value. It finds the smallest integer not > x.
printf("%f, %f\n", ceil(n), floor(n)); In this line ceil(1.54) round up the
1.54 to 2 and floor(1.54) round down the 1.54 to 1.
In the printf("%f, %f\n", ceil(n), floor(n)); statement, the format specifier
"%f %f" tells output to be float value. Hence it prints 2.000000 and 1.000000.
What will be the output of the program?
#include<stdio.h>
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
Step 1: int i; The variable i is declared as an integer type.
Step 1: int fun(); This prototype tells the compiler that the function fun() does
not accept any arguments and it returns an integer value.
Step 1: while(i) The value of i is not initialized so this while condition is failed. So,
it does not execute the while block.
Step 1: printf("Hello\n"); It prints "Hello".
Hence the output of the program is "Hello".
Point out the error in the program
#include<stdio.h>
int main()
{
int a=10;
void f();
a = f();
printf("%d\n", a);
return 0;
}
void f()
{
printf("Hi");
}
Error: Not allowed assignment
The function void f() is not visible to the compiler while going through main() function.
So we have to declare this prototype void f(); before to main() function. This kind of
error will not occur in modern compilers.
What will be the output of the program?
#include<stdio.h>
#define PRINT(int)
printf("int=%d, ", int);
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
The macro PRINT(int) print("%d,", int); prints the given variable value in an
integer format.
Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type
and initialized to 2, 3, 4 respectively.
Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.
Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.
Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.
Hence the output of the program is int=2, int=3, int=4.
What will be the output of the program?
#include<stdio.h>
#define JOIN(s1, s2)
printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *str1="India";
char *str2="BIX";
JOIN(str1, str2);
return 0;
}
str1=India str2=BIX
What will be the output of the program ?
#include<stdio.h>
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size
*/
z=y;
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
x=31, y=504, z=504
Will the program compile?
#include<stdio.h>
int main()
{
char str[5] = "IndiaBIX";
return 0;
}
C doesn't do array bounds checking at compile time, hence this compiles.
But, the modern compilers like Turbo C++ detects this as 'Error: Too many
initializers'.
GCC would give you a warning.
What will be the output of the program ?
#include<stdio.h>
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr=p;
ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*++ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
++*ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
return 0;
}
1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
Which of the following statements are correct about the program below?
#include<stdio.h>
int main()
{
char str[20], *s;
printf("Enter a string\n");
scanf("%s", str);
s=str;
while(*s != '\0')
{
if(*s >= 97 && *s <= 122)
*s = *s-32;
s++;
}
printf("%s",str);
return 0;
}
The code converts lower case character to upper case
This program converts the given string to upper case string.
Output:
Enter a string: indiabix
INDIABIX
Point out the error in the program?
#include<stdio.h>
#include<string.h>
void modify(struct emp*);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
Error: in prototype declaration unknown struct emp
The struct emp is mentioned in the prototype of the function modify() before declaring
the structure.To solve this problem declare struct emp before the modify() prototype.
Which of the following statements correctly assigns 12 to month using pointer
variable pdt?
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
return 0;
}
pdt.month = 12
&pdt.month = 12
d.month = 12
pdt->month = 12 ans
Point out the error in the following code?
typedef struct
{
int data;
NODEPTR link;
}*NODEPTR;
Error: in *NODEPTR
Error: typedef cannot be used until it is defined ans
No error
None of above
What will be the output of the program?
#include<stdio.h>
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}
Step 1: const char *s = ""; The constant variable s is declared as an pointer to
an array of characters type and initialized with an empty string.
Step 2: char str[] = "Hello"; The variable str is declared as an array of
charactrers type and initialized with a string "Hello".
Step 3: s = str; The value of the variable str is assigned to the variable s.
Therefore str contains the text "Hello".
Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed
untill the value of the variable s is available and it prints the each character of the
variable s.
Hence the output of the program is "Hello".