How to understand this line of c ?

T

tmp123

lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Some hints:

* Write the program with the correct tabulation
* Take into account that usually main parameters are argc and argv, but
they can be called with any name. And, if type is not declared K&R
assumes int.
* _ can be the name of a variable.
* Split "for" in its parts: a for statement is divided in
initialization, continue condition and update of state. The first one
and third ones can be empty.
* putchar returns the character written as unsigned int
* integer applied to string returns n-th element. (this is something I
do not know why).

Kind regards.
 
B

Broeisi

Ivan said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Don't. It's rubbish.

Well it ain't rubbish cause it is valid C!!
Try compiling it..

The output will be:

I Love You

But this code is obfuscated.... Only the real C gurus will understand it
I Guess.... not this novice guy...

Broeisi
 
T

tmp123

Even when "gcc" compiled with the flags -ansi and -pedantic, it
produces no errors nor warnings. Only with -std=c99 it produces 3
warnings.
 
F

Flash Gordon

Broeisi said:
Ivan said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
Don't. It's rubbish.

Well it ain't rubbish cause it is valid C!!

Actually, it's not. In C main either takes no arguments or two
arguments, not 1.
Try compiling it..

The output will be:

I Love You

Not necessarily. It was written assuming ASCII, and not all C
implementations use ASCII.
But this code is obfuscated.... Only the real C gurus will understand it
I Guess.... not this novice guy...

Depends. It's not that difficult. Just format it properly and learn a
couple of tricks and it is simple.
 
I

Ivan Budiselic

Broeisi said:
Ivan said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Don't. It's rubbish.

Well it ain't rubbish cause it is valid C!!
Try compiling it..

The output will be:

I Love You

Maybe on your machine. Certainly not on all.
But this code is obfuscated.... Only the real C gurus will understand it
I Guess.... not this novice guy...

There's nothing guruesque about it.
 
P

pemo

lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Here it is a little clearer [hopefully?]

#include <stdio.h>

// x will be 1 (the name of this app would be in argv[0]) if we
// don't invoke the app with some args.
//
main(x)
{
char c;

// zero then!
//
--x;

while(c = "J!Mpwf!Zpv\1"[x])
{
// No need for the -1 if we add 1 to the literal's characters...
//
// "I Love You\1"
//
c = c - 1;

x++;

putchar(c);
}
}
 
P

pemo

lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Ah, but run it with some params, and it's dissapointing. Perhaps the
initialiser in the for loop would have been better this way?

for(_^=_; putchar(_++["J!Mpwf!Zpv\1"]-1); );
 
R

Roberto Waltman

lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

The two following code fragments are equivalent:

/*-A-----------------------------------*/

#include <stdio.h>

int main(void)
{
char rubbish[] = "J!Mpwf!Zpv\1"; /* will 'print' trailing '\0' */
char *rp = rubbish;

while (*rp)
{
putchar ((*rp)-1);
rp++;
}
putchar('\n'); /* not in original code */
}

/*-B-----------------------------------*/

#include <stdio.h>

int main(void)
{
char rubbish[] = "I Love You\0"; /* '\0' is redundant here */
char *rp = rubbish;

while (*rp)
{
putchar(*rp);
rp++;
}
putchar('\n'); /* not in original code */
}

/*-------------------------------------*/
 
P

pemo

pemo said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Ah, but run it with some params, and it's dissapointing. Perhaps the
initialiser in the for loop would have been better this way?

for(_^=_; putchar(_++["J!Mpwf!Zpv\1"]-1); );

or even something like this?

for(_=!!_?_^_:_; putchar(_++["J!Mpwf!Zpv\1"]-1); );

Fun on c.l.c - I love it!
 
R

Randy Howard

