Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
A sample program
#include <stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum\n");
printf("Let`s check it out.\n");
printf("Pleasee enter your weight in pounds:\n");
/* Get user input */
scanf("%f",&weight);
value = 1700.0 * weight * 14.55833;
printf("Your weight in platinum is worth $%.2f.\n",value);
printf("You are easily worth that! If platinum prices drop,\n");
printf ( "eat more to maintain your value. \n");
return 0; } Are you worth your weight in platinum Let's check it out. Pleasee enter your weight in pounds:123
Your weight in platinum is worth $3044146.75.
You are easily worth that! If platinum prices drop,
eat more to maintain your value.
Copy the code
An avoirdupois ounce is an ounce, a Troy ounce, a unit of measurement used in the gold market in the United States and Europe. The quoted price on the international gold market, its unit “ounce” refers to the gold ounce. The avoirdupois ounce is a unit of measurement in the British system, also known as a unit of weight. 1 Troy ounce =28.350 grams, 1 Troy ounce =31.104 grams, 16 Troy ounce =l pound. The idea is to convert pounds to Troy ounces, which is 28.350÷31.104×16=14.5833. –
1. Procedural details
To print new types of variables, use %f in printf() to handle floating point values. The.2 in %.2 is used for precise control, specifying that only two decimal places are displayed for output floating point numbers
%f indicates that scanf() reads the floating-point number typed by user. &weight tells scanf() to assign the input value to the weight variable. Scanf () uses & to find the location of the weight variable. More on the role of & below
Variable and constant data
1. The constant
Constants are data types that are defined before a program runs and do not change during execution
2. The variable
Other data types that may be changed and assigned during the running of a program are called variables
Data type keywords
Bits, bytes, words, and word length
Did not distinguish for a long time, after reading this book, I can
Of course, it is not clear to the end, just like learning to drive a car does not need to master the principle of the same
First, bits, bytes, and words are all terms for computer data units or storage units
A bit is the smallest unit of computer storage, either 0 or 1
A Byte is a common unit of storage used by computers. One Byte is equal to eight bits.
The number of bits contained in each word of a computer is called the word length, which refers to the number of binary digits a computer can process at one time. The larger the word length, the faster the computer can process it
A word is a computer’s natural unit of storage. For an 8-bit computer, a word is only 8 bits long
Basic data types of C language
1. The int type
So numbers, integers, ints have to be integers
Declaration creates and marks storage space for variables and specifies initial values
2. Other integer types
Short int occupies less storage space than int and is often used to save space for small values
Long int is good for large values
Long Longs take up more storage space than longs and are suitable for larger applications
Unsigned int or unsigned is used for non-negative values
Signed emphasizes the intent of using the type of the sign
Example:
#include <stdio.h>
int main(void)
{
long int estine;
long johns;
short int erns;
short ribs;
unsigned int s_count;
unsigned players;
long long age;
return 0;
}
Copy the code
There are so many int types, how to choose? First, consider the unsigned type. This type of number is often used in counting because negative numbers are not used. Also, unsigned types can represent larger positive numbers.
Long constants and long long constants
Typically, numbers used in program code (for example, 2345) are stored as ints. If you use a large number like 1000000 that is beyond the scope of an int, the compiler treats it as a long int (assuming it can represent the number). Such as
The integer overflow
What happens if the integer is out of range for the corresponding type?
This condition is called integer overflow
3. Use the char type
Char is used to hold characters, such as letters or punctuation marks.
The char type is used to store characters (such as letters or punctuation marks), but technically char is an integer type. Because char actually stores integers, not characters. Computers use numerical coding to process characters, meaning that specific characters are represented by specific integers.
3.1 ASCII code?
How is real life data represented in a computer
Computers use binary representation for all data and operations
ASCII coding is a rule that expresses the information of life in binary
3.2 Escape Characters
4. _Bool type
Used to represent Boolean values, the logical values true and false
5. Float, double and long double
Flaot and double are used to represent decimals, and double is more accurate than float
5.1 Overflows sum of floating point values
An overflow is an overflow that exceeds the range that the current type can express. When an overflow occurs, the operation cannot be continued and an interrupt is usually required.
An underflow is less than the minimum value range and is called an underflow. In an underflow, the floating-point bits are generally forced to zero and the machine can continue operations.
5. Key concepts
The C language provides a large number of numeric types for the convenience of programmers. Taking the integer type as an example, C considers that one integer type is not enough and provides signed, unsigned, and different sizes of integers to meet the needs of different programs.
Computers encode numerically in memory to represent characters. The most common is ASCII encoding
Basic data types fall into two broad categories: integer and floating point
Java is a base data type and a reference data type
6. Programming exercises
[Img-x9qll0IT-1633227806357] (E:\Typora\Image\image-20211003001816163.png)
C Primer Plus 6th Edition (英 文版) chapter 3 (perfect revision