C Introduction to some basic resources recommendation and Program syntax overview _c language

Source: Internet
Author: User
Tags constant constant definition exception handling function definition function prototype lua mathematical functions netbeans

Why to learn C language?

Why study, use C language? Why do you want to learn a programming language that is probably older than yourself?

Choose a programming language, "Why and learn" the purpose is the most important, the purpose is not clear to learn. This is why a lot of student friends in the University of the compulsory C language but feel that did not learn the reason. Because the purpose of study is not clear, there is certainly no motivation for learning. Another reason is that C language is a very strong engineering practice language, it is not from some Institute of a university, but actually generated from the project needs, along with the rise of Unix and popular, concise semantics, powerful and not bloated, concise and not too simple, it is a home travel work to learn the necessary friend.

C language than C + + is one of the advantages of the principle of minimum surprise, one is one Ishi, will not produce some inexplicable additional products in private. In C + + For example, such as a function prototype void Passwithclassvalue (Coneclass clsParam1), a little understanding of C + + friends will know that if you do not implement the Coneclass copy constructor, The compiler will be kind enough to help you implement one, and when calling this function Passwithclassvalue, secretly call the copy constructor to generate a temporary object as a parameter to pass, for some cases, such as writing the operating system, such as the need to optimize performance scenarios, These self-righteous things are very wicked things.

C language itself only provides the necessary language characteristics, other complex features such as file processing, mathematical calculation, etc. are provided in the library function, even malloc, free this "must have" function, but also in the form of standard library functions, rather than as a C language core appears. In the very beginning of the great famous all-encompassing "K&r", the for can actually be done by while, except for a more concise, implied meaning, which is not necessary for the C language. To digress, Lua can be said to inherit the concise design philosophy of C in other programming languages, and even continue's almost essential keywords have been refused to join, as mentioned in the Lua maillist and wiki continue. Lua language maintainers believe that continue is not necessary for LUA, and does not consider adding this keyword to subsequent versions. This concise philosophy also makes C language portability, portability is particularly good, but also makes many embedded systems still use C as the main programming language.

The Java language has a slogan: "Write once, run everywhere," is a cross-platform gimmick. In fact, the C language from the early beginning of almost reached the "write, compile everywhere," in the ANSI in 1989 after the reunification of the C language standard (called C89), as long as the compiler on a specific platform to achieve the complete C89 standard, And your code does not use some special extensions (GCC and Microsoft have their own compiler-specific extensions), then the code can be compiled, and then implement the operating system-related functions of the library, C language transplant is very simple things. As an example of LUA, LUA itself is fully compliant with the C89 standard and does not use any specific extensions, which also ensures that a platform with a C-language compiler can compile and use Lua. It is very interesting that you can compile a hardware platform that runs the C language from A to Z.

C language is also a relatively rare application of a very wide range of language. such as writing operating system such a difficult problem, only C + +, assembly language can do. C languages can write server-side software such as Apache, Nginx, or GUI programs, such as GTK. The first edition of most programming languages is implemented through the C language, with the reference to "compile all at once", which guarantees the portability of these program languages. In the field of web development, C language application is relatively small, this is a trade-off result, web development needs to use PHP, Ruby, Python, such as dynamic language, can quickly online quickly modify, to the maximum extent to meet the changing needs of users, which is the weakness of C language. If the application of programming language from hardware to management software, web programs to do a very rough from the bottom to the arrangement, C language suitable for the field is relatively near the bottom of the hardware, and the new language is more emphasis on high-level management or web development of this relatively close to the end user domain. The more popular hybrid development model is to use C language to write low-level high-performance part of the code or background server code, while using dynamic language such as Python front-end development, give full play to their respective strengths.

The disadvantage of C language is often that it lacks this or that feature, such as the suggestion to join GC, some people suggest to join concurrent or concurrent support, some people mentioned that there is no more complete like C + + exception strategy. Some of these features can be implemented through the introduction of Third-party libraries, but the C language design philosophy actually determines that it will not be as "very powerful" as C + +. Even if you introduce some people's expected characteristics, it will still be some people like some people do not like the situation, the current function of the C language application is sufficient, other features can be implemented in a specific program language, and through the C API and C language program to interact. It is impossible for any craftsman to use only one tool to complete his work, and different tools can be combined to accomplish tasks faster and better.

Mention the C language API, also a little introduction, we know the Windows operating system API, Linux system API, or want to give Ruby, Python to write extension modules, C language form of the function definition is the only choice. C language is like a middle layer or glue, if you want to use different programming language to implement the function of the module mix, C language is the best choice.

