473,395 Members | 1,978 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,395 software developers and data experts.

Quines

Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !

I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
2.warning C4013: 'printf' undefined; assuming extern returning int

main()
{
char *c="Hello";
printf(c,34,c,34);
}
The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)

__________________________________________________ ____________________

Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c% c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);
}

Can anyone explain what happens in the printf statement?

Thank You
Jan 7 '08 #1
13 2255
On Jan 7, 12:07*pm, Tarique <peo_...@yahoo.comwrote:
Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express)!

I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
2.warning C4013: 'printf' undefined; assuming extern returning int
This is undefined behavior. You are calling a varadic function
without the presence of a prototype.
main()
{
* * * * char *c="Hello";
* * * * printf(c,34,c,34);}

The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)
There are no conversion specifiers in the format string.
__________________________________________________ ____________________

Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c% c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);
Your format string starts at the 3rd character of c. Notice the %c
before main()? That is the first format specifier. The rest should
be obvious.
}

Can anyone explain what happens in the printf statement?

Thank You
Jan 7 '08 #2
Tarique wrote:
Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !
It works because sometimes you have an unlucky accident which makes
defective code appear to work correctly. You shouldn't worry too much
about the details of why; just correct your code.
I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
es it easier
2.warning C4013: 'printf' undefined; assuming extern returning int
The correct return type of printf() happens to be 'int', so that
assumption accidentally worked. On the other hand, it should have
assumed that printf() is an ordinary function, not a variadic one,
which could in principle cause problems.
main()
{
char *c="Hello";
printf(c,34,c,34);
}
Since your program printed "Hello", it seems likely that the argument
passing conventions for variadic functions for this compiler are
compatible with those for ordinary functions, at least when you never
access the variable arguments, as in this case.
The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)
Since the first 'c' was interpreted as format string that contained no
format specifiers, printf() had no reason to read any of it's other
arguments.
__________________________________________________ ____________________

Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c% c%.102s%cn%c;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]) exit(0);} \n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);
}

Can anyone explain what happens in the printf statement?
Where did you get that code? Is it from an obfuscated C contest?
Simple rule: never write code like that. However, if you insist on
trying to figure it out, I recommend taking a little bit longer to
look at it before asking for help; it's awful because it's complicated
and hard to read, but the features of C that it uses are all
relatively straightforward - you should be able to solve it without
asking for help.
Jan 7 '08 #3
On Jan 7, 12:41*pm, jameskuy...@verizon.net wrote:
Tarique wrote:
Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !

It works because sometimes you have an unlucky accident which makes
defective code appear to work correctly. You shouldn't worry too much
about the details of why; just correct your code.
I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
es it easier
2.warning C4013: 'printf' undefined; assuming extern returning int

The correct return type of printf() happens to be 'int', so that
assumption accidentally worked. On the other hand, it should have
assumed that printf() is an ordinary function, not a variadic one,
which could in principle cause problems.
The return type is irrelevant (unless, perhaps, that is the only
problem). A varadic function may unpile the automatic storage needed
for the function call differently (indeed, I have known compilers that
did this). It would appear to the compiler as an integer function
that takes four arguments but it isn't: it is a varadic function
returning int, and for which the first element is a character pointer.
printf(c,34,c,34);
Jan 8 '08 #4
On Mon, 7 Jan 2008 12:41:26 -0800 (PST), ja*********@verizon.net wrote
in comp.lang.c:
Tarique wrote:
Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !

It works because sometimes you have an unlucky accident which makes
defective code appear to work correctly. You shouldn't worry too much
about the details of why; just correct your code.
I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
es it easier
2.warning C4013: 'printf' undefined; assuming extern returning int

The correct return type of printf() happens to be 'int', so that
assumption accidentally worked. On the other hand, it should have
assumed that printf() is an ordinary function, not a variadic one,
which could in principle cause problems.
main()
{
char *c="Hello";
printf(c,34,c,34);
}

Since your program printed "Hello", it seems likely that the argument
passing conventions for variadic functions for this compiler are
compatible with those for ordinary functions, at least when you never
access the variable arguments, as in this case.
Since the behavior is completely undefined, how do you know that? It
could outputting the third argument. Or the string literal from the
source code file fed to the compiler.
The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)

Since the first 'c' was interpreted as format string that contained no
format specifiers, printf() had no reason to read any of it's other
arguments.
You're assuming it is using the first 'c'. Occam's razor says that is
most likely, but certainly not guaranteed.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 8 '08 #5
ja*********@verizon.net wrote:
Tarique wrote:
......snip......
>Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c %c%.102s%cn%c;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]) exit(0);} \n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);
}

Can anyone explain what happens in the printf statement?

Where did you get that code? Is it from an obfuscated C contest?
Simple rule: never write code like that. However, if you insist on
trying to figure it out, I recommend taking a little bit longer to
look at it before asking for help; it's awful because it's complicated
and hard to read, but the features of C that it uses are all
relatively straightforward - you should be able to solve it without
asking for help.
This is the source:
http://www.nyx.net/~gthompso/self_c.txt
Jan 8 '08 #6
user923005 <dc*****@connx.comwrites:
On Jan 7, 12:07*pm, Tarique <peo_...@yahoo.comwrote:
>Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !

I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
2.warning C4013: 'printf' undefined; assuming extern returning int

This is undefined behavior. You are calling a varadic function
without the presence of a prototype.
>main()
{
* * * * char *c="Hello";
* * * * printf(c,34,c,34);}

The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)

There are no conversion specifiers in the format string.
Right. *If* there were a proper "#include <stdio.h>", providing a
valid declaration for printf(), then, since the first argument points
to a string containing no "%" characters, the remaining arguments
would be ignored. More generally, the format string specifies which
arguments (if any) are to be processed; any arguments not consumed by
the format string are quietly ignored (except that any side effects
will take place).

[...]

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 8 '08 #7
Jack Klein wrote:
On Mon, 7 Jan 2008 12:41:26 -0800 (PST), ja*********@verizon.net wrote
in comp.lang.c:
>Tarique wrote:
>>Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !
It works because sometimes you have an unlucky accident which makes
defective code appear to work correctly. You shouldn't worry too much
about the details of why; just correct your code.
>>I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
es it easier
>>2.warning C4013: 'printf' undefined; assuming extern returning int
The correct return type of printf() happens to be 'int', so that
assumption accidentally worked. On the other hand, it should have
assumed that printf() is an ordinary function, not a variadic one,
which could in principle cause problems.
>>main()
{
char *c="Hello";
printf(c,34,c,34);
}
Since your program printed "Hello", it seems likely that the argument
passing conventions for variadic functions for this compiler are
compatible with those for ordinary functions, at least when you never
access the variable arguments, as in this case.

Since the behavior is completely undefined, how do you know that? It
could outputting the third argument. Or the string literal from the
source code file fed to the compiler.
Sure, it could do literally anything. Pay attention to my wording - I
said "it seems likely", not "it is required by the standard". I've
already expressed the fact that this code is defective, and should
simply be corrected. The OP wanted to know why his clearly defective
code seemed to "work"; I was merely making plausible guesses as to the
way in which the undefined behavior of the code turned out to be the
behavior he actually observed.
>>The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)
Since the first 'c' was interpreted as format string that contained no
format specifiers, printf() had no reason to read any of it's other
arguments.

You're assuming it is using the first 'c'. Occam's razor says that is
most likely, but certainly not guaranteed.
Yes, I was shaving with Occam's razor, not talking about what is
guaranteed by the standard.
Jan 8 '08 #8
Tarique wrote:
....
This is the source:
http://www.nyx.net/~gthompso/self_c.txt
I like the last example, by smr. :-)
Jan 8 '08 #9
On Jan 7, 12:07*pm, Tarique <peo_...@yahoo.comwrote:
Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express)!

I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
2.warning C4013: 'printf' undefined; assuming extern returning int

main()
{
* * * * char *c="Hello";
* * * * printf(c,34,c,34);}

The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)

__________________________________________________ ____________________

Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c% c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);

}

Can anyone explain what happens in the printf statement?
Others have pointed out that the first piece of code causes undefined
behaviour. The second piece of code also causes undefined behaviour
because there's no prototype for exit(). Ignoring that others have
already made guesses on how printf() probably behaves.
Jan 8 '08 #10
James Kuyper wrote:
Tarique wrote:
...
>This is the source:
http://www.nyx.net/~gthompso/self_c.txt

I like the last example, by smr. :-)
When I try to compile it, I get:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

--
Army1987 (Replace "NOSPAM" with "email")
Jan 11 '08 #11
Army1987 wrote:
>
James Kuyper wrote:
Tarique wrote:
...
This is the source:
http://www.nyx.net/~gthompso/self_c.txt
I like the last example, by smr. :-)

When I try to compile it, I get:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
[Note: The example from smr is an empty file, and won the "Worst
Abuse of the Rules" award at the 1994 Obfuscated C Code contest.]

Meaning that you cannot run the program, meaning that no output will
be generated, meaning that you have just generated the source code.
(Perhaps the original rules allowed such a loophole?)

[/tmp]$ >empty.c
[/tmp]$ cc -o empty empty.c
/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
[/tmp]$ ./empty >empty.output
bash: ./empty: No such file or directory
[/tmp]$ ls -l empty*
-rw-rw-r-- 1 hvcomput hvcomput 0 Jan 11 17:49 empty.c
-rw-rw-r-- 1 hvcomput hvcomput 0 Jan 11 17:49 empty.output
[/tmp]$ sum empty*
00000 0 empty.c
00000 0 empty.output
[/tmp]$

Note that "empty.c" and "empty.output" are identical. QED.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jan 11 '08 #12
On Fri, 11 Jan 2008 17:52:32 -0500, Kenneth Brody wrote:
[Note: The example from smr is an empty file, and won the "Worst Abuse
of the Rules" award at the 1994 Obfuscated C Code contest.]

Meaning that you cannot run the program, meaning that no output will be
generated, meaning that you have just generated the source code.
(Perhaps the original rules allowed such a loophole?)
No, that is not the loophole.

Quoting from <http://www.de.ioccc.org/1994/smr.hint>:

While strictly speaking, smr.c is not a valid C program, it is
not an invalid C program either! Some C compilers will compile
an empty file into a program that does nothing. But even if your
compiler can't, the build instructions supplied with this entry
will produce an executable file. On most systems, the stdout
from the executable will exactly match original source.

Of course, this is wrong as far as relevant for this group. While
compilers are certainly allowed to translate empty source files into
executables that do nothing, or even executables that do something, such
programs violate a constraint and the compiler must issue a diagnostic.
If the implementation chooses not to make this diagnostic a hard error,
that's fine, but it doesn't make the code any less invalid as C.

(And the build instructions referenced don't interpret the program as C
source code.)
Jan 11 '08 #13
On Tue, 08 Jan 2008 01:37:42 +0530, Tarique <pe*****@yahoo.comwrote:
>Hello.Can anyone please explain why the output of the following
code is Hello? No header file is included and the code works (Compiled
as a 'c' code with the strictest warning level on Visual C++ 2008(express) !

I get the two warnings :
1.warning C4431: missing type specifier - int assumed. Note: C no longer
supports default-int
2.warning C4013: 'printf' undefined; assuming extern returning int

main()
{
char *c="Hello";
printf(c,34,c,34);
}
The code prints Hello irrespective of the integer values used in the
printf statement(both +ve and -ve)
The first argument to printf is a format string. In this case, the
string is "Hello". The string does not contain any format
specification. Therefore, all the other arguments to printf are
ignored and only the string is printed. It is exactly the same as
printf("Hello");
>
_________________________________________________ _____________________

Secondly.

#include<stdio.h>
main() {

char*c="\\\"#include<stdio.h>%cmain(){char*c=%c%c %c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n";

printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]); /*What about this line?*/
exit(0);
}

Can anyone explain what happens in the printf statement?
Take a piece of paper and work it out. It will make a much stronger
impression if you derive it rather than us spoon feeding it to you.
What does c+2 point to? What will printf do with this string? What
is the value of c[102]? When will printf want to process that value?
Repeat for the remaining arguments.
Remove del for email
Jan 12 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Aurélien Géron | last post by:
Hi all, As a newly converted Python lover, I am impressed at how concise Python code actually is. But really... how concise can it GET? As an experiment to find out the very limits of Python,...
4
by: Gremlin | last post by:
Well I'm back online with my same ISP. It looks like I had nothing to worry about after all in the end. I have been working on a lot the past few months, and perhaps it is better that way because...
6
by: Sweety | last post by:
hello experts , Is there any .exe file which creates the output of the source code in .c file itself . Condition is that instead of there is no .c file in same path the .exe file itself prints...
93
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the...
9
by: gg | last post by:
If somebody asks me to write a program that prints itself ( quine ) can I write the following program as an answer - #include <iostream> #include <fstream> using namespace std; int main (...
6
by: snaidis | last post by:
I'm new here and I have a quite smart riddle for you people: Try to write a program which prints itself. I have a pretty interesting article about this programming problem with the answer, but...
2
by: Chad | last post by:
I want to make a comment on the following url on this forum. http://groups.google.com/group/comp.lang.c/browse_thread/thread/b456d593ae3aeedb/483c48178ecd3dd6?hl=en#483c48178ecd3dd6 The comment...
11
by: lovecreatesbea... | last post by:
Ken Thompson mentioned a self-reproducing program that products an exact copy of its source code as output in his 1983 Turing paper, http://www.acm.org/classics/sep95/ . Can this be done in C? How...
2
by: Firecore | last post by:
Is it possible to have a piece of code, that saves itself to a text file?
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: 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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.