You are on page 1of 6

Find the output Set - 1

Question - 1
int main()
{
int x[10]={0,1,2,3,4,5,6,7,8,9};
int *ptr1,*ptr2;
ptr1=&x[0];
ptr2=&x[5];

printf("%p\n",(ptr1+ptr2));

return 0;
}

Output
error: invalid operands to binary + (have 'int *' and 'int
*')

Explanation
Error in the statement printf("%p\n",(ptr1+ptr2));

Addition of two pointers is not allowed.

Question - 2
int main()
{
int color=2;
switch(color)
{
case 0: printf("Black");
case 1: printf("Blue");
case 2: printf("Green");
case 3: printf("Aqua");
default: printf("Other");
}
return 0;
}
Output
GreenAquaOther

Explanation
There are no break statements, so all the statements after case 2 will be
executed including defaultstatement.

Question - 3
int main()
{
char str[10]="Hello";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;
}

Output
5,10

Explanation
strlen gives length of the string that is 5; sizeof gives total number of
occupied memory for a variable that is 8; since str is a pointer
so sizeof(str) may be 2,4 or 8. It depends on the computer architecture.

Question - 4
int main()
{
char *str="Hello";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;
}

Output
5,8
Explanation
strlen gives length of the string that is 5; sizeof gives total number of
occupied memory for a variable that is 10; Since str is a variable with maximum
number of characters 10, so sizeof will be 10.

Question - 5
void test(struct number n)
{
n.x=100;
}
struct number{ int x; };

int main()
{
struct number num;
test(num);
printf("%d\n",num.x);
return 0;
}

Output
error: parameter 1 ('n') has incomplete type

Explanation
Structure number should be defined before test function definition.

Question - 6
int main()
{
if(0);
printf("Hello");
printf("Hi");
return 0;
}

Output
HelloHi

Explanation
There is a semicolon after the if statement, so this statement will be considered
as separate statement; and here printf("Hello"); will not be associated with
the if statement. Both printf statements will be executed.

Question - 7
int main()
{
int x,y;
int *ptr;
x=100;
ptr=&x;
y=*ptr;
printf("%d\n",y);
return 0;
}

Output
100

Explanation
Here, y is assigned the value stored at the pointee variable, which is pointer
through ptr.

Question - 8
int main()
{
int val=1;

do{
val++;
++val;
}while(val++>25);

printf("%d\n",val);
return 0;
}

Output
4

Explanation
Here, do while loop executes once and then it will check condition while will be
false meanwhile value will be increased 3 times (two times in do while body and
once while checking the condition); hence value will be 4.

Question - 9
int main()
{
char *str="A%%B";
printf("A%%B ");
printf("%s\n",str);
return 0;
}

Output
A%B A%%B

Explanation
printf("A%%B"); will print "A%B" because "%%" will always print "%"
while str contains "A%%B" will print "A%%B" - in this statement "%%" will not
replace with single "%"

Question - 10
int main()
{
printf("%d,%d,%d\n",sizeof(char*),
sizeof(int*),sizeof(float*));;
return 0;
}
Output
8,8,8

Explanation
No matter which kind of pointers are. Pointer variables take same bytes in the
memory. The value may be 2, 4 or 8 depending on the computer architecture.

In our case we are using 64 bits computer architecture, so output is 8, 8, 8.

You might also like