Mention so much about the benefits of C language, then learn the C language is appropriate to see your own judgment, such as to carry out an embedded project, or need to carry out server-side development, or write a performance-related components, etc., C language is a relatively easy choice. In addition, the use of C + + in the process of intentional use of the language of thinking, learn C language concise crisp and clear design ideas, the level of programming design will be greatly improved.

C Language Learning Methods

C language learning can be in accordance with the following order of reference: Reading reference books, reading code, writing debugging practical procedures, online participation in discussion, research advanced topics.

The beginning of language learning is generally reading reference books. I suggest to choose some very classic good books, carefully complete repeatedly read several times, "the book read a lot of righteousness from the present." The advantage of the choice of C language learning is that these books basically cover all aspects of the C language programming area, not like C + +, even after reading a pile of books or some confused, still have such difficult to understand the trap.

1. Reference books

In the watercress listed a book list, we can directly refer to http://book.douban.com/doulist/636329/. The following is a brief review, and the reading order is best referred to in the order listed.

"The C programming Language" HTTP://BOOK.DOUBAN.COM/SUBJECT/1230004/: If you only want to buy a book to learn C language, just buy this one is enough. If you have enough money, I suggest you buy a few more, office, home put on a copy, can be turned over. Use three words to describe it is: classic! Classic! Classic! This thin, only 200-page book covers all aspects of the C language, unprecedented and after no one, any praise is not enough to describe it.

"The C Programming Language" (later known as K&r) contains a simple parser that includes malloc how to implement, including a complete operating system directory browsing program, which is highly practical, so to speak, If you learn any language to achieve the above function independently, you can basically be a beginner. Every paragraph in the K&r book contains a lot of practical experience in software development engineering that is worth exploring, if there is no development experience, in fact, can not see the contents of these icebergs, such as the beginning of a chapter on the use of writing complete code this way to teach, and in the book those C language traps or possible problems in the place, have mentioned, but because of the space limit, write very simple, it is difficult to let people read. I am reading this book in complete word for word, hoping to make some comments and write a few blog share.

"C Programming language (2nd edition)" http://book.douban.com/subject/1139336/: This is the Chinese version of K&r, you can first look at the Chinese version, and then read the original English, can learn English, Also can experience the original kind of simple and graceful style.

"C Traps and defects" http://book.douban.com/subject/2778632/

"C Expert Programming" http://book.douban.com/subject/2377310/

These two books are also the two necessary books to learn and use C-language friends, for example, "c Expert programming", dedicated to two or three chapters in the C language in detail the difference between the array and the pointer, the two books in a way to a certain extent to k&r skipped place to do a detailed supplement, strongly recommended.

"C Language Reference manual" http://book.douban.com/subject/2132084/: This is the last book you'd better buy home as a necessary reference book for a desk. Some of the previous books are either slightly abbreviated or focused on a particular topic, and are not suitable for searching when you encounter a problem. This "C Language Reference manual" can be seen as the C language programming "Xinhua dictionary", Comprehensive and authoritative. It also covers the content of the C99, closely follow the trend of the times.

The following books can be used as cross-references, are also valuable, but also recommended that everyone buy, good books such as friends, a long time new, such as I recommend these books in Douban or Amazon scored very high, and repeated reprint.

"C and the pointer" http://book.douban.com/subject/1229973/: The importance of pointers, learned C language (or C + +) Friends know that this book is the pointer to the same status as the C language, in fact, is also introduced from the beginning, As a teaching reference book is also possible.

"C Standard Library" http://book.douban.com/subject/3775842/: This book is devoted to the C language standard library How to implement, such as malloc algorithm, with the standard C language how to write? Strlen How should this function be implemented? Although many of the book's Code and the real C standard library A lot of difference (because the standard library needs to consider performance optimization, many functions have some specific trick), but definitely worth reference.

"You must know 495 C language questions" http://book.douban.com/subject/3422332/: This book is in fact the C-faq print version, C-faq in various programming languages FAQ can be called quality-class. If you want to apply for or recruit a C language related programmer, this book must refer to.

"Linux C Programming One-stop learning" http://book.douban.com/subject/4141733/: This book is based on a specific operating system Linux to introduce C language programming, can be used as a computer-related professional textbooks or introductory reference books, is also the only one of the books inside the original programming books, very rare. Almost everything in the book is open online directly, and it is a rare and open attitude to revise the opinions of readers. I highly recommend that you buy one.

