LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-02-2009, 11:03 PM   #1
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 219

Rep: Reputation: 27
C help


Hi,

This may be something really simple, but i havent been able to figure out the reason why the statement is valid (apart from the fact that the standard probably says so ). What use could it possibly be of?

Code:
#include <stdio.h>

int main()
{
        int n;
        n;      /*Why is this a valid statement?*/
        return 0;
}
thanks
 
Old 08-02-2009, 11:27 PM   #2
karamarisan
Member
 
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374

Rep: Reputation: 55
It's not necessarily simple - you have to think about it in more abstract terms. Don't look at code as a list of statements that each have to do something; look at each line as something to be evaluated, where the evaluation of functions (and sometimes other things) can change the state of the system. You may have heard the words 'substitution model' if you've taken basic CS classes; if not, don't worry about it. Consider this:

Code:
int ret = foo(0, bar(x), mything.baz);
1. Well, we're going to assign something to our new integer ret. What are we assigning? Check out the right side of the =.
2. Let's evaluate foo. To do that, we'll need to look at each of its arguments.
2-1. 0 is an int literal. No problem.
2-2. bar(x). Let's evaluate that. bar takes one argument.
2-2-1. x is a variable - somewhere else, it was defined as 62, so substitute 62 for x and continue.
2-2-1. bar(62). Dive into bar's function definition, and get a value. Let's say it comes back with 125.
2-2. Substitute 125 for bar(x). We're looking at foo(0, 125,...) right now.
2-3. mything.baz? Check out the baz field of the struct mything, and look, it's 15.
2. Great, now we've got foo(0, 125, 15). Plug all of those values into the appropriate places in foo's definition and evaluate that. Let's say that comes back as 70.
3. The right side is now a literal, so we can do the assignment. 70 is stored into ret.

A little long-winded, but do you see what I'm saying about evaluations? Whether calling foo() creates a window somewhere or not isn't important; all the system does is evaluate statements. If there were no 'int ret =', it'd be the same deal, and the return value would just be discarded. 'n;' evaluates to whatever's in n, and we move on to the next line. It doesn't do anything, but it fits the model we're working in. Call it a degenerate case.

I'm not entirely sure that will get across, so please let me know if I'm babbling.
 
Old 08-02-2009, 11:49 PM   #3
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 219

Original Poster
Rep: Reputation: 27
thanks

If I am getting this right, then the compiler evaluates n; to whatever (garbage) value it contains, now we have no variable on the left side so the compiler does nothing else on this line moves to the next line of code.
 
Old 08-03-2009, 12:07 AM   #4
karamarisan
Member
 
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374

Rep: Reputation: 55
Yep. If you get that there isn't really a difference from this perspective between 'evaluate n' and 'evaluate drawRectangle(0,0,50,50)', and if I am to be trusted ( ), then you've got it.
 
Old 08-03-2009, 12:40 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
To my way of thinking, "if 'n' is a simple variable, then I was not aware that any compiler would accept it. I therefore share austinium's sentiment: to me, "if such a statement is acceptable, then it's a bug in the compiler."

Very early compilers accepted many things that are regrettable today. There simply wasn't enough space to permit a "better" compiler to be written in those days.

Some programs nevertheless were written which took advantage of these peculiarities ... literally because they had to. Syntax checking existed in separate programs like lint because there was no room to put such tests into the compiler.

Last edited by sundialsvcs; 08-03-2009 at 12:42 AM.
 
Old 08-03-2009, 02:29 AM   #6
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 219

Original Poster
Rep: Reputation: 27
it compiles fine on cl(vc++ 6.0) and gcc 3.2.2
 
Old 08-03-2009, 05:03 PM   #7
mrtiller
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by austinium View Post
it compiles fine on cl(vc++ 6.0) and gcc 3.2.2
Also gcc (3.4.4 and possibly earlier) will warn you with
warning: statement with no effect
if you use the -Wall switch
 
  


Reply



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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration