Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: C && Basics

  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb 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 <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
    Code:
    0 50 1
    But it was
    Code:
    0 50 0
    I tried understanding by changing the code a bit. As follows.
    Code:
    #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
    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.

  • #2
    Regular Coder Apothem's Avatar
    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.

  • #3
    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

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Quote Originally Posted by arih7nt View Post
    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)

  • #5
    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.

  • #6
    Regular Coder Apothem's Avatar
    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.

  • #7
    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.

  • #8
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Quote Originally Posted by arih7nt View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •