...

View Full Version : C && Basics



arih7nt
12-18-2011, 09:46 AM
Hi Everyone, probably not the best place to put an intro.But I've more or less just started programming, reading from one of my course book.
I have a doubt regarding a question with predicting the outcome of a given code.


#include <stdio.h>
int main ()
{
int x=35;
printf ("%d %d %d\n",x==35,x=50,x>40);
return 0;
}

Output I had predicted was
0 50 1

But it was
0 50 0
I tried understanding by changing the code a bit. As follows.

#include <stdio.h>
int main ()
{
int x=35;
printf ("%d %d\n",x,x>40);
printf ("%d %d %d %d %d\n",x,x==35,x=50,x>40,x);
printf ("%d\n",x);
return 0;
}
Output
35 0
50 0 50 0 50
50


I can't understand how in the original code x>40 is false, as on printing the value of x after the inequality I get x=50.

Any Help ?

PS. I hope I didn't break any Forum rule in my first thread. Sorry if I did. :D
Also I've used the Codepad.org Compiler.

Apothem
12-18-2011, 07:43 PM
It may be the case that branches (ifs, fors, whiles, etc) are taken first before variables. Left-Right rules and such sort of fall apart when it comes to C and C++. The output is really dependent on the compiler and how it translates the code to ASM. As such if you wanted everything to work the way you think it should work, write the ASM version instead.

arih7nt
12-20-2011, 10:34 AM
Sorry but I'm a bit new to this ASM. Any particular good reference you would have ?
Thanks

Apothem
12-20-2011, 06:32 PM
Sorry but I'm a bit new to this ASM. Any particular good reference you would have ?
Thanks

ASM means Assembly. There are different versions of Assembly out there, and I do not know of any specific good one. The one I was taught was SPARC. A friend of mine learned Intel x86 Assembly (I am not sure if this is the correct terminology for this branch of Assembly). Assuming you are using a Windows machine with an Intel processor, you should start Googling about Intel Assembly.

Though I would not suggest you to literally make an ASM program. That small snippet of code you have there expands from about 7 to maybe over 20. Almost anything written in C is expanded by a growing factor (including if optimization are not done).

As such I think instead of adding another layer of indirection to your learning, you should just separate your branches (conditions) from parameters (because they seem to be handled differently from the compiler).



Another problem in C/C++ is something like this:
int x = 3;
x = x + (x=10);
Will it equal 20 or 13? The answer is literally based on how the compiler encodes the C to Assembly. It is, again, the same problem as yours.

arih7nt
12-21-2011, 06:43 AM
Brilliant Mate. I do think I'm getting a little bit of it.
And the piece of code you wrote is somewhat similar to the example just before the one I asked. So maybe the author does want the reader to explore this part of understanding.

Well. Just incase. I'm using windows with i5 win7 64, if that be of more help.
Thanks a ton.

Apothem
12-21-2011, 07:40 AM
I really cannot give much support on Windows ASM because I've only written ASM (again, SPARC) in a Linux environment (SPARC architecture). I am sure you can find some guides on Intel Assembly, but again help on that is really out of my scope of knowledge.

It may be the case that your book assumes that you're using a specific C/C++ compiler, and such it expects the output to be something specific. Since you are not using that type of compiler, the assembly code cannot be determined.

arih7nt
12-21-2011, 05:34 PM
The author recommends Turbo C in Win OS. So if I am not incorrect this ASM has something to do with computer architecture ?
Does that mean if I am using a Windows OS Intel Processor I should be expecting the same outcome everytime even with the use of different compilers ? i.e. the outcome may only change with different forms of Comp. Architecture.
Or the Assembly part has nothing to do with this ?

Sorry if I may sound too absurd or noob on this.

Apothem
12-21-2011, 07:02 PM
The author recommends Turbo C in Win OS. So if I am not incorrect this ASM has something to do with computer architecture ?
Does that mean if I am using a Windows OS Intel Processor I should be expecting the same outcome everytime even with the use of different compilers ? i.e. the outcome may only change with different forms of Comp. Architecture.
Or the Assembly part has nothing to do with this ?

Sorry if I may sound too absurd or noob on this.

You should probably install Turbo C onto your computer then.

The type of ASM language is dependent on the CPU's architecture, I believe. I am only 85% sure about this. More about it and its history is fortunately on Wikipedia (http://en.wikipedia.org/wiki/Assembly_language). In general it would be logical to think that one Intel architecture will be the same as the next. Though I am also 85% certain of this.

The outcome is generally the same over different machines, yes. But that is very reliant on the compiler you are using. If it is the case that you have five computers that have the same CPU architecture and you literally wrote out the Assembly code for each (and that each ASM file is the same in the other), then yes the output will, with high probability, be the same. If you have five computers with the same C code but with different compiles, the outcome may be different.

A compiler, in a high level perspective, takes your C code to attempt to translate it into ASM for your OS and convert it into object code (which you can run). ASM is a very low-level language (i.e. it is a low level programming language). If you compare C to ASM, C becomes the high level language.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum