Q:

Error: Assign string to the char variable in C | Common C program Errors

belongs to collection: C Common Errors Programs

0

Error: Assign string to the char variable in C | Common C program Errors

If you assign a string to the character variable, it may cause a warning or error (in some of the compilers) or segmentation fault error occurs.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the code:

#include <stdio.h>

int main(void) {
	
	char name="Amit shukla";
	printf("%s",name);
	
	return 0;
}

Output

Segmentation fault

How to fix?

Declare character array instead of char variable to assign string

 char name[]="Amit shukla";

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Error: Assignment of read-only variable in C | Com... >>
<< Error: Unterminated comment (Invalid comment block...