Like Tree19Likes

Basic C programming -1

This is a discussion on Basic C programming -1 within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<math.h> void(main(main)) { printf("%-d++",main+=pow(++main/*()*/,++main)); } Output is :- 12++ Please explain it . I could not understand it ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    Basic C programming -1

    Code:
    #include<stdio.h>
    #include<math.h>
    void(main(main))
    {
    printf("%-d++",main+=pow(++main/*()*/,++main));
    
    }
    
    
    Output is :-   12++
    
    Please explain it . I could not understand it . 
    Thank you
    Last edited by vivekgupta; 06-11-2012 at 07:22 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Your program doesn't even seem to be possible to compile, the parens are unbalanced and the main declaration is just plain wrong.

    Code:
    pow(++main/*()*/,++main);
    Unbalanced parentheses aside, this is undefined behavior. Getting rid of the comment, all you're doing is passing the address of main to pow() twice, and incrementing it both times. It's up to the compiler to decide the order, however, you're corrupting any future function calls as the address of main has changed.

    Code:
    void(main(main))
    What the hell?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    sorry for wrong code .. me beginner

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    What ........ing idiot is teaching you this stupid ........?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Basic C programming
    It would appear that whoever you're learning from is teaching you stupid C syntax magic tricks, NOT programming.

    This path you're on will not in any way lead to employment (if that is indeed your ultimate goal). If this is the only knowledge you will be able to demonstrate, your chances of getting a job will be 0%.

    Read the link in my signature.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Thankx buddy

    one more programe (Don't be angry please )
    Code:
    #include<stdio.h>
    void main()
    {
    int x;
    clrscr;
    x=~!printf;
    printf("%x",x);
    }

    Output is :- ffffffff

    WHY THIS printf CONSIDERED AS TRUE ??

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    NOT <non-zero> = 0

    BITWISE NOT 0 = 0xFFFFFFFF ( or rather all bits turned on )
    Devoted my life to programming...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    The printf call is considered true because it returns 1.

    What you probably meant to ask is why the output of the printf was what it was, but for that... you need to learn what the ! and ~ operators do, as well as what the %x does.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    No i know that what these operators do .But I am asking that printf is considered as TRUE because it return the value of its address in that expression ?? It is ture that every function has it's own memory address???

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    He didn't say anywhere that he was given this as an assignment or such. He has rather found this puzzle somewhere in the net

    Quote Originally Posted by memcpy View Post
    Code:
    void(main(main))
    What the hell?
    The inner "main" is "int argc" (the outer one is hidden) and equals 1.

    Code:
    main+=pow(++main/*()*/,++main)
    Assuming the result is 12 it could be compiled like: 3 + pow(3, 2).
    I never put signature, but I decided to make an exception.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by vivekgupta View Post
    It is ture that every function has it's own memory address???
    Of course, but the question is, why would that bother you right now, since you have yet to learn the basics of the language?
    Devoted my life to programming...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by vivekgupta
    But I am asking that printf is considered as TRUE because it return the value of its address in that expression ?
    That does not make sense. In fact, if you are really asking whether the expression printf("%x",x) evaluates to true, then my answer in post #8 applies: the expression evaluates to 1, which is a true value.

    Quote Originally Posted by vivekgupta
    It is ture that every function has it's own memory address?
    Why do you ask?

    Quote Originally Posted by kmdv
    Assuming the result is 12 it could be compiled like: 3 + pow(3, 2).
    Or something else, due to undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Quote Originally Posted by GReaper View Post
    Of course, but the question is, why would that bother you right now, since you have yet to learn the basics of the language?


    Actually I have knowledge about memory . :P

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Thanks for the help

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Ohh yeah i got it

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C programming help
    By jonny0503 in forum C Programming
    Replies: 3
    Last Post: 01-13-2012, 12:00 PM
  2. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 04:01 PM
  3. Very basic programming help please
    By xrated in forum C++ Programming
    Replies: 8
    Last Post: 08-14-2006, 03:02 PM
  4. basic C-programming Q
    By slyonedoofy in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 08:54 PM
  5. Basic Programming
    By plexy1 in forum C Programming
    Replies: 2
    Last Post: 09-05-2001, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21