The difference between the C language array of integers, character arrays, strings

One,

Storing a first integer array, the array is not added last '\ 0', the string will automatically add, so much the actual characters stored character when the size of the array than a 
second integer array each cell is 4 bytes, a character string is stored, each representing a character

Second, the difference between an array of characters and strings

C language is no specific string variable, if you want a string stored in a variable, you must use an array of characters, which uses a character array to store a string, each element in the array to store one character.

<1> defined: char c [10]

Interoperable character to an integer, so int c [10] can also define an array of characters. However, due to two different types of bytes allocated with the integer to define a waste of space, in addition to this definition of practical significance is not large, just to illustrate its legitimacy.

<2> Initialization: 

    The easiest way i.e. by-assignment: char c [10] = { 'I', '', 'a', 'm', '', 'h', 'a', 'p', 'p', ' y '}; 10 characters are assigned Dai c [0] to [9] 10 elements c.

   If the initial value of the number is less than the length of the array, then only those elements in the array of characters assigned Dai foregoing, the remaining elements automatically as the null character (i.e., '\ 0').
    The char c [12] = { ' I', '', 'a', 'm', '', 'h', 'a', 'p', 'p', 'y'}; is C [ 10], c [11] values are '\ 0'.

    C language, the arrays processed as character strings. When we use the general character array to store the string, must first determine a array large enough, and the actual and not take so much, and we only care about their significant bit to determine the actual length of the string, C defines a "string end flag ", with the character '\ 0'. If there is a string of 10 characters is '\ 0', then the significant character string is nine. In other words, in the face of the character '\ 0', indicating the end of the string, the string of characters that precedes it.
    The system for automatically adding a string constant '\ 0' as a terminator.

    For a statement: pirntf ( "? How do you do \ n"); in fact, when the string is stored in memory, the system automatically at the end of a character '\ 0' followed by the addition of a '\ 0' as the end of the string mark, in the implementation of the printf function, every time the output of a character check, look at a character is '\ 0'. In case of '\ 0' to stop output.

   String constants can be used to initialize an array of characters. Such as:

    char c[]={"I am happy"};

    It can also be directly written as:

   char c [] = "I am happy"; not aware of the length of the array 10 at this time, but 11, because the system automatically adds a '\ 0' terminator.

    Character array is not required for the last character '\ 0', and a method for processing character strings and arrays coincide, to facilitate determination of the actual length of the string, and can facilitate handling in the program, often at the end of the character array plus a '\ 0'.

<3> a character array of input and output 

    (1) input and output character by character. "% C" input or output a character with the character format.

    (2) the entire input or output a character string. With "% S" character format, meaning that the output string (String). E.g:

  char c[]={"china"};
  printf("%S", c);

If a character array contains more than one '\ 0', the first case of a '\ 0' output ends.

    (3) If the length of the input string longer than the defined character array, the array index will cause bounds, but this system is not being given.

    (4)% S with the input character format, in case the space, Tab and Enter input ends automatically.

When the input, scanf encounters a space is considered the end of a string. Test as follows:

#include <stdio.h>
int main()
{
    char str1[5], str2[5], str3[5];
    scanf("%s %s %s", str1, str2, str3);
    printf("%s %s %s\n", str1, str2, str3);
    return 0;
}

Operating results:
================================
How are you↙
How are you
======== ========================

#include <stdio.h>
int main()
{
    char str[13];
    scanf("%s", str);
    printf("%s\n", str);
    return 0;
}

运行结果:
================================
how are you?↙
how
================================

由第二个程序可知,实际并未将这12个字符加上'\0'送到数组str中,而是将空格前的字符"how"送到str中,将其视为字符串处理,在其后加上'\0' 。

Guess you like

Origin www.cnblogs.com/ggzhangxiaochao/p/11238491.html