skip to Main Content

Below is the code I’m compiling:

#include <stdio.h>
void main()
{
    printf("Hello%cWorldn", 0);
}

Below is the compiler output:

HelloWorld

Bing Chat-GPT response:start

Below is the same output by Bing chat-gpt in creative mode

Copilot:
The output of printf("Hello%cWorldn", 0) will be:

Hello

The reason is that the %c format specifier expects a single character as an argument, but you provided 0 instead. This is equivalent to the null character ”, which marks the end of a string in C1. Therefore, the printf function will stop printing the string after encountering the null character, and ignore the rest of the characters.

To print the whole string as it is, you need to use the %s format specifier, which expects a string as an argument. For example:

printf("Hello%sWorldn", "");

This will print:

HelloWorld

Alternatively, you can escape the backslash character with another backslash, like this:

printf("Hello\0Worldn");

This will also print:

HelloWorld

You:
but when I compiled the above code it gave me output as HelloWorld

Copilot:
That is strange. The code you posted should give a warning and print only Hello. Are you sure you compiled the same code? Maybe you have a different version of the compiler or the standard library that handles the null character differently. Can you show me the exact command you used to compile and run the code? 😊

You:
I have used gcc main.c -o app to compile

Copilot:
I see. And how did you run the app? Did you use any command-line arguments or input redirection? 😊

You:
I ran the app using ./app command I did not use any command-line arguments or input redirection

Copilot:
I see. Well, I’m not sure why you got a different output than expected. According to the C standard, the behavior of printf with a null character argument is undefined1. This means that different compilers or platforms may handle it differently, and there is no guarantee of consistency or correctness. Maybe your compiler or platform has some special treatment for the null character, or maybe you encountered some memory corruption or other bug.

To avoid this kind of problem, I suggest you always use valid arguments for the printf function, and follow the rules of the format specifiers. You can find more information about the printf function and its format specifiers in this article2.

I hope this helps. If you have any other questions, feel free to ask me. 😊

1: warning: format string contains β€˜β€™ within the string body [-Wformat] 2: printf in C – GeeksforGeeks

Bing Chat-GPT response:end

Please note GCC version:

coder@069e69391cf2:~/project/Rough/programs/3$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.2.1-9ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) 
  • I wanted to learn, in printf("Hello%cWorldn", 0) here %c expects matching int argument.
  • Hence I gave 0, this turns out to be NULL with respect to ASCII characters.

I need to know why there are two different outputs i.e. compiler prints HelloWorld & chat-gpt
says only Hello should be printed.

Please help to explain this behavior ?

Thanks.

3

Answers


  1. This is entirely explained by the fact that ChatGPT is wrong and is not a source of factual information. ChatGPT tells you this in its terms of use:

    Output may not always be accurate. You should not rely on Output from our Services as a sole source of truth or factual information, or as a substitute for professional advice.

    and:

    YOU ACCEPT AND AGREE THAT … YOU WILL NOT RELY ON OUTPUT AS A SOLE SOURCE OF TRUTH OR FACTUAL INFORMATION, OR AS A SUBSTITUTE FOR PROFESSIONAL ADVICE.

    Going further, printf("Hello%cWorldn", 0); tells printf to print β€œHello”, then to print the character with code 0, then to print β€œWorld” and a new-line character. The 0 character is sent to the terminal and typically results in no display, so the β€œW” will appear immediately after the β€œo”.

    Login or Signup to reply.
  2. When printf parses "Hello%cWorldn" and sees %c, it doesn’t grab the supplied argument, 0, inserts it instead of %c and then notices, "oh, the string ends here". It continues to parse until the format string hits a .

    The resulting string that it outputs will therefore be HelloWorldn.

    Login or Signup to reply.
  3. Your Bing chat-gpt’s output is wrong.

    The %c format specifier of printf takes an int argument (char is automatically promoted to int in variable-length argument), converts the value to unsigned char, and emit the result as a byte.

    Therefore, (assuming that ASCII is used) the code

    printf("Hello%cWorldn", 0);
    

    will emit bytes:

    48 65 6c 6c 6f 00 57 6f 72 6c 64 0a
    

    Your viewer application may ignore the NUL byte 00 and display this sequence of bytes as HelloWorld.

    The code

    printf("Hello%sWorldn", "");
    

    will emit bytes without NUL byte:

    48 65 6c 6c 6f 57 6f 72 6c 64 0a
    

    because the %s format specifier takes a (pointer to) string as the argument and strings in C are terminated by a NULL character . Therefore, what is passed here is a pointer to 2-byte array 00 00 and this is interpreted as 0-character string (the first byte is the terminator).

    The code

    printf("Hello\0Worldn");
    

    will emit bytes:

    48 65 6c 6c 6f 5c 30 57 6f 72 6c 64 0a
    

    Because \ is an escape sequence that means a character , and 0 after \ is simply a character 0 without any special meanings.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search