Broeisi wrote
(in article said:
Ivan said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

Don't. It's rubbish.

Well it ain't rubbish cause it is valid C!!

Not really no.
Try compiling it..

The output will be:

I Love You

Funny, this EBCDIC box in the corner outputs:

"Slartibartfast, system halted."

My DSK9000 outputs:

"My, what a silly little boy you are today."

Immediately afterward, it caught on fire, and now you owe me a
new one.

But this code is obfuscated.... Only the real C gurus will understand it
I Guess.... not this novice guy...

I guess not you either.
 
C

Christopher Benson-Manica

tmp123 said:
Even when "gcc" compiled with the flags -ansi and -pedantic, it
produces no errors nor warnings. Only with -std=c99 it produces 3
warnings.

You'd better try it again with -Wall. There are plenty of things
wrong with the code regardless of which standard one applies to it.
 
C

Christopher Benson-Manica

Broeisi said:
Well it ain't rubbish cause it is valid C!!
Try compiling it..

test.c:1: warning: return type defaults to `int'
test.c: In function `main':
test.c:1: warning: implicit declaration of function `putchar'
test.c:1: warning: control reaches end of non-void function

Maybe YOU should have tried compiling it.
 
J

Jordan Abel

Broeisi wrote
(in article said:
Ivan said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


Don't. It's rubbish.

Well it ain't rubbish cause it is valid C!!

Not really no.
Try compiling it..

The output will be:

I Love You

Funny, this EBCDIC box in the corner outputs:

"Slartibartfast, system halted."

My DSK9000 outputs:

"My, what a silly little boy you are today."

Immediately afterward, it caught on fire, and now you owe me a
new one.

Other than the one-argument main(), there's no actual undefined
behavior, as far as I can tell. of course, the values of specific
characters are implementation-defined, so it's not strictly conforming -
that doesn't give the DS9K license to catch on fire, though.
 
R

Randy Howard

Jordan Abel wrote
(in article said:
Broeisi wrote
(in article said:
Ivan Budiselic wrote:

main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


Don't. It's rubbish.



Well it ain't rubbish cause it is valid C!!

Not really no.
Try compiling it..

The output will be:

I Love You

Funny, this EBCDIC box in the corner outputs:

"Slartibartfast, system halted."

My DSK9000 outputs:

"My, what a silly little boy you are today."

Immediately afterward, it caught on fire, and now you owe me a
new one.

Other than the one-argument main(), there's no actual undefined
behavior, as far as I can tell. of course, the values of specific
characters are implementation-defined, so it's not strictly conforming -
that doesn't give the DS9K license to catch on fire, though.

How many examples of UB must be present in order to obtain such
a license?

BTW, where is stdio.h?
 
F

fidlee

Roberto said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

can someone please explain as to what the initialization part of the
for loop does. How does the underscore '_' play a part in the for loop?
(in the initialization and the putchar function)?
 
T

tmp123

fidlee said:
Roberto said:
lnzju said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

can someone please explain as to what the initialization part of the
for loop does. How does the underscore '_' play a part in the for loop?
(in the initialization and the putchar function)?

Hi,

1) "_" can be the name of a variable. In this case, it is the name of
the first parameter of the function main (usually called argc). If the
program is called without arguments, it takes value 1. (to be
practical, I will rename it to "i").

2) This variable is used as iterator of the for statement. It is
decremented at init (it takes value 0) and increment at for continue
condition (++).

3) It is used as index to char string. Taken into account that:

a <==> *(a+b)

The expression i["1234"] <=> *(i+"1234") <=> "1234" : i-th character
of the string.

Hope this answer was useful to you.

Kind regards.
 
F

fidlee

tmp123 said:
fidlee said:
Roberto said:
main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

can someone please explain as to what the initialization part of the
for loop does. How does the underscore '_' play a part in the for loop?
(in the initialization and the putchar function)?

Hi,

1) "_" can be the name of a variable. In this case, it is the name of
the first parameter of the function main (usually called argc). If the
program is called without arguments, it takes value 1. (to be
practical, I will rename it to "i").

2) This variable is used as iterator of the for statement. It is
decremented at init (it takes value 0) and increment at for continue
condition (++).

3) It is used as index to char string. Taken into account that:

a <==> *(a+b)

The expression i["1234"] <=> *(i+"1234") <=> "1234" : i-th character
of the string.

Hope this answer was useful to you.

Kind regards.


Thanks for answering this question.That brings up my next question.
What is the significance of argv and argc in main() ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top