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

What are the data types and variable constants in C language?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article, "what are the C language data types and variable constants?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language data types and variable constants" article.

First of all, let's list the basic data types of the C language (the unmentioned ones will be explained in the keyword topic later):

Bytes occupied by type name char (character)% c2short (short shaping)% d2int (shaping)% d4long int (long shaping)% ld

four

Long long int (long shaping)% lld8float (single precision floating point)% f4double (double precision floating point)% lf8

Character type: used to store characters, such as:'s characters

Integer type: used to store integers (unsigned and signed), such as 899pmmer 56

Floating-point type: used to store decimals, such as 10.0cent89.5

Why create so many data types? Because the computer's memory is limited, it is necessary to allocate the memory as reasonably as possible to avoid unnecessary waste of space.

The sizeof function is introduced here to measure the size of bytes occupied by a data type, as shown in the following use cases. Because the byte size is a very small integer, I chose to use a short integer to output. If you are interested, you can knock it yourself. Programming learning is inseparable from practice.

After a brief introduction to the data types of the C language, let's talk about constants and variables. Variables are very simple to understand, and the amount that can be changed is variables. Let's explain the scope and life cycle of variables.

1. Scope of the variable:

It refers to the scope of the function of this variable. According to the scope of the variable, the variable can be divided into global variable and local variable.

# includeint aquired 5; / / created a global variable outside the main function and initialized int main () {int bounded 6 bank / declared a local variable and initialized return 0;}

Here, the scope of variable an is the whole program, and the scope of variable b is only inside the main function. If you write another function, if you want to reference b in it, you will report an error, because it is beyond the scope of b, and you can reference an at will, as long as it is done in the right way.

two。 The life cycle of the variable:

Refers to the cycle of a variable from being created to being destroyed, and the variable can be used during the lifetime of the variable.

The life cycle of the global variable is the whole program, while the life cycle of the local variable is only within the function of its function, which is related to the location where the variable is stored in memory. Global variables are stored in the static area, while local variables are stored in the stack area.

There are four kinds of constants in C language:

1. Literal constant, such as: 100

This constant is usually used to assign values to other operands, such as int axiom 999999; char name='p'

Identifier constants defined by 2.define macros

For example: # define PI 3.1415926

It belongs to the header file in front of the whole program. If the user define a constant PI, the value of PI in the subsequent whole program is 3.1415926, which cannot be modified, and the compiler will report an error if forced modification.

Constant variables modified by 3.const

Such as: const int aura 100

It is worth mentioning that although an is a variable, it is given a constant attribute by const and cannot be modified.

4. Enumeration constant

Enum, which is also one of the keywords in C language, is defined as follows

Enum sex {male, famale}; above is the content of this article on "what are the data types and variable constants in C language?" I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Development

Wechat

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

12
Report