C Primer Plus (4)

Source: Internet
Author: User

4.2 string Introduction

A string is a sequence of one or more characters, such as "Zing went", but double quotation marks are not part of a string.

4.2.1char array type and NULL Character
C does not define a special variable type for a string, but stores it in a char array. The characters in the string exist in adjacent storage units. Each character occupies one unit, an array consists of adjacent storage units.
The character \ 0 is displayed at the last position of the array, which is an empty character. C uses it to mark the end of the character. The existence of this character means that the number of elements in the array must be at least one more than the number of characters to be stored.

4.2.2 use strings
% S tells printf to print a string. You do not need to insert null characters into the name array. scanf () will complete reading the input.
After scanf () starts reading the input, it will stop reading at the first blank character space, tab, or line break.
'X' is different from "x". The former is a basic type, and the latter is a derived type.

4.2.3strlen () function
Sizeof provides the data size in bytes. The strlen () function provides the length of a string in characters, excluding null characters.
Char name [40], then sizeof name = 40. Char name [] = "Mo" sizeof name = 3;

 

4.3 constants and C preprocessors

# Define NAME value will replace NAME with value during compilation, which is called the method of compiling. The semicolon is not used because it is an alternative mechanism, not a C statement.

4.3.1const Modifier
C90: Use the const keyword to convert a variable declaration to a constant declaration. Example: const int MONTHS = 12;

4.3.2 obvious constants defined by the System
The C header file limits. h and float. h provide detailed information about the size limits of integer and floating point types. Such as INT_MAX and INT_MIN.

 

4.4 research and utilization of printf ()

4.4.1printf () function
The command for requesting printf () to print a variable depends on the type of the variable. Using the % c % d Conversion instructions, they specify how to convert data into a printable form.
%: Print a percent sign. % G automatically selects % f or % e based on different values.
Printf () uses a value, whether it is a variable, constant, or expression. 4.4.2printf () Conversion Modifier
-: Left alignment
+: If the signed value is positive, the plus sign is displayed. If the value is negative, the minus sign is displayed.
Space: If the signed value is positive, the leading space is displayed. If the value is negative, the minus sign is used.
#: Use an optional Form of conversion instructions. For all floating-point forms, a decimal character is printed even if it is not followed by any number.
0: For All numeric formats, use leading zero instead of spaces to fill the field width.
Example

1 fA 1F 0x1f
** 42 ** 42 **-42 **
** 6 ** 006 ** 00006 ** 006 *
/Authe/

4.4.3printf () Return Value
It returns the number of printed characters, but does not include null characters. If an output error occurs, printf () returns a negative number.
4.4.4 print long strings
If a string enclosed by double quotation marks is followed by another string enclosed by double quotation marks, and the two are separated by only blank characters, C treats the combination as a string.
For example, "young" "lovers" is changed to "younglovers ".

 

4.5 research and utilization of scanf ()

If you use scanf () to read the value of a basic variable type discussed earlier, add an & before the variable name &.
If you use scanf () to read a string into a character array, do not use &.

4.5.1scanf () format characters

 

4.6 * modifier of printf () and scanf ()

In printf (), if you do not want to specify the field width in advance, but want the program to specify this value, you can use * to replace the number in the field width section, however, you must also use a parameter to tell the function field width.
For example, printf ("Weight = % *. * f \ n", width, precision, weight );

In scanf (), when * is placed between % and the description letter, it causes the function to skip the corresponding Input item.
For example, scanf ("% * d % d", & n); // skip two integers and copy the third integer to n.

 

Related Article

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.