C Language IV: one-dimensional arrays, character arrays

Source: Internet
Author: User

Array:

Definition: Data type array name [number of array elements] = {value 1, value 2,....}

Essence: Quickly define multiple variables of the same data type

The disadvantage of arrays: variables that are defined by the same data type.

Such as:

int c[5] = {0};//The number of elements of the given array, you can follow the pattern, all assigned the initial value is zero

int a[] = {4, 5, 6};//in the absence of the given array element number, you can follow the pattern, at this time, be sure to write clear all the initial value.

Access to array elements:

By the array name and subscript to access such as: a[1], cut down the mark from the beginning of the zero.

Note : 1, The subscript of the last element of the array is : number of array elements -1;

2, C language, does not detect array subscript out of bounds.

Array traversal: The process of taking the elements of an array one by one, followed by an array subscript.

Rule: array[i], if there is no assignment symbol "=" on the right, then it is a procedure to take the value in the array; If the right side has an assignment symbol "=", then it is a process of re-assigning a value to an element in the array.

The string is an array, but the character array is not necessarily a string.

%s starts output from the first character of the string until it encounters A/s end output

String manipulation functions:

1. function for calculating the length of the string: strlen (), and the length of the string to be seen by the naked eye;

2. String copy function: strcpy (char *,const char *), copy the latter content to the previous string;

Note: The latter string length cannot exceed the previous string length

3. String concatenation function: strcat (char *,const char *), stitching the latter string behind the previous string (starting with the position of the former string);

4. String comparison function: strcmp (const char *,const char *), compares the previous string and the latter string one after the other until the end of a character that is not equal, and returns the difference between the previous string and the next string unequal character ( The characters in the previous string minus the characters in the latter string)

C Language IV: one-dimensional arrays, character arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.