现在的位置: 首页 > 综合 > 正文

Expert C Programming阅读笔记 2

2017年12月15日 ⁄ 综合 ⁄ 共 2025字 ⁄ 字号 评论关闭

One new feature introduced with ANSI C is the convention that adjacent string literals are
concatenated into one string. 

 
printf( "A second favorite children's book " 
     "is 'Thomas the tank engine and the Naughty Enginedriver who " 
     "tied down Thomas's boiler safety valve'" );  

Thus, a small problem like this:

char* abc[]={ "fasdfasdfsadf"  /* Comma is forgotten */
       "fasdfasdfsdf"}

would cause some troubles.

First Time Through

 
generate_initializer(char * string) 
{
  static char separator=''; 
  printf( "%c %s /n", separator, string); 
  separator = ','; 
}
The first time through, this will print a space followed by an initializer. All subsequent
initializers (if any) will be preceded by a comma. Viewing the specification as "first time
through, prefix with a space" rather than "last time through, omit the comma suffix" makes
this simple to program.

Too Much Default Visibility
Whenever you define a C function, its name is globally visible by default. If you want to restrict access to the function, you are obliged to specify the static keyword.

       function apple (){ /* visible everywhere */ } 
extern function pear () { /* visible everywhere */ } 
static function turnip(){ /* not visible outside this file */ }

Software objects should have the most limited scope by default.

** MISC NOTE: int abc; sizeof abc; sizeof(int);

"Some of the Operators Have the Wrong Precedence"

read the book pp.46

 Some authorities recommend that there are only two precedence levels to remember in C: multiplication and division come before addition and subtraction. Everything else should be in parentheses. We think that's excellent advice.

PS: 谭浩强个大傻居然害得一群学生背运算顺序

Order of Evaluation
x = f() + g() * h();
The values returned by g() and h() will be grouped together for multiplication, but g and h
might be called in any order. Similarly, f might be called before or after the multiplication,
or even between g and h. All we can know for sure is that the multiplication will occur
before the addition (because the result of the multiplication is one of the operands in the
addition). It would still be poor style to write a program that relied on that knowledge. Most
programming languages don't specify the order of operand evaluation. It is left undefined so
that compiler-writers can take advantage of any quirks in the architecture, or special
knowledge of values that are already in registers.

 

抱歉!评论已关闭.