First introduction to C language - detailed introduction (systematic learning day 4)

Table of contents

Preface

1. Brief introduction, characteristics and basic composition of C language

a brief introdction:

Features:

Basic composition:

2. Understand C language programs

standard format:

Simple C program:

3. Detailed introduction of basic composition classification   

(1) Keywords

(2) Data type

Common storage units in computers

Value range of data type

Print input type

(3) Constants and variables

Classification of constants:

Demonstration of various constant codes:

Variable classification:

Here's how to define variables:

Summarize


Preface

   This article will introduce the basics of getting started with C language in detail.


1. Brief introduction, characteristics and basic composition of C language

a brief introdction:

C language is a process-oriented , abstract general-purpose programming language that is widely used in low-level development . C language can compile and process low-level memory in a simple way . C language is an efficient programming language that only generates a small amount of machine language and can run without any operating environment support.

Features:

(1) Concise language

(2) Have structured control statements

(3) Rich data types

(4) Rich operators

(5) Direct operations can be performed on physical addresses

(6) The code has good portability

(7) It can generate high-quality programs with high target code execution efficiency.

Basic composition:

  1. Keywords: There are some words in C language that are fixed for specific purposes, called keywords. These keywords have special meaning in the syntax and cannot be used as identifiers (variable names, function names, etc.). For example, int, if, for, etc. are all keywords of C language.  

  2. Identifiers: Identifiers are character sequences composed of letters, numbers, and underscores, used to represent variables, functions, structures, etc. Identifiers need to meet certain naming rules, such as starting with a letter or underscore, and keywords cannot be used as identifiers.

  3. Data Types: C language supports multiple data types, including basic data types and derived data types. Basic data types include integers (int), floating point types (float, double), character types (char), etc. Derived data types include arrays, structures, enumerations, etc.

  4. Variables: Variables are memory locations used to store and represent data and need to be declared before use. When declaring a variable, you need to specify its data type and variable name so that the compiler can allocate memory space for it.

  5. Operators: C language provides a rich set of operators for performing various mathematical and logical operations. Common operators include arithmetic operators (+, -, *, /, etc.), relational operators (<, >, ==, etc.), logical operators (&&, ||, etc.), etc.

  6. Control Structures: C language provides a variety of control structures for controlling the execution flow of the program. Common control structures include conditional statements (if-else), loop statements (for, while, do-while) and jump statements (break, continue, return, etc.).

  7. Functions: C language uses functions as the basic program organization unit. A function is an independent block of code that performs a specific task. C language provides a function library that contains a large number of commonly used functions, and you can also customize functions to implement specific functions.

  8. Arrays: An array is a contiguous memory space that stores data of the same type. Elements in an array can be accessed through subscripts. Array subscripts in C language start from 0.

  9. Pointers: Pointers are special variables used to store the memory addresses of other variables. Pointers can indirectly access and modify the value of variables, and dynamically allocate memory space.

  10. Structures: Structures are a custom data type that can combine different types of data to form a new composite type. The members of a structure can be of different data types.

2. Understand C language programs

standard format:

#include <stdio.h>                //引入头文件
int main(void)                    //main函数,程序的入口
{
			
	return 0;                     //结束函数,返回
}     

Simple C program:

#include<stdio.h>             //引入头文件

int main(void)                     //main 函数是程序的入口,程序从main函数第一行开始执行
{                           //一个项目中main(主)函数有且只有一个                           
	printf("hello world!\n");//printf 为输出打印的库函数,库函数的使用必须要包含一个头文件                         
	return 0;                //结束函数,返回
}

3. Detailed introduction of basic composition classification
   

(1) Keywords

Common keywords
Keywords effect
int   Declare integer variable
double Declare a double variable
long  Declare long integer variable
char Declare character variables
float   Declare floating point variables
short  Declare short integer variable
signed Declare signed type variables
unsigned Declare unsigned type variables
struct Declare structure variables; the memory size occupied by the structure is the sum of the memory occupied by its members
enum Declare enumeration type variables
union Declare union data type variables
static Declare static variables; the most misnamed keyword
switch  for switch variables
case for statement branching
default  Other branches in switch statements
break Jump out of the current loop; indicate the termination of this loop
continue End the current cycle and start the next cycle
register Declare register variables; fastest keyword
const Declare a read-only variable, and the read-only variable it modifies must be initialized at the same time as it is defined.
volatile Specifies that variables can be implicitly changed during execution; most volatile keyword
typedef  Used to name the data type
extern Declaring variables is declared in other files; the most popular keyword
return Subroutine return statement, used to terminate a function and return the following value
void Declare a function with no return value or parameters, declare a null type pointer
do The loop body of the loop statement
while  Loop condition of loop statement
for a loop statement
if  Conditional statements
else Negative branch of conditional statement
goto  unconditional jump statement
sizeof  Calculate the memory space occupied by the object
auto Declaring automatic variables, the compiler generally defaults to auto by default; the most generous keyword

(2) Data type

Data type size:

Common storage units in computers

unit illustrate
Bit The smallest storage unit can only store 0 or 1.
Byte Composed of 8 bits, it is the most basic storage unit in computers.
Kilobytes (KB) Approximately equal to 1024 bytes.
Megabyte (MB) Approximately equal to 1024 kilobytes.
Gigabyte (GB) Approximately equal to 1024 megabytes.
Terabytes (TB) Approximately equal to 1024 gigabytes.


To find out the size of their data types, we need to use a function sizeof in the C language. The function of this function is to find out the length (that is, the size of the memory occupied) of an object (data type or data object), so as to byte as unit). Note that the unit is bytes.   
The specific code is as follows:

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


 
   
    

 It can be seen from the running results:

The data type size of char type is 1 byte or 8 bits.

The data type size of short type is 2 bytes or 16 bits.

int类型、float类型的数据类型大小为4字节即32位。

long类型、long long类型、double类型的数据类型大小为8字节即64位。

注意,若整数数据类型前面加unsigned即为无符号整数数据类型,数据类型大小不变。

需要注意的是long在32位系统中占用4个字节(32位),而在64位系统中占用8个字节(64位)。因此,可以说unsigned long的大小为4字节或8字节,具体取决于编译器和操作系统的规范。

数据类型的取值范围

数据类型 取值范围
char -128 到 127
unsigned char 0 到 255
short -32,768 到 32,767
unsigned short 0 到 65,535
int -2,147,483,648 到 2,147,483,647
unsigned int 0 到 4,294,967,295
long -2,147,483,648 到 2,147,483,647
unsigned long 0 到 4,294,967,295
long long -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long long 0 到 18,446,744,073,709,551,615
float

精度约为6到7十进制位

double

精度约为15位十进制数

打印输入类型

数据类型 打印类型
short %hd
int %d
long %ld
long long %lld
float %f
doule %lf
char %c

(3)常量和变量

常量:

常量是指在程序中固定不变的数值或字符,它们的值在程序运行过程中不能更改。常量可以分为字面常量和符号常量。

常量的分类:

  1. 字面常量:字面常量是指直接写在代码中的常量值。例如,整数常量10、浮点数常量3.14、字符常量'a'等都是字面常量。

  2. 符号常量:符号常量是使用#defineconst关键字定义的常量。它们在程序中表示一个固定的数值或字符串,并且可以在程序中多次使用。

  3. enumEnumeration constants: refer to a set of constants with the same characteristics, defined in the C language by using keywords. Enumeration constants can be used to define a group of related constants to make the program clearer and more readable. Each enumeration constant has a unique name and corresponding value, which by default starts from 0 and increments. We can customize the value or order of enumeration constants.

Demonstration of various constant codes:

int main()
{
	120;//字面常量
	10;//字面常量
 
	//const 修饰常变量
	const int num = 10;//num是const修饰的常变量
	//num的值不可被改变,但本质还是一个变量
 
	//#define的标识符常量 
    #define MIN 100
	printf(" = %d\n", MIN);
 
	//枚举常量
	printf("%d\n", MALE);
	printf("%d\n", FEMALE);
	return 0;
	
}

variable: 

A variable is an identifier whose value can change during program execution. Variables can be used to store and represent different types of data, including integers, floating point numbers, characters, Boolean values, etc. Variables need to be declared before use, and their values ​​can be changed through assignment statements. In C language, the declaration of a variable requires specifying the type and name of the variable.

Variable classification:

Divided into local variables and global variables.

Their differences:

The scope of a local variable is the local scope where the variable is located. Its life cycle begins when it enters the scope life cycle and ends when it exits the scope life cycle.

The scope of global variables is the entire project, and its life cycle is the life cycle of the entire program.

Here's how to define variables:

int x;             // 声明一个整数类型的变量x
float pi = 3.14;   // 声明并初始化一个浮点数类型的变量pi
char ch = 'A';     // 声明并初始化一个字符类型的变量ch

The naming of variables needs to comply with certain rules, including starting with a letter or underscore, and consisting of letters, numbers, and underscores.

Both variables and constants play certain roles in programs. Constants are used to represent fixed values ​​or strings, while variables are used to store and process data. In programming, the use of constants and variables is a very important basic concept.


Summarize

       This article explains the detailed introductory basic knowledge of C language in detail. I hope it can help everyone!

      In the future, I will show you more important basic knowledge about C language. Thank you for supporting Lazy King!

      I hope this blog can bring help to all my friends. Finally, friends who have been invited by Lazy King will leave your precious comments and attention. Thank you!
 

Guess you like

Origin blog.csdn.net/weixin_58070962/article/details/133027686