C Language Study Notes (2)-data types, constants, and variables

Source: Internet
Author: User
Tags constant definition

 

1. Common Data Types

 

Int, short, long, unsigned, float, double, char, enum, void, pointer type, array type, struct, union, etc.

 

Basic data types: Int, short, long, unsigned, float, double, char, and enum are all basic data types. The feature is that the value cannot be further divided.

 

 

 

2 constants.

 

Constant definition method: # define identifier constant

 

Constants are classified into direct constants and symbolic constants.

 

Example: Calculate the circumference and area of any circle, and use constants to define π.

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Float r, l, area;

Scanf ("% f", & r );

L = 2 * PI * r;

Area = PI * r;

Printf ("\ n l = % f area = % f \ n", l, area );

}

 

Note: constant values cannot be modified. Constant names must be written in upper case and variable names must be in lower case;

 

Integer constant (integer ):

 

1. Decimal integer constant: 0 ~ 9 digits, no prefix, and so on, that is, an integer constant. Example: 237,-783, 65535;

 

2. normal number of octal nodes: The value ranges from 0 to 0 ~ 7. octal is generally an unsigned number. Example: 023;

 

3. hexadecimal integer constant: Prefix: 0X or 0x, from 0 ~ 9, ~ F or ~ F (Case Insensitive), for example

For example, 0X23A, 0XAA, and 0xFFF0;

 

4. long integer constant: use L or lowercase l as the suffix, such as 123L (decimal 123), 012l (octal 10), 0x15L (hexadecimal

Format 21 ).

 

5. The suffix of the unsigned number indicates that the unsigned type of the integer constant is indicated by the suffix U or U, for example, 58u. Different types of prefixes and suffixes can be used

Number. For example, 0xA5Lu indicates the hexadecimal unsigned long integer A5.

 

Real constants (floating point type ):

 

1. Decimal decimal form: from 0 ~ 9 and decimal point, such as 0.0, 0.34, 4.23.

 

2. exponential form: it consists of a base in decimal format, an increment sign e or E, and an increment Code, for example, 1.2E5 (2.1*10 ^ 5 ).

 

Character constants: character constants are stored in the memory in the form of ASCII code. character constants can represent all characters. character constants include normal character constants and escape characters.

 

1. Normal character constant: a character enclosed by ''single quotes, for example, 'A', '= ','? .

 

Note: character constants can only be single quotes, but cannot be double quotation marks. They can only be a single character and cannot be multiple characters. characters cannot be used in numerical operations.

 

2. Escape Character: The Escape Character starts with '\' and has a specific meaning. For example, '\ n' is a carriage return line break, 't' is a tabulation tab, and' \ 'is an escape character.

Backslash.

 

String constant: A String constant is a sequence composed of several characters enclosed by "", such as "hello" and "sdk SDS. Each character of a String constant occupies one byte. The design requires that the String constant end store an Terminator '\ 0'. If no Terminator exists, the program does not know when to stop the output. Escape characters must also be involved in strings. The C language does not have a response string variable, but you can use a single character value to store a string.

 

Typical question: "A" and 'A' in memory?

 

'A' occupies only one byte, and "A" occupies two bytes, because the string end contains an ending character and occupies one byte.

 

 

 

3.

 

Variable definition method: data type variable name 1, variable name 2, variable name 3 ,...;

 

Variables can also be classified into integer variables, real variables, and complex variables.

 

Usage specification of variables:

1. Definitions must be made before use;

 

2. Notes for variable assignment:

 

Several variables such as int I, j, and k can be defined simultaneously;

 

Int I = 3, j = 4, k = 8; cannot be written as int I = j = k = 5;

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.