Learn C language, must not only read, should be hands-on practice to complete the book inside the project requirements (such as writing a directory browser) and each chapter of the practice topic. This needs to have an experimental environment, the following for different operating systems to do a simple introduction.

2. Build the Experiment Environment

Also did not investigate, do not know now School Learning C language is still follow Tan Haoqiang teacher with TurboC2.0 programming, if still this combination, that is too bad, quickly put aside them.

The following main introduction of the different operating system platform integrated programming environment, based on the beginners and my personal preferences, it is not recommended to the command line with VIM programming, directly on the IDE.

Windows system recommends that you use the Code::Blocks software. The biggest advantage of this software is the MinGW gcc and gdb, as long as the download of 70M software package, you can fully support C + +, C language programming. A variety of features, such as debugging functions, are also powerful, and the version update is also relatively fast. Note Download Select the file with MinGW name, such as the latest version is Codeblocks-10.05mingw-setup.exe (version may be different). Home: http://www.codeblocks.org/

If you need to do the development of the Windows operating system, you can download Visual C + + Express.

Because Code::Blocks does not contain a Windows programming header file (actually because there is no Windows SDK), you cannot write a Windows operating system-related interface application or service class program. and vc++express with these header files and programming library, although the function is slightly simple, but for the practice of using the basic enough. Home: http://www.microsoft.com/express/windows/

For computer majors, it is recommended that you use the Linux operating system, or in a more specific way, use the Xubuntu operating system as your desktop, and the combination of NetBeans and GCC (and of course you can choose Code::Blocks). Under the Xubuntu can be apt-get install build-essential This command to install GCC related programs, you can already compile the C language program in terminal, but in order to use easily, you can choose NetBeans C + + support package, It can be downloaded on the NetBeans website. Home: http://netbeans.org/features/cpp/index.html

If you use Apple Mac, there is no doubt that Xcode is the perfect choice for programming, Xcode can be downloaded for free on the Apple Developer website and included Xcode in the iphone SDK. Home: http://developer.apple.com/technologies/tools/xcode.html

If you do not have the right programming environment on hand, you need to experiment with some simple code, you can use the services provided by http://codepad.org/, online writing run code.

Also recommended that you apply for a github.com account, in gist.github.com can save their practice code, you do not need to carry a U disk.

3. Network resources

If you want to use 10 minutes to understand the ins and outs of C language, previous life, Wikipedia this page Http://en.wikipedia.org/wiki/C_%28programming_language%29 is the best choice.

As you can see from Wikipedia, the C language 1972 was designed by Dennis Ritchie, the command-style, structured paradigm programming language. Type is a static weak type and needs to be explicitly defined. The latest international standard is C99. The design was mainly affected by B, ALGOL68, assembly language, pl/i, Fortran, C language also affected a large number of programming languages, such as C + +, objective-c, C #, Java, go, PHP, Python and so on (personally feel affected by C is very big is PHP, Basically have C Programming Foundation programmer, very easy can start PHP, in addition to the OO part of PHP.

The Wikipedia entry contains a great deal of information about the characteristics of the writer's lack of C language, such as object-oriented, multithreading, GC, exception handling, and so on, which, of course, is a bit fastidious and, if required, can be used in other programming languages. Another introduction focuses on "Undefined behavior," some of which we take for granted, in fact, are not clearly defined in the C language standard, assuming that these behaviors should be, when the program uses another compiler or a different version of the compiler to run, there may be bugs.

Next, the Wikipedia entry talks about the usefulness of C, which must be acknowledged that even though there are now hundreds of programming languages, known as "system-level", there are only a few new languages in which go can be called. Now large-scale software projects in the full use of C language is not likely, but the core can be built with C, relative to the high price of C + + development tools, C language-related free auxiliary development software is very rich, such as splint,valgrind, many core library after long-term use is very stable.

Because C language widely supports various platforms and compilers are relatively mature and reliable, many programming languages choose C language as a middle layer, such as Glasgow Haskell compiler is to do so.

Another place to find a lot of information about C programming is "delicious bookmarks", by searching for specific keywords (c + programming) to find a lot of resources worth digging http://delicious.com/search?p=c+programming. can also refer to dmoz.org C language classification http://www.dmoz.org/Computers/Programming/Languages/C/compared to delicious bookmark timeliness can be almost, but the classification of the system, the search is also easier.

