ProgrammingThis 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.
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.
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.
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;
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.
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....
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.
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.
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.
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 +.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.