First knowledge of C language

To learn a language, you must be broad before you are proficient.

Here, I will sort out the knowledge points of C language, which can be divided into the following 14 categories.

catalogue

1. What is C language?  

2. The first C language program

3. Data type

4. Variables and constants

5. String, escape character, comment

6. Select a statement

7. Circular statement

8. Function

9. Array

10. Operator

11. Common keywords

12. define constants and macros

13. Pointer

14. Structure

Made a guide map and be clear

1. What is C language?

C language was born in Bell Labs in 1972. It is portable and powerful.

C language is a high-level programming language, process oriented.

C language has its own rules. At present, there are two general standards C99 and C11.

The following are the advantages of C language: (see C Primer Plus for details)

2. The first C language program

#include <stdio.h>

int main()
{
    printf("hello world");
    return 0;
}

#include <stdio. h> Refers to the calling header file

stdio is standard input output. When printf() and scanf() functions are used, they need to be called.

int stands for integer type

main() is a function, which is the entry of a program. There is only one function in a project.

return 0; Indicates the end of the program.

3. Data type

Three types: character type, integer type and floating point type

The unit is byte (1 byte = 8 bit), and the rest are 1024 times

data typenameByte sizeFormatter
charcharacter1%c
shortShort 2%d
intinteger4%d
longLong integer4%d
long longLonger integer8%d
shortSingle-precision floating-point 4%f
doubleDouble precision floating point number8%lf

4. Variables and constants

As the name suggests, a constant is a constant quantity, and a variable is a variable quantity

Variable:

Variables can be divided into local variables and global variables

Here is a distinction between scope and lifecycle.

Scope:

Local variable scope refers to the local scope of the variable.

The scope of the global variable is the whole project.

Life cycle:

The life cycle of a local variable starts at the scope and ends at the scope.

The life cycle of global variables is the life cycle of the whole program.

#include <stdio.h>

int a = 10;       //This is a global variable
int main()
{
    int a = 9;    //This is a local variable
    printf("%d", a);
    return 0;
}

As shown in the code, when a local variable conflicts with a global variable, the local variable takes precedence

constant

Constants can be divided into three types: const modification, #define defined identifier constants and enumeration constants.

#include <stdio.h>

enum Sex
{
 MALE,
 FEMALE,
 SECRET
};
//Mal, female and secret in parentheses are enumeration constants

int main()
{
    3.14;//Literal constant
    1000;//Literal constant
    const float pai = 3.14f;   //const modified constant
    pai = 5.14;                //Constants cannot be modified
    #define MAX 100            //#Identifier constant for define    
    return 0;
}

5. String, escape character, comment

character string

String, that is, multiple characters.

There is no corresponding keyword in C language, but it can be run with character array.

#include <stdio.h>

int main()
{
    char arr1[] = "hello";
    char arr2[] = {'h', 'e', 'l', 'l', 'o'};
    char arr3[] = {'h', 'e', 'l', 'l', 'o', '\0'}};
    return 0;
}

arr1 is a standard string notation.

Note the comparison between arr2 and arr3,

There is an extra '\ 0' in arr3, which is the end statement of the string. When the string is written in double quotation marks, it is added by default (that is, '\ 0' in arr1 by default)

Since there is no end flag for arr2, arr2 is output at this time

There was perfect garbled code.

arr3 is the corresponding solution, which shows the importance of '\ 0'.

Note: '\ 0' occupies one byte of memory space, but it itself is not counted as string content.

Escape character

Escape characterinterpretation
\'Used to represent character constants'
\"Double quotation marks used to represent the inside of a string
\\Used to represent a backslash to prevent it from being interpreted as an escape sequence character.
\aWarning character, beep
\bBackspace character
\fFeed character
\nLine feed
\renter
\tHorizontal tab
\vvertical tab
\?Used when writing consecutive question marks to prevent them from being parsed into three letter words
\dddddd represents 1 ~ 3 octal digits. For example: \ 130 X
\xdddd represents 2 hexadecimal digits. For example: \ x30

These escape characters do not need to be memorized specially. They are familiar with them when they are often used.

#include <stdio.h>

int main()
{
    printf("c:\code\test.c\n");
    return 0;
}

So what does this line of code output?

Note that the escape characters' \ c ',' \ t 'and' \ n 'are escape characters, and' \ c 'has no corresponding meaning. Only' c 'is printed

notes

Comments are used to supplement the code, and some unnecessary code can also be removed

Always write notes!

Always write notes!

Always write notes!

There are two styles of annotation.

1)/* xxxxxxxx */

2) / / xxxxxxxxxxxx (recommended)

Add the shortcut key of vs: ctrl + k, and then press ctrl + c

6. Select a statement

There are two kinds of selection statements: switch and if statements

I won't go into detail here. I'll talk about it in depth after learning. I'll directly demonstrate the usage of the code

#include <stdio.h>

int main()
{
   int coding = 0;
   printf("Will you type the code? (option 1) or 0):>");
   scanf("%d", &coding);
   if(coding == 1)
   {
      printf("Hold on, you'll have a good time offer\n");
   }
    else
   {
      printf("Give up and go home to sell sweet potatoes\n");
   }
   return 0;
}

Then write the same with switch:

#include <stdio.h>

int main()
{
   int coding = 0;
   printf("Will you type the code? (option 1) or 0):>");
   scanf("%d", &coding);
   switch(coding)
   {
   case 1:
      printf("Hold on, you'll have a good time offer\n");
      break;
   case 2:
      printf("Give up and go home to sell sweet potatoes\n");
      break;
   }
   return 0;
}

7. Circular statement

