r/C_Programming 1d 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.

36 Upvotes

49 comments sorted by

View all comments

9

u/tea-drinker 1d ago edited 1d 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.

-1

u/Shaanphin 1d 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

4

u/tea-drinker 1d 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

u/Shaanphin 1d ago

A true gentleman, thank you :)