+ 1

Why it's comes to error?

#include<stdio.h> int main(){ char ch[20]; ch="study To night"; printf("%s",ch); }

18th Sep 2020, 7:24 AM
Suparna Podder
Suparna Podder - avatar
5 Réponses
+ 4
String assignment does not exist in th C language, You can use void strcpy(char str1[], char str2[]) from string.h. This is equal to str1=str2 in C++; So in your case : strcpy( ch, "study to night"); Don't forget including <string.h>
18th Sep 2020, 7:31 AM
CHERIEF Houcine Abdelkader
CHERIEF Houcine Abdelkader - avatar
+ 4
String is not assignable to ch this is invalid . You need to use strcpy . Or you can do this if you want to print your string #include<stdio.h> int main(){ char ch[20]="study To night"; printf("%s",ch); }
18th Sep 2020, 7:35 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
It will give you an error because you are trying to assign a string "literally" to a char array, C doesn't support that. But you can assign it to a char* (well because it is a pointer, not an allocated memory of chars). So this is allowed, char *s; s = "study To night"; And it becomes a string literal which you can't modify directly.
20th Sep 2020, 11:30 PM
LastSecond959
LastSecond959 - avatar