Learning C (1)

Source: Internet
Author: User
Int  _ Tmain (  Int  Argc, _ tchar  *  Argv [])
{

Char STR [ 20 ], Str2 [ 20 ];
Cout < Str < Endl; // Because space is allocated and no data is written, garbled characters are returned when the address where no data is written is read at the output location.
For ( Int I = 0 ; I < 20 ; I ++ )
{
STR [I] = ' A ' ;
// If (I = 10)
// {
// STR [10] = '\ 0 '; // The string ends when \ 0 is met, so strlen (STR) = 10
// }
}
STR [ 19 ] = ' \ 0 ' ; // End string. garbled characters are returned if this sentence is not used.

Strcpy (str2, STR ); // If you assign a string to a column with equal capacity, garbled characters may occur because the last character of the string must be \ 0.
Cout < Str < " , " < Sizeof (STR) < " , " < Strlen (STR) < " , " < Str2 < Endl;

Int K = 5 ;
K = K + ( ++ K );
Cout < K < Endl;

Int B1;
CIN > B1;
Return 0 ;
}



The output result is as follows:







Char STR [ 20 ];

Defines an array of 20 characters, and the system will allocate 20 consecutive spaces to STR; At the beginning, there is no value in each space;

STR points to the first space of the array. STR is equivalent to a constant pointer, so STR cannot be written. = ?;



Sizeof (STR) the length of the space occupied by the table 'str, Char STR [ 20 ] Indicates that the length is 20, that is, the value of sizeof (STR) has been defined,

Is the length of the character to create an array.



Strlen (STR) indicates the number of characters in Str. There are 19 characters. The last \ 0 is not counted.



Int K = 5 ;
K = K + ( ++ K );

Last K = 12 This is really not easy to understand





Understanding: I can understand ++ And ++ I:
For an expression; if ++ I; is equivalent to adding a row before the expressionCodeI = I + 1 ;
Likewise, I ++ ; Is equivalent to adding a line of code after the expression I = I + 1

Int K = 5 ; K = K + ( ++ K); equivalent to int K = 5 ; K = K + 1 ; K = K + K;

Test Question: recursive monkey peach stealing

// The monkey has a peach problem. On the first day, the monkey picked up a few peaches and immediately ate half of them. They were not addicted, and they ate another one.
// The next day, I will eat half of the remaining peaches and one more. In the future, I will eat the remaining half of the previous day every day.
// By 10th days, there was only one peach left. Programming try to find out how many peaches are picked on the first day.
Int Eat ( Int Currentcount, Int I)
{
If (I = 10 )
{
Return Currentcount;
} Else {
Currentcount = (Currentcount + 1 ) * 2 ;
I ++ ;
}
Return Eat (currentcount, I );
}

Cout < Eat ( 1 , 10 ) < Endl;

We can see that we picked 1534 peaches on the first day.

The last monkey to steal peaches actually stole 1534.

 

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.