Learn about the C/s + + language (1)-The main thing is to solve the initial awkward feeling

Source: Internet
Author: User

The most profound thing about C is why it is required to do so, why to write so, why to use it so.

(# include <> or include "")include contains the meaning of which library file is included. If the C language is compared to the military, the library should be a special military, specialized in something, such as the Engineering Corps, the open-circuit in the mountains, the water Bridge. If you need input and output (printf (), scanf ())in C, use the (#include <stdio.h>) header file. Otherwise the program will not support it. If the army does not have a corps of engineers, then he can only mobilized porters or the army to do what the engineers do, so efficiency is certainly less than the specialized corps. The C language is the same, I do not use (#include <stdio.h>) header files can also ah, write Ah, but inefficient.

(  int main ()  )   is the main function entry, and the program starts here and ends here. For example, the Army needs a certain force to do the main combat units, the beginning of the Shing, the final blow, the definitive win. Why is the C + + language so stipulated, if not so, the beginning of how to write, some people will confused, do not start, and the logic of the process of thinking is not easy to clarify. And a conventional rule is good, the army needs martial law 17 prohibitions 54 chop. If we do not write this, the program will not support, the compilation will not pass. In the army, to commit military law, the consequences are very serious, taboo will be beheaded to shoot the same, people die, how to play, we are not sentence days so cattle.

(int  a = 0;)   program is used after defining the declaration, otherwise the program does not support, why? Designed in such a way as required. If not defined, who knows who it is. Of course, we can initialize at the time of definition, that is, assigning values. To make a metaphor, is how many troops in the army, or the establishment. In ancient times it was ten people, five people, who you belong to, or which one, or what (there may only be nothing, not a dead-in). Definition is to put you into which Wu or what, with you go to war when the call to a certain Wu and so on, the assignment can also say you this Wu Chung or how many people. (int  a = 0;) &NBSP, why would you say that a is assigned to a value of 0 instead of equal to 0? For example, if an army actually had 10000 people, and a war, 4000 people died. Then his number is not changed to 6000. How do you change that at this time? equals, well, that's equal to the number of troops in an army equals 6000 people. If it is 6000 people, continue fighting; 5000 people retreat. Then how did this play out? Number of troops (a) equals 6000, or fighting (  if (a=6000)  ) , equals 5000 people retreat ( if (a  =)) , this is not right, whether he should retreat, or should continue to fight. so = = just came out.

(int  short  long   double  float   and so on) , each data type each has the usefulness, also each has each limitation, needs to pay attention when using. Compile the time not to exceed the bounds of good, more than a major event. War beats the good, defeated, hehe. Why is it so stipulated? The memory space of the computer is limited, although the memory is much larger now, but should know to know. The use of a certain force in the army, need to do some small things, do not disturb the enemy, it is necessary to streamline the army, select the Elite bar. When the matter of silence, sift into some ordinary troops can grow momentum, although reduce combat effectiveness. The effect of using memory in C as a result of memory loss (albeit a little).

(for (int count = 1;  Count <= 100; count++) This is a for loop that can be calculated from 1 plus to 100. If it is an int 100 variables, then add these 100 variables together, is it silly? So there is a for loop in the design. This statement first defines a local variable of count in the For loop, when (Count <=)executes the statement inside the for loop, after execution (count++;). Then determine if count is less than or equal to 100, is again (count++;). This is the loop, when the condition (count <=;) is not set, jump out of the for loop. With the army to fight the analogy, do not break the gap, I just one time to put how many troops, until I fill in this meat grinder, which is unsuccessful chengren. This is not a moment to feel bad. Well, I think so, too.

Now children generally do not obey, adults are usually bamboo shoots fried meat. Make a metaphor oh, don't talk about abusing children.

  (if (child not obedient = = true)

{

Sautéed pork with bamboo shoots ();

}) If the statement is only judged once, it is only a one-time educational effect. If he still does not obey, then the bamboo shoots fry the meat again. But if not. Of course there is a metaphor, which is a surprise check, check, um, you know. If the army fights, it is to fight Yingzhou, you do not obey, jump up, you are the one to fight. But, you still clamor, how to do? Then you need to use the while.

  (while (child not obedient = = true)

{

Sautéed pork with bamboo shoots ();

Control loop statement;

}) This while statement is always executed, do not obey the bamboo shoots fried meat, and so on when the child obedient stop bamboo shoots fried meat. Another metaphor is a kind of guarding you, dedicated to staring at you. Hit Yingzhou is always hit until you obey, just don't hit you.

 (do{

Sautéed pork with bamboo shoots ();

Control loop statement;

}while (Child not obedient = = true);) do ... while loop is the first bamboo shoots fried meat, and then to see if children listen to not obedient. This method is not advisable, in any case, at least to fry pork at the same time, (this put on who is not good for it) not obedient, and then a bamboo shoots fried meat.  This is the most violent, it is recommended not to choose this. This deal with Yingzhou most cool most yy, no matter how, first beat a meal to say. But what do you need to do about a certain disobedient? C language Design Another thing, switch comes in handy.

 (Switch (the number of non-obedient)

{

Case 1:

Number of hits;

Break

Case 2:

......

}) Use this need to pay attention to the use of the break statement to end, otherwise singled out into 1 to pick N, open matchless, most likely is OU. When the army fights, it is the one that does not obey, which is what to fight.

Using the switch statement also has a trick, such as a dice, the size of points, 123 is small, 456 is large.

  (Switch (dice size)

{

Case 1:

Case 2:

Case 3:

printf ("Small \ n");

Break

Case 4:

Case 5:

Case 6:

printf ("Big \ n");

Break

Default:

Break

}) This is a trick of using a break, the dice size is 123, he will execute (case 1:case 2:case 3:) statement After, then execute printf () Statement, and finally (break;), jump out of switch. As for (default:) is the dice size does not conform to the individual (case:) branch statements, jump out of switch. This is just in case, it is recommended to add (default:) Branch statement When writing a switch statement.

However, if you want to save a lot of student's name, age, school number, class, etc. information, is not a bit troublesome. Therefore, there is a user-defined data type in the C + + language.

There are also custom data types in the C language, such as struct structures, union unions (also known as shared bodies), enum enum types.

Structs can contain many data types, such as int, double, pointer, array, and so on. There are many classes, tanker, pilots, artillery, pure infantry, and so on, that can be compared to the army. It's hard to win a pure infantry, but it's a tough fight to win. Like some yy novel, Spear line assassination of invincible, it is impossible, so many wars in ancient times have a few battles purely by a certain class to win? It is very powerful to assemble a class, and when writing code you need to assemble a number of different data types together, so it is not easy to use, and the thinking is clear.

The structure is good, but the memory space is a bit large, and some data do not want to use the time can disappear, do not occupy memory. Then the C language of the consortium is to solve this problem.

The same data type, do not want to write more data type what to do? C language Design in the enumeration can be done, it can be easily a--k to the playing cards to 1--13, and not write what int what drop.

Function: In order to achieve a certain function, I wrote many times, do not want to write so many times how to do? Call the Function! Write a function to implement a function, in the main function and then go to call it, only one line is not very concise. For example, a special force was set up in the Army for beheading. When the war broke out, it was necessary to end the enemy headquarters, is not only the next command, command Special forces to do this thing ah. And then the next war, you just need a command. (function call). Instead of having to set up a special forces on their own every war, the dissolution of the war, and the establishment of the war once again.

Arrays: How do you store and process a sequence of n integers in a program? For example, the number of cards (the size of the king does not count), with the enumeration can be solved, very simple, 13, done. If you're going to handle 100 or 1000, 10,000 of them. Use int variable to express so many numbers, the amount--when I did not say. Array can be resolved. It can store n data continuously, and can access the required data with a subscript. For example, military targeting, assigned you the number of first, and then find your target number, read a few rings, record data. The next time you want to know your grades, find out when you are the first date to know your results.

Pointer:(int cabinet = +; int *p = &cabinet;) Go to the supermarket to buy things, usually in advance will put the things on hand (such as 100 Yuan RMB) into the locker, the waiter will give you a number card (int *p = &cabinet;), the cabinet cabinet put things, and the pointer P is that number card, You can find that cupboard with that number plate, which is &cabinet. Find the cabinet after you can rely on the number card (*P) to obtain the 100 yuan. Another way to understand is where you live, which street is the first building, this is the address, the address is stored in the person you, and I want to find you can rely on your address directly to find you. Why are you using pointers? The general variable is that I know your name from another person and then go to see you. But I know where you live, and I'll go straight to you. Pointers can manipulate memory directly and are more efficient than normal variables.

Assembly language can be any way to write, but once a thing modified, it can cause the death or other serious consequences, is there a problem? If you are not allowed to use it, is not another stick to kill Ah. So in C + +, there is a pointer to something.

If you need to modify several values in the calling function, and the function returns only one value, is there no way to resolve it? What to do? Cold? The pointer accesses the memory directly using the address, and the value in the address can be changed. Note that the address does not change, only the value in the address has changed. For example, Xiao Ming ate apples, bananas, watermelons, people did not change, but the belly of things changed.

In fact, after learning C language, a different angle to think about the problem, standing in the solution, the developer's point of view, you will know the C language in fact, what is the reason for this provision. And it won't be awkward for you to understand (of course, when I'm not saying it). Use it is also handy, because know its essence, dry to heart on the bottom.

Learn about the C/s + + language (1)-The main thing is to solve the initial awkward feeling

Related Article

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.