473,473 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with "goto" in c

13 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i=7;
  5.     goto x;
  6.     if(1){
  7.         static int i=5;
  8.         x:   printf("%d",i);
  9.     }
  10.     return 0;
  11. }
  12.  
output == 5

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i=7;
  5.     goto x;
  6.     if(1){
  7.         int i=5;
  8.         x:   printf("%d",i);
  9.     }
  10.     return 0;
  11. }
  12.  
output == 134513723 (garbage value i guess)


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i=0;
  5.  
  6.     if(1){
  7.         int i=5;
  8.         x:   printf("%d",i);
  9.     }
  10.  
  11.     goto x;
  12.     return 0;
  13. }
  14.  
output== infinite loop of 5

Why garbage value in case 2 while not in case 1 and case 3
and also why the value inside 'if' is getting printed since control of execution never reaches there ??..help !!
( compiler== gcc 4.4.3 )
Jun 1 '10 #1
8 3200
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Why it should not reach if condition? It has to reach there.

Case 1 and 3 looks correct, however i get answer as zero for 2nd case.

Regards
Dheeraj Joshi
Jun 1 '10 #2
noob15
13 New Member
@dheerajjoshim
I'm not sure how goto statement works during the compiling phase...but i think it should directly jump to line 8 thus skipping the part "int i=5". Also any idea why case 2 giving 0 as output in your compiler ?
Jun 1 '10 #3
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
You defined i as static.

Regards
Dheeraj Joshi
Jun 1 '10 #4
Banfa
9,065 Recognized Expert Moderator Expert
  1. Don't use goto
  2. DON'T USE goto
  3. DON'T USE goto

Now assuming that you aren't actually intending to use goto but are just enquiring about the nature of a programming element.

In case 1, i is a static variable. It exists for the lifetime of the program, although it is only in scope inside the if statement block. Because it has program lifetime it is created and initialised before main is entered so when you jump to the print statement i already has the correct value.

In listing 2 i is not static, it is automatic and has lifetime of the block that contains it. The goto skips the statement that initialises it and you get a garbage value. Since it is accessing a possibly non-existent automatic variable that has not been initialised this is undefined behaviour and so should be avoided.

In listing 3 the if statement is executed before the goto (and since 1 is true the block is executed). This initialises the memory used by i. However Strictly i only has lifetime of its containing block so when the goto statement alters the execution path back to the print statement i does not strictly exist although the printf access the same bit of memory that has already been initialised so it appears to print the correct value but is in fact undefined behaviour. Additionally since the goto jumps back up the code an infinite loop happens because nothing ever moves the follow of execution past the goto on line 11 to the return on line 12.


And to answer your final question the flow of execution does reach the printf statements, that is precisely what the goto statements do, alter the flow of execution in a non-structured way to the label indicated.
Jun 1 '10 #5
noob15
13 New Member
@Banfa
Just inquiring about the nature of a programming element...thnx for the reply,cleared many things....i've lil doubt in case 2. Since the goto skips the statement that initializes i then why don't it read the value of i already present i.e. i=7 ?
Jun 1 '10 #6
Banfa
9,065 Recognized Expert Moderator Expert
That is because those 2 variables may have the same name (i) but they are not the same variable and exist in different locations.

if you declare a variable with the same name as a variable that is in scope but declared in a different scope then you just hide that variable.
Jun 1 '10 #7
noob15
13 New Member
Thanks Banfa and dheerajjoshim !! :)
Jun 1 '10 #8
donbock
2,426 Recognized Expert Top Contributor
a. Don't mask variables.
b. DON'T MASK variables.
c. DON'T MASK variables.
A reader is more likely to notice a goto instruction than redefinition of a variable. Confusion is guaranteed, so I consider masking a variable to be worse than using a goto.
Jun 1 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.