C language program editing and learning-C language for programmers

Source: Internet
Author: User
Tags rewind
When talking about the C language, I think all my friends who have learned it feel like "Let me rejoice and let me worry ." I'm glad that the C language is very powerful and widely used. Once you have mastered it, you can say to others, "I am a computer expert !", In addition, it will be easy to learn other languages in the future. The concern is that the C language is as broad and profound as "Shaolin martial arts", which is too difficult to learn. In fact, I think the C language is not "difficult (difficult)". As long as you can clarify your ideas and master its essence, it is very easy and fun to learn C language by yourself. Today, I will talk with you about how to learn C language or what aspects should I start.

Basic knowledge

I. background of C Language

In my personal experience, no matter which language you want to learn, you should first understand the background of your language, or the development history of it.

C language is a kind of advanced programming language. Its predecessor is "ALGOL ". Its founders are brown W. cancanal and Dennis M. LiQi. C language came out with great limitations because it can only be used on UNIX systems. However, with the advancement of science and technology and the development of the computer industry, the C language gradually disconnects from UNIX. In 1987, the American Standardization Association formulated an international standard for C language ("ansi c"). Since then, it has become a widely used programming language. C language has many advantages, mainly including the following four points:

1. It has the advantages of both advanced and low-level languages and is an intermediate language.

2. It is a structured programming language and is very suitable for structured programming.

3. A wide range of data types, operators, and functions are available.

4. directly deal with memory, making it easy and easy to modify and edit other programs and documents.

Ii. Two different learning methods

I have learned many programming languages, such as C, C ++ (C Language extension), QBASIC, VB (basic visualization), JavaScript, JScript, VBScript, Java, ASP, foxPro, Perl, and so on. In fact, the advanced programming language is divided into two major languages. All the way to C-based programming languages, such as JavaScript and Java, are very similar in function calling, Program Statement writing, and loop control. The other is a program language headed by basic, such as Foxpro and VBScript. Similar languages include function calling, Program Statement writing, and loop control, but they are different from those in C. Therefore, if you have previously started from QBASIC, it is best to wash your brain before learning the C language. Do not use the basic learning methods and ideas on C.

At this point, I think you must have a perceptual knowledge of the C language! Let's take a closer look and get in touch with it in an all-round way. To learn the C language, you must start with the following four points. That is to say, as long as you can master these four points, you can basically achieve it.

C Language

I. Input and Output

C language input and output are very strict. Maybe we don't care about this problem in other programming languages, but in C language, we must thoroughly understand it. Due to limited space, I cannot discuss it in detail. If you are interested, you can refer to the second edition of C program design published by Tsinghua University Press by Mr. Tan haoqiang, a famous programming language professor. But here I have some points to talk about.

1. Input of two-dimensional floating point Array

The input of a two-dimensional floating point array (that is, assigning a value to a two-dimensional floating point array through the keyboard) has not been detailed in many professional books. When assigning values to a two-dimensional floating point array, you must declare a variable first, assign the value to the variable, and assign the value to the two-dimensional floating point array. Example:

# Include "stdio. H"
Main ()
{
Float a [2] [3], X;
Int I, J;
For (I = 0; I <2; I ++)
For (j = 0; j <3; j ++)
{Scanf ("% F", & X );
A [I] [J] = x ;}
}
Cannot be written:

# Include "stdio. H"
Main ()
{
Float a [2] [3];
Int I, J;
For (I = 0; I <2; I ++)
For (j = 0; j <3; j ++)
Scanf ("% F", & A [I] [J]);
}

Similarly, the structured floating point group variables must be input in this format. Example:

# Include "stdio. H"
Struct student
{Float B [3];
Int X;
} A [2];
Main ()
{
Float X;
Int I, J;
For (I = 0; I <2; I ++)
For (j = 0; j <3; j ++)
{Scanf ("% F", & X );
A [I]. B [J] = x ;}
}

2. Note the characters after "%" in the output format.

The C language output is complex and not complex, because common operations are very simple. It may not be complicated. I remember tutoring our C language professor before a Level Test: "If the C language is difficult to test, no pointer is required, as long as the exam output format is used, I think 99% of students fail." We did not agree at that time. From this example, we can see the complexity of the C language output format. Therefore, when learning it, you must learn to identify the characters after "%" in the output format. Each character has its meaning and function.

Ii. Priority

Speaking of priority, many friends do not understand or describe very vague. For this reason, I would like to use an example to give you a concept. What is priority? For example, when we are in the bus terminal, we will always meet people over 70 years old who can get on the bus without waiting in line. This is the meaning of priority. C Programs run like waiting in a car. First, take care of the operators with higher priority. If the priority is the same, it is like meeting two (or more) elderly people over 70 years old, let them get on the bus in turn. However, the Priority Calculation in C language is not the same. It can only be said that in most cases, the priority of some operators has their own characteristics, so you should pay attention to this. For example, conditional expression: condition? Result 1: result 2: many friends know this expression. Its function is related to if... Else... The condition judgment statement is very similar, and its priority is not completed according to the rules of C language. Therefore, programmers with high priority must be flexible and never stick to it.

