In the following code is is guaranteed that "0\n" be printed?
#include <stdio.h>
int main(void)
{
int c = 0;
printf("%d\n",c);
printf("%d,%d\n",++c,++c);
}
More generally, if a program has undefined behavior does the entire program become undefined or only from the sequence point that begins the problematic code?
Please note: I am not asking about what the compiler does with the second printf. I am asking if the first printf is guaranteed to occur.
I know that undefined behavior is capable of blowing up your computer, crashing your program, or whatnot.
In C the use of any automatic variable before it has been initialized yields undefined behavior, as does integer division by zero, signed integer overflow, indexing an array outside of its defined bounds (see buffer overflow), or null pointer dereferencing.
Undefined Behavior in C and C++ So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.
Well even ignoring things like "Anything could happen! the program could travel back in time and prevent itself from running in the first place!", it's perfectly possible for a compiler to detect some forms of undefined behavior and not compile in that case in which case you'll wouldn't have gotten it to run in the first place. So yes, undefined behavior is contagious in principle if not necessarily so in practice most of the time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With