473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

explain this c code...

This one is a puzzle a fren has challenged me to solve.
I tried it myself but could not get the complete answer.
so here I am to seek ur help.


Can anyone tell me the o/p of this c code without compiling...

main(_){for(--_;putchar(_++["SEJBT!SPDLT\1"]-1););}

on compiling it prints "RDIAS ROCKS".
Aug 3 '07 #1
13 1906
r035198x
13,262 8TB
This one is a puzzle a fren has challenged me to solve.
I tried it myself but could not get the complete answer.
so here I am to seek ur help.


Can anyone tell me the o/p of this c code without compiling...

main(_){for(--_;putchar(_++["SEJBT!SPDLT\1"]-1););}

on compiling it prints "RDIAS ROCKS".
I'm not a c compiler but that code won't compile.
Is the puzzle supposed to be how to make it compile?
Aug 3 '07 #2
JosAH
11,448 Expert 8TB
I'm not a c compiler but that code won't compile.
Is the puzzle supposed to be how to make it compile?
Except for just the 'main(_)' thingie it compiles and it obfuscates matters by naming
the argc parameter '_' and by using a[i] as i[a] which is perfectly legal in C.
It prints all the characters from that string minus1, so the last character stops
the for loop; that program Sir, I consider baby obfuscation 101 :-P

kind regards,

Jos (<--- the grand DeObfuscator strikes again ;-)
Aug 3 '07 #3
r035198x
13,262 8TB
Except for just the 'main(_)' thingie it compiles and it obfuscates matters by naming
the argc parameter '_' and by using a[i] as i[a] which is perfectly legal in C.
It prints all the characters from that string minus1, so the last character stops
the for loop; that program Sir, I consider baby obfuscation 101 :-P

kind regards,

Jos (<--- the grand DeObfuscator strikes again ;-)
Argh, I was using a c++ compile command on it.
Aug 3 '07 #4
Darryl
86
This one is a puzzle a fren has challenged me to solve.
I tried it myself but could not get the complete answer.
so here I am to seek ur help.


Can anyone tell me the o/p of this c code without compiling...

main(_){for(--_;putchar(_++["SEJBT!SPDLT\1"]-1););}

on compiling it prints "RDIAS ROCKS".
Solve what? You ask can anyone tell the output of the Code and then turn around and say the output is "RDIAS ROCKS"

here's a rewrite to make it more understandable
Expand|Select|Wrap|Line Numbers
  1. int main( int argc, const char* argv[] )
  2. {
  3.      char str[] = "SEJBT!SPDLT\1";
  4.  
  5.      for (--argc; str[argc]-1 != 0; argc++)
  6.      {
  7.            putchar(str[i]-1);
  8.      }
  9. }
of course the program only works as expected if no command line parameters is used, otherswise for every parameter you'd lose 1 char at the front and with enough parameters you'd easily go past the end bound of the string.

Also as the last poster stated, the original code probably won't compile on most compilers, it is dependant on the compiler automatically assigning the variable "_" to an int which most compliant compilers now a days won't do but will instead produce an error.
Aug 3 '07 #5
JosAH
11,448 Expert 8TB
Argh, I was using a c++ compile command on it.
The body of that ugly little thingy is still valid C++ ...

kind regards,

Jos (programming language lawyer par excellance ;-)
Aug 3 '07 #6
Darryl
86
The body of that ugly little thingy is still valid C++ ...

kind regards,

Jos (programming language lawyer par excellance ;-)
Not true, C++ currently does not allow automatic typing of variables. Maybe once the new auto keyword is added, but even then I am not sure if it can be used with parameters, only with assignment I believe.
Aug 3 '07 #7
JosAH
11,448 Expert 8TB
Not true, C++ currently does not allow automatic typing of variables. Maybe once the new auto keyword is added, but even then I am not sure if it can be used with parameters, only with assignment I believe.
Of course I was only talking about the body of that little rascal (see above). The
main() definition is all wrong of course and there are some include files missing
etc. etc. Good ol' K&R1 C would've loved the entire thing though ;-)

kind regards,

Jos
Aug 3 '07 #8
r035198x
13,262 8TB
The body of that ugly little thingy is still valid C++ ...

kind regards,

Jos (programming language lawyer par excellance ;-)
Won't that require auto typing?
P.S I really need to give my c++ more time these days
Aug 3 '07 #9
JosAH
11,448 Expert 8TB
Won't that require auto typing?
P.S I really need to give my c++ more time these days
No, I was assuming an:

Expand|Select|Wrap|Line Numbers
  1. int _= 1;
  2.  
thingy just before the statements in that body; it doesn't matter much ;-)

kind regards,

Jos
Aug 3 '07 #10
r035198x
13,262 8TB
No, I was assuming an:

Expand|Select|Wrap|Line Numbers
  1. int _= 1;
  2.  
thingy just before the statements in that body; it doesn't matter much ;-)

kind regards,

Jos
Of course.
B.T.W I can now see your avatar (or lack of it) on the leader board.
Aug 3 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
Check out Obfuscated C and Other Mysteries by Don Libes (Wiley & Sons 1993).
Aug 3 '07 #12
JosAH
11,448 Expert 8TB
Check out Obfuscated C and Other Mysteries by Don Libes (Wiley & Sons 1993).
Even better: check the IOCCC site (International Obfuscated C Code Contest);

that was so much fun in the K&R1 days. I think there's even an old entry from
my sick brain in there (I could be wrong though).

K&R1 C was the best language for those purposes. C++ has too many rules
for that and Java is a sissy language when it comes to obfuscation ;-)

kind regards,

Jos

edit: there! I found an old entry; it didn't make it as a winner though:

Expand|Select|Wrap|Line Numbers
  1. main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
  2. 0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
  3. 0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
  4. ("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}
  5.  
It generates magic squares of odd size and it doesn't use any memory even
proportional to n (the odd number). Cute eh? ;-)
Aug 3 '07 #13
JosAH
11,448 Expert 8TB
Of course.
B.T.W I can now see your avatar (or lack of it) on the leader board.
So I noticed; btw what do you think of my fine avatar?

kind regards,

Jos;-)
Aug 3 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
10
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
5
by: Jon Lapham | last post by:
I have been using the EXPLAIN ANALYZE command to debug some performance bottlenecks in my database. In doing so, I have found an oddity (to me anyway). The "19ms" total runtime reported below...
4
by: marklawford | last post by:
Not having earned my DBA badge from the scouts just yet I'm a little lost with an error I'm getting. We've just upgraded our development database from 7.2 to 8.2 as the first step in upgrading...
0
by: JAW | last post by:
This plan seems like it should perform well. Does anyone see anything. SQL Statement Text: DECLARE MTR - RDG - EST - CSR CURSOR FOR
11
by: Faisal Vali | last post by:
Hi - I'm new to javascript and I was reading the book Javascript Professional Projects - there is a fragment that has me a little perplexed, and I was wondering if anyone could explain why and how...
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
0
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
1
by: w.l.fischer | last post by:
Hi, the following sequence: set current explain mode yes; set current explain snapshot yes; update ...; set current explain mode no;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.