当前位置:网站首页>[C language] keyword supplement

[C language] keyword supplement

2022-06-27 04:26:00 Ordinary people 1

author :@ Ordinary person 1

special column :《C Language from 0 To 1》

In a word : In the past , All is prologue

explain : The past is irreparable , The future can change


Thank you for your praise and attention , At the same time, you are welcome to visit my Ordinary house


front , Through this blog keyword The keywords we learned earlier are supplemented and expanded , meanwhile , Knowing and learning from us 3 Less commonly used keywords , Old rules , Now? , Through this new blog —— We still make some supplementary expansion to the relevant content of the keyword , At the same time, some keywords are simply analyzed .

Don't talk much , Go straight into The theme

Basic data type

We are no stranger to defining variables , I started to deal with them from the very beginning , So let's say Some other content to make it easier to understand types .

image-20220625223439330

How do we look at data types

Define the nature of variables : Open up a space in memory , To hold data .

And define a variable , Is the type of , This is determined by the basic grammar . that , The type determines : The size of the variable space .

#include <stdio.h> 
#include <windows.h> 
int main() 
{
     
    printf("%d\n", sizeof(char)); //1 
    printf("%d\n", sizeof(short)); //2 
    printf("%d\n", sizeof(int)); //4 
    printf("%d\n", sizeof(long)); //4 
    printf("%d\n", sizeof(long long)); //8 
    printf("%d\n", sizeof(float)); //4 
    printf("%d\n", sizeof(double)); //8 
    system("pause");
    return 0; 
}


// Why according to the type , Open up a space , Isn't it good to use the whole memory directly ?  Not good. . 

Any time , None of your programs are running , There are many other programs running . You used the whole piece , Let others do

in addition , You used it all , It must be at any time , Is it all used up? Not for the time being , But here you are , For computers , It's waste .

Here comes the question , I use some memory , How much is decided by what is actually decided by your scene , Your computing scenario , Determines what class you use Type of variable .

The type you use , Determines how many bytes of space you open up .

therefore ,C In language , Why are there so many types ? To meet different computing scenarios . This is actually equivalent to a “ Mold ” 了

Okay , About the above sizeof() It's also a keyword , Now let's briefly introduce it

The most wronged keyword ——sizeof

It has been mistaken for a function for many years

actually ,sizeof Is an operator, not a function , This is what we mentioned before , Now again

as for sizeof I won't explain its use here , about sizeof, We are familiar with , This is our old friend !

signed、unsigned keyword

signed—— A signed

unsigned—— Unsigned

Signed integers vs Unsigned integer

char

  unsigned char 

  signed char 

short

  unsigned short [int] 

  signed short [int] 

int

  unsigned int 

  signed int 

long

  unsigned long [int] 

  signed long [int]

As mentioned before, the creation of a variable is to open up space in memory , The size of space is determined according to different types .

that , How is the data stored in the opened memory ? We said this in the data storage , Now simply recall :

  • Signed number

We know , Compiler is a Allocate four bytes of space . So how to store it ?

First , For signed Numbers , Be sure to indicate whether the data is positive or negative . So we usually use the highest bit as the symbol bit .

Original code 、 Inverse code 、 Complement code

There are three representations of signed numbers in computers , The original code 、 Inverse and complement .

The three representations have two parts: sign bit and numeric bit , The sign bits are all used 0 Express “ just ”, use 1 Express “ negative ”, The three representations of numerical bits are different Same as .

If a data is negative , Then we should follow the following rules for transformation :

Original code : Directly translate binary into binary in the form of positive and negative numbers .

Inverse code : Change the sign bit of the original code , The other bits can be inverted in turn .

Complement code : Inverse code +1 You get the complement .

If a number is positive , Then its original and inverse complements are the same

  • An unsigned number

    There is no need to transform , No sign bits are required , The former is the same as the latter

    For plastic surgery : Data stored in memory is actually stored in the complement

int a = 20; //20 It's a positive integer.  
//0000 0000 0000 0000 0000 0000 0001 0100 
int b = -10; //-10 It's a negative integer  
//1000 0000 0000 0000 0000 0000 0000 1010 
//1111 1111 1111 1111 1111 1111 1111 0101 
//1111 1111 1111 1111 1111 1111 1111 0110 
// Complement code to source code  
 Method 1 : First -1, The sign bit remains unchanged , According to the not . 
 Method 2 : Go through the process from original code to complement code .

About binary, there is a problem that everyone has a headache, that is : How to quickly convert decimal binary

Here's a simple way :1 Followed by n individual 0, Namely 2 Of n Power

The large and small ends supplement

The problem of the size end and its calculation method were also mentioned before , Here is just a simple supplement :

CPU The basic unit of memory access is bytes

Data in bytes , There are high weight bits and low weight bits . Memory has a high address in bytes , Other low addresses .

formula : Small

Deeply understand the storage and retrieval of variable content

