c programe language learn notes 3

爱,突然的昨天晚上寝室停电

看到第二章了,感觉英文水平不够啊

还是把自己觉得要留意的总结一下

1  External and static variables are initialized to zero by default. Automatic variables for which is no explicit initializer have undefined (i.e., garbage) values.
外部变量和静态变量初始的时候默认值为零,自动变量在初始的时候是没有默认值的。

2 The % operator cannot be applied to a float or double.
c >= '0' && c <= '9'
can be replaced by
isdigit(c)
We will use the <ctype.h> functions from now on.
c >= '0' && c <= '9' 可以被 isdigit(c) 代替,当然要包括头文件<ctype.h>

3  If either operand is long double, convert the other to long double.
 Otherwise, if either operand is double, convert the other to double.
 Otherwise, if either operand is float, convert the other to float.
 Otherwise, convert char and short to int.
 Then, if either operand is long, convert the other to long.
应该是用于双目运算符时候,低一级的数值类型要向高以及转化

4 The % operator cannot be applied to a float or double.
取模不能用于浮点型或双精度型,也就是说只能用于int型的数值

5 Since an argument of a function call is an expression,
type conversion also takes place when arguments are passed to functions.
In the absence of a function prototype, char and short become int,
and float becomes double.
This is why we have declared function arguments to be int and double even when the function is called with char and float.
重点解释一下我认为的吧,
如果你的函数没有定义返回类型,那么你的函数自变量在返回的时候会自动从char,short转化为int。而float会转化为double型的。
所以这就是为什么我们定义函数自变量的为int和double,当这个函数被char和float调用的时候。
//不过自己还是懵懵懂懂的,希望那位大哥可以说的明白一些^。^

2.1
Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.

#include <stdio.h>
#include <limits.h>
int 
main ()
{
 printf("Size of Char %d/n", CHAR_BIT);
 printf("Size of Char Max %d/n", CHAR_MAX);
 printf("Size of Char Min %d/n", CHAR_MIN);
 printf("Size of int min %d/n", INT_MIN);
 printf("Size of int max %d/n", INT_MAX);
 printf("Size of long min %ld/n", LONG_MIN);       /* RB */
 printf("Size of long max %ld/n", LONG_MAX);       /* RB */
 printf("Size of short min %d/n", SHRT_MIN);
 printf("Size of short max %d/n", SHRT_MAX);
 printf("Size of unsigned char %u/n", UCHAR_MAX); /* SF */
 printf("Size of unsigned long %lu/n", ULONG_MAX); /* RB */
 printf("Size of unsigned int %u/n", UINT_MAX);    /* RB */
 printf("Size of unsigned short %u/n", USHRT_MAX); /* SF */
 return 0;
} 
这个函数是copy来的,主要是不懂得某些c中的标准函数,还是要多看附录啊
2.2
Exercise 2-2 discusses a for loop from the text. Here it is:
for(i=0;i<lim-1;)
{
 if((getchar())!='/n')
  if(c!=EOF)

   s[i++]=c;
}
和参考答案不一样,觉得这样也是可以的哦,而且比较简洁的说
2.3
Write the 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 .
程序思路:
将一个十六进制数,依次从左到右将其各自的数据乘以16的 n 次幂, 
这里 n 的取值为:从右往左起顺序排列数再减去 1, 如 123 那么1排第2位, 
2排第1位,3排第0位, 如果有小数位,那么依次从小数点往后是-1、-2、-3 ... 
为了简单就写整数位的

long htoi (char s[]) {
 int i;
 int len=strlen(s);
 int pos=len-1;
 long sum=0;
    for(i=0;i<len;i++)
    {
     sum+=s[pos]*power(16,i);
     pos--;
    }
    return sum;
}
long power(int m,int n) {
    int i;
    long sum=1;
    for(i=1;i<n;i++)
    {
     sum*=m;              
    }
    return sum;
}
2.4 
Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
 
void squeeze2(char s1[], char s2[])
{
  int i, j, k;
  int instr2 = 0;

       
       
        
         
       
       
  for(i = j = 0; s1[i] != '/0'; i++)
  {
    instr2 = 0;
    for(k = 0; s2[k] != '/0' && !instr2; k++)
    {
      if(s2[k] == s1[i])
      {
        instr2 = 1;
      }
    } 

       
       
        
         
       
       
    if(!instr2)
    {
      s1[j++] = s1[i];//觉得这里是这个程序思想的要点,如果没有相同的就放入字符数组中。
	//可能这样些会更明白一些
	//s1[j]=s1[i];
	//j++;
    } 
  }
  s1[j] = '/0';
}

觉得这个程序比我自己想的要好许多
2.5
Write the function any(s1,s2) , which returns the first location in the 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.)
int squeeze1(char s1[], char s2[]) {
 int i,j;
 int pos=0;
 for(i=0;flag==-1&&s1[i]!='/0';i++)
  for(j=0;flag==-1&&s2[j]!='/0';j++)
   if(s1[i]==s2[j])
    pos=i;                
                     
                                                            
 return pos;
}
觉得这样写也不个办法啊,算了只要看的懂就可以了的说,该写的时候写吧
……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值