3. pointer

In my opinion, pointers in C are the most distinctive and of course the most difficult to learn. A pointer is actually a form of expression of a variable, but it records not a value but an address. Just as a person can express himself by name or ID card number. I have been involved in programming for more than three years. During this period, I have received emails from many netizens asking me how to learn pointers. As far as I feel, it is best to learn how computer hardware works first, such as direct addressing and indirect addressing. It is easier to learn more about the pointer after you understand this content, after all, C language is a language between machine language and advanced language. Without some hardware knowledge, it is difficult to understand its true meaning. However, everything is not absolute. It doesn't matter if you don't have this knowledge. You just need to know the second point I have summarized and add more exercises:

1. the pointer is an address variable. The pointer has two values: Address and content. Different expressions can have different values, which is similar to the characters marked by a home address on different occasions. For example, the home address registered by the parents in their unit represents themselves, and the same home address you registered in the school represents yourself.

2. A pointer can be operated, and its algorithm is consistent with that of a variable.

In addition, do not use pointer variables unless you have. Because pointers are complicated, they can be used as machines if they are not used properly ". Therefore, I suggest you understand the pointer as long as you can understand it. Of course, if you are preparing to take the test, it will be another matter.

Iv. Functions

Although many programming languages have functions, I think C functions are the most attractive. If you can fully master C-language functions, learning C ++ is not a problem (C ++ is a high-level programming language built on C, but different from C, it adds many functions .). The method for learning a function is relatively simple. There are only two words to remember: Remember the function, the purpose of the function, and how to input and output the function. Some friends think that functions in programming languages are not very useful. In fact, this is not true. functions are essentially a common program. Using them can help us save a lot of programming time, before writing a program, a smart programmer always finds out how many programs he has written can be replaced by functions. The author once made a string comparison experiment, using the strcmp () function in C language as long as one sentence, and self-compiled 30 sentences are not fair, you can imagine how practical the function is!

Here I am writing this article. Here I will give you a registry comparison program written in C language. For comments of this program, see "/*… */"After the text, when the program is running (in DOS mode), the input method is as follows: Compare xx1.reg xxx2.reg xxx3.txt, note that there is a space between the field and the field. (Compare is the program name)

Program code:

# Include "stdio. H"/* define header files or include files */

Main (argc, argv)/* defines the main function with parameters */

Int argc;/* define the parameter type */
Char * argv [];/* define the second parameter type */
{File * FP1, * fp2, * fp3;/* define file pointer */
Char a, B;/* define the character variable */
If (FP1 = fopen (argv [1], "R") = NULL)
/* Open the first registry backup file. If it does not exist, jump out of the program and display
"The file don't open !" */
{Printf ("the file don't open! ");
Exit (0 );}
If (fp2 = fopen (argv [2], "R") = NULL)/* open the second Registry backup file, if it does not exist
The program jumps out and "the file don't open!" is displayed !" */
{Printf ("the file don't open! ");
Exit (0 );}
If (fp3 = fopen (argv [3], "W") = NULL)/* creates a new text file to store comparison results. */
{Printf ("the file don't open! ");
Exit (0 );}
Rewind (FP1);/* specifies that the file pointer FP1 points to the header of the first registry file */
Rewind (fp2);/* specifies that the file pointer fp2 points to the header of the second Registry File */
While (! Feof (FP1) |! Feof (fp2)/* Start comparison */
{A = fgetc (FP1);/* read the content of the first registry file and assign it to the character variable */
B = fgetc (fp2);/* read the content of the second Registry file and assign it to the character variable B */
If (! = B) fputc (B, fp3 );
/* If the character variables A and B are different, write the differences to the new text file */
If (feof (FP1) fputc (B, fp3 );
/* If the first registry file has been read and the second file has not been read,
Write the remaining part of the second file to the new file */
If (feof (fp2) fputc (A, fp3);}/* If the second Registry file has been read,
If there is still unread part of the first file, write the remaining part of the first file to the new file */
Fclose (FP1);/* close the first registry file */
Fclose (fp2);/* close the second Registry File */
Fclose (fp3);/* close new file */
}

For an uncertain C language, a few thousand words are not clear. However, as long as you read this article, I think it will be of great help for you to learn this language. At the same time, I also know how to learn C language and how to master the essence of C language in the shortest time. In addition, the knowledge points involved in this article are obtained by the author through practice. Therefore, if you have not mentioned any questions in other professional books, you can refer to this article. Finally, I wish you a smooth C language learning!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.