Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What happens when when the string literal contains double quotes with in it and why I am not getting error?

What I have tried:

#include<stdio.h>
int main()
{
    printf("%s","abcdefghijklmn""opqrstuvwxyz");
    return 0;
}
Posted
Updated 22-Apr-21 20:28pm

You have to "escape" the double quote character, in the same way that you add a newline:
C++
printf("line one\nline two\n");
"\n" is a escape sequence that C recognises as a "special character" which represents a newline, and there are several of them but the most common are:
\n    Newline
\t    Tab
\\    A literal back slash
\'    A literal single quote
\"    A literal double quote

So to insert a double quote:
C++
printf("Joe says \"Hello world!\"");
Will print
Joes says "Hello world!"
on the console.
 
Share this answer
 
Comments
CPallini 23-Apr-21 2:14am    
5.
Our Griff already showed you how to escape the double quotes. As an addition I'm trying to address the doubt
Quote:
why I am not getting error?
You're not getting an error because
C
"abcdefghijklmn""opqrstuvwxyz"
is a valid construct, namely a concatenation of string literals (see, for instance c - How does concatenation of two string literals work? - Stack Overflow[^]).


The following program spots the differences
C
#include <stdio.h>
  
int main()
{
  const char * escaped = "foo\"bar"; // insert a double quote in the string using the proper escape sequence
  const char * concatenated = "foo""bar"; // concatenate the two string literals.

  printf("escaped: %s\n", escaped);
  printf("concatenated: %s\n", concatenated);

  return 0;
}

output:
escaped: foo"bar                                                                                                                                           
concatenated: foobar    
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900