Basic data types for C languages

Source: Internet
Author: User

The C language contains data types as shown in the following:


2. Introduction to various data types

2.1 Integral type

Shaping includes short integer, Shaping, and long-shaping.

2.1.1 Short Shaping

Short a=1;

2.1.2 Plastic Surgery

Typically 4 bytes (32 bits), the highest bit represents the symbol, 0 is a positive number, 1 is negative, the value range is -2147483648~2147483647, in memory the order of storage is status before, high, for example, 0x12345678 in memory storage as follows:

Address: 0x0012ff78 0x0012ff79 0x0012ff7a 0x0012ff7b

Data: 78 56 34 12

Definition: Using the INT keyword, for example:

int a=6;

2.1.3 Long Plastic

Long a=10;

2.2 Floating-point type

Floating-point types include single-and double-precision types.

2.2.1 Single-precision type

Floating-point type, also known as the real type, is called the single precision. Typically takes 4 bytes (32 bits),

float a=4.5;

Address: 0x0012ff78 0x0012ff79 0x0012ff7a 0x0012ff7b

Data: 00 00 90 40

2.2.2 Double-precision type

Typically 8 bytes (64 bits)

Double a=4.5;

Address: 0x0012ff78 0x0012ff79 0x0012ff7a 0x0012ff7b 0x0012ff7c 0x0012ff7d 0x0012ff7e 0x0012ff7f

Data: 00 00 00 00 00 00 12 40

2.3 Character types

In a variety of different systems, the character type takes up one byte (8 bits). Defined as follows:

Char c= ' a ';

You can also assign a value to the ASCII code that corresponds to the character, as follows:

Char c=97;

3. Data type and "die"
The six keywords short, int, long, char, float, double represent the six basic data types in C language.

How to understand them? For example: Have you seen that thing with Lotus root? (Never seen?) Briquettes have always seen it.) That thing called the Lotus, holding it in the good coal heap so a click, a briquettes out. Radius 12cm,12 a hole. The size of the briquettes of different types of coal-mine is not the same, the number of holes is not the same. This lotus is actually a mold.

Now we associate, short, int, long, char, float, double these six things are not very similar to different types of lotus? Take them in the memory of a click, different sizes of memory allocated well, of course, don't forget to give them a nice name.

On a 32-bit system, the memory size of short clicks is 2 bytes;
int clicks out of memory size is 4 bytes;
The memory size of a long click is 4 bytes;
The memory size of float click is 4 bytes;
Double clicks out of the memory size is 8 byte;
The amount of memory that char clicks out is 1 bytes.
(Note this refers to the general situation, may be different platforms will vary, specific platform can be tested with the sizeof keyword)

Very simple, huh? Is it cool? is very simple, and indeed very good, but the problem is that you pull out so much memory block, you can not give him a name called x1,x2,x3,x4,x5 ... or 1th Changjiang River, No. 2nd Changjiang River ... it. They look like this (not your boss, Dick, old three ...), and after a while you'll forget which name matches which memory block (who married who?). ^_^). So it's absolutely important to give them a good name. Let's take a look at what kind of name is good.

4. Naming rules for variables
1) The name should be intuitive and can be spelt, it is expected to understand the text, easy to remember and read.
Identifiers are best used in English words or combinations, and pinyin is not allowed. The English words in the program are generally not too complicated, the words should be accurate.

2) The length of the designation should conform to the "Min-length && max-information" principle.
C is a concise language, and the name should be concise. For example, variable name maxval is more useful than maxvalueuntiloverflow. The length of the identifier is generally not too long, longer words can be shortened by removing the "vowel".

In addition, English words as far as possible not abbreviated, especially the use of professional nouns, if there are abbreviations, in the same system, the same word must use the same notation, and indicate its meaning.

3) When an identifier is made up of multiple words, the first letter of each word is capitalized and the rest is all lowercase. Like what:
int currentval;
The name looks clearer, much better than a long string of characters.

4) Try to avoid the number of numbers in the name, such as Value1,value2, unless it is logically necessary to number. For example, when driving the development of the PIN name, non-numbered names instead of bad.
Beginners always like to use a numbered variable name or function name, which looks simple and convenient, but is actually a time bomb. This habit must be changed by beginners.

5) Add a range qualifier to a global variable or function that is used in conjunction with multiple files (it is recommended to use the module name (abbreviated) as the range qualifier).
Naming rules for (gui_, etc) identifiers:

6) The identifier name is divided into two parts: the canonical identifier prefix (suffix) + meaning identification. Non-global variables can be prefixed without using a range qualifier.

7) scope prefix naming rules.

8) data type prefix naming rules.

9) meaning identify naming conventions, variable names using noun phrases, function names using verb phrases. For example:

Variable meaning identifier composition: target word + verb (past participle) + [adverbial] + [destination];

function meaning identifier composition: verb (general present) + target word +[adverbial]+[destination];

10) The program must not appear in the case of a similar identifier only by case-sensitive. For example:
int x, x; Variable x is easily confused with X
void foo (int x); function Foo is easily confused with Foo
void FOO (float x);
Here's another thing to pay special attention to is the difference between 1 (number 1) and L (Small Letter L), 0 (number 0), and O (lowercase letter O). The two pairs are really hard to tell, and one of my colleagues has been tossed about by this question once.

11) A function name is prohibited from being used in other places. For example:
#include "C_standards.h"
void foo (int p_1)
{
int x = p_1;
}
void static_p (void)
{
int foo = 1u;
}

12) All macro definitions, enumeration constants, read-only variables are named in uppercase letters, and the words are split with underscores. For example:
const int max_length = 100; This is not a constant, but a read-only variable, please look backwards
#define FILE_PATH "/usr/tmp"

13) Considering the habitual problem, the local variables can be used in the general naming method, only N, I, J and so on as the cyclic variable.
Be sure not to write code like this:
int p;
char i;
int C;
char * A;
Generally speaking, n,m,i,j,k are used to denote variables of type int, such as C,ch, which represent character type variables; Of course, this is only a general habit, except i,j,k and so on can be used to represent the loop variable, the other character variable name try not to use.

14) Define variables and never forget to initialize them. When you define a variable, the compiler does not necessarily empty the memory, and its value may be invalid data. This is a very detailed discussion in the memory management chapter, please see.

15) The operation of different types of data should pay attention to the problem of precision expansion, the general low-precision data will be extended to high-precision data.

Basic data types for C languages

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.