📄 chap2.lst
字号:
listing 1
const int a;
listing 2
const int count = 100;
listing 3
const volatile unsigned char *port = (const volatile char *) 0x30;
listing 4
int i, j, l;
short int si;
unsigned int ui;
double balance, profit, loss;
listing 5
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
listing 6
void f(void)
{
int t;
scanf("%d", &t);
if(t==1) {
char s[80]; /* s exists only inside this block */
printf("enter name:");
gets(s);
process(s);
}
/* s is not known here */
}
listing 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;
}
listing 8
#include <stdio.h>
void func1(void), func2(void);
int count; /* count is global */
int main(void)
{
count = 100;
func1();
return 0;
}
void func1(void)
{
func2();
printf("count is %d", count); /* will print 100 */
}
void func2(void)
{
int count;
for(count=1; count<10; count++)
putchar(' ');
}
listing 9
#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;
listing 10
#include <stdio.h>
#include <conio.h>
int count(int i);
int main(void)
{
do {
count(0);
} while(!kbhit());
printf("count called %d times", count(1));
return 0;
}
int count(int i)
{
static int c=0;
if(i) return c;
else c++;
return 0;
}
listing 11
int series(void)
{
static int series_num;
series_num = series_num+23;
return series_num;
}
listing 12
/* This must all be in one file - preferably by itself */
static int series_num;
int series(void);
void series_start(int seed);
int series(void)
{
series_num = series_num + 23;
return series_num;
}
/* initialize series_num */
void series_start(int seed)
{
series_num = seed;
}
listing 13
int int_pwr(register int m, register int e)
{
register int temp;
temp = 1;
for(; e; e--) temp *= m;
return temp;
}
listing 14
x = y = z = 0;
listing 15
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 */
}
listing 16
char ch = 'a';
int first = 0;
float balance = 123.23;
listing 17
ch = '\t';
printf("%c this is a test\n", ch);
listing 18
int x, y;
x = 10;
y = 3;
printf("%d", x/y); /* will display 3 */
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 */
listing 19
x = x + 1;
listing 20
++x;
listing 21
x = x - 1;
listing 22
--x;
listing 23
x = x + 1;
listing 24
++x;
listing 26
x = 10;
y = ++x;
listing 27
x = 10;
y = x++;
listing 28
int x;
x = 100;
printf("%d", x > 10);
listing 29
char get_char_from_modem(void)
{
char ch;
ch = read_modem(); /* get a character from the
modem port */
return(ch & 127);
}
listing 30
/* A simple cipher function. */
char encode(char ch)
{
return(~ch); /* complement it */
}
listing 31
x = 10;
y = x>9 ? 100 : 200;
listing 32
x = 10;
if(x>9) y = 100;
else y = 200;
listing 33
m = &count;
listing 34
q = *m;
listing 35
char *pch;
listing 36
int x, *y, count;
listing 37
#include <stdio.h>
/* Assignment with * and &. */
int main(void)
{
int target, source;
int *m;
source = 10;
m = &source;
target = *m;
printf("%d", target);
return 0;
}
listing 38
double f;
printf("%f ", sizeof f);
printf("%d", sizeof(int));
listing 39
/* Write 6 integers to a disk file */
void put_rec(FILE *fp, int rec[6])
{
int size, num;
size = sizeof(int) * 6;
num = fwrite(rec, size, 1, fp);
if(num!=1) printf("Write Error");
}
listing 41
y = 10;
x = (y=y-5, 25/y);
listing 42
struct employee {
char name[80];
int age;
float wage;
} emp;
struct employee *p = &emp; /* address of emp into p */
listing 43
emp.wage = 123.23;
listing 44
p->wage = 123.23;
listing 45
#include <stdio.h>
char s[80];
int main(void)
{
s[3] = 'X';
printf("%c", s[3]);
return 0;
}
listing 46
(float) x/2
listing 47
#include <stdio.h>
/* Print i and i/2 with fractions. */
int main(void)
{
int i;
for(i=1; i<=100; ++i )
printf("%d / 2 is: %f\n", i, (float) i/2);
return 0;
}
listing 48
x=10/y~(127/x);
x = 10 / y ~(127/x);
listing 49
x=y/3-34*temp&127;
x = (y/3) - (34*temp) & 127;
listing 50
x = x + 10;
listing 51
x += 10;
listing 52
x = x - 100;
listing 53
x -= 100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -