C language notes Day01

data type

Char character data type char ch = 'a';
Short integer short num = 10;
Integer int
Long long integer
long long longer integer
Float single precision floating point number float weight = 55.5;
Double double d = 0.0;

Print an integer printf ('% d', 1000;)

sizeof - Keyword - Operator - space occupied by calculation type or variable - unit: bytes
printf("%d\n",sizeof(char));

Units in the computer:
Bit - bit
Byte - byte = 8bit
kb - 1024 byte
mb - 1024 kb
gb - 1024 mb
tb - 1024 gb
pb - 1024 tb

printf("%d\n",sizeof(char)); // 1
printf("%d\n",sizeof(short)); // 2
printf("%d\n",sizeof(int));// 4
printf("%d\n",sizeof(long));// 4 C language standard: sizeof (long) > = sizeof (int)
printf("%d\n",sizeof(long long)); // 8
printf("%d\n",sizeof(float));// 4
printf("%d\n",sizeof(double));// 8

variable and constant

The constant value is represented by the concept of constant in C language, and the variable value is represented in C language.

Method of defining variables

int age  = 20;
double weight = 98.1;
printf("%lf",weight); //Use% f for float output and% lf for double output

%d - integer
%f - float float
%lf- double

Classification of variables

Local variable global variable
int a = 10; //global variable


int main(){
	int a = 20; // local variable
	printf("%d\n",a); //Output 20 when the global and local variable names conflict, local priority. Naming global and local variables the same is not recommended.
	return 0;
}

Write a code to find the sum of two integers

The scanf function is an input function

int main(){

int a = 0;
int b = 0;
int sum = 0;
scanf("%d %d",&a,&b);
sum = a + b;
printf("sum = %d",sum);
return 0;
}

Note: compilation may report errors

resolvent:
1. In the first line of the source file, add: #define_ CRT_ SECURE_ NO_ WARNINGS 1
2.

Scope and lifecycle of variables

Scope

scope is a programming concept. Generally speaking, the names used in a piece of program code are not always valid / available

The scope of the code that limits the availability of the name is the scope of the name.

  1. The scope of a local variable is the local scope of the variable.
  2. 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

  1. The life cycle of a local variable is the beginning of the scope life cycle and the end of the scope life cycle.
  2. The life cycle of global variables is: the life cycle of the whole program.

constant

The definition forms of constants and variables in C language are different.

Constants in C language are divided into the following types:

  1. Literal constant
  2. const modified constant
  3. #Identifier constant defined by define
  4. enumeration constant
#include <stdio.h>
//give an example
enum Sex
{
 MALE,
 FEMALE,
 SECRET
};
//Ale, female and secret in parentheses are enumeration constants
int main()
{
    //Literal constant presentation
    3.14;//Literal constant
    1000;//Literal constant
    
    //const modified constant
    const float pai = 3.14f;   //pai here is a constant variable modified by const
    pai = 5.14;//It cannot be modified directly! The compiler will report an error
    
    //#Demonstration of identifier constant of define
    #define MAX 100
    printf("max = %d\n", MAX);
    
    //Enumeration constant demo
    printf("%d\n", MALE); //Output result 0
    printf("%d\n", FEMALE); //Output result 1
    printf("%d\n", SECRET); //Output result 2
    //Note: enumeration constants start from 0 by default and increase by 1 in turn

Note: the pai in the above example is called const modified constant variable. Const modified constant variable is only limited at the grammatical level in C language

The variable pai cannot be changed directly, but pai is essentially a variable, so it is called a constant variable.

String + escape character + comment

character string

"hello bit.\n"

This string of characters enclosed by double quotes is called String Literal, or simply string.

Note: the end flag of the string is an escape character of \ 0. When calculating the length of a string, \ 0 is the end flag and is not counted as the content of the string.

#include <stdio.h>
//What is the print result of the following code? Why? (highlight the importance of '\ 0')
int main()
{
    char arr1[] = "bit"; //Output result bit
    char arr2[] = {'b', 'i', 't'}; //Output result bit: it
    char arr3[] = {'b', 'i', 't', '\0'}; //Output result bit
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);
    return 0;
}

Escape character

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

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

In fact, the result of the program is as follows:


Let's look at some escape characters.

Escape characterinterpretation
?Used when writing consecutive question marks to prevent them from being parsed into three letter words
\'Used to represent character constant '
\''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
\dddddd represents 1-3 octal digits, such as: \ 130 X
\xdddd represents 2 hexadecimal digits. For example: \ x30
#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 ". How to do it?
    printf("%c\n", '\'');
    printf("%s\n", "\"");
    return 0;
}

Written test questions:

//What does the program output?
#include <stdio.h>
int main()
{
    printf("%d\n", strlen("abcdef"));
    //The strlen() function must be referenced #include < string h> Can be used
    // \62 is parsed into an escape character
    printf("%d\n", strlen("c:\test\628\test.c"));
    return 0;
}

notes

  1. Unnecessary codes in the code can be deleted directly or commented out
  2. Some codes in the code are difficult to understand. You can add notes
#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:

Notes in C language style / xxxxxx/
Defect: comments cannot be nested

C + + style comments / / xxxxxxxx
You can annotate one line or multiple lines

Select statement

#include <stdio.h>
int main()
{
    int coding = 0;
    printf("Will you type the code? (option 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;
}

Circular statement

How to implement loop in C language?
1.while statement - explanation
2.for statement
3.do... while statement

int main()
{
    printf("Join the programmer\n");
    int line = 0;
    while(line<=20000)
   {
        line++;
        printf("I'll keep trying to type the code\n");
   }
    if(line>20000)
        printf("good offer\n");
    return 0;
}

function

#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;
}
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;
}

array

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

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

2. Subscript of array

Language specification: each element of the array has a subscript, which starts from 0.
Arrays can be accessed through subscripts.
For example:

int arr[10] = {0};
//If the array has 10 elements, the subscript range is 0-9


3 use of array

#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;
}

Operator


unary operator

! Logical reverse operation
-Negative value
+Positive value
&Take address
Type length of sizeof operand in bytes
~The binary bit negation of a number
A -- front and rear a --
++Front and rear++
*Indirect access operator (dereference operator)
(cast type)

Relational operator

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

Logical operator

&&Logic and
||Logical or

Conditional Operator

exp1 ? exp2 : exp3

comma expression

exp1, exp2, exp3, ...expN

Subscript references, function calls, and structure members

[] () . ->

Keyword typedef

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

//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. The types of these two variables are the same
    unsigned int num1 = 0;
    uint_32 num2 = 0;
    return 0;
}

Keyword static

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

  1. Modify local variables - called static local variables
  2. Modify global variables - called static global variables
  3. Modifier function - called static function

Modify 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;
}

#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 and changes the life cycle of the variable
Let the static local variable remain out of scope, and the life cycle will not end until the end of the program.

Modify global variables

//Code 1
//add.c
int g_val = 2018;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0;
}
//Code 2
//add.c
static int g_val = 2018;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0;
}

Code 1 is normal, and code 2 will have connectivity errors during compilation.

Conclusion:

A global variable is modified by static, so that this global variable can only be used in this source file, not in other source files
Use.

Modifier function

//Code 1
//add.c
int Add(int x, int y)
{
    return c+y;
}
//test.c
int main()
{
    printf("%d\n", Add(2, 3));
    return 0;
}
/Code 2
//add.c
static int Add(int x, int y)
{
    return c+y;
}
//test.c
int main()
{
    printf("%d\n", Add(2, 3));
    return 0;
}

Code 1 is normal, and code 2 will have connectivity errors when compiling

Conclusion:

A function is modified by static, so that this function can only be used in this source file, not in other source files.

#define defines constants and macros

//define defines the 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;
}

Pointer

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 location of the memory unit
Address.


Variables are created in memory (space is allocated in memory). Each memory unit has an address, so variables also have an address.
The address of the extracted variable is as follows:

#include <stdio.h>
int main()
{
 int num = 10;
 &num;//Take out 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 in the form of address
 return 0;
}


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

int num = 10;
int *p;//p is an integer pointer variable
p = &num;

Examples of pointer usage:

include <stdio.h>
int main()
{
 	int num = 10;
 	int *p = &num;
 	*p = 20;
    return 0;
}

Taking the shaping pointer as an example, it can be extended to other types, such as:

#include <stdio.h>
int main()
{
	char ch = 'w';
 	char* pc = &ch;
	*pc = 'q';
 	printf("%c\n", ch);
    return 0;
}

Size of pointer variable

#include <stdio.h>
//The size of the pointer variable depends on the size of the address
//The address under 32-bit platform is 32 bits (i.e. 4 bytes)
//The address under 64 bit platform is 64 bit (i.e. 8 bytes)
int main()
{
    printf("%d\n", sizeof(char *)); //4
    printf("%d\n", sizeof(short *)); //4
    printf("%d\n", sizeof(int *)); //4
    printf("%d\n", sizeof(double *)); //4
    return 0;
}

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

structural morphology

Structure is a particularly important knowledge point in C language. Structure enables C language to describe complex types.
For example, describe the student. The student includes: Name + age + gender + student number.
Here we can only use structure to describe.

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

Initialization of structure:

//Print 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);

Keywords: C Back-end

Added by webtuto on Thu, 17 Feb 2022 23:01:28 +0200