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
shorting algorithmS
Why does this code crash?
What is data types?
How do you search data in a data file using random access method?
Is null always defined as 0(zero)?
Write a program in "C" to calculate the root of a quadratic equation ax^2+bx+c=0, where the value of a,b & c are known.
What is the best way to store flag values in a program?
What are bitwise shift operators in c programming?
What does c mean before a date?
If null and 0 are equivalent as null pointer constants, which should I use?
What is wrong with this declaration?
code for quick sort?
What is #include stdio h and #include conio h?
What does the c in ctime mean?
How many levels of indirection in pointers can you have in a single declaration?