Home >Backend Development >C#.Net Tutorial >Introduction to C language: talk about basic knowledge (data types, variables, functions, arrays, etc.)

Introduction to C language: talk about basic knowledge (data types, variables, functions, arrays, etc.)

青灯夜游
青灯夜游forward
2022-07-08 10:38:563965browse

This article will help you learn C language and talk about the basic knowledge of C language (data types, variables, functions, arrays, etc.). I hope it will be helpful to everyone!

Introduction to C language: talk about basic knowledge (data types, variables, functions, arrays, etc.)

What is C language

  • Simply put

C language is a computer language , widely used in low-level development, using the language to write code programs and solve problems

So for the computer major, C language and learning C language are very important


Computer Language Development

As far as computers are concerned, the initial implementation of binary code (1/0) was achieved by powering on the computer to communicate with the computer and then forming a binary code

But It was too troublesome, so we developed mnemonics (assembly language), and then formed the B language, and then developed the C language

And then various interpreted languages ​​appeared (Java, python, etc.)


Write the first C language code

  • Tools

Recommended VS2019 compiler

Basic format

#include<stdio.h>  
//<>内是头文件名称;stdio代表standard input output;     即标准输入输出头文件(与后面所执行任务要用的库语言所关联)
 
int main()               //主函数,程序的入口(有且只有一个);
 
{   //int 代表整型;即表示main函数调用返回整型值
 
   任务;
 
  return 0;
 
}

Data type

char character short (int) short integer type int integer type long (int) long integer type long long (int) long integer type

float single-precision floating-point type double double-precision floating-point type (integer type is used for integers, and floating point type is used for decimals)

There are so many data types, it is for more convenience It’s good to apply for memory space from computer (try to save space and optimize memory )

unit

From the above The memory applied for each data type is: 1 2 4 4 8 4 8 (unit bytes, individual differences vary depending on the number of computers)

Example; short is 2 bytes, which is 16 bits (binary)

Range: the minimum is all 0, which means 0; the maximum is all 1, the range obtained by the weight bit is 2*10^16-1


Variables

  • Type

Variables are divided into local variables and global variables

Scope

Local variables: In the local scope where local variables are located

Global variables: the entire project

Life cycle

Local variables: the period starts when entering the local scope and ends when leaving

Global variables: the life cycle of the program

Note: When the defined variable has the same name, the local priority## in the local scope #;

C language and law stipulate that

variables must be defined at the front of the current code block.


Constant

Type of constant in C language:

    Literal constant: 3.14, "abc", etc.
  • Constant variables modified by const: const—constant attributes, the essence is constants defined by variables
  • #define: Example: #define MAX 100
  • Enumeration constants: enum Enumeration: enumerate one by one; Example: enum Sex {male, female, secret}

Function

In the coding process, it is inevitable to encounter the repeated use of a certain set of statements. Creating a function at this time can make coding much easier and faster - simplifying reuse.

    For example, create an addition function (custom)
int Add(int x, int y)
{
  int z = 0;
  z = x + y;
  return z;   
}
int main()
{
  int a = 10;
  int b = 20;
  int ret = 0;
  ret = Add(a,     b);  
  printf("%d\n", ret)
  return 0;
}

Array

The array is an Grouping a collection of elements of the same type

    Creating an array is also equivalent to applying for space from the computer. It is a connected space with a label
  • For this array, its label starts from 0 Initially, the elements in the array are generally accessed in the form of array subscripts
  • The array name is also a special address
Array initialization


Operator

Arithmetic: Multiply* Division/Remainder % Addition Subtraction-

Shift (2 Base): First represent the number in binary and shift it, and then represent it into the corresponding number after the shift

 位操作

  • 按位于:两个数以二进制竖着排列,有0则为0,都是1才为1

  • 按位或:两个数以二进制竖着排列,有1则为1,都是0才为0
  • 按位于:两个数以二进制竖着排列,相同则为0,相异才为1

赋值

注意区别=与==:一个是赋值,一个是判断相等

单目操作

(操作数个数决定是单还是其他,例 1+2:1和2是操作数,为双目操作符)

关系/逻辑/条件

 

 

  • 解释: 表达式1成立,结果为表达式2,否则为3

逗号表达式

  • 解释:从左向右依次计算,结果去最后一个表达式 


关键字


字符串

定义

即“ ”中的内容(例:“abc”)

结束标志

  • “\0”(\0不做字符串的内容)
  • 注:字符串可以存放在字符数组中;C语言无字符串类型

局别

  •  示图1中的arr2数组元素型初始化,它的长度未定义,会随机生成,直到遇到“\0”,来结束字符串

 求字符串长度

sizeof(arr[])计算内容包括“\0”,算作一个bite

strlen(arr)不包括“\0”,计算字符串内容长度(需要审引库函数—

转义字符

\0是一个字符,还有\t,\n等代表不同意思的字符

转义字符则是转变原来的意思

例如你想单纯打印\n,那么则需要在“\n”前再打一个“\”,来转变“\n”原本的意思

注释

注释即用来注明,解释代码步骤的意思,让自己和读者能更好的理解

C语言——/*   */  

C++——//
  • 注意:除了用来解释,还可以删除不需要的代码;注解不能嵌套使用


选择语句 

if(条件)                     多选择:if(条件)

执行语句;                                 执行语句;

else    \\反之                                else if(条件) 

执行语句;                                  执行语句;

                                                    else...

循环

while循环:                                        
初始化;
while(条件)

{  执行和调整语句;}

for循环
for(初始化;条件;调整)

{    执行语句; }

do while循环
do

{  执行和调整语句;}

while(条件)

注:while先判断条件,符合再执行语句,而do while循环先执行语句,再判断条件是否再进行循环;在长幅篇的代码中,用for循环比较适合,用while不利于更改如果有需要的话

相关推荐:《C视频教程

The above is the detailed content of Introduction to C language: talk about basic knowledge (data types, variables, functions, arrays, etc.). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete