Thread: Basic C programming -1

  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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What ........ing idiot is teaching you this stupid ........?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    2,738
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    733
    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).

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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

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, 01:00 PM
  2. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 05: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, 09:54 PM
  5. Basic Programming
    By plexy1 in forum C Programming
    Replies: 2
    Last Post: 09-05-2001, 02:03 PM