Unusual code. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Unusual code.

arunjarunj Posts: 2Member
Hi all,

I'm using gcc 3.4.2. The following code compiles and prints "Hi there!".

[code]
#include

void print() {
printf("Hi there!");
}

int main()
{
int i;
printf; "Hello", i;
getchar;
print();
getchar();
return 0;
}
[/code]

Plz explain.
Thanks and regards,

Arun

Comments

  • Griz803Griz803 Posts: 100Member
    Well, in the lines I've commented below, the code seems to have statements that will evaluate and print something first. But, when you look closer, it is garbage code that evaluates to a long hand version of "Do Nothing.". Follow the comments.
    : [code]
    : #include
    :
    : void print() {
    : printf("Hi there!");
    : }
    :
    : int main()
    : {
    : int i;/* First statement, actually allocates an integer. */
    : printf; "Hello", i; /* This line is actually two separate
    * statements. The printf is "called"
    * as a pointer argument function and
    * returns a pointer to the function.
    * The next statement is a comma operation
    * that is purposely vague and meaningless
    * to disquise the fact that the printf is
    * called with null arguments. The lack of
    * parantheses and presence of an extra
    * semi-colon should cue you here. The 2nd
    * statement evaluates to the origianl
    * value of 'i', but is not used. Another
    * do-nothing. */
    : getchar;/* Another function pointer that is not used or
    * assigned to a variable. */
    : print();/* Calls a real user defined function that calls
    * printf with the argument "Hi, there!" */
    : getchar();/* Calls getchar. */
    : return 0;
    : }
    : [/code]

    The following is what the help sheet has to say about the function of commas other than in an argument list:
    The left operand E1 is evaluated as a void expression, then E2 is evaluated to give the result and type of the comma expression. By recursion, the expression

    E1, E2, ..., En

    results in the left-to-right evaluation of each Ei, with the value and type of En giving the result of the whole expression.

    I hope that this helps demonstrate that in C you can have a lot of obfuscating code and still produce a program that compiles and runs. The statements here are all acceptable to the compiler and do no harm to the running program execution, but they don't contribute anything either and would slow the execution some also. Good luck.
  • arunjarunj Posts: 2Member
    Hi Griz!

    Thanks a lot for a beautiful explanation.

    Regards,

    Arun
  • tsagldtsagld Posts: 621Member
    : Hi Griz!
    :
    : Thanks a lot for a beautiful explanation.
    :
    : Regards,
    :
    : Arun
    :
    For fun, take a look here:
    http://www.ioccc.org/


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl


Sign In or Register to comment.