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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Where static variables are stored in c?

584


What is difference between stdio h and conio h?

885


When is a null pointer used?

641


What is a macro?

655


Define Array of pointers.

633






What is an expression?

654


What are identifiers c?

562


Is fortran still used today?

602


What does #pragma once mean?

684


What does & mean in scanf?

601


How many types of operators are there in c?

615


How can a string be converted to a number?

517


What is a char c?

592


Write a program to print factorial of given number using recursion?

603


How many types of functions are there in c?

581