Thread: unusual code.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Exclamation unusual code.

    Hi all,

    This code runs in gcc 3.4.2 and prints "Hi there!".

    Code:
    #include <stdio.h>
    
    void print() {
    	 printf("Hi there!");
    }
    
    int main()
    {
     	int i;
     	printf; "Hello", i;
     	getchar;
     	print();
     	getchar();
     	return 0;
    }
    Can anybody plz explain this to me?

    Thanks n regards,

    Arun

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Explain what?

    > printf; "Hello", i;
    > getchar;
    These are all expressions - they don't evaluate to a function call (you need some () to make that happen), and the result isn't assigned anywhere.
    You can just delete the first two lines and nothing will change.

    I mean, if you had a function pointer, you might do this
    int (*fn)(void) = getchar;
    But since there is no left hand side of the expression, nothing happens.

    I get
    gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:10: warning: left-hand operand of comma expression has no effect
    foo.c:10: warning: statement with no effect
    foo.c:10: warning: statement with no effect
    foo.c:11: warning: statement with no effect
    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.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    That's what I wanted to know. What's there in this "()"?

    If I comment out the declaration of "i" at the 3rd line in "main()" I get the error " 'i' undeclared ". But there is no such error for "printf" or "getchar".
    How are "printf" and "getchar" exactly interpreted?

    Thanks,

    Arun

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well they're just symbols declared in stdio.h.

    Normally, they're functions - when you have () following them - regard () as the function call operator.

    But without (), the symbol names are just 'pointer to function', and can be assigned like any other variable.
    Or in this case, not assigned at all, and hence the "statement has no effect"
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Cool

    Hi!

    Thanks a lot for reminding me!
    I really missed the point that function names are nothing but pointers.

    Regards,

    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM