Through these 14 points, let you quickly get started with C language (1)

foreword

As a beginner in programming, although it was not smooth sailing when learning C language, I also deeply realized the fun of programming. The following is a series of basic knowledge about C language that I compiled after I first came into contact with C language. I hope it can help you who are also beginners in C language!


Table of contents

  • What is C language
  • The first C language program
  • type of data
  • variable, constant
  • string + escape character + comment
  • select statement
  • loop statement
  • function
  • array
  • operator
  • common keywords
  • define defines constants and macros
  • pointer
  • structure

1. What is C language

Simply put, C language is a general- purpose computer programming language that is widely used in low-level development . The design goal of the C language is to provide a programming language that can be compiled in an easy way, handle low-level memory, generate a small amount of machine code, and can run without any operating environment support.

In the 1980s, in order to avoid differences in the C language grammar used by various developers, the US National Bureau of Standards formulated a complete set of American National Standard grammar for the C language, known as ANSI C, also known as C89 / C90 , as the initial standard of the C language, later produced a series of standards such as C99 and C11.

C language is a process-oriented computer programming language, which is different from object-oriented programming languages ​​such as C++ and Java. Its compilers mainly include Clang, GCC , WIN-TC, SUBLIME, MSVC , Turbo C, etc.


2. The first C language program

Write a code to print hello world on the screen

//头文件的包含,printf是库函数,库函数的使用是需要包含头文件的
#include <stdio.h>//#的作用:预处理指令的开头就是#-->#include  #define
//std i o-->standard input output  标准输入输出  .h-->header
int main()//main是主函数的意思,是程序的入口,有且仅有一个  int 函数的返回类型:整型
{
    
    
	//打印hello world
	printf("hello world\n");//""括起来的叫字符串
	return 0;// C语言中的习惯:返回0表示正常返回,返回非0表示异常返回
}

3. Data type

  • char---->character data type
  • short---->short integer
  • int---->shaping
  • long---->long integer
  • long long---->longer shaping
  • float---->single-precision floating-point number
  • double---->double-precision floating-point number

Since there are so many data types, what is the size of each type?

#include <stdio.h>
 
int main()
{
    
    
	printf("%d\n", sizeof(char));//1   单位:字节
	printf("%d\n", sizeof(short));//2    
	printf("%d\n", sizeof(int));//4 
	printf("%d\n", sizeof(long));//4或8 C语言标准规定:sizeof(long)>=sizeof(int)
	printf("%d\n", sizeof(long long));//8
	printf("%d\n", sizeof(float));//4       float 精度低
	printf("%d\n", sizeof(double));//8      double 精度高
	return 0;
}
//%d 打印10进制的整数     注意:"%"不要漏!!!
//sizeof-->......的大小

Summarize:

type of data Size (unit: bytes)
char 1
short 2
int 4
long 4/8
long long 8
float 4
double 8

Attached:

unit Unit conversion
bit /
byte 1 byte = 8 bit
KB 1 KB = 1024 byte
MB 1 MB = 1024 KB
GB 1 GB = 1024 MB
TB 1TB = 1024GB
PB 1 PB = 1024 TB

type of use

int main()
{
    
    
    char ch = 'w';//单个字符用单引号('')引起来   字符串用双引号("")引起来
    int weight = 120;
    int salary = 20000; 
    return 0;
}

4. Variables, constants

Some values ​​in life are constant , such as: gender, ID number, blood type, etc.; some values ​​are variable , such as: age, weight, salary, etc. The constant value is represented by the concept of constant in C language ; the variable value is represented by the concept of variable in C language .

4.1 Method of defining variables

int main()
{
    
    
     int age = 150;
     float weight = 45.5f;
     char ch = 'w';
     return 0;
}

tips:
(i) Pay attention to adding the data type when defining a variable
(ii) When defining a floating-point number, no matter whether the data type is float or double, the compiler defaults to double. To make it into a float type, add Upper 'f', such as: float weight = 45.5f;

4.2 Naming of variables

  • Can only consist of letters (both uppercase and lowercase), numbers, and underscores ( _ ). (eg: int a$b is wrong)
  • Cannot start with a number. (eg: int 3c is wrong)
  • Cannot exceed 63 characters in length.
  • Variable names are case sensitive. (eg: int _3C and int _3c are different)
  • Variable names cannot use keywords. (eg: int float is wrong)

4.3 Classification of variables

  • local variable
  • global variable
int b = 20;//全局变量-->大括号外面的

void test()
{
    
    
	int c;//局部变量
}

int main()
{
    
    
	int a = 10;//局部变量-->大括号里面的
	
	return 0;
}
#include <stdio.h>

int a = 20;

int main()
{
    
    
	int a = 10;
//局部变量和全局变量的名字可以相同,当我们既可以使用局部,又可以使用全局变量的时候,局部变量优先。
	printf("%d\n", a);

	return 0;
}

4.4 Use of variables

Write a program to calculate the sum of 2 integers

#include <stdio.h>

int main()
{
    
    
	int num1 = 0;
	int num2 = 0;
	int sum = 0;

	//1.输入2个整数
	scanf("%d %d", &num1, &num2);//scanf - 输入   &-->取地址
	//2.求和
	sum = num1 + num2;
	//3.输出
	printf("%d\n", sum);//printf - 输出/打印

	return 0;
}

Tips:
(i) \n means newline, don’t omit to add
(ii) do not add \n after the scanf function

4.5 Scope and Lifecycle of Variables

scope:

  Scope ( scope ) is a programming concept. Generally speaking, a name used in a piece of program code is not always valid/available,
and the code scope that limits the availability of this name is the scope of this name.

  • The scope of a local variable is the local scope in which the variable resides
  • The scope of global variables is the entire project

The scope of a local variable is the local scope in which the variable resides:
local variable scope


The scope of global variables is the entire project:

Example 1:
global variable scope

Example 2:

//第一个源文件:add.c

int g_val = 2022;//全局变量 --> 作用域是整个工程
//第二个源文件:test.c

#include <stdio.h>

extern int g_val;//extern是用来声明外部符号的

int main()
{
    
    
	printf("%d\n", g_val);//所以这里的g_val能被打印出来

	return 0;
}

Note: The source file of C language is .c, not .cpp


life cycle:

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

  • The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of the out of scope ends.
  • The life cycle of global variables is: the life cycle of the entire program.

4.6 Constants

The constants in C language are divided into the following categories:

  • literal constant
  • const modified constant variable
  • Identifier constants defined by #define
  • enumeration constant

Literal constants:

int main()
{
    
    
	//100;
	//3.14;
	//'a';
	//"abcdef";
	return 0;
}

Constant variables modified by const:

int main()
{
    
    
    const int n = 10;
	
	n = 20;//这样写是错误的,因为n已经被const修饰,不能改变它的数值了

	int arr[10] = {
    
    0};//数组,这样写是正确的
	
	int arr[n] = {
    
    0};//这样写是错误的,因为arr[] = {0}的[]中填的应该是常量
    
    return 0;
}

Summary: After being modified by const, the value cannot be changed, so it has constant properties , but it is essentially a variable


Identifier constants defined by #define:

#include <stdio.h>
#define MAX 100

int main()
{
    
    
	printf("%d\n", MAX);
	
	MAX = 200;//这样写是错误的,因为MAX是被#define定义的标识符常量
	
	return 0;
}

Enum constants:

//枚举 -> 一一列举
//性别:男、女、保密
//三原色:红、绿、蓝
//枚举的关键字-->enum
#include <stdio.h>

enum Sex
{
    
    
	//下面是enum Sex类型变量的可能取值,这三个可能取值就是枚举常量
	MALE,
	FEMALE,
	SECRET
};

int main()
{
    
    
	enum Sex s = MALE;//性别
	
	printf("%d\n", MALE);//0
	printf("%d\n", FEMALE);//1
	printf("%d\n", SECRET);//2
	
	return 0;
}

Attached:

Through these 14 points, you can quickly get started with C language (2)
Through these 14 points, you can quickly get started with C language (3)
Through these 14 points, you can quickly get started with C language (4)

Guess you like

Origin blog.csdn.net/weixin_73077334/article/details/126175252