LinuxQuestions.org
Review your favorite Linux distribution.
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 11-30-2005, 08:59 AM   #1
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Help on basic C


Hello all,

Suddenly I have a doubt in basics.
Is C a block structured language?
In K&R C is said to be a non-block structured because a function cant be placed inside another function.
However in another book
Code:
stmts;
{
      stmts;
      {
           stmts;
      }
}
the above code is given to exemplify that C++(and hence C) is block-structured.

Also I need to know how
Code:
 j=i++ + ++i;
will be interpreted?
Is it compiler dependent?
 
Old 11-30-2005, 09:08 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Re: Help on basic C

Quote:
Originally posted by vivekr
Hello all,

Suddenly I have a doubt in basics.
Is C a block structured language?
In K&R C is said to be a non-block structured because a function cant be placed inside another function.
However in another book
Code:
stmts;
{
      stmts;
      {
           stmts;
      }
}
the above code is given to exemplify that C++(and hence C) is block-structured.

Also I need to know how
Code:
 j=i++ + ++i;
will be interpreted?
Is it compiler dependent?
Im not quite sure of what you are asking with the "stmts" example.
as for
Code:
 j=i++ + ++i;
I remember a interview question that was like this, but i cant remember if this is the same question(there one was undefined).
if this valid then the answer would be:
j=i+i+2;
 
Old 11-30-2005, 09:11 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The code you give is compiler dependent because it uses the increment operator on the same variable more than once on the same line. I have certainly found compilers in the past that have generated different results, (please don't ask me which ones - this was more than five years ago and I have trouble remembering what I did yesterday). You could possibly get different results from the same compiler depending upon the optimisation switches that you used.

The problem basically revolves around will the compiler perform the ++i before it looks at the i++.

Oh yes, block language. ANSI C is a block language, each block is independent of the code around it, thus function can call functions, and can gracefully descend into recursive hell.

graeme.
 
Old 11-30-2005, 09:13 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by graemef
The code you give is compiler dependent because it uses the increment operator on the same variable more than once on the same line....
Guess thats why i didnt get the job lmao
 
Old 11-30-2005, 09:22 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
j=i++ + ++i;

Just to clarify my answer to the above:

if i = 3 then j may take the value 7 or 8, depending on the compilier. The code is valid it's just that the result is undefined by the language description.

The way it gets these results is as follows:

Compilier is aggressive with the pre-increment operator

Code:
++i     //so i now equals 4
j=i + i // so j = 8
i++     // so i now equals 5
Compilier is strict left to right
Code:
old_i = 3     // temp variable created
++i           //so i now equals 4
j = old_i + i // so j = 7
i++           // so i now equals 5
Note how the second approach will require a temp variable to hold the original value of i.

graeme.
 
Old 11-30-2005, 10:52 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,474

Rep: Reputation: 222Reputation: 222Reputation: 222
Quote:
Also I need to know how

j=i++ + ++i;



will be interpreted?
Is it compiler dependent?
why?
it's a stupid bit of code.

It's like saying I'd like to know what happens if I stick a fork in my eye

why not compile it and run it?
 
Old 11-30-2005, 11:07 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
j = i++ + ++i;

As I said it is compiler dependent and so running it on different compilers may - but not necessarily - give different results. Sure the code can and should be rewritten to remove any ambiguity but I feel that the original question was valid.

As an aside I've seen that code as an examination question in a final exam and as a lecturer I had to explain why it was not a good question to the powers that be.

graeme.
 
Old 11-30-2005, 12:08 PM   #8
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
Thanks to graemef for your support.
Thanks to dmail for his responses.

Quote:
Originally posted by graemef

Oh yes, block language. ANSI C is a block language, each block is independent of the code around it, thus function can call functions, and can gracefully descend into recursive hell.
Actually K&R refers to definition of functions within functions and not calling.

And to bigs... I have a great respect for you and I am not a fool to post here without even trying. I compiled it and got a result. However since I am going tol appear in placements where questions of these sort are expected I thought of conforming the info I had.

Anyway thank you graemef for your support
 
Old 12-01-2005, 03:49 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,474

Rep: Reputation: 222Reputation: 222Reputation: 222
true true , apologies

well if it was an interview question there's no problem getting it wrong as long as you show that you are thinking it through. They aren't trying to catch you out, they are looking for honesty. If you think it's confusing then say so but still have a stab.

I'm not entirely convinced it's compiler dependent.
I would say, that parsing from left to right it's
(i++) + (++i) as the unary ++ binds closer than the binary +.
 
Old 12-01-2005, 07:33 AM   #10
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
j=i++ + ++i;
The result of that piece of code is undefined, because it attemps to modify the variable i more than once between two
sequence points. For more details on this matter, read sections 3.1-3-4 (expressions) of the usenet group comp.lang.c C faq.
It can be found here.
 
Old 12-01-2005, 07:55 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,474

Rep: Reputation: 222Reputation: 222Reputation: 222
excellent hive.
we got there in the end.
 
Old 12-01-2005, 08:04 AM   #12
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
That was a great link
Thank you hive.
Do you have answer for the other question I posted?
 
Old 12-01-2005, 08:20 AM   #13
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, I haven't really thought much about if C is a pure block structured language or not, but to quote Jack Klein:
Quote:
The only reason that I can think of that C would be considered less of a block
structured language than Pascal, for example, is that C does not provide
nested functions. That is you can't define a function inside another function.
[snip]
All function definitions in C are at the same level, and the only mechanism to
control linkage is the static keyword which limits linkage to a single source
file.
 
  


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
Very Basic Help Lyko Linux - Newbie 72 03-17-2005 09:53 PM
really basic os? gravy Linux - Newbie 7 02-26-2005 10:20 PM
basic Ryan450 Linux - General 5 08-21-2004 12:59 AM
Really Basic ?'s Susan Linux - Newbie 3 10-26-2001 02:51 PM
I'm a BASIC chap, looking for some info on BASIC programming CragStar Programming 2 01-21-2001 10:19 AM

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

All times are GMT -5. The time now is 04:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration