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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-30-2005, 07:59 AM
|
#1
|
Member
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68
Rep:
|
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
will be interpreted?
Is it compiler dependent?
|
|
|
11-30-2005, 08:08 AM
|
#2
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
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
will be interpreted?
Is it compiler dependent?
|
Im not quite sure of what you are asking with the "stmts" example.
as for
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;
|
|
|
11-30-2005, 08:11 AM
|
#3
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
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.
|
|
|
11-30-2005, 08:13 AM
|
#4
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
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
|
|
|
11-30-2005, 08:22 AM
|
#5
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
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.
|
|
|
11-30-2005, 09:52 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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?
|
|
|
11-30-2005, 10:07 AM
|
#7
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
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.
|
|
|
11-30-2005, 11:08 AM
|
#8
|
Member
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68
Original Poster
Rep:
|
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
|
|
|
12-01-2005, 02:49 AM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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 +.
|
|
|
12-01-2005, 06:33 AM
|
#10
|
Member
Registered: Sep 2004
Posts: 273
Rep:
|
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.
|
|
|
12-01-2005, 06:55 AM
|
#11
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
excellent hive.
we got there in the end.
|
|
|
12-01-2005, 07:04 AM
|
#12
|
Member
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68
Original Poster
Rep:
|
That was a great link
Thank you hive.
Do you have answer for the other question I posted?
|
|
|
12-01-2005, 07:20 AM
|
#13
|
Member
Registered: Sep 2004
Posts: 273
Rep:
|
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.
|
|
|
|
All times are GMT -5. The time now is 12:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|