There are three types of circular statements: while, for, and do while

Here is a brief demonstration of the use of while, which will be discussed later.

//An instance of a while loop
#include <stdio.h>

int main()
{
    int line = 0;
    while(line<=100000)
   {
        line++;
        printf("I'll keep trying to type the code\n");
   }
    if(line>100000)
        printf("Win baifumei\n");
    return 0;
}

8. Function

Function is to simplify the code and facilitate repeated use. Directly on the instance.

#include <stdio.h>

//Implement an addition function
int Add(int x, int y)
{
   int z = x+y;
   return z;
}

int main()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;
    printf("Enter two operands:>");
    scanf("%d %d", &num1, &num2);
    sum = Add(num1, num2);
    printf("sum = %d\n", sum);
    return 0;
}

9. Array

An array is a collection of elements of the same type.

Here are some precautions:

1) The byte size of the array follows the capacity of the array.

The result of this figure is 4

 

The result of this figure is 6

2) The size of the array must be constant.

const int num = 10;

Where num is a constant variable,

That is, num itself is a variable, but after const modification, it becomes a constant from the syntax level and is still a variable in essence.

int arr[num] = {0};

So this way of writing is wrong.

10. Operator

Here is a brief list, which will be sorted out later:

  1.Arithmetic operator

  Used for various numerical operations. Including plus(+),reduce(-),ride(*),except(/),Seeking remainder(Or modular operation,%),Self increasing(++),Self subtraction(--)There are seven kinds.

  2.Relational operator

  Used for comparison operations. Including greater than(>),less than(<),be equal to(==), Greater than or equal to(>=),Less than or equal to(<=)Sum is not equal to(!=)Six.

  3.Logical operator

  Used for logical operations. Including and(&&),or(||),wrong(!)Three.

  4.Bitwise operator

  The quantity involved in the operation is calculated according to binary bits. Including bit and(&),Bit or(|),Bit non(~),Bit exclusive or(^),Shift left(<<),Shift right(>>)Six.

  5.Assignment Operators 

  It is used for assignment operation and is divided into simple assignment(=),Compound arithmetic assignment(+=,-=,*=,/=,%=)And compound bit operation assignment(&=,|=,^=,>>=,<<=)There are 11 species in total.

  6.Conditional operator

  This is a ternary operator for conditional evaluation(a ? b : c). 

  7.Comma Operator 

  Used to combine several expressions into one expression(a,b, c). 

  8.Pointer operators 

  Used to fetch content(*)And fetch address(&)Two operations.

  9.Byte count operator

  Used to calculate the number of bytes occupied by the data type(sizeof). 

  10.Special operator

  Bracketed(),subscript[],member(→,.)And so on.

 11. Common keywords

auto  break   case  char  const   continue  default  do   double else  enum   extern float  for   goto  if   int   long  register    return   short  signed  sizeof   static  struct  switch  typedef union  unsigned   void  volatile  while

You don't have to memorize it. You can remember it when you use it more. I'll talk about it later.

12. define constants and macros

Macros are similar to functions. See the code for specific usage

//define defines an identifier constant
#define MAX 1000

//define macro
#define ADD(x, y) ((x)+(y))

#include <stdio.h>

int main()
{
    int sum = ADD(2, 3);
    printf("sum = %d\n", sum);
    sum = 10*ADD(2, 3);
    printf("sum = %d\n", sum);
    return 0;
}

13. Pointer

To understand pointers, you need to understand memory.

Memory is a particularly important memory on the computer. The operation of programs in the computer is carried out in memory.

Therefore, in order to use memory effectively, the memory is divided into small memory units, and the size of each memory unit is 1 byte.

In order to effectively access each unit of memory, the memory unit is numbered. These numbers are called the address of the memory unit.

#include <stdio.h>

int main()
{
    int num = 10;
    &num;                     //Fetch the address of num
    //Note: there are 4 bytes of num, each byte has an address, and the address of the first byte (smaller address) is taken out
    printf("%p\n", &num);     //Print address,% p is printed as an address
    return 0;
}

How to store the address needs to define the pointer variable.

#include <stdio.h>

int main()
{
    int num = 10;
    int *p = &num;         //Take the address of num and store it in p, which is the pointer variable
    *p = 20;               //'*' here is the dereference character, that is, parse p, and then store 20 in the parsed address
    return 0;
}

Of course, a pointer variable is essentially a variable and has a size.  

#include <stdio.h>
int main()
{
    printf("%d\n", sizeof(char *));
    printf("%d\n", sizeof(short *));
    printf("%d\n", sizeof(int *));
    printf("%d\n", sizeof(double *));
    return 0;
}

Conclusion: the pointer size is 4 bytes on 32-bit platform and 8 bytes on 64 bit platform.

14. Structure

Describe a student. The student includes: Name + age + gender + student number.

Generally, if a data type cannot be stored, the structure is used to represent this kind of data.

Because structures enable C language to describe complex types.

For example:

struct Stu
{
    char name[20];//name
    int age;      //Age
    char sex[5];  //Gender
    char id[15]; //Student number
};//Here ';' Not less

//Initialize structure information
struct Stu s = {"Zhang San", 20, "male", "20180101"};

//. access operators for structure members
printf("name = %s age = %d sex = %s id = %s\n", s.name, s.age, s.sex, s.id);

//->Operator
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", ps->name, ps->age, ps->sex, ps-
>id);

I have sorted out the C language knowledge points here, and I will continue to update them in the later learning,

Welcome to make corrections. I think it's good. You can point a praise.

Keywords: C

Added by scrypted on Sun, 16 Jan 2022 06:27:44 +0200