r/C_Programming • u/ImAnArbalest • Sep 10 '26
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.
44
u/Objective_Pepper_217 Sep 10 '26
Yep A is wrong
3
u/mjmvideos Sep 10 '26
Did you try it?
5
u/strange-the-quark Sep 10 '26
If only there was some way to execute the code (or at least attempt compilation) and find out...
19
u/Terrible-Chef-6674 Sep 10 '26
The compiler should reject that code for the reason you say. The first argument has the wrong type.
8
u/SmokeMuch7356 Sep 10 '26
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.printfexpects aconst 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/sciencekm Sep 10 '26 edited Sep 10 '26
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.
3
u/PitchBlackEagle Sep 11 '26
Use K.N. King's book. the book 1988 is fine, but it was written for those who had programming experience back then, and needed to get up to speed with C.
Stay clear of Indian resources, including books. Bad quality of writing, clever "haha, got you" sort of quizzes which end up teaching nothing.
1
u/dearmoon0 Sep 10 '26
what does the compile say
2
u/septum-funk Sep 10 '26
D, it won't even compile
2
u/RareAdvertising361 Sep 11 '26
with some hacking you could get all four answers to be true. if you could write to the address pointed to by 'Hello, World!', you could make A,B, and D come true. or if you do nothing it'll probably segfault, in which case C
1
u/doubleee22 Sep 11 '26
maybe u should’ve tried to compile it and check? isn’t it more efficient thn making a post a whole post outta it?
1
u/HelloWorldYozvak Sep 12 '26
Yup C is the correct answer. If only the instructor can C that 😉. No seriously, I’d tell the instructor of your discoveries to see if maybe other students reported this too.
1
u/port443 Sep 11 '26
Weird, I got B
Jokes aside, it will print whatever is at the address of
'Hello, World'. You should see warnings about "makes pointer from integer without a cast". You can see the integer (which will be used as a pointer) that 'Hello, World' resolves to like so:So, as long as you know or control what is at address 0x726c6421, that's the answer.
0
u/realdreamer1993 Sep 11 '26
imho the stdio.h like printf() should act as the helper only in learning , not used as the main subject.
You should focus on the C core fisrt like variable, data type, function, pointer. and create operation in function.
-7
u/moritz12d Sep 10 '26
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 '.
0
u/[deleted] Sep 10 '26
[deleted] — view deleted comment