CHAP2

代码 1
int i,j,l;
short int si;
unsigned int ui;
double balance, profit, loss;


代码 2
void func1(void)
{
  int x;
  x = 10;
}


void func2(void)
{
  int x;
  x = -199;
}


代码 3
void f(void)
{
  int t; 
  scanf("%d%*c", &t); 

  if(t==1) {
    char s[80];  /* this is created only upon
                    entry into this block */
    printf("Enter name:");
    gets(s);
    /* do something ... */
  }

  /* s not known here */
}


代码 4   变量作用域问题.之前一直搞错.这样是对的,且两个x变量各不相同
#include <stdio.h>

int main(void)
{
  int x;
  x = 10;

  if(x == 10) {
    int x; /* this x hides the outer x */
    x = 99;
    printf("Inner x: %d\n", x);    //99
  }

  printf("Outer x: %d\n", x);    //10

  return 0;
}


代码 5    错误,把 int j 在 i=10 之前即可.这又是什么原因?先定义后使用是指所有变量都定义后才能使用么?
/* This function is in error if compiled as a C89 program.
*/
void f(void)
{
  int i;
  i = 10;

  int j;  /* this line will cause an error */
  j = 20;
}


代码 6    自定义函数声明
#include <stdio.h>

void f(void); 

int main(void)
{
  int i; 
  for(i=0; i<10; i++)  f();
  return 0;
}


void f(void)
{
  int j = 10;
  printf("%d ", j);
  j++;  /* this line has no lasting effect */
}


代码 7
/* Return 1 if c is part of string s; 0 otherwise */
int is_in(char *s, char c)
{
  while(*s)
    if(*s==c) 
        return 1;
    else 
        s++;

  return 0;
}


代码 8    全局变量
#include <stdio.h>

int count;  /* count is global  */

void func1(void);
void func2(void);

int main(void)
{
  count = 100;
  func1();

  return 0;
}

void func1(void) 
{
  int temp;
  temp = count;
  func2();
  printf("count is %d", count); /* will print 100 */
}

void func2(void)
{
  int count;    /*全局变量被覆盖,已成局部变量*/
  for(count=1; count<10; count++)
    putchar('.');
}


代码 9    关键字const,限定一个变量不允许被改.
const int a=10;


代码 10
#include <stdio.h>

void sp_to_dash(const char *str);

int main(void)
{
  sp_to_dash("this is a test");
  return 0;
}

void sp_to_dash(const char *str)    /*指针变量接收字符串*/
{
  while(*str) 
  {
    if(*str== ' ') 
        printf("%c", '-');
    else 
        printf("%c", *str);
    str++;
  }
}



代码 11    常量const不能被修改
/* This is wrong. */
void sp_to_dash(const char *str)
{
  while(*str) 
  {
    if(*str==' ' ) 
        *str = '-'; /* can't do this; str is const */
    printf("%c", *str);
    str++;
  }
}



代码 12    关键字volatile,告诉compiler不能做任何优化
const volatile char *port = (const volatile char *) 0x30;



代码 13     关键字extern,全局变量声明
            若本文件 引用别的文件中的全局变量,一定要加上extern 声明一下
#include <stdio.h>

int main(void)
{
  extern int first, last; /* use global vars */
  printf("%d %d", first, last);
  return 0;
}

/* global definition of first and last */
int first = 10, last = 20;



代码 14     关键字static,存储在静态数据区
int series(void)
{
  static int series_num;
  series_num = series_num+23;
  return series_num;
} 

/*static的例子
int main(void)
{
 int a ;
 int i = 0;
 while(1)
 {
 a = series();
 printf("%d\n",a);
 if(i>5) break;
 i++;
 }
 return 0;
}

int series(void)
{
  static int series_num = 1;    /*去掉static再比较运行结果*/
  series_num = series_num+1;
  return series_num;
} 
*/



代码 15
int series(void)
{
  static int series_num = 100;
  series_num = series_num+23;
  return series_num;
}



代码 16
/* This must all be in one file - preferably(较好;宁可) by itself. */
static int series_num;

void series_start(int seed);

int series(void);

int series(void)
{
  series_num = series_num+23;
  return series_num;
}

/* initialize series_num */
void series_start(int seed)
{
  series_num = seed;
}



代码 17    关键字register
int int_pwr(register int m,  register int e)
{
  register int temp;

  temp = 1;

  for(; e; e--) temp = temp * m;
  return temp;
}



代码 18
char ch = 'a';
int first = 0;
double balance = 123.23;



代码 19
wchar_t wc;
wc = L'A';



代码 20    十六进制hex,八进制octonary
int hex = 0x80;   /* 128 in decimal */
int oct = 012;    /* 10 in decimal */



代码 21
#include <stdio.h>

int main(void)
{
  printf("\n\tThis is a test.");
  return 0;
}



代码 22
int x;
char ch;
float  f;

void func(void)
{
  ch = x;    /* line 1 */
  x = f;     /* line 2 */
  f = ch;    /* line 3 */
  f = x;     /* line 4 */
}



代码 23
x = y = z = 0;


代码 24
x = x+10; 


代码 25
x += 10;


代码 26
x = x-100; 


代码 27
x -= 100;


代码 28
int x, y;

x = 5;
y = 2;


printf("%d ", x/y);   /* will display 2 */
printf("%d ", x%y);   /* will display 1, the remainder of the integer division */

x = 1;
y = 2;

printf("%d %d", x/y, x%y); /*  will display 0 1 */


代码 29
x = x+1;


代码 30
++x; 


代码 31
x = x-1;


代码 32
x--;


代码 33
x = x+1;


代码 34
++x;


代码 36
x = 10;
y = ++x;


代码 37
x = 10;
y = x++;


代码 38    逻辑运算符
#include <stdio.h>

int xor(int a, int b);

int main(void)
{
  printf("%d", xor(1, 0));
  printf("%d", xor(1, 1));
  printf("%d", xor(0, 1));
  printf("%d", xor(0, 0));

  return 0;
}

/* Perform a logical XOR operation using the two arguments. */
int xor(int a, int b)
{
  return (a || b) && !(a && b);
}


代码 39
int x; 

x = 100;
printf("%d", x>10);


代码 40
char get_char_from_modem(void)
{
  char ch;
  ch = read_modem(); /* get a character from the modem port */
  return(ch & 127);
}


代码 41    位运算
/* A bit shift example. */
#include <stdio.h>

int main(void)
{
  unsigned int i;
  int j;

  i = 1;

  /* left shifts */
  for(j=0; j<4; j++) {
    i = i << 1;  /* left shift i by 1, which is same as a multiply by 2 */
    printf("Left shift %d: %d\n", j, i);
  }

  /* right shifts */
  for(j=0; j<4; j++) {
    i = i >> 1;  /* right shift i by 1, which
                    is same as a division by 2 */
    printf("Right shift %d: %d\n", j, i);
  }

  return 0;
}



代码 42    ~这个是什么意思?
/* A simple cipher function. */
char encode(char ch)
{
  return(~ch); /* complement it */
}


代码 43    三元运算符
x = 10;
y = x>9 ? 100 : 200;



代码 44    条件语句
x = 10;
if(x>9) 
    y = 100;
else 
    y = 200;



代码 45    取地址符
m = &count;


代码 46    指针指向的变量?
q = *m; 


代码 47    定义一个char类型的指针变量?
char *ch;


代码 48
int x, *y, count;


代码 49
#include <stdio.h>

int main(void)
{
  int target, source;
  int *m;

  source = 10;
  m = &source;
  target = *m;

  printf("%d", target);

  return 0;
}



代码 50    sizeof,数据类型所占的内存字节数
double f;

printf("%d ", sizeof f);
printf("%d", sizeof(int));



代码 51     文件操作,fwrite  /* Write 6 integers to a disk file. */
void put_rec(int rec[6], FILE *fp)
{
  int len; 

  len = fwrite(rec, sizeof(int)*6, 1, fp);
  if(len != 1) 
    printf("Write Error");
}



代码 52
x = (y=3, y+1);



代码 53    结构体
struct employee
{
  char name[80];
  int age;
  float wage;
} emp; 

struct employee *p = &emp; /* address of emp into p */


代码 54    结构体访问成员
emp.wage = 123.23;


代码 55    结构体访问成员    
p->wage = 123.23;



代码 56    数组
#include <stdio.h>
char s[80];

int main(void)
{
  s[3] = 'X';
  printf("%c", s[3]);

  return 0;
} 



代码 57
x = f1() + f2();



代码 58
(float) x/2



代码 59
#include <stdio.h>

int main(void) /* print i and i/2 with fractions */
{
  int i;

  for(i=1; i<=100; ++i)
    printf("%d / 2 is: %f\n", i, (float) i /2);

  return 0;
}



代码 60
x=10/y~(127/x);

x = 10 / y ~(127/x);


代码 61
x = y/3-34*temp+127;


x = (y/3) - (34*temp) + 127;  

你可能感兴趣的:(CHAP2)