

This is because name is a char array, and we know that array names decay to pointers in C. It's because there was a space after Dennis.Īlso notice that we have used the code name instead of &name with scanf(). The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).Įxample 1: scanf() to read a string #include Įven though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the name string. You can use the scanf() function to read a string. Note: Use the strcpy() function to copy the string instead. For example, char c Ĭ = "C programming" // Error! array type is not assignable. This is bad and you should never do this.Īrrays and strings are second-class citizens in C they do not support the assignment operator once it is declared. Here, we are trying to assign 6 characters (the last character is '\0') to a char array having 5 characters.

Let's take another example: char c = "abcde"
