Chinaunix首页 | 论坛 | 博客
  • 博客访问: 306319
  • 博文数量: 63
  • 博客积分: 1482
  • 博客等级: 上尉
  • 技术积分: 1185
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 19:06
个人简介

hello world!

文章分类

全部博文(63)

分类: C/C++

2011-07-15 21:52:24

The C programming language –第二章学习 Chapter 2 – Types,operators and Expressions

2.1 Variable Names:

Although we didn't say so in Chapter 1, there are some restrictions on the names of variables and symbolic constants. Names are made up of letters and digits; the first character must be a letter. The underscore ``_'' counts as a letter; it is sometimes useful for improving the readability of long variable names. Don't begin variable names with underscore, however, since library routines often use such names. Upper and lower case letters are distinct, so x and X are two different names. Traditional C practice is to use lower case for variable names, and alupper case for symbolic constants.

At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees uniqueness only for 6 characters and a single case. Keywords like if else, int, float, etc., are reserved: you can't use them as variable names. They must be in lower case.    

2.2 Variable Names:

There are only a few basic data types in C:

char a single byte, capable of holding one character in the local character set

int an integer, typically reflecting the natural size of integers on the host machine

float single-precision floating point

double double-precision floating point

In addition, there are a number of qualifiers that can be applied to these basic types. short and long apply to integers:

   short int sh;

   long int counter;

The word int can be omitted in such declarations, and typically it is.

2.3 Constants:

2.4 Declarations:

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. For an array, the const qualifier says that the elements will not be altered.

   const double e = 2.71828182845905;

   const char msg[] = "warning: ";

The const declaration can also be used with array arguments, to indicate that the function does not change that array:

   int strlen(const char[]);

The result is implementation-defined if an attempt is made to change a const.

2.5 Arithmetic Operators:

2.6 Relational and Logical Operators:

The relational operators are

       >   >=   <   <=

They all have the same precedence. Just below them in precedence are the equality operators:

   ==   !=

2.7 Type conversions:

Exercise 2-3. Write a function htoi(s), which converts a string of hexadecimal digits  (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F.

/*convert hexadecimal to integer*/

int htoi(char s[])

{

       int i,n,u;

      

       n=0;

      

       for(i=0;(s[i]!='\0') && (s[i]!='\n');)

              ++i;

 

       u=i;

       printf("have %d bit.\n",u);

       for(i=2;i 

       {     

              printf("text:%c\n",s[i]);

              if(isdigit(s[i]))

                     n=n*16+(s[i]-'0');

              if((s[i]>='a')&&(s[i]<='f'))

              {

                     n=16*n+((s[i]-'a')+10);

              }

              if((s[i]>='A')&&(s[i]<='F'))

              {

                     n=16*n+((s[i]-'A')+10);

              }

       }

       printf("%d\n",n);

       return n;

}

2.8 Increment and Decrement operators:

C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. We have frequently used ++ to increment variables, as in

   if (c == '\n')

       ++nl;

The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then

   x = n++;

sets x to 5, but

   x = ++n;

The increment and decrement operators can only be applied to variables; an expression  like (i+j)++ is illegal.

Exercise 2-4. Write an alternative version of squeeze(s1,s2) that deletes each charac -ter in s1 that matches any character in the string s2.

 /*

  *deletes each character in s1 that

  *matches any character in the sting s2

  */

 void my_squeeze(char s1[],char s2[])

{

       int i,j,k=0;

 

       printf("debuge!\n");

             

       for(i=0;s1[i]!='\0';i++)

       {

              printf("s1: %c\n",s1[i]);

              for(j=0;s2[j]!='\0';j++)

              {

                     printf("s2:%c\n",s2[j]);

                     if(s1[i]!=s2[j])

                     {

                            s1[k]=s1[i];

                            printf("s1[%d]:%c!\n",k,s1[k]);

                            ++k;

                     }

              }

       }

       s1[k]='\0';   

       printf("debuge end!\n");

}

Exercise 2-5. Write the function any(s1,s2), which returns the first location in a string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2.  (The standard library function strpbrk does the same job but returns a pointer to the location.)

 /*

  *deletes each character in s1 that

  *matches any character in the sting s2

  */

 

int any(char s1[],char s2[])

 {

      int i,j,k=0,f=0;

      int loc,n=0;

     

      assert((s1!=NULL)&&(s2!=NULL)); //s1地址和s2地址加非0断言

     

       printf("debuge!\n");

      

      for(j=0;s2[j]!='\0';++j)

      {

             for(i=0;s1[i]!='\0';++i)

             {

                     if(s1[i]==s2[j])

                     {

                            printf("%c : %d\n",s2[j],i);

                            f=1;

                            printf("1!\n");

                            loc=n*10+(i+1);//字符数组中的0位,即实际上的第1

                            break;

                     }

               }

        }

        

      printf("debuge end!\n");

        

 if(f==0)

      return -1;

 else

      return loc;     //多个返回数值怎么写?

 }

2.9 Bitwise Operators

C provides six operators for bit manipulation; these may only be applied to integral operands that is, char, short, int, and long, whether signed or unsigned.

&   bitwise AND

|    bitwise inclusive OR

^   bitwise exclusive OR

<<   left shift

>>   right shift

~    one's complement (unary)

The bitwise AND operator & is often used to mask off some set of bits, for example

   n = n & 0177;

sets to zero all but the low-order 7 bits of n.

The bitwise OR operator | is used to turn bits on:

   x = x | SET_ON;

sets to one in x the bits that are set to one in SET_ON.

Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

/*

  *get bits:returns the(right adjusted) n-bit field of x that begins at position p

  *set bits:returns x with the n bits that begin at position p set to

  *                the rightmost n bits of y,leaving the other bits unchanged

  */

 unsigned getbits(unsigned x,int p,int n)

 {

      return (x>>(p-n)&~(~0<

 }

Exercise 2-7. Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.  

unsigned int setbits(unsigned int x,int p, int n,unsigned int y)

 {

      return ( ( (~0<

 }

  Exercise 2-8. Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n positions.

/*

  *rightrot: the integer x rotated to the rignt by n positions

  */

 unsigned rightrot(unsigned x,int n)

 {

      return ((x & (~ (~0<>n);

 }

2.10 Assignment operators and Expressions

An expression such as

   i = i + 2

in which the variable on the left side is repeated immediately on the right, can be written in the compressed form

   i += 2

The operator += is called an assignment operator. Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op=, where op is one of

   +   -   *   /   %   <<   >>   &   ^   |

If expr1 and expr2 are expressions, then

   expr1 op= expr2

is equivalent to

   expr1 = (expr1) op (expr2)

except that expr1 is computed only once. Notice the parentheses around expr2:

   x *= y + 1

means

   x = x * (y + 1)

rather than

   x = x * y + 1

 

阅读(1701) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~