in GATE retagged by
1,005 views
0 votes
0 votes

What is the output of the following code snippet?

#include<stdio.h>
int main ( )
{
int x = 2, y = 5;
if (x < y)
   return (x = x+y);
else
   printf (“ GO Mock 1  ”);
   printf(“ Welcome to GO Mock test ”);
}
  1. GO Mock 1
  2. Welcome to GO Mock Test
  3. No output
  4. GO Mock 1Welcome to GO Mock Test
in GATE retagged by
by
1.0k views

1 Answer

3 votes
3 votes
Best answer
There is no compilation error but there will no output because function is returning a value and if statement is true in this case.
selected by

17 Comments

int main () {}?
1
1
what is your doubt ? specify please .
0
0
should the main function be written as int main()? or just main() is enough?
0
0

what is int here ? it is return type

in GCC we write int main() it is a good practice

if you do not write int and just write main() it is fine in GCC, as by default main() takes int in GCC :)

1
1
“ Welcome to GO Mock test ”  never printed for this code. right?
0
0
yes, it will never printed.
2
2
Even for not else case too?

It is under scope of main().So, after else if it will print ?

Will it not give correct output?
0
0

What will print , if "else" part of the program is in execution?

0
0

@srestha

see in this question, if condition part is true but it return some value , Not print anything ..

there will no output because function is returning a value and if statement is true in this case.

0
0

but the time runnung the code, I am getting runtime error

Why?

https://ideone.com/fGjPn6

0
0
because of this line

return (x = x+y);
0
0
why for that line?

More than one return possible in C code. rt?
0
0
yes possible but return for if is not supported ..
0
0
There is no issue with the code. @Srestha you should not use online compiler for learning purpose. Probably they are using return value as means of detecting error.
2
2
can anyone please tell what will happen after return statement??? it will halt??
0
0
main functions return to OS, that means program execution completes !
2
2

@Bikram sir, I feel "Welcome to GO Mock Test" must be printed since in the else statement there are no curly braces which implies that only the first statement preceding the else is a part of else and rest aren't considered as a part of else.
So as per this logic the second line should be printed by default.

0
0
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

64.3k questions

77.9k answers

244k comments

80.0k users