3
void main() {
    int num;
    num = 1;
    num++;
    num = num + 9;
    printf('%u',num);
}

What is wrong with this? It says segmentation fault. Written in C.

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
Ian Duncan
  • 31
  • 1
  • 2
    also use `int main(int argc, char **argv)`! – Benoit Apr 04 '11 at 14:32
  • Although in this case it is fairly obvious which line the error is on, please include this info explicitly in your future posts - it makes everyone's life easier (including your own). – Péter Török Apr 04 '11 at 14:35
  • It's a good idea to look at the output of your compiler. I am sure it did warn you already about the printf(). – Mackie Messer Apr 04 '11 at 14:42

4 Answers4

8

'%u' should be "%u" (double quotes). C is not SQL, and '%u' is considered a multicharacter literal.

And as Erik said, %d is better in your case.

Community
  • 1
  • 1
Benoit
  • 76,634
  • 23
  • 210
  • 236
3

printf("%d") - printf takes a C string not a character. %d is the correct format specifier for an integer.

Erik
  • 88,732
  • 13
  • 198
  • 189
3

That '%u' ( a multi-character char constant) should be "%u" (a char array). The char constant is being misinterpreted as a pointer to random memory.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

Note that you would have gotten an error from passing the wrong type (int instead of const char *) as the first argument of printf, except that you forgot to #include <stdio.h> or prototype printf yourself. And since printf is a variadic function, this results in UB too.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711