Zero basics, C language, first knowledge of C language

1. What is C language

C language is a general computer programming language, which is widely used in Bottom development. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code and run without any running environment support. Although C language provides many low-level processing functions, it still maintains good cross platform characteristics. C language programs written in a standard specification can be compiled on many computer platforms, even including some embedded processors (single chip microcomputer or MCU) and supercomputers.
In the 1980s, in order to avoid differences in C language syntax used by various developers, the American National Bureau of standards formulated a complete set of American national standard syntax for C language, called ANSI C, as the original standard of C language. [1] At present, the C11 standard issued by the international organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) on December 8, 2011 is the third official standard of C language and the latest standard of C language. The standard better supports Chinese function name and Chinese identifier, and realizes Chinese character programming to a certain extent.
C language is a process oriented computer programming language, which is different from object-oriented programming languages such as C + +, Java and so on. Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc

2. The first C language program

#include <stdio.h>
int main()
{
	printf("hello\n");
	printf("he he");
	return 0;
}
//Explanation:
//The main function is the entry to the program
//There is only one main function in a project

\n is a newline symbol, and the execution result is as follows:

3. Data type

char        //Character data type
short       //Short 
int         //plastic
long        //Long integer
long long   //Longer shaping
float       //Single-precision floating-point 
double      //Double precision floating point number
//Does C language have string type/ nothing
  • Why is there such a type?
  • What is the size of each type?

Print type byte 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(long));
    printf("%d\n", sizeof(long long));
    printf("%d\n", sizeof(float));
    printf("%d\n", sizeof(double));
    printf("%d\n", sizeof(long double));
    return 0; }

sizeof() is usually used to calculate the size of a type or variable in bytes. You can see the size of various types of bytes. The running results are as follows:

Note: there are so many types to more richly express various values in life. Various types of use:

char ch = 'w';
int weight = 120;
int salary = 20000;

4. Variables and constants

The basic data type quantity can be divided into constant and variable according to whether its value is variable or not.
In the process of program execution, the amount whose value does not change is called constant, and the amount whose value is variable is called variable. They can be classified in combination with data types. For example, they can be divided into integer constant, integer variable, floating point constant, floating point variable, character constant and character variable

4.1 method of defining variables

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

4.2 classification of variables

  • local variable
  • global variable
#include <stdio.h>
int global = 2019;//global variable
int main()
{
    int local = 2018;//local variable
    //Is there a problem with the global defined below?
    int global = 2020;//local variable
    printf("global = %d\n", global);
    return 0; }

Execution results:

Summary:
There is no problem with the definition of the local variable global variable above!
When the local variable has the same name as the global variable, the local variable takes precedence. The running result of the above program prints out the value of the local variable global

4.3 use of variables

#include <stdio.h>
int main()
{
    int num1 = 0;
   int num2 = 0;
    int sum = 0;
    printf("Enter two operands:>");
    scanf("%d %d", &num1, &num2);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0; }
//Here are the input and output statements
//scanf
//printf

4.4 scope and life cycle of variables

Scope
scope, programming concept. Generally speaking, the names used in a piece of program code are not always valid / available
of
The scope of the code that limits the availability of the name is the scope of the name.

  • The scope of a local variable is the local scope of the variable.
  • The scope of the global variable is the whole project.

life cycle
The life cycle of a variable refers to the period between the creation of a variable and its destruction

  • The life cycle of a local variable is the beginning of the scope life cycle and the end of the scope life cycle.
  • The life cycle of a global variable is the life cycle of the entire program.

4.5 constants

The definition forms of constants and variables in C language are different.
Constants in C language are divided into the following types:

  • Literal constant
  • const modified constant
  • const modified constant
  • #Identifier constant defined by define
  • enumeration constant
#include <stdio.h>
//give an example
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;//ok?
 #define MAX 100            //#Identifier constant for define    
    return 0; }

const int n=10;
n=20;// Its value cannot be modified
int arr [n]={0};// Although n is modified by const, it is still a variable in essence and cannot be used to specify the size of the array

Note: the const modified number cannot modify its value, but it is essentially a variable and cannot be used as the size of the array (before C99)
The identifier constant defined by define cannot change its value, and can be used as the size of the array

5. String + escape character + comment

5.1 string

C language has no string type
"hello bit.\n"
This string of characters enclosed by double quotes is called String Literal, or string for short.

Strings can be stored in character arrays, for example:

#include <stdio.h>
//What is the print result of the following code? Why? (highlight the importance of '\ 0')
int main()
{
    char arr1[] = "bit";//Hide a \ 0 at the end of the string, which is in the end table of the string
    char arr2[] = {'b', 'i', 't'};
    char arr3[] = {'b', 'i', 't', '\0'};
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);//Like the print result of arr1, \ 0 is the end flag of the string 
    return 0; }

Operation results:

Find string length:

int len1 = strlen(arr1);
int len2 = strlen(arr2);
int len3 = strlen(arr3);

printf("len1 = %d\n", len1);
printf("len2 = %d\n", len2);
printf("len3 = %d\n", len3);

5.2 escape characters

Suppose we want to print a directory c:\code\test.c on the screen
How do we write code?

#include <stdio.h>
int main()
{
 printf("c:\code\test.c\n");
    return 0; }

Actual operation results:

I have to mention the escape character here. As the name suggests, the escape character is to change its meaning. Let's look at some escape characters.

  • Question 1: how to print a single quote 'on the screen?
  • Question 2: print a string on the screen. The content of the string is a pair of quotation marks“
