Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does printf("a" "b" "c") prints abc in C [duplicate]

Tags:

c

I have the following code in C:

#include<stdio.h>
void main(){
    printf("a" "b" "c");
}

it outputs:

abc

Can anyone explain why?

I am guessing that it is "a" "b" "c" preprocessed as "abc". Am I right or Is it something else?

like image 709
SMR Avatar asked Jul 17 '14 12:07

SMR


People also ask

What is printf () function in C language?

printf() The function printf() is used to print the message along with the values of variables. Here is the syntax of printf() in C language, printf(const char *str, ...); Here is an example of printf() in C language, Example. Live Demo

What is the difference between printf () and sprintf () in C++?

The function printf () is used to print the message along with the values of variables. printf (const char *str, ...); Welcome! The value of a : 24 The function sprintf () is also known as string print function. It do not print the string. It stores the character stream on char buffer.

How to use printf() in a better way?

As we know that, printf () is used to print the text and value on the output device, here some of the examples that we wrote to use the printf () in a better way or for an advance programming. printf ("Hello world How are you?"); Hello world How are you? To print double quote, we use \". Hello "World", How are you?

What is the output of a=50 printf() in C++?

What is the output of a=50,printf ("%d%d%d", ++a, a++,--a)? The output is that the C or C++ compiler produces a number of compilation errors. The first will be that “a” has not been declared, and you will need to put an “int” in front of it.


2 Answers

Adjacent string literals are concatenated as part of translation phase 6.

Brief summary of phases (source: C99 standard, paraphrased)

  1. Trigraphs and multi-byte characters in the source file are mapped to the source character set
  2. Lines ending in \ are spliced
  3. File parsed into a set of preprocessing tokens
  4. Preprocessing directives processed
  5. Character constants and string literals are migrated to the execution character set
  6. Adjacent string literals are concatenated.
  7. The rest of compilation (excluding linking)
  8. Linking
like image 183
M.M Avatar answered Oct 22 '22 06:10

M.M


Adjacent string literals are merged in compilation translation phase 6. Since "a" "b" "c" is further treated as "abc" string literal.

In case of you are not familiar with this term, phase 6 is somewhat between preprocessing and actual, "proper" compilation.

like image 32
Grzegorz Szpetkowski Avatar answered Oct 22 '22 05:10

Grzegorz Szpetkowski