Programmers are often lazy, "copycat", "copy" is very popular also very effective, when a function or keyword is not very understanding, see how others use, it is very enlightening. Here are a few commonly used code search sites, the most common is Google's codesearch:http://codesearch.google.com, you can use different conditions and regular expressions to search for specific keywords. You can also refer to Wikipedia's previous "article with C language Example" category, the code is very good. You can also search for related items on github.com. In the previous blog post I also introduced a Web site called the Rosetta code http://rosettacode.org/this site can find different programming language for a problem solution, for learning more convenient.

Learning to program also requires a lot of reading classic code, and learn Chinese English need to read a lot of classics a reason, C language programming high quality project that is "flags fluttering, Sea of people", personal advice can look at Lua, Sqlite, nginx These items code, the code is not much, and the quality of the code is relatively high. Also can look at the Linux kernel code, there are many books to help read. We can refer to the book "Code Reading" about how to read the codes well.

Read a few books, Code wrote some, but also slightly read other people's code, you should use the C language to complete the real work encountered problems, let C language really become your Swiss Army knife. Only if you often use the C language to do programming, often think about how to design an elegant and efficient system through C, in order to understand the C language design philosophy more profoundly.

You can also go to http://stackoverflow.com to take part in answering questions and browsing other people's questions to learn from them, such as this http://stackoverflow.com/questions/2054939/ Char-is-signed-or-unsigned-by-default introduced a C-language small trap for char types.

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

When C language learning is about the same time, you can also learn a dynamic language, such as Lua or Python, try to mix in the actual work of the dynamic language and C language, a plus a play out of the force is not only two, but very two (joke, haha).


The first glimpse of programming code in C
C language Introduction is very simple, because the common grammatical structure is so few, the following list some of these grammatical structure of simple program examples, can be a quick review of C language. The program contains bool variables, characters, constants, mathematical functions, special characters, for loops and functions, and so on.

1. Use 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);  Coercion type conversion
    istrue= (scanf ("%f", &input) ==1);
  return 0;
}

2. Character

#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 will read the return character into the variable, and the final result will not reach the expected! Use the latter method to 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. constant

#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 ()
{
  //constant definition and C preprocessor System predefined constants
  const int =345;
  printf ("%d,%d,%d,%d\n", Int_max, Flt_max_10_exp, char_min);

4. Mathematical Functions

#include <stdio.h>
#include <math.h>
#define PI 3.1415926
void Main (void)
{
  // I for the first few lines, draw 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"):p rintf ("");
  }
  return 0;
}

#include <stdio.h>
int main (void)
{
//compute E, Top 50, actually the first 10 results, just for demonstration, double can meet the precision 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 your salary by month:");  \a sound age, seemingly
  printf ("$______\b\b\b\b\b\b");  \b Backspace
  if (scanf ("%f", &salary) ==1) {
    printf ("\t$%.2f per Manth is $%.2f/year", salary, salary*12.0); c26/>printf ("\rgee!wa h\n");  \ r make the cursor move to the starting position of the current line, it's subtle. Oh
  } return
    
  0;

/* Please enter your salary by month: $23.4__ *
* Gee!wa h$23.40 per Manth-

6. For loop

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
  int k;
  For (k=1,printf ("%d:hi \ n", K);p rintf ("K =%d \ n", K),k*k<26;
    K+=2,printf ("Now K =%d\n", K))
  {
    printf ("K are%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 (void)
{
  int i,max;
  while (scanf ("%d", &max) ==1) {
    double sum1=0,sum2=0;
    for (i=1;i<=max;i++) {
      sum1+=1.00/i;
      I%2==0? (sum2-=1.0/i):(sum2+=1.0/i);
    printf ("sum1=%lf sum2=%lf\n", sum1,sum2);
  }
  return 0;
}


7. function

#include <stdio.h>
#include <stdlib.h>
int square (int s);  
void echo (void);
int main ()
{
  int i;
  for (i=0;i<10;i++)
  {
    printf ("Squire%d s value is%d\n", I,square (i));
  }
  printf ("what\nno/nbonzo?\n");
  Echo ();
  return 0;
}

int square (int s) {return
  s*s;
}
void echo (void) {
  printf ("A function without input");
}

    /* Another      *

/#include <stdio.h>
#include <stdlib.h>
void one_three (void);
void two (void);
int main ()
{
  printf ("Start now:\n");
  One_three ();
  printf ("done!");
  return 0;
}
void One_three (void) {
  printf ("one\n");
  Two ();
  printf ("three\n");
}
void Two (void) {
  printf ("two\n");
}

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.