当前位置:网站首页>Simple understanding of basic language of C language

Simple understanding of basic language of C language

2022-06-13 07:06:00 Though even

Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right

List of articles

  • Preface
  • One 、 About C Language my understanding
  • Two 、C Basic grammar of language
    • 1. first C Language program
    • 2. Basic data type
    • 3. Constant 、 Variable
    • 4. character string 、 Escape character 、 notes
    • 5. Judgment statement 、 Loop statement
    • 6. function
    • 7. Array
    • 8. The operator
    • 9. Common keywords
    • 10.define Define constants and macros
    • 11. The pointer
    • 12. Structure
  • summary


Preface

With the development of society , Nowadays people rely more and more on electronic devices . Mobile phone is a typical electronic device . Mobile phones can be said to have been a part of people's bodies , If a person can not use a mobile phone one day , Then there are only a few possibilities : He is an old man , Or individual children and other special groups . Most of us can't turn on our cell phones every day , We are attracted by all kinds of software on mobile phones , Such as QQ, Microblogging , Tiktok , Games, etc. . So how did the software come out ? You may all know , Written in code , The content of this article written today is the most basic thing in the code . Next , I will introduce myself to C Language understanding , and C The basic grammar of language .


One 、 What is? C Language ?

Official explanation :

C Language is a language Process oriented Of 、 Abstraction The general programming language of , Widely used in Bottom development .C Language can be in a simple way compile 、 Dealing with low grade Memory .C Language produces only a small amount of machine language And no need Running environment Support an efficient programming language that can run . Even though C Many low-level processing languages are provided function , But still keep Cross platform Characteristics of , Written in a standard specification C Language programs can include similar Embedded processor as well as supercomputer And many other operating platforms Computer platforms on compile . [1] 

I know :C Language is a high-level language for human communication . like , We learn English the same , To be able to communicate with foreigners and understand the language of foreign books , Learning knowledge .

Two 、C Basic grammar of language

1. first C Language program

The code is as follows :

#include<stdio.h>

int mian()
{   
    printf("Hello world");
    return 0;
}

I feel like learning a language , The first program is usually output “Hello world". Just follow the instructions .

2. Variable 、 Constant

Constant :

Constant , That is, constant quantity  

stay C In language , Constants are easy to understand, for example : A number :2, He just can't change the quantity , So it's called a constant . 

Variable : 

  Variable , As the name suggests, it is the quantity that will change , And variables are quantities that can be changed .

Variables can be divided into two types and their scopes and lifecycles :

Global and local variables :

Scope : That is, the scope of action

Life cycle : The range from variable start to variable destroy

Global variables : Variables written outside curly braces

local variable : Variables in braces

#include<stdio.h>
int result = 12;// Global variables , Variables that everyone can use 
int main()
{
	int x = 16;// local variable , That is, variables in braces ,
	// It can only be used inside curly braces 

	int x1 = result + x;// Global variables can be used by everyone 
	return 0;
}
int Add(int a, int b)
{
	int c = 0;
	//c = x + a + b;// It's used here ,main The local variable inside is wrong 
	c = result + a + b;// Global variables can be used by everyone 
	return 0;
}

3. data type  

Computer language is to solve the problems in our life , We will encounter all kinds of data in our life , such as : The age of a person is usually an integer , A person's height is a decimal , A person's name is made up of characters and so on . The same is true of computer languages .

   

    int // integer , It's an integer 
	char // Character type , Such as the letter ‘a’
	short // Short , That is to say, the range of the representation is larger than that of int smaller 
	long // Long integer , Than int The scope of representation should be large 
	long long // This ratio long  Even bigger 
	float  // floating-point   , It's actually a decimal 
	double // Double precision type , Than float The precision of type is large 
     // What data , Just use what type , Otherwise, it must be wrong 

 4. character , Escape character , notes  

A character is a simple symbol such as :‘t’ ‘ Yang ' It's all characters ;

Escape character , It means it's not what he meant , We prefix the characters with   \  , such as '\t' It's not a character   ‘ t’ It means , It means ”Tab“ That is, tabs . This is to make it easier to write code .

Common escape characters are :

  These things don't need to be remembered , When we type too much code, we will remember .

notes : That is to let others understand your article , The explanation you added next ;

Notes are generally in the form of    //    and /*    */ Two kinds of , The former can only be a one line comment , You can't change your career , The latter can be a paragraph ( A lot of things ) notes

5. Judge sentence patterns and circular sentences  

Judgment statement : In life , We often encounter things to judge . such as : When we buy things , If we have enough money, we'll buy , Or go away . So the judgment statement is to complete this task .

#include<stdio.h>
// Judgment statement 
int main()
{
	// The judgment statement has   if   else They are a couple 
	// The format is as follows 
	int a = 1;
	//if There must be a judgment statement in parentheses , Only when it is true , To perform 
	if (a > 0)// If the conditions are met , Is executed if What's in the curly braces behind you 
	{
		printf("a>0");
	}
	else {// otherwise , Is executed else Statements followed by braces 
		printf("a<=0");
	}

	return 0;
}

  Loop statement : And the cycle, as the name suggests , Just do it over and over again . When we do one thing at a time , The steps are the same , Then we can use circulation to help us improve efficiency . 

#include<stdio.h>
// Loop statement   
int main()
{
	// There are two main types of loop statements while and for()
	int a = 1;
	int i = 0;
	while (a<5)
	{
		printf("Hello world!\n");
		a = a + 1;
	}
	//for Loop statement where i = 0 Is the beginning of the cycle ,
	//i<5 Is the condition for the end of the loop 
	//i++ In fact, it is i= i+1, Represents the span of each cycle 
	for (i = 0; i < 5; i++) {
		printf("Hello world\n");
	}
	return 0;
}

6. function

function : A function is the body of code used to implement a function . 

#include<stdio.h>

// This is the function , Used to add , Simplified code 
int Add(int x, int y) {
	int c1 = 0;
	c1 = x + y;
	return c1;
}
int main()
{
	int a = 12;
	int c = 56;
	int result = a + c;
	printf("%d", result);
	// This code can only calculate the sum of two numbers we want once 
	// If we have to do it many times , That's it 
	int a1 = Add(12, 56);
	printf("%d", a1);
	return 0;
}

7. Array  

Array : A combination of the same elements

For example, we will 1~10 Save your numbers , Where do we exist , The answer is arrays

Definition of array An array type + Array name +[  ] In square brackets is the array size , There are several elements in it

Such as :int arr[10] = {1,2,3,4,5,6,7,8,9,10}; Ahead int Is the type of array , That is to say, the elements stored inside are int type , In square brackets 10 It specifies that this array can only be used for 10 Elements , The curly braces are the elements to be placed ; 

Index of the array : Be careful :[ ] Here is an operator , The object of the operation is arr And the numbers inside

#include<stdio.h>

int mian()
{
    int c = 0;
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
// The subscript of an array is from 0 Start counting , Namely arr[0]  Namely  1; Count from left to right 
    c = arr[5]// In fact, that is arr[5]==6,c == 6;
    printf("%d\n",c);
    return 0;
}

8. The operator  

Operators have :

 

Notice that multiplication and division are not the same as math , At the same time, the operation of division is a little different from that in mathematics ,C In language   ”/“ The result of division is an integer , namely 7/2=3......1, hinder 1 Don't just , The result is equal to the 3;

Monocular operators  

  Relational operator

< 
<=
>
>=
==   C Equality in language is two =
!=    It's not equal to 

Logical operators

&&    Logic and  , Just and , That is, in mathematics “ And ”
||    Logic or , Or , It's in mathematics “ or ”

  Conditional operators

exp1? exp2 : exp3 ;
//exp1 Is an expression , If exp1 Establishment and implementation exp2, Otherwise execution exp3

  Subscript call , Function call , Members use

[ ] Is the subscript reference operator 
( ) Is a function reference operator 
. The decimal point is the structure member caller 

Comma expression

The operation order is from left to right

9. Common keywords

Common keywords are

  Be careful ; Keyword cannot be used as your variable name

10.define Define constants and macros

  use define Define constants , It is equivalent to a global variable , It can be used anywhere in that function . It is placed in the static area

I will slowly realize  

11. The pointer

11.1 Memory

Before we know about pointers, let's learn about memory

When the computer is working , It runs in memory , Therefore, many memory units will be opened in the memory , The size of each memory unit is 1 byte . Each memory cell will have a number , This number is the address .  How do we store this address , Now we will use the pointer .

11.2 The pointer

#include<stdio.h>

int main()
{
	int a = 12;// Variable assignment 
	int* pa = &a;// take a Assign an address to a pointer variable pa
//pa Is a variable name. ,* No. is a pointer variable type ,int What is stored in the pointer is int type 
	printf("%d", sizeof(pa));// The size of the pointer is different in different compilation environments ,32 Is it 4 byte ,64 Is it 8 byte 
	return 0;
}

12. Structure

  When we have to output a lot of personal data , such as : height , weight , Student number ;

So what should we do , At this time, use the structure to solve .

  Use of structures :

 


summary


That's what we're going to talk about today , This article only briefly introduces C The use of some simple grammar in almost all languages , And a language that knows grammar is only the simplest , We still have to learn how to use !!

原网站

版权声明
本文为[Though even]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130654432440.html