A . Definition string (single character) and the array of characters
1. Define a single character
char I = 'X' (defined single character)
After the string to create a constant can not be modified
2. Definition of an array of characters
char i [4]=”xxxx”; char i []=”xxxx”; char i [4]={‘x’’x’’x’’x’};
Where the number of the number of elements in the array of defined than the actual assignment of one more for storing \ 0 position
Only when defining the array of characters to the entire string at once assigned to it, once the definition is over, it is only a character by character the assignment.
Two strings (individual characters) and the input character array
1.scanf function
To string (a single character) assignment
scanf ( "% 2s", & x); (wherein x is defined name char type variable)
To the character array assignment
scanf ( "% 2s", x); (where x is defined in the character array name)
2.fgets function
char food[5]; printf("Enter favorite food:"); fgets(food, sizeof(food), stdin);
fegets brackets mean content: Food ( the name of the array of characters) , the sizeof (Food) the maximum length of the received characters , stdin data from the keyboard
And scanf comparison, this function allows the user to enter a string with spaces
Three output string (single character) and the array of characters
1. The output of a single character
Printf ( "% C", X) ; for a single character output
X is char name type variable
2. The output of the array of characters
Printf ( " % S " , X ) ; the string output
X is a character array name
IV. Between character arrays and pointers are some considerations
the first:
char cards []; In this expression cards is an array of characters, and must be assigned immediately
#include <stdio.h> void stack_deck ( char Cards []) { the printf ( " % S \ n- " , Cards); } int main ( void ) { char Cook [] = " the Hello World! " ; stack_deck (Cook) ; return 0 ; } in this expression Cards [] is a function pointer variable of type char, he first byte address of the storage array cook.
second:
char s[] ="xxxx"; char * t = s;
When a character array variable is assigned to a pointer, the pointer variable will contain the address information of the array, and the array length information is lost