C Language Learning Diary 6

Source: Internet
Author: User
Tags float double

1. Data type

1.1 Basic data types

Data types are divided into 2 classes: basic data type + composite type

Base type: char short int long float double

Composite type: Array struct body common body class (C + +)

1.1.1 Memory consumption with sizeof operator

The data type is like a mold, which instantiates a C-language variable. Variables are stored in memory and need to occupy a certain amount of memory space. How much space a variable occupies is determined by the data type of the variable.

For each data type, memory consumption is different on different machine platforms. We generally talk about the 32-bit CPU as the default hardware platform to describe:

Char 1 byte short 2 int 4 long 8 float 4 Double 8

1.1.2 Signed number and unsigned number

For the number of integer types, such as char short int long, the number of symbols and unsigned numbers is divided. For floating-point types such as float and double, there is only a signed number and no unsigned number.

1.1.3 The difference between the integer number and the floating-point number storage mode

For the C language, the number (variable) is stored in a grid of memory. Storage is stored in binary mode, for the signed number and unsigned number, the storage method is different. For example, for int

unsigned int unsigned number, 32 bits are all used to save the contents of the number, so the range of the number represented is 0-4294967295 (2^32-1)

int has a signed number, the highest level in 32 bits is used to store the symbol (0 for positive, 1 for negative), and the remaining 31 bits to store the data. So can represent the range -2^31 ~ 2^31-1

Conclusion: From an absolute value, the range represented by the unsigned number is a little larger.

For floating-point types such as float and double, it is stored in memory differently than the number of integers. so float and int, though all 4 bytes, are completely different in the way they are stored in memory. So the same 4 bytes of memory, if the storage is in accordance with the int storage, take the time must be in accordance with the int type to fetch.

Summary: There are two main types of access, one is the integer type is a floating point type, the two access methods are completely different, there is no association, so it is absolutely impossible to change a variable access mode. In integer and floating-point type, for example, 4 integer char, short, int, long is only the range of different size, storage is exactly the same. The float and double storage principles are the same and differ in way, resulting in different ranges and accuracies of floating-point types that can be represented.

1.2 Null type (keyword void)

    The type of void in the C language, which represents any type, rather than an empty meaning. The meaning of any type is not to be who you want to become, but to say that its type is unknown and not yet specified.

void * is a pointer of type void. This is a pointer variable that points to a number of type void. The number of void types means that the number may be int, it could be a float, it could be a struct, that type is possible, but I don't know it at the moment.

The function of the void pointer is that the program does not know the type of the variable, but the programmer knows it in his own mind. How do programmers know? What type is it when assigning a value to this variable, and what type is it when it is taken? These types are correct, compatible, and fully owned by the programmer. The compiler can see void and there's no way to help you type check.

In the function's argument list and return value, void represents the meaning that a function parameter list is void, indicating that the function call does not need to be passed to it. The return value type is void, indicating that the function does not return a meaningful return value. So the caller is not going to want to use the return value.

C Language Design basic concept:

C language believes programmers are always right, C language Trust programmers are masters, C language gives programmers the greatest rights. So C programmers must be responsible for the right and wrong of the program, must always be clear-headed, know what they are doing.

#include <stdio.h>

int main (void)
{
int a = 444;
void *pvoid;
pvoid=&a;

printf ("*pvoid=%d.\n*pvoid=%f.\n", * (int *) pvoid,* (float *) pvoid);
In this case, because the compiler doesn't know what type of pointer pvoid is, we force him to use the int * type when we use it.
The second mandatory float *, the compiler will not error, he did not know, but it is wrong to take out.
return 0;
}

1.3 Conversion of data types

There are various types of data in the C language, and you need to define variables of various types when writing programs, which need to be involved in the operation. C language has a basic requirement is: Different types of variables can not be directly calculated. That is, a variable of type int and float cannot be subtraction directly, and the two types must be made special to the same type first.

1.3.1-Implicit conversion

Is the automatic conversion, is the C language by default, without the programmer to interfere.

C Language Concept: Implicit type conversions are converted by default toward higher precision and a larger range of directions.

1.3.2 Cast

The C language does not do this by default, but programmers I want to do this.

1.4 C language and bool type (only two values 0, 1, is true and false)

There is no bool,c++ in the native type of C language. In the C language, if you need to use the bool type, you can use an int instead.

2. Variables and Constants

2.1. Variables

2.1.1, local variables

Variables defined inside a function

2.1.1.1, normal local variable auto

2.1.1.2, static local variable static

2.1.1.3, register local variable register

Usually only in the kernel or in the startup code, you need to use the same variable repeatedly in this case will be used

2.1.2, global variables

Variables defined outside the function

2.1.2.1, general global variables

2.1.2.2, static global variables (shielding effect, single source file alone)

2.1.2.3, cross-file referencing global variables extern

Contrast:

1, the definition is not initialized at the same time, the value of the local variable is random, and the value of the global variable defaults to 0.

2, the scope of use: The global variable has a file scope (file scope, after the definition of the position is valid), and local variables only the code block scope. A block of code is a piece of code that is expanded with {}.

3, the Variable allocation location: The global variable is allocated on the data segment, and the local variable is allocated on the stack.

Data segment: Data Gencun is a number, like a global variable is the existence of data segments.

Code snippet: The program code is stored, and is generally read-only.

Stackstack: Advanced back out. Local variables in the C language are allocated in the stack. When the program runs, the local variables are put into the stack, the local variables and the address in the stack are the birth of local variables, out of the code block, the variable address is released, the variable is dead.

Insert a section of Linux gcc for use.

Multi-file C language Project

1, simple C language program only a C file a.c, compile when GCC a.c-o a

2, the complex C language program is composed of multiple C files. For example, a program has 2 C files, when compiled by GCC A.C B.c-o AB

C Language Learning Diary 6

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.