📄 chap19.lst
字号:
listing 1
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char num[80];
gets(num);
printf("Absolute value is %d.\n", abs(atoi(num)));
return 0;
}
listing 2
ch = read_port();
assert(!(ch & 128)); /* check bit 7 */
listing 3
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char num1[80], num2[80];
printf("Enter first number: ");
gets(num1);
printf("Enter second number: ");
gets(num2);
printf("The sum is: %f", atof(num1)+atof(num2));
return 0;
}
listing 4
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char num1[80], num2[80];
printf("Enter first number: ");
gets(num1);
printf("Enter second number: ");
gets(num2);
printf("The sum is: %d", atoi(num1)+atoi(num2));
return 0;
}
listing 5
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char num1[80], num2[80];
printf("Enter first number: ");
gets(num1);
printf("Enter second number: ");
gets(num2);
printf("The sum is: %ld", atol(num1)+atol(num2));
return 0;
}
listing 6
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char *alpha="abcdefghijklmnopqrstuvwxyz";
int comp(const void *, const void *);
int main(void)
{
char ch;
char *p;
do {
printf("Enter a character: ");
scanf("%c%*c", &ch);
ch = tolower(ch);
p = (char *) bsearch(&ch, alpha, 26, 1, comp);
if(p) printf("is in alphabet\n");
else printf("is not in alphabet\n");
} while(p);
return 0;
}
/* Compare two characters. */
int comp(const void *ch, const void *s)
{
return *(char *)ch - *(char *)s;
}
listing 7
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
div_t n;
n = div(10,3);
printf("Quotient and remainder: %d %d\n", n.quot, n.rem);
return 0;
}
listing 8
int decpnt, sign;
char *out;
out = ecvt(10.12, 5, &decpnt, &sign);
listing 9
int decpnt, sign;
char *out;
out = fcvt(10.12, 5, &decpnt, &sign);
listing 10
/* compute and print payroll checks */
system("payroll");
_fpreset();
listing 11
char buf[80];
gcvt(10.12, 5, buf);
listing 12
p = getenv("PATH");
listing 13
#include <conio.h>
#include <string.h>
void pswd (char *pw)
{
char *input;
do {
input = getpass("Enter your password:");
}while (strcmp("starbar", input));
printf("You're in!");
}
listing 14
printf("This process ID of this program is %d\n", getpid());
listing 15
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char p[17];
itoa(1423, p, 16);
printf(p);
return 0;
}
listing 16
#include <stdio.h>
#include <stdlib.h>
long int get_labs()
{
char num[80];
gets(num);
return labs(atol(num));
}
listing 17
long quot; /* the quotient */
long rem; /* the remainder */
listing 18
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
ldiv_t n;
n = ldiv(100000L,3L);
printf("Quotient and remainder: %ld %ld.\n", n.quot, n.rem);
return 0;
}
listing 19
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
char *alpha="abcdefghijklmnopqrstuvwxyz";
int comp(const void *, const void *);
int main(void)
{
char ch;
char *p;
size_t num=26;
do {
printf("Enter a character: ");
scanf("%c%*c", &ch);
ch = tolower(ch);|
p = (char *) lfind(&ch, alpha, &num, 1, comp);
if(p) printf("is in alphabet\n");
else printf("is not in alphabet\n");
} while(p);
return 0;
}
/* Compare two characters. */
int comp(const void *ch, const void *s)
{
return *(char *)ch - *(char *)s;
}
listing 20
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf ebuf;
void f2(void);
int main(void)
{
int i;
printf("1 ");
i = setjmp(ebuf);
if ( i !=0 ) {
printf("%d",i);
exit(1);
}
f2();
return 0;
}
void f2(void)
{
printf("2 ");
longjmp(ebuf, 3);
}
listing 21
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char p[34];
ltoa(1423, p, 16);
printf(p);
return 0;
}
listing 22
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long l = 1;
printf("1 rotated left 2 bits = %ld\n", _lrotl(l,2));
l=16;
printf("16 rotated right 2 bits = %ld\n", _lrotr(l,2));
return 0;
}
listing 23
#include <stdlib.h>
#include <stdio.h>
int main (void)
{
printf("max of 10, 20 is %d\n", max (10, 20));
printf("min of 10, 20 is %d\n", min (10, 20));
return 0;
}
listing 24
printf("%d", mblen(mb, 2));
listing 25
mbstowcs(wstr, mb, 4);
listing 26
mbtowc(wstr, mbstr, 2);
listing 27
#include <stdio.h>
#include <stdlib.h>
int num[10] = {
1,3,6,5,8,7,9,6,2,0
};
int comp(const int *, const int *);
int main(void)
{
int i;
printf("Original array: ");
for(i=0; i<10; i++) printf("%d ",num[i]);
printf("\n");
qsort(num, 10, sizeof(int),
(int(*)(const void *, const void *)) comp);
printf("Sorted array: ");
for(i=0; i<10; i++) printf("%d ", num[i]);
return 0;
}
/* compare the integers */
int comp(const int *i, const int *j)
{
return *i - *j;
}
listing 28
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void myhandler(int);
int main(void)
{
signal(SIGTERM, myhandler);
raise(SIGTERM);
printf("This line will not be executed.\n");
return 0;
}
void myhandler(int notused)
{
printf("Program terminated.\n");
exit(1);
}
listing 29
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
for(i=0; i<10; i++)
printf("%d ", rand());
return 0;
}
listing 30
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
randomize();
for(i=0; i<10; i++) printf("%d ", random(25));
return 0;
}
listing 31
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned val = 64;
printf("Rotated left 2 bits = %d\n", _rotl(val,2));
printf("Rotated right 2 bits = %d\n", _rotr(val,2));
return 0;
}
listing 32
_setcursortype(_SOLIDCURSOR);
listing 33
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf ebuf;
void f2(void);
int main(void)
{
int i;
printf("1 ");
i = setjmp(ebuf);
if(i != 0) {
printf("%d",i);
exit(1);
}
f2();
return 0;
}
void f2(void)
{
printf("2 ");
longjmp(ebuf, 3);
}
listing 34
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char fpath[64];
if(argc!=3) {
printf("Usage: FINDFILE <fname> <ename>");
return 1;
}
_searchenv(argv[1], argv[2], fpath);
/* fpath will contain path if file is found */
if(*fpath) printf("Path: %s", fpath);
return 0;
}
listing 35
signal(SIGINT, myint);
listing 36
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Seed rand with the system time
and display the first 10 numbers.
*/
int main(void)
{
int i, stime;
long ltime;
/* get the current calendar time */
ltime = time(NULL);
stime = (unsigned int) ltime/2;
srand(stime);
for(i=0; i<10; i++) printf("%d ", rand());
return 0;
}
listing 37
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
char *end, *start="100.00 pliers 200.00 hammers";
end = start;
while(*start) {
printf("%f, ",strtod(start, &end));
printf("Remainder: %s\n", end);
start = end;
/* move past the non-digits */
while(!isdigit(*start) && *start) start++;
}
return 0;
}
listing 38
100.000000, Remainder: pliers 200.00 hammers
200.000000, Remainder: hammers
listing 39
#include <stdio.h>
#include <stdlib.h>
long int read_long()
{
char start[80], *end;
printf("Enter a number: ");
gets(start);
return strtol(start, &end, 10);
}
listing 40
char dest[3];
swab("Hi", dest, 2);
printf(dest);
listing 41
#include <stdlib.h>
int main(void)
{
system("dir");
return 0;
}
listing 42
int ch;
ch = getche():
ch = toascii(ch);
listing 43
umask(S_IREAD);
listing 44
#include <utime.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc!=2) {
printf("Usage: SETTIME <fname>");
return 1;
}
/* set to current system time */
utime(argv[1], NULL);
return 0;
}
listing 45
/* Variable length argument example - sum a series.*/
#include <stdio.h>
#include <stdarg.h>
double sum_series(int, ...);
int main(void)
{
double d;
d = sum_series(5, 0.5, 0.25, 0.125, 0.0625, 0.03125);
printf("Sum of series is %f\n",d);
return 0;
}
double sum_series(int num, ...)
{
double sum = 0.0, t;
va_list argptr;
/* initialize argptr */
va_start(argptr, num);
/* sum the series */
for(; num; num--) {
t = va_arg(argptr,double);
sum += t;
}
/* do orderly shutdown */
va_end(argptr);
return sum;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -