The last time I learned C was when I was a freshman. I made up my mind that I would never learn programming in my life. And then it really smells good.

Indeed, C language for the pit of the first language is more difficult, at that time also did let me back, after a long programming, just feel, C language is really YYDS, now look back, there should be some new harvest.

C, a small and simple language, do not need to learn a lot, just like practicing sword, only with a sword, you can fight the sword to go. But you only have one sword, and no matter what opponent you meet, you can only hand the sword in your hand. And like Java, when walking home with mouth, when fighting on the aircraft cannon casually used. This depends on each person’s belief, if you like simple and easy, C is undoubtedly the best choice.

C is just a language, the same as Chinese and English, but in a different way. Its purpose is to write down our thoughts in a language we understand, and then the compiler translates them into a language that the machine can read, and then the machine runs to get the results we want. So it doesn’t matter what editor or compiler you use, what matters is understanding the difference between the way a human brain thinks and the way a machine thinks. But it’s not easy, and it takes a lot of programming to figure it out.

Every language has its own syntax, and C has its own syntax. You have to follow a rule.

Introduction to C Language

In 1972, Bell LABS, Dennis Ritch and Ken Thompson invented C for Unix.

Its predecessor is B language, developed by Thompson.

The biggest characteristic of C language is: concise, efficient, flexible, powerful. In fact, this is not a big deal, the products of that era are like this, the big guys just want to make programming a little bit more visual, the object is assembly.

C just to make the assembly look better.

Second, portability is a big reason for its creation. Since assembly language is a direct reference to hardware, the need to write a new language without switching machines is a real headache.

With C, one set of code, multiple compilers can be ported smoothly.

In those days, anyone who could write code was now considered a god, so memory management was not an issue, unlike today, when big companies design languages that don’t let programmers manage memory, after all, you never know what kind of mess you’ll get.

The greatest feature of C is direct access to the physical address, which is also the sword in our hands. If you use it well, you can cut off everything. If you don’t use it well, the graveyard grass may be three meters high.

The syntax of C can be learned in a week, but the study and use of Pointers, that is a lifetime.

The grammar of the C

From math to computers

30 * 3; // statements that need to be identified by semicolons, like English words separated by Spaces. {30 * 3; } // Paragraph (curly braces), the words inside must be separated by a semicolon. // The inner bracket can separate the outer bracket; // It has a one-way perspective, just like the black film on a car, the inner layer can see the outer layer, and vice versa. // A mathematical function, input a number for processing to get a result. Int main() {30 * 3; f(x) = 3*x + 3; return 0; } // It is not necessary to give a number, and it is not necessary to have a result. A function is a process of processing some data.Copy the code

The basic grammar

Let’s start with hello World, which everyone knows how to write:

//#include <Windows.h>

#include <stdio.h>
// The preprocessor directive begins with a # and contains the stdio.h header
//<> start the search from the system location. "" Start the search from the current source file directory.
// Standard Library

int main(a)// The main function, where it starts and ends
{
	int num;
	//int indicates the type, which is the Keyword; Num is a variable name and a type of identifier.
	// declare a variable called num. All variables should be declared before they are used.
	//C99 and C11, like C++, only need to be declared before use. The old C standard was that all variables had to be defined at the beginning.
	// The identifier consists of letters, digits, and underscores (_), and cannot start with a digit.

	num = 1;/ / assignment.
	// Create and mark storage space for variables. Uninitialized values are random.

	int num2 = 2; // Assign values when declared, called initialize.

	printf("Hello World! num:%d\n",num);
	// The system function formats the output
	// The actual arguments in parentheses are called formal arguments.
	After the function is executed, control is given to the calling function, the main function.
	//\n stands for newline character, which is an escape sequence.
	//%d represents a placeholder to indicate the position of the output num.

	//system("pause");
	return 0;// Return a value, normally 0. To whom? The return value of the main function to the operating system.
}

// The function body is enclosed in {}, each statement; The end of the

/ * * * * * * * * comment * * * * * * * * * * this annotation, also called a piece of comment / / * * note this comment line cannot be nested * comment line can block comment * pay attention to the comment line and line breaks together of * * * * * * * * * * * * * * * * * /

// Why not build in input/output: Because not all programs need output.

/ * * * * * * * * * * * * * * * * * * to declare variables to use: * define variables in one place, make it easier for readers to find and understand the purpose of the program. * Declaring variables forces you to do some planning before you write a program. * Helps you find small errors in your program, such as misspelled variable names. * * * * * * * * * * * * * * * * * /

Copy the code

This code is from my previous notes, with the basic syntax highlighted. As you can see, the grammatical structure of C is top-down, as if you were writing an article or building a house.

We start with the foundation, and then we build brick by brick, and when we find things that can be packaged together, like Windows (which contain aluminum and glass), we encapsulate them into a function so that we don’t have to build Windows every time. From the bottom up, logical.

Structural features of C program:

C program source program preprocessing command + global variable declaration + function function (function head + function body) function body (local variable declaration + execution statement)Copy the code
C language structure: #include // precompiler directive int main(void) //main() is always the first function to be called statementsCopy the code

C language statement composition:

  • Data + operator + identifier + keyword = statement
  • The statement is divided into: labeled statement, compound statement, expression statement, select statement, iteration statement, jump statement.

C language implementation

7 Steps to using C:

  1. Define program goals and analyze problems using concepts, not computer language.
  2. Design program, interface, organization, goals, duration.
  3. Write the code
  4. A compiler is also a program whose job is to convert source code into executable code.
  5. To run the program
  6. Test and debug programs, bugs.
  7. Maintain and modify programs

compile

C programming generation (compiled language) :

Source file – compiler – object code – linker (library file, startup file, etc.) – executable file

Editor + Compiler + Debugger = IDE(Integrated Development Environment)

Compiler: GUN Compiler Collection (GCC) and MinGW (GCC tools for Windows)

GCC processing process:

  1. The preprocessing stage is used to process all preprocessing instructions.
  2. Compilation is the process of converting to a binary file.
  3. Links merge all the object text and other necessary files together to get the final executable.
GCC -e preprocesses only the source file. -std=c89/c99 Specifies the C language standard to be complied withCopy the code

Development steps of C language program under Linux:

  1. Use VI to write C language source program, save and exit after completion.
  2. Use GCC < source file name > to process the source program to get the computer recognized executable file.
  3. Execute the resulting file using the command./a.out.

Development steps under Windows:

Use cl filenames.c and then filenames.exe

Writing specification of C language:

  1. Try to place one statement in a line. Long statements can be placed in multiple lines.
  2. Use Spaces where appropriate to improve readability.
  3. Statements enclosed in the same brace are left aligned, and statements enclosed in different braces are separated by indentation.
  4. Statements that are not closely related can be separated by Spaces.
  5. Identifier:
    • Small Hump, XiAn, Windows platform
    • Underscore, xi_an,Linux platform

Error type:

  • Grammar mistakes
  • Semantic error

Debugging method:

  1. Simulate the computer itself.
  2. Print debugging.
  3. Breakpoint debugging.

conclusion

  • If something is wrong, please point it out.
  • If you don’t understand, please point out and LET me add chestnuts.
  • If you feel OK, you can like it and let more people see it.