Smallest "C" Program !!

D

Dev

Hello Folks,

I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?

And, as we know, smallest program means, it should execute
single statement, I wrote ";" (Semicolon), so, program output will
display blank screen.

But Friends, I am not sure, Does anyone has perfect solution?

Thanks..
 
I

Ico

Dev said:
I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?

And, as we know, smallest program means, it should execute
single statement,

Wrong. As we know, the smallest program should at least have a main()
function.
But Friends, I am not sure, Does anyone has perfect solution?

int main(void){return 0;}
 
J

John Devereux

Dev said:
Hello Folks,

I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?

And, as we know, smallest program means, it should execute
single statement, I wrote ";" (Semicolon), so, program output will
display blank screen.

But Friends, I am not sure, Does anyone has perfect solution?

I think it may depend on whether the C implementation is for a
"hosted" (e.g. Desktop PC) or "freestanding" (e.g. embedded)
system. Your program compiled OK (with gcc) but gave me "undefined
reference to main" at the link stage.

Assuming a system where you do not need main(), then I would think you
could reduce the size by a further 100% by omitting the ";".
 
T

Thomas Maier-Komor

Ico said:
Wrong. As we know, the smallest program should at least have a main()
function.


int main(void){return 0;}

you can even omit "return 0;". See ISO/IEC9899 5.1.2.2.3.

Tom
 
T

Thomas Maier-Komor

Uncle said:
try this:

main(){}

cheers
Nikolaos Kavvadias

correct for ANSI C, but ISO C requires main to be int, and implicit int
is deprecated in ISO C.
 
V

Vladimir S. Oka

Dev said:
I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?

I wonder what kind of "aptitude" this tests...
And, as we know, smallest program means, it should execute
single statement, I wrote ";" (Semicolon), so, program output will
display blank screen.

This is not correct on two counts: first, why would the smallest
program have to execute even a single statement (well it does have, but
that statement is `return 0;`, implicit or spelled out); second, even
if you do use an empty statement (and that's what including a single
`;` does, `;` itself is /not/ a statement, the statement is nothingness
that precedes it) the program still does not "display blank screen" --
it does absolutely nothing.
But Friends, I am not sure, Does anyone has perfect solution?

I don't know what "perfect" means in this case, but I believe the
smallest Standard C (C90) program is:

int main(void)
{
return 0;
}

If you really want to push C90 almost to the limit, you could reduce it
(at the expense of form and readability) to:

main()
{
return 0;
}

In C99, you can even omit `return`, as `return 0;` is implied:

main()
{
}

Note, that you do not need `;`, and this program produces absolutely no
output whatsever.

PS
If you want to make it really unreadable, and minimal, go for:

main(){}
 
D

Dik T. Winter

>
> correct for ANSI C, but ISO C requires main to be int, and implicit int
> is deprecated in ISO C.

Deprecated, yes, but still correct. (BTW what is the difference between
ANSI C and ISO C except that in C89 the numbering of the chapters differed?)
 
D

Dik T. Winter

> main()
> {
> return 0;
> }
>
> In C99, you can even omit `return`, as `return 0;` is implied:

You could also omit it in C89. Falling off a non-void function was
allowed. The only caveat was that the use of the returned result
was undefined behaviour. So no problem if you do not use the return
value. In the case of main it is stated that the return value is
returned to the environment. Whether that is "use" in terms of the
C standard is open for debate, I think it is not.
 
K

Kenneth Brody

Vladimir S. Oka said:
I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?
[...]
If you want to make it really unreadable, and minimal, go for:

main(){}

Can you "cheat" with command-line flags?

If I use the command line:

cc "-Dx=main(){}" smallest.c

I can create a "smallest.c" consisting of simply:

x

And the source is 100% portable! You may have to change the compiler
options, of course, but the source remains unchanged. :)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
V

Vladimir S. Oka

Dik said:
You could also omit it in C89. Falling off a non-void function was
allowed. The only caveat was that the use of the returned result
was undefined behaviour. So no problem if you do not use the return
value. In the case of main it is stated that the return value is
returned to the environment. Whether that is "use" in terms of the
C standard is open for debate, I think it is not.

