Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com C && Basics

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-18-2011, 10:46 AM PM User | #1 arih7nt New to the CF scene   Join Date: Dec 2011 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts C && Basics 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. Code: ```#include int main () { int x=35; printf ("%d %d %d\n",x==35,x=50,x>40); return 0; }``` Output I had predicted was Code: `0 50 1` But it was Code: `0 50 0` I tried understanding by changing the code a bit. As follows. Code: ```#include 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 Code: ```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. Also I've used the Codepad.org Compiler.
 12-18-2011, 08:43 PM PM User | #2 Apothem Regular Coder     Join Date: Mar 2008 Posts: 380 Thanks: 36 Thanked 25 Times in 25 Posts 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. Last edited by Apothem; 12-18-2011 at 08:52 PM..
 12-20-2011, 11:34 AM PM User | #3 arih7nt New to the CF scene   Join Date: Dec 2011 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts Sorry but I'm a bit new to this ASM. Any particular good reference you would have ? Thanks
12-20-2011, 07:32 PM   PM User | #4
Apothem
Regular Coder

Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Quote:
 Originally Posted by arih7nt 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.

Last edited by Apothem; 12-20-2011 at 09:14 PM..

 Users who have thanked Apothem for this post: arih7nt (12-21-2011)
 12-21-2011, 07:43 AM PM User | #5 arih7nt New to the CF scene   Join Date: Dec 2011 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts 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.
 12-21-2011, 08:40 AM PM User | #6 Apothem Regular Coder     Join Date: Mar 2008 Posts: 380 Thanks: 36 Thanked 25 Times in 25 Posts 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.
 12-21-2011, 06:34 PM PM User | #7 arih7nt New to the CF scene   Join Date: Dec 2011 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts 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.
12-21-2011, 08:02 PM   PM User | #8
Apothem
Regular Coder

Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Quote:
 Originally Posted by arih7nt 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. 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:57 PM.