mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-15 11:14:24 +02:00
[c,Eng] (#5362)
* Update c.md fix goto https://github.com/adambard/learnxinyminutes-docs/issues/5293 * Update c.md Removed the variable bool this is for https://github.com/adambard/learnxinyminutes-docs/issues/5293
This commit is contained in:
17
c.md
17
c.md
@@ -387,22 +387,23 @@ int main (int argc, char** argv)
|
||||
/*
|
||||
Using "goto" in C
|
||||
*/
|
||||
typedef enum { false, true } bool;
|
||||
// for C don't have bool as data type before C99 :(
|
||||
bool disaster = false;
|
||||
int i, j;
|
||||
for(i=0; i<100; ++i)
|
||||
for(j=0; j<100; ++j)
|
||||
{
|
||||
if((i + j) >= 150)
|
||||
disaster = true;
|
||||
if(disaster)
|
||||
goto error; // exit both for loops
|
||||
if((i + j) >= 150) {
|
||||
goto error; // exit both for loops immediately
|
||||
}
|
||||
}
|
||||
printf("No error found. Completed normally.\n");
|
||||
goto end;
|
||||
|
||||
error: // this is a label that you can "jump" to with "goto error;"
|
||||
printf("Error occurred at i = %d & j = %d.\n", i, j);
|
||||
end:
|
||||
return 0
|
||||
/*
|
||||
https://ideone.com/GuPhd6
|
||||
https://ideone.com/z7nzKJ
|
||||
this will print out "Error occurred at i = 51 & j = 99."
|
||||
*/
|
||||
/*
|
||||
|
Reference in New Issue
Block a user