Some basic resource recommendations and an overview of program syntax for getting started with the C language

  • 2020-05-07 20:06:30
  • OfStack

Why learn C?

Why learn and use C? Why learn a programming language that may be older than you?

Choose a programming language, "why to learn" this purpose is the most important, the purpose is not clear will not learn well. This is why many students and friends who are required to take the C language in college feel they don't understand it. Because the purpose of learning is not clear, of course, there is no motivation to learn. And one reason is that C language is the language of practical engineering, it is not from a research institute, a university college, truly, truly, but from the project needs to produce, along with the rise of Unix and popular, concise clear semantics, powerful and not bloated, simple and not too simple, it is necessary for work and study friend home trip.

One of the advantages of C over C++ is the principle of least surprise. 1 is 12, 2 is 2, and it doesn't produce 1's in secret. Do with C + + example, such as this one function prototype void PassWithClassValue (COneClass clsParam1), slightly understand C + + friends will know, if you don't realize COneClass copy constructor, the compiler will kindly help you achieve one, and the call to this function PassWithClassValue, secretly call copy constructor generated a temporary object is passed as a parameter, for some cases, such as its operating system scenario, the performance of such must optimize the self-righteous things is very evil things.

The C language itself provides only the necessary language features. Other complex functions such as file processing, mathematical calculation, and so on are provided in the form of library functions. Even the "must have" functions such as malloc and free are provided in the form of standard library functions rather than as the core of the C language. As mentioned at the beginning of the great and famous all-encompassing book K&R, for can actually be done with while, but for can be written more succinctly. By implication, for is not really necessary for C. Track 1 point, said Lua can say in other programming languages inherited C language concise design philosophy, even continue this almost essential keyword 1 straight refused to join, in Lua maillist and wiki mentioned continue this problem, maintainers think continue Lua language for Lua is not necessary, don't consider this keyword is added in the future versions. This philosophy of simplicity also makes the C language especially excellent in portability and portability, and also makes the C language still used as the main programming working language in many embedded systems.

Java has a slogan: "write once, run everywhere", which is the cross-platform gimmick. Actually C language from early start almost "one write and compile" everywhere, in ANSI system in 1989 after 1 C language standard (called C89), as long as your specific platform compiler implements complete C89 standard, but also your code does not use some special extension (GCC and Microsoft has its own compiler specific extension), then the code 1 set can be compiled through, to achieve 1 operating system under the relevant function library, C language transplantation is the simple things. Lua can be used as an example. Lua itself is fully compliant with C89 without any specific extensions, which also ensures that any platform with a compiler for C can compile using Lua. It is interesting to see that hardware platforms that can compile and run C can now queue from A to Z.

The C language is also a relatively rare language with a wide range of applications. For example, writing an operating system is such a difficult problem, only C++, assembly language can do. The C language can be used to write server-side software such as Apache, Nginx, or GUI programs such as GTK. The first version of most programming languages is implemented through C language, which guarantees the portability of these programming languages with the help of the aforementioned "compile everywhere once written". In the Web development field, the C language has relatively few applications, which is also the result of one choice. The development of Web requires the use of dynamic languages such as PHP, Ruby and Python, which can be quickly launched and modified to meet the changing needs of users. This is also the weakness of C. If the application fields of program language are roughly arranged from hardware to management software and Web programs from bottom to top, C language is suitable for the fields that are closer to the hardware, while the emerging languages are more focused on the fields that are closer to the end users, such as high-level management or Web development. The popular hybrid development mode is to use C language to write low-level high-performance part of the code or background server code, and use dynamic language such as Python to do front-end development, to give full play to their respective advantages.

