undefined behaviour of c program
I was going through a book in c and this code is confusing me with the concept of undefined behavioral
Code:
int i=3; Code:
main() |
"Undefined behaviour" basically means you cannot tell what will happen. Consider the following line of the code you posted:
Quote:
Quote:
Quote:
Of course with truly "undefined behaviour", anything could happen - IIRC there was an old version of GCC which turned any use an unrecognised "#pragma" directive into instructions to try and start one of a few popular games if it found them on your system. |
As I read that code.
You have an array or 20 elements but none of the elements of the array have been initialised. Then you have an integer which is initialised to 0 Next you initialise the first element in the array to the value of i (zero) and then increment i by one Finally you print out the first two elements of the array and i At the point of printing the first element of the array and i have been initialised, but the second element of the array hasn't Now I'm not entirely certain what you are asking but a[1] hasn't been initialised and so it will print out what happened to be in the memory when the variable was defined. |
Quote:
|
Quote:
IIRC in my Advanced C Book http://books.google.com.au/books/abo...4C&redir_esc=y he says that could even include launching a ballistic missile ... if you have the relevant hardware attachment ;) |
|
All times are GMT -5. The time now is 03:38 AM. |