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; Also I need to know how Code:
j=i++ + ++i; Is it compiler dependent? |
Re: Help on basic C
Quote:
as for Code:
j=i++ + ++i; 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. graeme. |
Quote:
|
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 Code:
old_i = 3 // temp variable created graeme. |
Quote:
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? |
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. |
Thanks to graemef for your support.
Thanks to dmail for his responses. Quote:
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 |
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 +. |
Code:
j=i++ + ++i; 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. |
excellent hive.
we got there in the end. |
That was a great link
Thank you hive. Do you have answer for the other question I posted? |
Well, I haven't really thought much about if C is a pure block structured language or not, but to quote Jack Klein:
Quote:
|
All times are GMT -5. The time now is 01:16 AM. |