The disadvantages of the C language are often referred to as its lack of one or another feature, such as the suggestion to add GC, the suggestion to add parallel or concurrent support, or the suggestion that there is no complete exception policy similar to C++. Some of these features can be implemented by introducing a third party library, but the design philosophy of the C language actually dictates that it will not be as "powerful" as C++. Even if some desirable features are introduced, it will still be the case that some people like and some people don't. The current functionality is sufficient for the C language application domain, and other features can be implemented in a specific programming language and interact with programs written in C API and C. It is impossible for any craftsman to use only one tool to complete his work. Only by combining different tools can he complete the task faster and better.

Mentioned API C language, also a little introduction 1, we know windows api of the operating system or Linux system api, or want to Ruby, Python extension module, C language forms of function definition is only 1 choice. The C language is like a middle layer or glue. If you want to mix functional modules implemented in different programming languages, C is the best choice.

With all this talk about the benefits of C, it's up to you to decide whether C is the right language to learn, such as for an embedded project, or for server-side development, or for writing a performance-related component, C is a good choice. In addition, C++ can also be used intentionally in the use of C language way of thinking, learn C language concise and clear design ideas, programming design level will be greatly improved.

C language learning method

The C language can be learned in the order of reading reference books, reading code, writing and debugging actual programs, participating in discussions online, and researching advanced topics.

The first step in learning a language is to read reference books. I suggest choosing a few very classic books and reading them carefully and completely over and over again. The advantage of learning C is that these books cover almost all aspects of the programming field of C. Unlike C++, even after finishing one pile of books, you will still be a little confused.

1. Reference books

In douban held up a book, you can directly reference http: / / book douban. com/doulist / 636329 /. In the following brief comment 1, it is best to read in the order listed.

The The C Programming Language http: / / book douban. com/subject / 1230004 / : if you want to buy 1 book learning C language, just want to buy this one is enough. If you fund is enough, suggest you buy a few more, the office, the home is put on 1, conveniently can turn over. Use 3 words to describe it is: classic! Classic! Classic! At just over 200 pages, the book covers all aspects of the C language, and no one has written it before or since.

Behind the The C Programming Language (called K & R) it contains a simple syntax parser, contains malloc how to implement, contains a complete operating system directory browsing program, the program of high practicability, it can be said that if learning any more than one language can implement on your own function, basically can be started. K & R each paragraph contains a very worth exploring in the book of software engineering practice experience, if there is no one set of development experience, is really don't see the iceberg below the content, such as the beginning of the chapter 1 is put forward to teaching with writing complete code this way, and in the book who C language trap or may be a problem, all have mentioned, but due to the limitation of space, write very simple, it is hard to under 1 just read it. I'm reading the book word for word, hoping to annotate it and write a few blog posts to share.

"C programming language (version 2 ・ version)" http: / / book douban. com/subject / 1139336 / : this is the Chinese version, K & R can look from the Chinese version first, then read 1 original English again, can not only learn English, and can realize the original that simple elegant style.

C trap and defect http: / / book douban. com/subject / 2778632 /

C experts programming http: / / book douban. com/subject / 2377310 /

These two books are also a must for anyone who is learning and using C, such as C expert programming, which is dedicated to two or three chapters detailing the differences between arrays and Pointers in C. These two books are to some extent a detailed supplement to what K&R skipped and highly recommended.

C language reference manual http: / / book douban. com/subject / 2132084 / : this is the last 1 strongly recommend that you buy home as best desk book essential reference books. The previous books, which were either slightly abbreviated or focused on a particular topic, were not suitable for a problem search. This "C language reference manual" can be regarded as the "xinhua dictionary" of C language programming, comprehensive and authoritative. It also covers C99, keeping up with The Times.

The following books can be used as cross-references and are all valuable, which is also recommended for everyone to buy. Good books are like friends, and they get newer with time. For example, these books I recommend are highly rated on douban or amazon, and they are published repeatedly.

C and pointer http: / / book douban. com/subject / 1229973 / : the importance of the pointer, studied C language (or C + +) friends all know that the book is elevated to the pointer and C language equal status, is introduced from the very beginning, as a teaching reference books is also possible.

