C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Errors (Q.No. 8)
8.
Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
void fun();
int i = 1;
while(i <= 5)
{
printf("%d\n", i);
if(i>2)
goto here;
}
return 0;
}
void fun()
{
here:
printf("It works");
}
Answer: Option
Explanation:
A label is used as the target of a goto statement, and that label must be within the same function as the goto statement.
Syntax: goto <identifier> ;
Control is unconditionally transferred to the location of a local label specified by <identifier>.
Example:
#include <stdio.h>
int main()
{
int i=1;
while(i>0)
{
printf("%d", i++);
if(i==5)
goto mylabel;
}
mylabel:
return 0;
}
Output: 1,2,3,4
Discussion:
5 comments Page 1 of 1.
Khush said:
1 decade ago
#include <stdio.h>
int main()
{
int i=1;
while(i>0)
{
printf("%d", i++);
if(i==5)
goto mylabel;
}
mylabel:
return 0;
}
In this example is there will any change in output will done if i==5. As there is no printf statement with it.
int main()
{
int i=1;
while(i>0)
{
printf("%d", i++);
if(i==5)
goto mylabel;
}
mylabel:
return 0;
}
In this example is there will any change in output will done if i==5. As there is no printf statement with it.
Poorani said:
1 decade ago
Actually why it is an error?
Since label (here) is in another function it is not working ah? I didn't get it?
Since label (here) is in another function it is not working ah? I didn't get it?
Sv (savitru) said:
1 decade ago
Can we declare a new function inside the main function and define it outside main? Is it possible? Someone explain me details.
Yogesh yadav said:
1 decade ago
In this while there is no way to increment the value of I so that I can reach to more than 2 and then reach the goto statement. So before reaching the goto statement, the program is going to end.
Shiwam said:
1 decade ago
Is it does not give prototype error? Why?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers