Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As the string literals will be allocated with only read-only memmory, We could not change any character or concatenate the string literal.
But here in this case why I am able to concatenate the string literal ? and why I am not getting any error?

What I have tried:

#include<stdio.h>
int main()
{
printf("abcde""fghijklmnop");
return 0;
}
Posted
Updated 23-Apr-21 19:57pm

1 solution

Because the concatenation is done at compile time - so it's a single string literal that is created.

To be honest, I'd forget that syntax even exists if I was you - it's only of any use when you want to split a really long string over multiple lines to improve code readability:
C++
printf("The Chronicles of Narnia\n"
"Chapter One\n"
"The Wrong Door\n"
"THIS IS A STORY ABOUT SOMETHING THAT HAPPENED long ago when your grandfather was a child."
" It is a very important story because it shows how all the comings and goings between our"
" own world and the land of Narnia first began.\n"
...);
 
Share this answer
 
Comments
_-_-_-me 24-Apr-21 4:01am    
While using strcat() function , does the concatenation of the strings take place at run time? Because when strcat() function is used , error is obtained.
OriginalGriff 24-Apr-21 4:31am    
Yes. All functions calls are run time - so calling strcat means passing two parameters: the source and the destination strings. It finds the first null in the destination, and copies the source into the destination starting at that point.

Concatenation using two strings side-by-side as the parameters to strcat will give you an error because the function requires two parameters.

BTW: when you talk about errors, copy and paste the error message - remember that we can't see your screen. access your HDD, or read your mind - all we get to work with is exactly what you type!
So saying "error is obtained" or "it don't work" is no help to anyone, as we have no idea what error you get (and there are many), when you get it, or what you did to make it happen! :laugh:
_-_-_-me 25-Apr-21 1:11am    
Okay,next time when I ask questions , I will clearly mention the error. Thank you.
OriginalGriff 25-Apr-21 1:27am    
:thumbsup:
Perfect!
_-_-_-me 24-Apr-21 7:33am    
okay , thank you very much.

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