Part 1: preparation
1. Download and install the compiler. C language needs to be interpreted by the compiler into machine code that the computer can understand. Compilers are usually free, and different compilers are generally used on different operating systems. For Windows system, you can try Microsoft Visual Studio Express, which is the most popular multilingual IDE (integrated development environment) on Windows platform, and it integrates the C language compiler developed by Microsoft.
Xcode is an excellent multilingual IDE for OS X system, which integrates the compiler of C language.
For Linux, gcc is a good and most popular choice.
2. Understand the basic concepts. C language is an ancient language, but it is very powerful. It was originally designed for Unix operating system, and then it was transplanted to almost all operating systems and got many extensions. The modern version of C language is C++. C language is essentially composed of functions, in which variables, conditional statements, loops and other statements can be used to store and process data.
3. Check out some basic codes. The following is a very basic code written in C language. Read these codes, try to understand how different parts of this language work, and have a preliminary understanding of the operating principle of the program.
# include & ltstdio.h & gtint main() { printf("Hello,World!
"); getchar(); Returns 0; } The #include instruction here appears before the program starts, and its function is to load the library containing the functions you need. In this example, stdio is introduced. H enables us to use the printf () and getchar () functions.
The main () instruction here will tell the compiler that the program needs to run a function named "main", which will return an integer value after running. All C languages must run a "main" function.
The {} symbol indicates that everything in parentheses is part of the function. In this example, they mark everything as part of the "main" function.
The printf () function can display the contents in parentheses on the user screen. Double quotes ensure that the character is output exactly as it should be.
This combination tells the compiler to move the cursor to the next line after the string is output.
; The symbol indicates the end of a line. Most C codes end with semicolons.
The getchar () function tells the compiler that the program cannot continue running until a key is entered. Considering that most compilers will close the program window immediately after running the program, this function is still very useful because it can keep the program running until a key is pressed.
The return 0 instruction indicates the end of the program. Please note that the "main" function is an int type function, which means that an integer needs to be returned at the end of the function. If 0 is returned, it means that the program is executed correctly, and other numbers mean that there is an error when the program is running.
Try to compile this program. Enter the above code into your code editor and save it as *. C "file. Compile with your compiler. Generally speaking, just click the Build or Run button.
5. Develop the habit of taking notes. Comments are part of the code. It won't be compiled, but it can tell you what the code did. This is very helpful to remind yourself what your code does and let other developers understand your code. To add comments in C language, you only need to add/* before the part to be commented and */after it.
Don't be stingy with your notes, except for those that are particularly simple and clear, try to add notes.
The comment function can also quickly block some code without deleting it. As long as you wrap the code you want to exclude with comment tags, they will not be compiled. If you want to change it back, just delete the comment tag.
Part 2: Use of variables
1, understand the function of variables. Variables are used to store data, whether calculated or input by users. Variables should be defined before use and there are different types to choose from. There are several common variable types: int, char and float. Each variable type represents a data storage format.
2. Learn to declare variables. Variables should be created before use, or called "declarations". To declare a variable, just write the name of the variable after the variable type. For example, here are some examples of variable declarations:
Floating x; Character name; int a,b,c,d; Note that you can declare multiple variables in one line as long as they are of the same type. You just need to separate the variable names with commas.
Like most C codes, the declaration of variables ends with a semicolon.
3. Know where to declare variables. The declaration of variables must be placed before each code block (a code block refers to a piece of code enclosed in braces). If variables are declared after the code block, the program will not execute correctly.
4. Use variables to store user input. Now that you know the basic principles of some variables, you can write a simple program to store the user's input. This time you need to use another function called scanf, whose function is to assign the user's input to the specified variable.
# include & ltstdio .h & gtint main(){ int x; Printf ("Please enter a number:"); scanf( "%d ",& ampx); Printf ("You entered: %d", x); getchar(); Returns 0; } The "%d" symbol here tells the scanf function to find an integer in the user input.
& in front of x; This symbol tells scanf where to find the variable to be modified and stores the input integer value.
The last printf command reads the entered integer and returns it to the user.
5. Handle variables. You can use mathematical expressions to handle previously stored variables. We need to pay attention to an important difference: in mathematical expressions, single = is an assignment number, which is used to assign the value on the right of the equal sign to the variable on the left of the equal sign, while = = is used to compare whether two variables are equal.
x = 3 * 4; /* Let x be 3*4, that is,12 */x = x+3; /* Increase the value of x by 3, and then assign the new value to x */x = =15; /* Check whether x is equal to15 */x.
Part 3: Using Conditional Statements
1. Understand the basic concepts of conditional statements. Most programs are driven by conditional statements, which can judge whether a condition is true or false, and then perform different actions accordingly. The most basic conditional statement is the if statement. TRUE and FALSE in C language are a little different from what you usually understand. TRUE and any number other than 0 are always equal. When you perform a comparison, if the result is true, it will return "1". If the result is false, it will return 0. Knowing these can help you better understand the execution process of IF statement.
2. Learn basic comparative symbols. Conditional statements are based on relatively large mathematical expressions. Here are some of the most commonly used comparison symbols:
& gt/* is greater than */
10 >5 TRUE6 & lt 15 true 8 & gt; = 8 TRUE4 & lt= 8 TRUE3 == 3 TRUE4! = 5 true
3. Write an IF statement. Using the IF statement, you can decide how to run the program after evaluating an expression. After learning other conditional statements, you can combine them to achieve more powerful functions, but now you can get familiar with them by writing a simple code.
# include & ltstdio.h & gtint main(){ if(3 & lt; 5) printf( "3 is less than 5 "); getchar(); }
4. Use ELSE/ELSE IF statement to extend your conditional judgment. In the IF statement, you can add ELSE and ELSE IF statements to handle more different results. When the judgment result in IF is FALSE, the statement after ELSE is executed. ELSE IF allows you to use multiple IF statements in a code block to handle more situations. Read the code below to see how they work.
# include & ltstdio.h & gtint main(){ int age; Printf ("Please enter your age:"); scanf( "%d ",$ age); If (age<= 12) {printf ("You are a child!
" ); } else if (age & lt20) {printf ("It feels good to be young!
" ); } else if (age & lt40) {printf ("You are full of youthful vitality!
" ); } else {printf ("the age full of wisdom!
" ); } returns 0; } This code receives the data input by the user and passes it to the IF statement. If the data meets the first condition, the first printf is executed. If the first condition is not met, the subsequent ELSE IF will judge one by one until a branch that meets the condition appears. If there are no branches that meet the conditions, execute the ELSE statement. .
Part four: Learning circular sentences.
1, understand the principle of circulation. Loop is an important part of programming. They allow you to execute a piece of code repeatedly until certain conditions are met. This mechanism makes it easy for you to repeat actions, and it also saves the trouble of making conditional judgments every time. There are three types of loops: FOR, WHILE and DO? And.
2. Use the FOR loop. This is the most common and easy to use loop type. It will continue to run the function in the loop until the loop condition no longer holds. The FOR loop needs to contain three statements: initializing variables, loop conditions, and how to update variables. If you don't need one of the statements, just add a semicolon in the space, otherwise the loop will run indefinitely.
# include & ltstdio.h & gtint main(){ int y; for(y = 0; y & lt 15; y++; ){ printf( "%d
“,y); } getchar(); } In the above program, y is set to 0, and the condition for the loop to continue running is that y is less than 15. The y value in each period is printed and increased by 1. Once y= 15, the loop ends.
3. Use the WHILE loop. The WHILE loop is much simpler than the FOR loop. They only have one statement, and as long as the statement is true, the loop will continue to execute. You don't need to initialize or update variables, but you can do these things in the loop body.
# include & ltstdio.h & gtint main(){ int y; while(y & lt; = 15 ){ printf( "%d
“,y); y++; } getchar(); Every time this loop is executed, the y++ command will increase the value of y by 1. Once y reaches 16, the cycle ends. (Remember, the loop will only be executed if y is less than or equal to 15. )
4. use DO? WHILE loop. This loop is very useful when you want to ensure that a loop is executed at least once. In FOR and WHILE loops, the loop condition is detected before the loop starts, which means that it may not pass the first detection, and then the loop body will not execute it once. But, do it. . . WHILE loop executes the loop body once before detection, which ensures that the loop body is executed at least once.
# include & ltstdio.h & gtint main(){ int y; y = 5; Do {printf ("loop executed!
"); } while ( y! = 5 ); getchar(); } In the above loop, even if the result of loop condition detection is false, a message will be displayed. The value of the variable y is set to 5, and the WHILE loop is set to run only when y is not equal to 5, so the loop will terminate when a condition is detected. But the information is still displayed because the condition detection is after the information is output.
Do what? The WHILE statement in the WHILE loop must start with; The ending. This is the only case where the loop body ends with a semicolon.
Part 5: Using Functions
1, understand the basic principle of the function. Functions are self-contained blocks of code that can be called by other parts of the program. Using functions can make it easier for you to repeat a piece of code and make the program easier to read and modify. Functions can contain all the technologies mentioned above, even other functions. Main () in the above example is a function, so is getchar ().
Functions are very important to write efficient and readable code. Using functions well can make your program clearer.
2. Start with the function prototype. Before you really start writing a function, you'd better figure out what function you want to complete and start writing from the prototype of the function. The basic syntax format of the function is: "return value type function name (parameter 1, parameter 2,? ); "。 For example, here is a function that adds two numbers:
int add ( int x,int y); The above code creates a function that adds the inputs x and y and returns their sum.
3. Add the function to the program. You can use the above function prototype to realize the function of adding two numbers input by a user. The following program shows how the "add" function handles the input number.
# include & ltstdio .h & gtint add ( int x,int y); int main(){ int x; int y; Printf ("Please enter two numbers to sum:"); scanf( "%d ",& ampx); scanf( "%d ",& ampy); Printf ("The sum of the numbers you entered is% d..
" add( x,y)); getchar(); }int add ( int x,int y){ return x+y; Note that the prototype of the function also needs to be placed at the top of the program, so as to ensure that the compiler knows the existence of the function and its return type when calling the function. However, this is only necessary if you want to implement the function after it is called. If you put the implementation of the add () function directly before the main () function, it is the same even if you don't declare the function prototype.
The implementation code of this function is actually placed at the bottom of the program. The main () function gets two integers input by the user and passes them to the add () function for processing, and then the add () function returns the calculation result to main ().
When the add () function is defined, you can call it anywhere in the program.
Part VI: Continuous learning
1, find some books related to C language programming. This guide covers the most basic part of C language, but it is only a scratch for the complete system of C language. If you can have a good reference book, you can save a lot of trouble on the way to learning C language.
2. Join some clubs. Whether online or offline, there are some great communities dedicated to learning and developing excellent programming languages. If we can find some like-minded C language programmers and communicate with them, we will certainly make rapid progress. If possible, you can also try the hackathon. Organizations or individuals who participate in this activity need to put forward their own procedures and solutions to given problems within a limited time, so it can cultivate people's creativity. You can also meet many excellent programmers. The hackathon is also held regularly all over the world.
3. Take some courses. Although you don't need to go back to school to get a computer degree, taking some related courses properly will make your learning process a qualitative leap. Nothing can help you more than the words and deeds of a C language expert. Usually, you can always find some training courses online, and there are also some professional computer training institutions to choose from. There are also excellent courses in some universities that are open to the public free of charge and can be attended.
4. consider learning C++. If you have mastered the C language, knowing C++ will be of great benefit to you. Because C++ is a more modern version of C language, which is more flexible and convenient. C++ is designed with the idea of object-oriented. After mastering C++, you can write powerful programs in almost all operating systems.
Prompt to write more comments to the program. Comments can not only help others who may see your code better understand it, but also help you understand what your code means and why you write it this way. When writing code, you may know exactly what you want to do, but what about two or three months later? You may have forgotten.
If you are troubled by grammatical errors during compilation, remember to use Google or other search engines to search for the problems you encounter. It is possible that someone encountered the same problem and posted a solution.
Your source code needs to be in the. C extension suffix, so that the compiler can know that this is a C language source file.