LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-07-2012, 03:26 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
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)

}
 
Old 12-07-2012, 04:30 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

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

Quote:
Originally Posted by batman4 View Post
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 View Post
Code:
a[i]= i;
i++;
or:

Quote:
Originally Posted by batman4 View Post
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.
 
1 members found this post helpful.
Old 12-07-2012, 04:31 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
1 members found this post helpful.
Old 12-07-2012, 05:24 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by graemef View Post
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.
 
Old 12-07-2012, 06:09 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
Old 12-07-2012, 08:55 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Another recent and related LQ thread seems to have elicited a well composed reply.

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C program - Undefined reference error Astrodude Programming 3 07-23-2010 01:20 PM
[SOLVED] fork behaviour......what is the output of this program vivignesh Linux - Newbie 9 07-06-2010 05:11 PM
undefined references when compiling PLIB program Alkibiades Programming 3 03-13-2010 02:18 PM
Undefined reference in C++ program Komakino Programming 3 12-03-2006 04:42 PM
shutdown(2) after fork(2) behaviour changed or undefined? MScheffer Linux - Networking 3 07-15-2005 09:11 AM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration