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變量來分配字符串
char name[]="Amit shukla";
翻譯自: https://www.includehelp.com/c-programs/assign-string-to-the-char-variable-in-c.aspx