What will be the result of the following program?
main()
{
char p[]="String";
int x=0;

if(p=="String")
{
printf("Pass 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
else
{
printf("Fail 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
}

a) Pass 1, Pass 2
b) Fail 1, Fail 2
c) Pass 1, Fail 2
d) Fail 1, Pass 2
e) syntax error during compilation

Answers were Sorted based on User's Feedback



What will be the result of the following program? main() ..

Answer / jaroosh

Fail 1 , Pass 2.

Some explanation,
1. Fail 1
first of all, to compare strings in C, you use this strcmp
function, so this WOULD give PASS 1 :
if(strcmp(p,"String") == 0)
but
if(p=="String")
will fail because this line means :
if address of p is the same as address of some temporary
storage for literals, where literal "String" is stored,
which is very rarely true, because storing literals is
compiler specific and is very hard to estimate at runtime.
2. Pass 2
sizeof(p) gives 7, because sizeof(char) is 1 byte, and we
have 7 chars in array storing "String", which are :
[0]S
[1]t
[2]r
[3]i
[4]n
[5]g
[6]\0 (EOS)
now, clearly sizeof(p) - 2 is [5] which is "g"
thats why
if(p[sizeof(p)-2]=='g')
is true.

Is This Answer Correct ?    10 Yes 0 No

What will be the result of the following program? main() ..

Answer / guest

d) Fail 1, Pass 2

Is This Answer Correct ?    6 Yes 0 No

What will be the result of the following program? main() ..

Answer / reepal

e) syntex error during compilation

Is This Answer Correct ?    3 Yes 1 No

What will be the result of the following program? main() ..

Answer / rakhi

b) Fail 1 Fail 2

Is This Answer Correct ?    2 Yes 2 No

What will be the result of the following program? main() ..

Answer / guest

a) Pass 1, Pass 2

Is This Answer Correct ?    1 Yes 1 No

What will be the result of the following program? main() ..

Answer / vikram

b)fail1,fail2
bcoz whenever we compare strings,we use strcmp()
function,hence the condition in if() will not be true,
the control will go into else part and will print fail1,then
size of array p is 6 and sizeof(p)-2 results to 4 and hence
p[4]=='n'which again makes the condition in the if()
false,hence fail2 in else part will be printed.
thnx

Is This Answer Correct ?    1 Yes 1 No

What will be the result of the following program? main() ..

Answer / guest

c)Pass 1, Fail 2

Is This Answer Correct ?    1 Yes 2 No

What will be the result of the following program? main() ..

Answer / vinay deore

Fail 1,Fail 2

Is This Answer Correct ?    0 Yes 2 No

What will be the result of the following program? main() ..

Answer / guest

Fail 1,Fail 2

Is This Answer Correct ?    0 Yes 2 No

What will be the result of the following program? main() ..

Answer / shruti

syntax error during compilation..


we cannot compare strings using if(p == "string")

so will not work..

NOTE:
while comparing string otherwise also we use 'string'
(string in single quotes ' ' not " ")..

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

What is derived datatype in c?

0 Answers  


what is the c source code for the below output? 5555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 5555555555

0 Answers   Wipro,


dibakar & vekatesh..uttejana here..abt ur reply for in place reversal of linked list..wats p stands for there?

1 Answers  


What is const keyword in c?

0 Answers  


Q.11 Generate the following pattern using code in any language(c/c++/java) for n no. of rows 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

2 Answers  






find second largest element in array w/o using sorting techniques? use onle one for loop.

15 Answers   BitWise, Zycus Infotech,


write a program to display the numbers in the following format 4 4 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 4

9 Answers   IBM, NIIT, Winit,


How to declare pointer variables?

0 Answers  


Give me basis knowledge of c , c++...

5 Answers  


What is action and transformation in spark?

0 Answers  


WAP – represent a char in binary format

4 Answers   Motorola, Wipro,


find the output of the following program main() { int x=5, *p; p=&x; printf("%d",++*p); }

10 Answers   Amdocs, TCS,


Categories