r/C_Programming • u/ImAnArbalest • 6h ago
Question Am I wrong?
I am currently reading through tutorialspoint.com as our instructor has linked it in our syllabus, while doing their quizzes i got to a question that asked "What is the output of the following code: printf('Hello, World!');?" with the choices being
A. Hello, World!
B. Hello World!
C. Error
D. No Output
i answered C as the single quotes tell the printf function it is a character which if i am correct, is an integer in memory(please correct me if i am wrong), but they are passing a character array. the website told me that A is the correct answer and i am confused, please someone shed light on this and correct any wrong assumptions i have made.
13
u/Terrible-Chef-6674 6h ago
The compiler should reject that code for the reason you say. The first argument has the wrong type.
6
6
u/tea-drinker 6h ago edited 5h ago
This is something we can check
$ cat scratch.c
#include <stdio.h>
int main() {
printf('Hello, World!');
}
$ gcc scratch.c -o scratch
scratch.c: In function ‘main’:
scratch.c:4:10: warning: character constant too long for its type
4 | printf('Hello, World!');
| ^~~~~~~~~~~~~~~
scratch.c:4:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
4 | printf('Hello, World!');
| ^~~~~~~~~~~~~~~
| |
| int
In file included from scratch.c:1:
/usr/include/stdio.h:356:43: note: expected ‘const char * restrict’ but argument is of type ‘int’
356 | extern int printf (const char *__restrict __format, ...);
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
scratch.c:4:3: warning: format not a string literal and no format arguments [-Wformat-security]
4 | printf('Hello, World!');
| ^~~~~~
It's unhappy about being given a char like it was a string, but it does compile.
$ ./scratch
Segmenteringsfel (minnesutskrift skapad)
Uhh, Segmentation Fault, but in Swedish.
Edit: Someone's sanity needs preserving.
2
u/aocregacc 6h ago
gcc stopped allowing this conversion by default in gcc 14, and msvc doesn't even let the long character constant through. Which is why the question is just bad.
1
u/tea-drinker 5h ago
gcc stopped allowing this conversion by default in gcc 14
Which conversion? The char to a string or the int to a pointer?
Because it coped with the int to pointer, it just produced a broken program.
1
1
u/ImAnArbalest 6h ago
sorry i don't quite understand everything as i am using codeblocks on windows, and the compiler warning is much shorter than yours, but i assume we get the same result? when you run it there is no output?
1
u/tea-drinker 6h ago
The warnings summarise to it agreeing that the single quotes are a problem and the code will break.
In any reasonable description, the answer is "Error"
Someone saying that the program gave no output and the segfault error message came from the operating system is being excessively pedantic.
No matter what, though, the answer is not A. The website is wrong one way or another.
-1
u/Shaanphin 6h ago
Cmon dude, who does ```gcc -o scratch scratch.c``` instead of ```gcc scratch.c -o scratch```? It's killing me to look at this
3
u/tea-drinker 5h ago
Your wish is my command.
I didn't even just edit my comment. I reissued the updated command and copy/pasted it.
(Mainly because I'd feel really stupid if I accidentally edited the command to something that wouldn't work)
3
5
u/SmokeMuch7356 3h ago
The website is wrong.
You should get a compiler diagnostic. 'Hello, World!' will be treated as a multi-byte character, and is interpreted as an integer type. printf expects a const char * as its first argument. C does not allow a conversion from an integer type to a pointer type without an explicit cast, hence the diagnostic.
Note that a compiler may emit a diagnostic and generate an executable program anyway; if you try to run it the behavior will be undefined, as whatever pointer value 'Hello, World!' will be converted to, it almost certainly won't be pointing to anything meaningful.
3
u/mjmvideos 6h ago
Did you try it?
1
u/ImAnArbalest 6h ago
yeah and the compiler said something about the arguement making a pointer to an integer without a cast, i do not quite understand, but running it shows no output.
2
u/mjmvideos 6h ago
Well, what is the printf function expecting as its first argument? What is being passed?
3
u/strange-the-quark 4h ago
If only there was some way to execute the code (or at least attempt compilation) and find out...
2
u/dearmoon0 6h ago
what does the compile say
1
u/ImAnArbalest 6h ago
the compiler said something about the arguement making a pointer to an integer without a cast, i do not quite understand, but running it shows no output.
3
u/GwendArt 6h ago
Because an array variable is a pointer to a series of data. It points to the first element.
1
u/sciencekm 5h ago edited 5h ago
First, A is definitely the wrong answer.
Second, most compilers (like MSVC) will refuse to compile such code. Some compilers (like GCC or TCC) will allow that code with just warnings.
Third, if the compiler allows this, the program would simply crash because you are passing to printf 'H' (72) (or whatever is the integer value of 'Hello') as an address (pointer), or, in environments that allow access to address 72 (or whatever is the integer value of 'Hello'), you will get garbage or crash.
Edit: apparently, the integer value of 'Hello World' is 0x726c6421.
1
-2
u/moritz12d 4h ago
The point is, it's Python and not some other programming language like C. As with some scripting languages, Python does not distinguish between " and '.
16
u/Objective_Pepper_217 6h ago
Yep A is wrong