LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help on basic C (https://www.linuxquestions.org/questions/programming-9/help-on-basic-c-387879/)

vivekr 11-30-2005 08:59 AM

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?

dmail 11-30-2005 09:08 AM

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;

graemef 11-30-2005 09:11 AM

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.

dmail 11-30-2005 09:13 AM

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

graemef 11-30-2005 09:22 AM

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.

bigearsbilly 11-30-2005 10:52 AM

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?

graemef 11-30-2005 11:07 AM

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.

vivekr 11-30-2005 12:08 PM

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

bigearsbilly 12-01-2005 03:49 AM

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 +.

Hivemind 12-01-2005 07:33 AM

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.

bigearsbilly 12-01-2005 07:55 AM

excellent hive.
we got there in the end.

vivekr 12-01-2005 08:04 AM

That was a great link
Thank you hive.
Do you have answer for the other question I posted?

Hivemind 12-01-2005 08:20 AM

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 02:42 AM.