C language back to the first place

Source: Internet
Author: User

By freeuniverser

There are more and more programming languages, but many of them have been modified on the basis of the existing language. In essence, it does not bring anything to programmers. This reminds programmers not to be restricted by a certain language, don't be overly infatuated with a language, because there is no perfect language yet (maybe human pursuit of perfection is a beautiful mistake ). On tianxiao this month, the C language has returned to the first place, but you don't have to worry too much about it. It's just for your reference.

Examples of some small programs in C language:

Count blanks, tabs and newlines


/* First Version */
# Include <stdio. h>
Int main (void)
{
Int blanks;
Int tabs;
Int newlines;
Int c;
 
Blanks = 0;
Tabs = 0;
Newlines = 0;

While (c = getchar ())! = EOF)
{
If (c = '')
Blanks ++;
If (c = '\ t ')
Tabs ++;
If (c = '\ n ')
Newlines ++;
}
Printf ("\ nBlanks: % d \ nTab: % d \ nNewline: % d \ n ",
Blanks, tabs, newlines );
Return 0;
}
Comments during program writing are very important. In the past, when I first wrote a program, I wrote a lot of letters in the name list. In fact, a good name is as important as a comment, and even the name and description are more powerful than the comments. If the program is small, but the project involving big points does not pay attention to comments and naming, it is easy to cause problems and it will be difficult to maintain the program later, no one wants to go deeper into what your variables actually represent, and you don't even know what they meant.


1/* Second Version */
2 # include <stdio. h>
3
4 int mian (void)
5 {
6 int blanks;
7 int tabs;
8 int newlines;
9 int c;
10 int done;
11 int lastchar;
12
13 blanks = 0;
14 tabs = 0;
15 newlines = 0;
16 done = 0;
17 lastchar = 0;
18
19 while (done = 0)
20 {
21 c = getchat ();
22 if (c = '')
23 blanks = blanks + 1;
24 if (c = '\ t ')
25 tabs = tabs + 1;
26 if (c = '\ n ')
27 newlines = newlines + 1;
28 if (c = EOF)
29 {
30 if (lastchar = '\ n ')
31 {
32 newlines = newlines + 1;
33}
34 done = 1;
35}
36 lastchar = c;
37}
38 printf ("\ nBlanks: % d \ nTabs: % d \ nNewlines: % d \ n ",
39 blanks, tabs, newlines );
40 return 0;
41}
The self-added and auto-subtraction operators seem convenient, but abuse is actually very bad and easy to bypass. In fact, many languages are designed to play tricks. In this way, you can use less words to occupy less memory. You can ...... Now the memory is not small, and there is nothing to do with typing a few more letters. In fact, it is saving time. If it is maintained later, it will be less effort. Isn't it good?

Copy input to output


1/* First Version */
2 # include <stdio. h>
3 # define NONBLANK 'C'
4 int main (void)
5 {
6 int c;
7 int lastchar;
8 lastchar = NONBLANK;
9
10 while (c = getchar ())! = EOF)
11 {
12 if (c! = '')
13 putchar (c );
14 if (c = ''& lastchar! = '')
15 putchar (c );
16 lastchar = c;
17}
18}
Using getchar () and putchar () together is not tiring. There are many such functions that can be used for use. Of course you can also write your own, but why not use them? If it is not a special need, you may as well choose "tailism ".


1/******* Second Version *********************/
2 int c;
3 int inspace;
4 inspace = 0;
5 while (c = getchar ())! = EOF)
6 {
7 if (c = '')
8 {
9 if (inspace = 0)
10 {
11 inspace = 1;
12 putchar (c );
13}
14}
15 if (c! = '')
16 {
17 inspace = 0;
18 putchar (c );
19}
20}
Copy input to output, replace tab by \ t, backspace by \ B and backslash \\

The \ B of this program is a problem and may be a bit wrong or not ideal. If you are interested, you can write it yourself.


1 *****************************/
2
3 int c;
4 while (c = getchar ())! = EOF)
5 {
6 if (c = '\ t ')
7 printf ("\ t ");
8 if (c = '\ B ')
9 printf ("\ B ");
10 if (c = '\\')
11 printf ("\\\\");
12 if (c! = '\ T' & c! = '\ B' & c! = '\\')
13 putchar (c );
14}
Although it is a bit monotonous to use the if condition, it is very common. This is in line with the human logic ...... You can see that the words used in the programming language are not too difficult. Compared with many languages, you will find that, for example, while, if, for, and so on, many of them are repeated, just like writing an article, just make a logical arrangement and combination of different words. The same is true for the program, but it is just a form of expression.


1 ***************************/
2 int c, t;
3 while (c = getchar ())! = EOF)
4 {
5 t = 0;
6 if (c = '\\')
7 {
8 putchar ('\\');
9 putchar ('\\');
10 t = 1;
11}
12 if (c = '\ t ')
13 {
14 putchar ('\\');
15 putchar ('T ');
16 t = 1;
17}
18 if (c = '\ B ')
19 {
20 putchar ('\\');
21 putchar ('B ');
22 t = 1;
23}
24 if (t = 0)
25 putchar (c );
26}
Putchar () is a combination of two swords, which makes it clearer. This is like a fence. You can bundle two, three, and four ...... The last step is to connect. Separate writing may be clearer and organized. You can also squeeze one piece. Note the separation. No one specifies that the fence cannot be bent.


1 ***********************/
2 # define DOUBLELINE '\\'
3 int c;
4 while (c = getchar ())! = EOF)
5 {
6 switch (c)
7 {
8 case DOUBLELINE:
9 putchar (DOUBLELINE );
10 putchar (DOUBLELINE );
11 break;
12 case '\ t ':
13 putchar (DOUBLELINE );
14 putchar ('T ');
15 break;
16 case '\ B ':
17 putchar (DOUBLELINE );
18 putchar ('B ');
19 break;
20 default:
21 putchar (c );
22 break;
23}
24}
Switch-case is clearer and more organized, but it is often less effective than if. In fact, many times, a language provides a variety of ways to express the same meaning, but it only depends on the occasion and your own interests. Don't be too constrained. How can we express it, just make it simple and clear.

Prints its input one word per line


1 int c;
2 int inspace;
3 inspace = 0;
4 while (c = getchar ())! = EOF)
5 {
6 if (c = ''| c = '\ T' | c =' \ n ')
7 {
8 if (inspace = 0)
9 {
10 inspace = 1;
11 putchar ('\ n ');
12}
13}
14 else
15 {
16 inspace = 0;
17 putchar (c );
18}
19}
Inspace has already played a significant role in the above. Like done above, as a control function, it is a bit equivalent to a switch. After all, the procedural language is born in the human mind with the wisdom of nature, and the result must also come from the real society and reflect the real society.

Many languages have encountered bottlenecks since now, and many of them are fried with leftovers. Then, they just need to change their coats and new bottles of old wine. In fact, there is no language that can unify the procedural world, and there is no need to have a religious plot. We look forward to a concise, clear, and convenient language.

 

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.