My take would be that, in a hosted environment, as it is not possible
to know what the environment would do with the return result, including
actually using it, a value has to be returned as if it's known it's
going to be used. Hence, you shouldn't omit the return value from main.

For example, if you use the same program from the command line you may
not need, or care, about the return value (e.g. a quick `cp` in *nix).
OTH, a shell script may want to know the return value and alter it's
flow accordingly (did `cp` succeed?).

I'm prepared to be wrong...
 
J

John Bode

Kenneth said:
Vladimir S. Oka said:
I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?
[...]
If you want to make it really unreadable, and minimal, go for:

main(){}

Can you "cheat" with command-line flags?

If I use the command line:

cc "-Dx=main(){}" smallest.c

I can create a "smallest.c" consisting of simply:

x

And the source is 100% portable! You may have to change the compiler
options, of course, but the source remains unchanged. :)

Wasn't that an IOCCC entry? And didn't it inspire a new rule?
 
J

Jordan Abel

correct for ANSI C, but ISO C requires main to be int, and implicit int
is deprecated in ISO C.

C99 requires main to be int [explicitly declared]

C89 requires main to explicitly return a value.

K&R doesn't so much "require" things, but i suspect that having main
fail to return a value would be an offense roughly equivalent to void
main on more modern systems.

[other variants of C are off-topic]

K&R, C89:
main(){return 0;}

C99:
int main(){}
 
J

Jordan Abel

You could also omit it in C89. Falling off a non-void function was
allowed. The only caveat was that the use of the returned result
was undefined behaviour. So no problem if you do not use the return
value. In the case of main it is stated that the return value is
returned to the environment. Whether that is "use" in terms of the
C standard is open for debate, I think it is not.

I think it is - this is roughly the K&R equivalent of a function
returning void, as far as I can tell. And we all know void main is
illegal.
 
L

Laurent Deniau

Jordan said:
correct for ANSI C, but ISO C requires main to be int, and implicit int
is deprecated in ISO C.


C99 requires main to be int [explicitly declared]

C89 requires main to explicitly return a value.

K&R doesn't so much "require" things, but i suspect that having main
fail to return a value would be an offense roughly equivalent to void
main on more modern systems.

[other variants of C are off-topic]

K&R, C89:
main(){return 0;}
main(void) {return 0;}
C99:
int main(){}

5.1.2.2.1
int main(void){}
 
P

pemo

Dev said:
Hello Folks,

I had faced this objective in one of my aptitude exams, that
"What could be the smallest "C" program?

And, as we know, smallest program means, it should execute
single statement, I wrote ";" (Semicolon), so, program output will
display blank screen.

But Friends, I am not sure, Does anyone has perfect solution?

As it's less characters than the other replies, a c99 smallest might be ...


main(void){exit(0);}
 
P

pemo

pemo said:
As it's less characters than the other replies, a c99 smallest might
be ...

main(void){exit(0);}

Should have added ... of course, you'll get warnings about using exit(0),
but it should work ok, and is within the scope of your 'spec' I think.
 
J

Jordan Abel

main(void) {return 0;}

Is this even legal in K&R? It is by no means necessary in c89.
5.1.2.2.1
int main(void){}

In neither case is this required. While I agree that it is good
practice, I omitted it in the interest of shortness [the topic of this
thread]

(I am prepared to support my claim that it's not required, if you think
it's necessary. My argument is largely based on 6.7.5.3.14 as compared
to the wording in 6.7.5.3.10)
 
L

Laurent Deniau

Jordan said:
main(void) {return 0;}


Is this even legal in K&R? It is by no means necessary in c89.

5.1.2.2.1
int main(void){}


In neither case is this required. While I agree that it is good
practice, I omitted it in the interest of shortness [the topic of this
thread]

(I am prepared to support my claim that it's not required, if you think
it's necessary. My argument is largely based on 6.7.5.3.14 as compared

Ok ;-)

a+, ld.
 
R

Richard Heathfield

pemo said:
As it's less
s/less/fewer/

characters than the other replies, a c99 smallest might be
...


main(void){exit(0);}

In C99, a declaration is required for exit(). Its absence means that the
program is not a legal C99 program. Furthermore, C99 eliminates implicit
int. Here is the shortest legal version of your program:

#include<stdlib.h>
int main(){exit(0);}
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top