#include <stdio.h>
int main()
{
    //Question 1: how to print a single quotation mark 'on the screen?
    //Question 2: print a string on the screen. The content of the string is a double quotation mark ". What should I do?
    printf("%c\n", '\'');//Print character
    printf("%s\n", "\"");//Print string
    return 0; }


Written test questions:

//What does the program output?
#include <stdio.h>
int main()
{
    printf("%d\n", strlen("abcdef"));//6
    // \32 is parsed into an escape character
    printf("%d\n", strlen("c:\test\328\test.c"));//14
    return 0; }

Escape character '\ t', '\ 32'

To print the "c:\code\test.c" string, you should:

#include <stdio.h>
int main()
{
 printf("c:\\code\\test.c\n");
    return 0; }


\ddd escape character
ddd represents 1-3 octal digits, such as: \ 130X

#include <stdio.h>
int main()
{
	printf("%c\n",'\162');
	return 0;
}


Why is the running result r????
Because 162 is octal, its decimal system is 114, and the character of ASCII table corresponding to decimal 114 is r
ASCII code table: ASCII code table

***\The xdd escape character represents 2 hexadecimals, such as: \ x30***

#include <stdio.h>
int main()
{
	printf("%c\n",'\x62');//b
	return 0;
}

6. Notes

  • Unnecessary code in the code can be deleted directly or commented out
  • Some codes in the code are difficult to understand. You can add notes
    For example:
#include <stdio.h>
int Add(int x, int y) {
    return x+y; }
/*C Language style notes
int Sub(int x, int y)
{
    return x-y;
}
*/
int main()
{
    //C + + annotation style
    //int a = 10;
    //Call the Add function to complete the addition
    printf("%d\n", Add(1, 2));
    return 0; }

***There are two styles of annotation:

  • C language style notes / xxxxxx/
    Defect: comments cannot be nested
  • C + + style comments / / xxxxxxxx
    You can annotate one line or multiple lines***

7. Select a statement

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

8. Circular statement

How to implement loop in C language?

  • while statement - explanation
  • for statement (later)
  • do... while statement (later)
//An instance of a while loop
#include <stdio.h>
int main()
{
    printf("Add bit\n");
    int line = 0;
    while(line<=20000)
   {
        line++;
        printf("I'll keep trying to type the code\n");
   }
    if(line>20000)
        printf("Win baifumei\n");
    return 0; }
    

9. Function

Function is characterized by code simplification and code reuse.

#include <stdio.h>
int main()
{
    int num1 = 0;
   int num2 = 0;
    int sum = 0;
    printf("Enter two operands:>");
    scanf("%d %d", &a, &b);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0; }
    
//***The above code is written as a function as follows:***
#include <stdio.h>
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; }

10. Array

How to store 1-10 numbers?
C language gives the definition of array: a set of elements of the same type

10.1 array definition

int arr[10] = {1,2,3,4,5,6,7,8,9,10};// Define an integer array with up to 10 elements

10.2 subscript of array

The subscript of the array starts from 0. The array can be accessed through subscript, such as:

int arr[10] = {1,2,3,4,5,6,7,8,9,10}
The array has 10 elements with subscripts from 0 to 9
The second element: arr[1]=2;

10.3 use of arrays

#include <stdio.h>
int main()
{
 int i = 0;
 int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 for(i=0; i<10; i++)
 {
       printf("%d ", arr[i]);
 }
 printf("\n");
    return 0; }

Print array elements:

11. Operator

arithmetic operator

+    -    *    /   %

Shift operators

>>    <<

Bitwise operators

& ^ |

Assignment operator

= += -= *= /= &= ^= |= >>= <<=

unary operator

! Logical reverse operation
-Negative value
+Positive value
&Get address
Type length of sizeof operand in bytes
~Bitwise negation of a number
– front, rear –
++Front and rear++
*Indirect access operator (dereference operator) (type) cast

Relational operator

>
=
<=
 <=
 !=   Used to test "inequality"
 ==      Used to test equality

Logical operator

&&
||

Conditional Operator

exp1 ? exp2 : exp3

comma expression

exp1,exp2,exp3 ,...expN

Subscript references, function calls, and structure members

> [] () . ->

12. 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

12.1 keyword typedef

typedef, as the name suggests, is a type definition, which should be understood here as type renaming.
For example:

//Rename unsigned int uint_32, so uint_32 is also a type name
typedef unsigned int uint_32;
int main()
{
    //Look at num1 and num2. These two variables are of the same type
    unsigned int num1 = 0;
    uint_32 num2 = 0;
    return 0; }

12.2 keyword static

In C language:
static is used to modify variables and functions

  1. Decorated local variable - static local variable
  2. Decorated global variable - static global variable
  3. . modifier function - static function

12.2.1 modifying local variables

//Code 1
#include <stdio.h>
void test()
{
    int i = 0;
    i++;
    printf("%d ", i);
}
int main()
{
 int i = 0;
    for(i=0; i<10; i++)
   {
        test();
   }
    return 0; }
//Code 2
#include <stdio.h>
void test()
{
    //static modifies a local variable
    static int i = 0;
    i++;
    printf("%d ", i);
}
int main()
{
 int i = 0;
    for(i=0; i<10; i++)
   {
        test();
   }
    return 0;}

Compare the effects of code 1 and code 2 to understand the meaning of static modifying local variables.
Conclusion:
Static modifies a local variable, which changes the life cycle of the variable, so that the static local variable remains out of the scope until the end of the program,
The life cycle is over.

Keywords: C

Added by greatepier on Wed, 13 Oct 2021 00:50:20 +0300