C Language Learning notes-6. Arrays

Source: Internet
Author: User

Definition and use of one or one-D arrays

  In the 1.C language, the subscript of an array element is starting from 0

2. When defining an array, you need to set its length, which must be an integer constant

Note: Constants take advantage of macro definitions: replacing Long data with short identifiers facilitates program modifications and upgrades replace hard-to-remember data with easy-to-understand identifiers

Format: #define PI 3.14 (no semicolon)

3. If you assign an initial value to all array elements, you may not specify the length of the array

When initializing, if the number of initial values is less than the number of elements, the remaining auto-complement 0

Two, character array and string

  1. Strings are usually stored in a character array, with each element holding one character and ending with '/'

2. String initialization: char str[] = "Hello";

Error initialization: Char str[20];

STR = "Hello"; String constants are read-only

Functions in the 3.<string.h>:

(1) Puts function: output a string

(2) Gets function: Enter a string and save it in a character array

Difference from scanf: scanf input until a white space character is entered until the line break is encountered

Note: The empty buffer statement should be added between two inputs: Fflush (stdin);

(3) Strcat function: String connection prototype: strcat (Dest, SRC);

(4) strcpy function: String copy prototype: strcpy (Dest, SRC);

(5) strcmp function: string comparison str1 <str2 returns a negative integer

STR1 = = str2 returns 0 comparison mode: From left to right, in turn, compare ASCII

STR1 > STR2 Returns a positive integer

(6) Strlen function: Calculates the length of a string (excluding '% ')

4.SCANF input string should be careful not to write &

scanf ("%s", str); The character array name itself is already an address

5.SCANF Supplemental Instructions:
(1)%[^ ...] Indicates a read to the end of a particular content encounter

(2)%[...] Indicates that only specific content is read

Example: scanf ("%[^\n]", str); Enter Hello world! Enter Str:hello world!

scanf ("%s", str); Enter Hello world! Enter Str:hello

scanf ("%[12345678]", str) input 1234hello5678 return str:1234

(3) The control character "%-4d" means that the output is left aligned

C Language Learning notes-6. 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.