Standard library C http: / / book douban. com/subject / 3775842 / : this book is dedicated to introduce C language how to implement the standard library, such as malloc algorithm, using standard C language how to write? How should strlen be implemented? Although much of the code in the book is quite different from the real C standard library (many functions have a specific trick because the standard library needs to consider performance optimization), it is definitely worth referring to.

"You must know that 495 C language problem" http: / / book douban. com/subject / 3422332 / : this book is actually C - FAQ printed version, C - FAQ in various programming languages FAQ 1 flow can be called quality. If you want to apply for or recruit programmers in C language, please refer to this book.

The Linux C programming learning 1 station type "http: / / book douban. com/subject / 4141733 / : this book is based on specific operating systems Linux to introduce C programming language, can be used as computer related professional introductory textbooks or reference books, is also a book inside and its original programming books, very rare. Almost all the contents of the book are published directly on the Internet, and are revised according to readers' opinions, which is also a rare open attitude. It is highly recommended that you buy one.

To learn C, you should not only read books, but also practice the project requirements in the book (such as writing a directory browser) and the exercise questions in each chapter. This requires an environment where experiments can be conducted. The following is a brief introduction to different operating systems.

2. Build in a hands-on experimental environment

I have not investigated whether the C language is still used by tan haoqiang in TurboC 2.0 programming. If it is still this combination, it is too bad. Please throw them away.

The following mainly introduces the integrated programming environment under different operating system platforms, based on beginners and my personal preference, it is not recommended to use the command line vim programming, directly on IDE.

Windows system Code::blocks is recommended. The biggest advantage of this software is that it comes with GCC and GDB based on mingw. As long as you download about 70M software package, you can fully support C++, C language programming. Various features (such as debugging) are also powerful, and version updates are fast. Note to download files with the name mingw, for example, the latest version is codeblocks-10.05 mingw-setup.exe (version may be different). Homepage: http: / / www. codeblocks. org /

If you need to develop Windows operating system, you can download Visual C++ 2010 Express.

Because Code::Blocks does not contain Windows programming header files (actually because there is no Windows SDK), it is impossible to write Windows OS related interface applications or service class programs. VC++Express comes with these header files and programming libraries, which are a bit rudimentary, but pretty much enough for practice. Homepage: http: / / www. microsoft. com/express/windows /

For computer students, it is recommended to use Linux operating system, or more specifically, Xubuntu operating system as a desktop, using the combination of Netbeans and GCC (of course, Code::Blocks). Xubuntu can be installed through apt-get install build-essential, gcc related programs can be compiled in Terminal Code language program, but for ease of use, you can choose Netbeans C++ support package, C website can be downloaded. Homepage: http: / / netbeans org features/cpp/index html

If you use Mac, XCode is undoubtedly a great choice for programming. XCode is available as a free download on the apple developer website, and XCode is included in IPhone SDK. Homepage: http: / / developer. apple. com technologies/tools/xcode html

If you don't have the appropriate programming environment, also need to experiment 1 some simple code, you can use http: / / codepad org/services, online running code.

In addition, it is recommended that you apply for an account of github.com, where you can save your exercise code, so you don't need to carry the U disk with you.

3. Network resources

Under 10 minutes if you want to use time to learn about 1 C language, all men are mortal, the sequence of events of the wikipedia page http: / / en wikipedia. org wiki / 28 programming_language C_ % % 29 is the best choice.

According to wikipedia, C is an imperative, structured paradigm programming language designed by Dennis Ritchie in 1972. The type is static and weak and needs to be explicitly defined. The latest international standard is C99. The design is mainly influenced by B, ALGOL68, assembly language, PL/I, FORTRAN, C language has also influenced a lot of programming languages, such as C++, Objective-C, C#, Java, Go, PHP, Python and so on. PHP is easy to use, except for the OO part of PHP).

There is a lot of detail in the wikipedia entry about the features that the authors think C is missing, such as object orientation, multithreading, GC, exception handling, and so on. The other one focuses on "undefined behavior." some of the results that we take for granted are not explicitly defined in the C language standard.

