Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use basic data types, variables and constants in C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use basic data types, variables and constants in C++". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to use basic data types, variables and constants in C++" can help you solve the problem.

Variable (variable)-- three elements: variable name-- used in the program, variable type-- opens up memory space size, variable value-- the actual data stored.

Variable definition

Definition syntax: type name variable name = variable value

Int c = 10

Open up memory space for variables, variable declaration will not open up memory space.

Variable declaration

Syntax-- int c; variable definition with no variable value, called declaration, extern int c; add the extern keyword.

Characteristics

If you want to use a variable, you must have a definition. Before using a variable, the compiler must see the variable definition. If not, the compiler will automatically look for a variable declaration and promote it to a definition.

If the extern keyword is added before the variable is declared, it cannot be promoted.

# include

Int main (void)

{

Extern int a; / / variable a declaration

A = 200 position / use variable, which cannot be defined at this time, and the compiler automatically promotes the above declaration to the definition. / / add extern, error reported, a cannot be found

/ / int a = 30; / / variable a definition (redefine variable)

Printf ("a =% d\ n", a)

Return 0

}

Constant (constant): an integer that will not be modified

"hello", "A", 30,-67, 3.67534 (floating point constant)

# define PI 3.1415 (macro definition) syntax: # define macro name, macro value.

Const int a = 5; definition syntax, const type name variable name = variable value, const keyword, the variable modified by this keyword is represented as a read-only variable.

# include

# define PI 3.14

Int main (void)

{

/ / area of the circle: s = PI * r * r

/ / perimeter of the circle: l = 2 * PI * r

Int r = 20

Float s = PI * r * r; / / expression as the value of a variable

Float l = 2 * PI * r

/ / printf ("area of circle:% f\ n", s); / / area of circle: 1256.000000 displays 6 decimal places by default

/ / printf ("perimeter of circle:% f\ n", l); / / perimeter of circle: 125.599998

/ / printf ("area of circle:% .2f\ n", s); / / area of circle: 1256.00

/ / printf ("perimeter of the circle:% .2f\ n", l); / / perimeter of the circle: 125.60 specifies two places after the decimal point, rounded to the third place

Float b = 20.75434

Printf ("b =% 6.2f\ n", b); / / displays a total of 6 digits, including the decimal point, keeping two places after the decimal point, rounding the third place, and filling in less than six places with spaces

Printf ("b = .2f\ n", b); / / displays a total of 6 digits, including the decimal point, keeping two places after the decimal point, rounding the third place, and making up for less than six places with 0

Return 0

}

Identifier identifier: general terms for variables and constants

Identifiers cannot be keywords. Function names: system, printf, int, main, return

It can only be composed of letters, numbers, and underscores: Amurz, Amurz, 0-9, underscore _

Cannot start with a number: int a4ir = 67; (ok), int _ 34d = 43; (ok), float 65ty_7 = 54; (error)

Strict case distinction:

We use uppercase to define constants: # define MAX 100

Variables are usually defined in lowercase

Naming convention

Big hump method: multiple words form a variable name, with the first letter of each word capitalized

Small hump method: multiple words form variable names, the first letter of each word is lowercase, and the first letter of each other word is capitalized.

Lowercase + underscore: for C language only

Sizeof keyword: sizeof is not a function

How to use it:

Sizeof variable name-returns the size of the variable, in whole number sections

Sizeof type name-returns the size of the data type, in whole number sections.

Integer type

Signed integer

-get the minimum and maximum values of the data type. You can use # include

Unsigned integer

This is the end of the introduction to "how to use basic data types, variables and constants in C++". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report