Is it too obvious?

1
2
3
4
5
#include<stdio.h>
int main() {
    printf("Huh??!");
    return 0;
}

If the above C code snippet is compiled strictly according to a C standard and executed, what would the output be?

Here 's a text file that contains 8 lines and in each line, there's a number followed by a string. If you think the string on the line with number x x is the output of the above code, give your answer as x x .

Details and Assumptions:

  • As an example, if you think the output is "Huh?!", give your answer as 5 5 .
  • Compiling strictly according to a C standard refers to compiling according to a C standard (for example, C99, C11, etc) instead of some non-standard C revision like GNU99, GNU11, etc.
1 3 4 6 8 7 2 5

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Christopher Boo
Jul 7, 2016

A good way to solve this is to throw the program into your compiler. Hah!

1
2
3
4
5
6
7
>>> gcc prog.c -o prog
prog.c:3:16: warning: trigraph ??! ignored, use -trigraphs to enable [-Wtrigraphs]
>>> ./prog
Huh??!
>>> gcc prog.c -trigraphs -o prog
>>> ./prog
Huh|

I just do what we normally do (compile then execute) but I saw a warning and simply followed the instruction. This is what I got.

The problem name in the direct link of this problem was actually a hint for the problem solver to research a bit about it and solve the problem without actually running the code.

Prasun Biswas - 4 years, 11 months ago

Log in to reply

Can you explain why the code behaves like this?

Christopher Boo - 4 years, 10 months ago

Log in to reply

If the code is compiled strictly according to a C standard, then it shouldn't generate any warnings at all and would output "Huh|".

When you compile using gcc, the default compile standard is GNU11 (which ignores trigraphs by default) which is a bit different from a ISO C standard (like C99, C11, etc). If you do gcc -std=c11 prog.c -o prog , then the compiler won't generate any warning at all.

As for why the "|" is printed instead of "??!", that's because of trigraphs which are part of the ISO C standard. Trigraphs were actually invented because some older keyboards didn't support the entire C language character set. You may think of these trigraphs like macros for the extra characters. For keyboards that don't have the "|" character, using the trigraph would be helpful if the C programmer needs to use that character.

Nowadays, the modern keyboards however support these extra characters and so di/trigraphs don't have much use/importance now.

Prasun Biswas - 4 years, 10 months ago

Log in to reply

@Prasun Biswas trigraphs are removed from C++17

Hasmik Garyaka - 3 years, 7 months ago

Log in to reply

@Hasmik Garyaka C++17 is a C++ standard, not a C standard. As of now, the latest ISO C standard is C11 (which does support trigraphs).

Prasun Biswas - 3 years, 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...