The wikipedia entry goes on to talk about the usefulness of the C language, and it must be admitted that although there are now hundreds of programming languages, few can be called "system level", and only Go can be called "system level" among the emerging languages. It is not possible to fully use C in large-scale software projects, but the core part can be built by C. Compared with the high price of C++ development tools, the free auxiliary development software related to C language is very rich, such as splint and valgrind. Many core libraries are also very stable after long-term use.

Since C has a wide range of support platforms and compilers are relatively mature and reliable, many programming languages choose C as an intermediate layer, such as Glasgow Haskell compiler.

The other one can find a large number of C language related information is "delicious", by searching for a specific keyword (C + programming). Then you will find a lot to mining resources http: / / delicious com/search & # 63; p = c + programming. . You can also reference dmoz org C language classification http: / / www dmoz. org/Computers/Programming/Languages/C/delicious than aging performance almost, but the classification comparison system, also easier to find one.

Programmers tend to be lazy, "fetch" or "copy" is popular and effective, and it can be very enlightening to see how others use a function or keyword when you don't quite understand it. Here are a few commonly used code search site, the most commonly used is google codesearch: http: / / codesearch google. com, can search certain keywords in different conditions and the regular expression. In addition, you can refer to the "articles with C language examples" category on wikipedia, where the code is also very good. You can also search for related items on github.com. In the previous blog post I also introduced a website called Rosetta code http: / / rosettacode org/different programs can be found on the site for the solution to a problem, is used to study is very convenient.

Learning programming also needs to read a lot of famous classic code, and learning Chinese and English need to read a lot of classics 1 reason, C language programming quality project that is "colorful flag dance, people mountain sea sea", personal Suggestions can see Lua, Sqlite, Nginx these projects code, code quantity is not much, and the code quality is also relatively high. In addition, you can look at the Linux kernel code, there are many books available to help interpret. For information on how to read code well, see Code Reading.

After reading a few books, writing some code, and reading a little bit of other people's code, you should use C to solve real work problems and make C your Swiss army knife. Only when you often use C language for programming work, often think about how to design an elegant and efficient system through C, can you have a deeper understanding of C language design philosophy.

Can also to http: / / stackoverflow com participate in answering questions, browse the question answer to knowledge of others, for example, this article http: / / stackoverflow com/questions / 2054939 / char - is - signed or - unsigned by - default introduces a C language about char small type of trap.

In C language learning, there are some difficult points to pay attention to, such as the differences between pointer and array, how to interpret complex type definitions, how to correctly use preprocessing preprocessor and macro definitions. In fact, these contents in the previous books are repeatedly mentioned, if the step-by-step learning down, should not be a problem.

When C is around the same time, you can also learn a dynamic language, such as Lua or Python. Try to mix the dynamic language with C in real work projects.


C program code writing method
The
C language is easy to get started because there are only a few common grammatical structures. Here are some simple examples of these structures for a quick review of the C language. The program includes the use of bool variables, characters, constants, mathematical functions, special characters, for loops, functions, and so on.

1. Use the bool variable


#include <stdio.h>
#include <stdbool.h>
int main(void)
{
  float input;
  bool isTrue=(scanf("%f",&input)==1);
  while(isTrue){
    printf("you typed %.d\n",(int)input);  // cast 
    isTrue=(scanf("%f",&input)==1);
  }
  return 0;
}

(2) characters


#include <stdio.h>
#include <stdlib.h>
int main()
{
  char beep;  // with  int beep;  equivalent 
  while(scanf("%c",&beep)==1){
    printf("you typed a %c \n", beep);
  }
  //scanf  returns 1 Starting to read into the variable, the final result is not as expected! Can be used after 1 methods 
  return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
  char beep;
  while(gets(&beep)){
    printf("you typed a %c \n", beep);
  }
  return 0;
}

