ADVERTISEMENT
ADVERTISEMENT

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

Here, we will learn how and what an error or warning occurs, when we assign string to the character variable in C?
Submitted by IncludeHelp, on August 23, 2018

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.

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";

C Common Errors Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.