C expert programming note 2

Source: Internet
Author: User

Chapter 2 This is not a Bug, but a Language Feature
At any time, if such a statement malloc (strlen (str); is met, it can almost be determined that it is incorrect, malloc (strlen (str) + 1 ); yes;

Conclusion-progress is an important driver for the gradual development of computer software engineering and programming language design art;

[Cpp]
# Include <stdio. h>
 
Int main (int argc, char ** argv)
{
Const int one = 1;
 
Int I = 1;
Switch (I ){
Case one: printf ("1 ");
}
}


The above code compilation fails, indicating that const does not actually represent a constant;
The break statement actually jumps out of the latest loop or switch statement;

When the operand of sizeof is a type name, parentheses must be added on both sides (which is often mistaken for a function), but parentheses are not required for the operand if it is a variable;

Some experts suggest note that two priorities in the C language are enough: multiplication and division are greater than addition and subtraction, and brackets are added when designing other operators;

Operator combination: it is the arbitration. When several operators have the same priority, it determines which one to execute first;

All the value assignment operators (including those conforming to the value assignment) have the right combination. (They are also combined with the single object operator );

[Cpp]
# Include <stdio. h>
# Include <string. h>
Char * f ()
{
Char buf [10];
 
Strcpy (buf, "abcdefg ");
Return buf;
}
Int main (int argc, char ** argv)
{
Printf ("% s", f ());
}

The above Code cannot print the expected value, because the buf is a local variable. At the end of the function, no one knows what the address pointed to by this pointer is because the variable has been destroyed;

From CodeBlog

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.