3. Constants


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#define PI 3.1416
#define TEXT "hello world"
#define HTML 'H'
int main()
{
  // Constants defined   As well as C The preprocessor   System predefined constants 
  const int china =345;
  printf("%d ,%d ,%d  . %d\n", INT_MAX, china, FLT_MAX_10_EXP, CHAR_MIN);
}

4. Mathematical functions


#include <stdio.h>
#include <math.h>
#define PI 3.1415926
void main(void)
{
  //i  For line number, draw the cosine curve 
  int i, j, blankNUm;
  float cosValue;
  for(i=0;i<21;i++){
    cosValue =1-i/10.0;
    blankNUm =(int)(180/5/PI*acos(cosValue));

    for(j=0;j<blankNUm;j++)
      printf("");
    printf("*");
    for(j=0;j<(73-2-2*blankNUm);j++)
      printf("");
    i!=20?printf("*\n"):printf("");
  }
  return 0;
}


#include <stdio.h>
int main(void)
{
  // To calculate  e  Before, 50 The term, actually before the root 10 Study results 1 Sample, just for demonstration, with double Can meet the accuracy requirements 
  int i;
  double factorial=1.0, e=0;
  for(i=1;i<=50;i++){
    factorial*=i;
    e+=1.0/factorial;
  }
  printf("%f \n",e);
  return 0;
}

5. Special characters


#include <stdio.h>
#include <stdlib.h>
int main()
{
  float salary;

  printf("\aplease enter you salary by month:");  // \a Ringing age, it seems 
  printf(" $______\b\b\b\b\b\b");  // \b backspace 
  if(scanf("%f",&salary)==1){
    printf("\t$%.2f per manth is $%.2f per year", salary, salary*12.0);
    printf("\rgEE!wa h\n");  // \r Causes the cursor to move to the beginning of the current line , It's very subtle here 
  }
    
  return 0;
}

/* please enter you salary by month: $23.4__ */
/* gEE!wa h$23.40 per manth is $280.80 per year */

6. for loop


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int k;
  for(k=1,printf("%d:hi \n",k);printf("k =%d \n",k),k*k<26;
    k+=2,printf("now k =%d\n",k))
  {
    printf("k is %d in the loop \n",k);
  }
  return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int i;
  int word[8];
  for(i=0;i<8;i++){
    word[i] =pow(2,i);
  }
  i =0;
  do{
    printf("%d ",word[i]);
  }while(++i<8);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int i;
  double arr1[8],arr2[8];
  for(i=0;i<8;i++){
    scanf("%lf",&arr1[i]);
  }
  arr2[0] =arr1[0];
  for(i=1;i<8;i++){
    arr2[i] =arr2[i-1] +arr1[i];
  }
  for(i=0;i<8;i++){
    printf("%.2f\t",arr1[i]);
  }
  printf("\n");
  for(i=0;i<8;i++){
    printf("%.2f\t",arr2[i]);
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main()
{
  char beep;  // with  int beep;  equivalent 
  while(scanf("%c",&beep)==1){
    printf("you typed a %c \n", beep);
  }
  //scanf  returns 1 Starting to read into the variable, the final result is not as expected! Can be used after 1 methods 
  return 0;
}
0

7. Function


#include <stdio.h>
#include <stdlib.h>
int main()
{
  char beep;  // with  int beep;  equivalent 
  while(scanf("%c",&beep)==1){
    printf("you typed a %c \n", beep);
  }
  //scanf  returns 1 Starting to read into the variable, the final result is not as expected! Can be used after 1 methods 
  return 0;
}
1

#include <stdio.h>
#include <stdlib.h>
int main()
{
  char beep;  // with  int beep;  equivalent 
  while(scanf("%c",&beep)==1){
    printf("you typed a %c \n", beep);
  }
  //scanf  returns 1 Starting to read into the variable, the final result is not as expected! Can be used after 1 methods 
  return 0;
}
2

Related articles: