C Primer Plus Reading notes the fourth chapter

Source: Internet
Author: User

The title of this chapter is the formatted input/output of the string, with emphasis on input and output.

The first example code in this chapter is similar to the previous example code, and the new feature is the use of an array to hold the string, the C preprocessing command, and the strlen () function.

These concepts are described in detail below.

1. String

The C language does not define a specific variable type for a string, but is stored in a char array.

Attention:

1.SCANF () reads a string and stops reading at the first blank string, tab, or newline character. So using the%s scanf () will only read one word instead of the entire sentence (c is typically used by get () to handle a generic string).

2.strlen () and sizeof () differences. sizeof is an operator, not a function. The () behind it is not necessary, and it does not have to be written without a disagreement. The sizeof operator calculates the amount of space the system allocates for the variable or type name that follows it, regardless of whether it stores useful data. Strlen () is a library function, () must be a string pointer or a string constant, which returns the number of characters before the first ' \ ' in the detected object, without ' s '.

2.C Preprocessor

Usage: #define TaxRate 0.015. At the time of compiling the program, 0.015 will replace all TaxRate, which is known as the compile time entry method. Constants defined in this way are often referred to as apparent constants. In general, it is customary to capitalize the constant name.

C90 adds a new way to create symbolic constants: Using the const keyword. For example, const int MONTHS = 12; The C language can also create symbolic constants through the enumeration function, which is discussed later.

The system also defines some obvious constants, such as the same line in the header file Limits.h and float.h: #define INT_MAX +32767.

3.printf () and scanf ()

1.printf. Format printf (control-string, item1, item2 ...); Where the control string includes the actual characters to be printed and the conversion description. To print% requires a percent. The conversion description modifiers for printf () are many and very powerful and flexible. The meaning of the conversion is that it converts the numeric values stored in the computer's binary format into a series of characters for easy display. The "transformations" here can be understood as "translations".

The 4.4.4 section focuses on mismatched conversions, which involve a lot of computer knowledge at the bottom. In particular, parameter passing works: The computer places the value of the variable on the stack, but reads it according to the conversion descriptor.

In addition, the printf () return value is the number of characters that are printed.

2.SCANF (). SCANF () is the reverse operation of print (), which converts the input string into various forms: integers, floating-point numbers, characters, and strings. and printf () is converting these forms into text to be displayed on the screen. SCANF () uses a pointer to a variable. If you want to read a value of a basic variable type, add a &; if you read a string to a character array without & in the variable name. SCANF () returns the number of entries that were successfully read in.

  

This chapter is more informative, especially in detail. It is not necessary to memorize all of them, and to be familiar with them in future practice.

The programming exercises are similar to those in the previous chapter, and are both printf () and scanf () exercises.

To be Continued ...

C Primer Plus Reading notes the fourth chapter

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.