LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   undefined behaviour of c program (https://www.linuxquestions.org/questions/programming-9/undefined-behaviour-of-c-program-4175440399/)

batman4 12-07-2012 03:26 AM

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;
i++;
printf("%d",i)

Code:

main()
{
static int a[20];
int i=0;
a[i]= i++;
printf("%d,%d,%d" a[0],a[1],i)

}


JohnGraham 12-07-2012 04:30 AM

"Undefined behaviour" basically means you cannot tell what will happen. Consider the following line of the code you posted:

Quote:

Originally Posted by batman4 (Post 4844368)
Code:

a[i]= i++;

The ++ operator will increment its operand (in this case "i") after its value has been taken - but will this be before or after the value of i has been used to figure out where in the array "a" the value should be placed? I.e. is the above code equivalent to:

Quote:

Originally Posted by batman4 (Post 4844368)
Code:

a[i]= i;
i++;


or:

Quote:

Originally Posted by batman4 (Post 4844368)
Code:

int saved_i = i; // Need to save in this example, as incrementing on line before will change value.
i++;
a[i]= saved_i;


Either could happen, and you cannot tell which. After you've got a little more experience in C, research "sequence points" if you're still interested.

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.

graemef 12-07-2012 04:31 AM

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.

JohnGraham 12-07-2012 05:24 AM

Quote:

Originally Posted by graemef (Post 4844403)
Next you initialise the first element in the array to the value of i (zero) and then increment i by one

This could be what happens, but is undefined - see e.g. the comp.lang.c FAQ.

chrism01 12-07-2012 06:09 AM

Quote:

Of course with truly "undefined behaviour", anything could happen
Indeed :)
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 ;)

theNbomr 12-07-2012 08:55 AM

Another recent and related LQ thread seems to have elicited a well composed reply.

--- rod.


All times are GMT -5. The time now is 03:38 AM.