signed int b = -10;
unsigned int d = -10; //( Is it legal ?) 

// Conclusion : 
// save : Literal data must first be converted into complement , Put it in the space . therefore , The so-called sign bit , It depends entirely on whether the data itself carries +- Number . And whether the variable is signed   irrelevant ! 
// take : To get data, you must first look at the type of the variable itself , Then decide whether to look at the highest sign bit . If you don't need to , Direct binary to decimal . If you need   want , Then it needs to be converted to the original code , Then you can identify .( Of course , Where is the highest sign bit , Also specify the size end )

Why is it all complement

In computer system , All values are represented and stored by complements . The reason lies in , Use complement , Symbol bits and value fields can be treated in a unified way ; meanwhile , Addition and subtraction can also be handled in a unified way (CPU Only adders ). Besides , Complement code and original code are converted to each other , Its operation process is the same , No need for additional hardware circuits .

The range of shaping values is supplemented

Simplicity , We use char For example

unsigned char: [0,2^8-1]

signed char : [-2^7, 2^7-1] //char Equivalent

Why? ? Specific data types , The range of data values that can be represented ( The range consists of multiple consecutive data , In essence, it is the number of permutations formed by multiple bits .

Sum up the law : The range of integers

Unsigned :[0,2^n-1]

A signed :[-2^(n-1), 2^(n-1)-1]

bool type

We didn't specifically say bool type , Simply understand its existence .

In depth understanding of C in Of bool type

C Language has no bool type

c99 Before , Mainly c90 There is no , At present, most books , They all think there is no . Because of the book , Generally, it lags behind the industry .

however c99 Introduced _Bool type ( You're not mistaken ,_Bool It's a type , But in the new header file stdbool.h in , It was written again as bool, In order to ensure C/C++ Compatibility )

// Test code  1
#include <stdio.h> 
#include <stdbool.h> // Without this header file, an error will be reported , When using new features, be sure to add  
#include <windows.h> 
int main() 
{
     
    bool ret = false; 
    ret = true;
    printf("%d\n", sizeof(ret)); //vs2013  and  Linux All of them are 1 
    system("pause"); 
    return 0; 
}
// Test code 2
#include <stdio.h>
#include <windows.h> 
int main() 
{
     // stay vs in , The cursor selects BOOL, Right click , As you can see, go to the definition , You can see BOOL What is it?  
    BOOL ret = FALSE; 
    ret = TRUE; 
    printf("%d\n", sizeof(ret)); // The output is 4, Because in the source code , That's the definition :typedef int BOOL;
    system("pause"); 
    return 0; 
}
// We found that , I can even make it up ... What the hell? ??  This is all Microsoft I made it myself BOOL value . stay vs Transfer to BOOL Corresponding header file , Turn to the top , You can see Microsoft's copyright information .  Okay , Who should I listen to ? Make sure that the code is cross platform , Exclusive types defined by Microsoft , Other platforms do not support .( Later in language   At the programming level , Any content that is directly related to the platform , We don't recommend 

summary :

  1. priority of use c90, It's the way we've been using before and after

  2. In case you have to use bool, recommend c99 standard , Not recommended MS Customize .

that ,C How to proceed in bool Value and 0 Comparison? ?

// It's uncomfortable , because C90 and C99 One doesn't support bool, One support . So and 0 Comparative theory compares scores in two cases . 
// however , The final conclusion is the same . So we directly follow the most high-frequency one behind us C90 Speaking of ,
#include <stdio.h> 
#include <stdbool.h> 
#include <windows.h> 
int main() 
{
     
    int pass = 0; //0 Said the false ,C90, We are used to using int Express bool
    //bool pass = false; //C99 
    if (pass == 0)
    {
    
        // Theoretically feasible , But at this time pass It should be regarded as bool Looking at ,== Used for integer comparison , Not recommended  //TODO 
    }
    if (pass == false)
    {
     // Not recommended , Although in C99 It also works in  //TODO 
    }
    if (pass)
    {
     
        // recommend  //TODO 
    }// Theoretically feasible , But at this time pass It should be regarded as bool Looking at ,== Used for integer comparison , Not recommended  // in addition , Not 0 It's true , But not 0 There are many. , It's not necessarily true here  
    if (pass != 1)
    {
     
        //TODO 
    }
    if (pass != true)
    {
     
        // Not recommended , Although in C99 It also works in  //TODO 
    }if (!pass)
    {
     
        // recommend  //TODO 
    }
    system("pause"); 
    return 0; 
}
 Conclusion :bool type , Direct determination , Compare with specific values without operators .

summary

That's all for the introduction of some keywords , If there are some mistakes, please tell me , We learn from each other . meanwhile , You can also discuss with each other . Ashamed to speak , I had another day , It's almost evening again 12 spot , Let's finish here first

原网站

版权声明
本文为[Ordinary people 1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270402161754.html