Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I come across a question in a coding competion

"write a code that returns SIGSEGV(Segmentation fault ) " .

Points were given on the basis of length of code. The prgramming languages available were asm,c,c++,java .

I knew c language better and submitted my code as

   main(){char *c="h";c[3]='j';}    

But others solve it in shorter length than me . How it is possible ? Can any one explain plz.

Platform UBUNTU 10.04

share|improve this question

migrated from Jul 26 '12 at 2:22

This question came from our site for professional and enthusiast programmers.

int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; } – sehe Dec 24 '11 at 22:00
movl $0,0 (GNU AS syntax, x86_64 - the modern assembly segfault) – Mat Dec 24 '11 at 22:04
The question really can't be answered without reference to a specific platform. There is, for example, no C code that is guaranteed to give a SIGSEGV on every platform. – David Schwartz Dec 24 '11 at 22:05
@sehe: That'd be both faster and shorter if you would do std::cout << "SIGSEGV(Segmentation fault )\n"; instead. :-) – Omnifarious Dec 24 '11 at 22:23
This is a question that has an answer. There is a shortest program that produces the desired result. I don't agree that it should've been closed. – Omnifarious Dec 24 '11 at 23:49
up vote 17 down vote accepted

bending the rules a bit ; the C linker doesn't care if 'main' is a function or not :

int main=0;

indeed, the default type is 'int' and the default global initialiser is 0 :

share|improve this answer
That's pretty darn short. +1 And it compiles (though with a warning) – sehe Dec 24 '11 at 22:19
+1: Though, if it were my question, that would be bending the rules too far for me to accept as an answer. But that's still really impressive, and likely about the shortest you can go. – Omnifarious Dec 24 '11 at 22:28
+1, eloquently coded. – Michael Krelin - hacker Dec 25 '11 at 17:55

There are many ways to make a program return SIGSEGV. One of these is via segmentation fault due to stack overflow of a recursive function calling itself a lot of times. In C, a short (if not the shortest) code for this would be:

share|improve this answer
is there any shorter code than this in other language..? – Arya Dec 24 '11 at 21:58
In java, even the 'public static void main' is larger than this. Probably there's a shorter solution in asm, but I'm not really sure about it... – Neo Adonis Dec 24 '11 at 22:00

In C, something like this is fairly short:

int main(void)
  return *(int *)0=1;

...or for a less proper version with minimal whitespace:


...even less proper, but shorter (the source code, at least):


It calls puts() with an implicit prototype, but still "works" if linked with the standard library.

share|improve this answer

Besides may other ways of suicide as demonstrated by others, your way could be expressed a bit shorter as

share|improve this answer

Assembly, 3 characters


C, 5 characters

share|improve this answer

Nah, as short as this :

int main;
share|improve this answer
As the accepted answer shows, you don't even need the int . – luser droog Oct 21 '12 at 17:34

Ruby, 18 characters

I know this wasn't requested, but have a Ruby one, because why not. :D

Process.kill 11,$$
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.