First knowledge of C language 1

Keywords: C

Programming is a sea of stars. If you want to sail in this sea of programming, you must first build a boat. This blog will take you to know the sea of C language for the first time, that is, to have a general understanding of C language. Then we will come back and carefully study each knowledge point. We will witness the wonderful grammar of C language from macro to micro.

This article uses the vs2019 compiler.

Note that this article does not cover how to configure variables or how to install the compiler, but some basic syntax.

hello world

c language starts from the first line of the main function

All C language code must have the main function -- entry

#include <stdio.h>// #Include means "include", which contains stdio.h files
                  //Stdio - standard input / output standard input / output (std- standard) (i- input) (o- output).
​
//Int means integer; The int in front of main indicates that an integer value is returned after the main function call.
int main()//The main function - main is the entry of the program. The main function has and has one.
{
    printf("hello,cccc\n");//Equal to print function -- print function, "\ n" means line feed after output
    //printf is a library function, which is provided to us by C language itself. So call #include < stdio >
    return 0;//Return means to return. It returns the integer int 0.
}

data type

  • Why are there so many types?

char          // Character data type
 short         // Short  
int           // integer
 long          // Long integer
 long long     // Longer integer
 float         // Single-precision floating-point 
double        // Double precision floating point number

  • What is the size of each type?   The unit is dyte (bytes)

char         //1 
short        //2 
int          //4
long         //4 or 8
long long    //8
float        //4
double       //8

Why are there so many types?

char         //Character data type
short        //Short  
int          //integer
long         //Long integer
long long    //Longer integer
float        //Single-precision floating-point 
double       //Double precision floating point number
 
  //%c -- print character
  //%d -- print integer
  //%f -- print floating point numbers
  //%p -- print as address
  //%x -- print hexadecimal digits
  //%LF --- print double precision floating-point numbers. It is better to use this% lf for printing double type
  //%s ---- print string
    
 //char character type
 int main()
{
    char ch ='A';//A space is requested from the memory. The name of the space is ch. this space is used to store "a"
    printf("%c\n",ch);// %c -- print data in character format, and the print result is: A
    return 0;
}
​
//int -- integer
int main()
{
    int age=20;
    printf("%d\n",age);//  %d -- print integer decimal data, and the print result is: 20
    return 0;
}
​
//short int -- short integer
​
//Long -- long integer
int mian()
{
    long num=100;
    printf("%d\n",num);The print result is also: 100
    return 0;
}
​
//float -- single precision floating point type
int main()
{
    float f = 2.0;
    printf("%f\n", f);//The print result is: 5.00000. The float type can contain five decimal points.
    return 0;
}
​
//Double -- double precision floating point type
int main()
{
    double f = 3.14;
    printf("%f\n", f);//You can use% f for printing here, but you'd better use% lf, which means double precision printing.
    return 0;
}
 

What is the size of each type?

Unit bit bit < byte byte < KB < MB < GB < TB < Pb in computer

A bit can store only one binary 0 or 1. So learn about binary and decimal conversion.

1byte=8bit

1kb=1024byte

1mb=1024kb

1tb=1024mb

1pb=1024mb

//Calculate occupancy for each type
int main()
{   //sizeof is a function to calculate the size. As follows, you can test the space occupied by the following data types and variables.
    //The unit of sizeof is byte.
    printf("%d\n", sizeof(char));// 1byte
    printf("%d\n", sizeof(int));// 4byte
    printf("%d\n", sizeof(long));// 4byte, long can be 4 or 8byte
    printf("%d\n", sizeof(short));// 2byte
    printf("%d\n", sizeof(long long));// 8byte
    printf("%d\n", sizeof(float));// 4byte
    printf("%d\n", sizeof(double));// 8byte
   
    short int age=20 //short corresponds to two bytes. Apply for two bytes from memory, that is, 16 bit s. The number that can be stored is 2 ^ 16-1 = 65535.
        //If the data is not very large, it is recommended to use small integers. The fewer bit s, the more space can be saved.
}//These numbers printed out are "byte -- byte"
​
#include <stdio.h>
int main()
{
    //     Age 20
    short age = 20;//Apply for 2 bytes = 16bit from the memory to store 20 bits
    float weight = 95.6f;//The system may report an error because the system treats 95.6 as a double precision floating-point to the double type; Add an "f" after 95.6, indicating that it is a single precision floating point. Apply to the system for 4 bytes to store decimals.
    
   //According to the C language standard sizeof (long) > = sizeof (int), depending on the platform, long may be 4 or 8
    return 0;
}

Variable, constant

Constant:

Some values in life will not change (PI, ID number, blood type, gender, etc.)

Variable:

Some values can be changed (age, weight, salary, etc.)

How to define variables

Type + variable name + assignment

int age=150;
float weight=45.5f;
char ch='w';

Classification of variables

  • local variable

  • global variable

  1. Global variables -- variables defined outside the code block ({})

  2. Local variables -- variables defined within the code block ({})

  3. If the local variable name is the same as the global variable name, there will be no conflict, but the local variable takes precedence. It is suggested that the names of global variables and local variables should not be the same, which is easy to cause bug s. Local variables can only be used in local code block {}.

//Calculate occupancy for each type
int main()
{   //sizeof is a function to calculate the size. As follows, you can test the space occupied by the following data types and variables.
    //The unit of sizeof is byte.
    printf("%d\n", sizeof(char));// 1byte
    printf("%d\n", sizeof(int));// 4byte
    printf("%d\n", sizeof(long));// 4byte, long can be 4 or 8byte
    printf("%d\n", sizeof(short));// 2byte
    printf("%d\n", sizeof(long long));// 8byte
    printf("%d\n", sizeof(float));// 4byte
    printf("%d\n", sizeof(double));// 8byte
   
    short int age=20 //short corresponds to two bytes. Apply for two bytes from memory, that is, 16 bit s. The number that can be stored is 2 ^ 16-1 = 65535.
        //If the data is not very large, it is recommended to use small integers. The fewer bit s, the more space can be saved.
}//These numbers printed out are "byte -- byte"
​
#include <stdio.h>
int main()
{
    //     Age 20
    short age = 20;//Apply for 2 bytes = 16bit from the memory to store 20 bits
    float weight = 95.6f;//The system may report an error because the system treats 95.6 as a double precision floating-point to the double type; Add an "f" after 95.6, indicating that it is a single precision floating point. Apply to the system for 4 bytes to store decimals.
    
   //According to the C language standard sizeof (long) > = sizeof (int), depending on the platform, long may be 4 or 8
    return 0;
}

 

Posted by jason_kraft on Sun, 24 Oct 2021 12:11:11 -0700