/* abort.c */ #include #include main(){ int x, y; printf("Please input two integers(x,y) for division除數: "); scanf("%d %d", &x, &y); system("color 2E"); /* Is the divisor zero? */ if ( y==0 ) { printf( "Sorry, the divisor can't be ZERO!\a\n" ); abort(); // 取消執行 } /* Print the result. */ printf("%d / %d = %d\n", x, y, x/y); system("pause"); } /* abs.c */ #include main(){ int num, abs_num; printf("請輸入一整數: "); scanf("%d", &num); abs_num=abs(num); printf("此數 %i 的絕對值為: %d\n", num, abs_num); } /* access.c (存取權限) szeto */ #include #include main(){ // R_OK=4,W_OK=2,X_OK=1,F_OK=0 printf("R_OK=%i\n", access("exp.exe",R_OK)); printf("W_OK=%i\n", access("exp.exe",W_OK)); printf("X_OK=%i\n", access("exp.exe",X_OK)); printf("F_OK=%i\n", access("exp.exe",F_OK)); printf("F_OK=%i\n", access("not-exist.txt",F_OK)); // -1 } /* acos.c */ #include #include #include #define RadianToDegree 57.29578 main(){ double v1, acos_val; for (v1 = -1.0; v1 <= 1.0; v1 += 0.1){ acos_val = acos(v1)*RadianToDegree; if (errno != EDOM) printf("Arc cosine (%4.1f) = %3.0f\n", v1, acos_val); else printf("Input error"); } } /* asctime.c */ #include #include main(){ struct tm *currenttime; time_t bintime; time(&bintime); currenttime=localtime(&bintime); printf("目前時間: %s\n", asctime(currenttime)); } /* asin.c */ #include #include #include #define RadianToDegree 57.29578 main(){ double v1, asin_val; for (v1 = -1.0; v1 <= 1.0; v1 += 0.1){ asin_val = asin(v1)*RadianToDegree; if (errno != EDOM) printf("Arc sin(%4.1f) = %3.0f\n", v1, asin_val); else printf("Input error"); } } /* assert.c */ #include #include main(){ int x, y; printf("Please input two integer for division: "); scanf("%d %d", &x, &y); /* Is the divisor zero? */ assert(y != 0); // if(y==0) exit(0); /* Print the result. */ printf("%d / %d = %d\n", x, y, x/y); } /* atan.c */ #include #include #include #define RadianToDegree 57.29578 main(){ double v1, atan_val; for (v1 = -1.0; v1 <= 1.0; v1 += 0.1){ atan_val = atan(v1)*RadianToDegree; if (errno != EDOM) printf("Arc tangent(%4.1f) = %3.0f\n", v1, atan_val); else printf("Input error"); } } /* atan2.c */ #include #include #include #define RadianToDegree 57.29578 main(){ char s[10]; double y, x, atan2_val; printf("輸入y與x,然後計算atan2\n"); printf("y: "); gets(s); y = atof(s); printf("x: "); gets(s); x = atof(s); atan2_val = atan2(y, x)*RadianToDegree; if (errno != EDOM) printf("atan2(%.0f/%.0f) = %.2f\n", y, x, atan2_val); else printf("Input error"); } /* atexit.c */ /* Function Prototyping */ void Fun1(); void Fun2(); void Fun3(); void Fun4(); void Fun5(); void Fun6(); main(){ printf("Now recording Function 1..\n"); atexit(Fun1); printf("Now recording Function 2..\n"); atexit(Fun2); printf("Now recording Function 3..\n"); atexit(Fun3); printf("Now recording Function 4..\n"); atexit(Fun4); printf("Now recording Function 5..\n"); atexit(Fun5); printf("Now recording Function 6..\n"); atexit(Fun6); printf("The main program stops!\n"); } /* Function 1. */ void Fun1(){ printf("Function 1 is called.\n"); } /* Function 2. */ void Fun2(){ printf("Function 2 is called.\n"); } /* Function 3. */ void Fun3(){ printf("Function 3 is called.\n"); } /* Function 4. */ void Fun4(){ printf("Function 4 is called.\n"); } /* Function 5. */ void Fun5(){ printf("Function 5 is called.\n"); } /* Function 6. */ void Fun6(){ printf("Function 6 is called.\n"); } /* atof.c 文字轉小數 */ #include main(){ char s[10]; double v; printf("請輸入一浮點數的字串 (e.g.123.456): "); gets(s); v = atof(s); printf("轉換後的數值為 %g\n", v); } /* atoi.c 文字轉整數 */ #include main(){ char s[10]; int v; printf("請輸入一整數字串 (e.g.123.456e2): "); gets(s); v = atoi(s); printf("轉後的整數為 %d\n", v); } /* atol.c 文字轉長整數 */ #include main(){ char s[10]; long v; printf("請輸入一長整數字串 (e.g.123.456e2): "); gets(s); v = atol(s); printf("轉後的整數為 %ld\n",v); } /* bsearch.c 二分搜尋 */ #include #define NUM(arr) sizeof(arr)/sizeof(arr[0]) int intarr[]={11,15,17,122,143,144,199,200,395,466,578,999}; int compare(const int *elem1, const int *elem2){ return(*elem1 - *elem2); } int check(int key){ int *ans; ans = bsearch(&key, intarr, NUM(intarr), sizeof(int), compare); return (ans != NULL); } main(){ int inputint=0; printf("This is a game to test your luck.\n"); printf("Please input one integer: "); scanf("%d", &inputint); if (check(inputint)) printf("Bingo! %d is in the internal array.\n", inputint); else printf("%d isn't in the internal array.\n", inputint); } /* calloc.c */ #include #include main(){ char *Buff; /* Buffer allocated. */ Buff = (char *)calloc(80, sizeof(char)); printf("Buffer allocated.\n"); printf("Please input a string: "); gets(Buff); printf("The string which you input is : \"%s\".\n", Buff); } /* ceil.c 上捨入 */ #include #include main(){ double v1, ceil_val; printf("請輸入一浮點數: "); scanf("%lf", &v1); ceil_val = ceil(v1); printf("ceiling of %.2f = %.2f\n", v1, ceil_val); } /* char_type_test.c */ #include #include #include /* Define a table of function names and numbers */ typedef struct TABLE{ char functionname[16]; int funcnum; }FUNC_TABLE; #define CNTRL 0 #define DIGIT 1 #define GRAPH 2 #define LOWER 3 #define PRINT 4 #define PUNCT 5 #define SPACE 6 #define UPPER 7 #define XDIGIT 8 /* Now declare the tabel and initialize it */ static FUNC_TABLE isfuncs[9] = { "iscntrl",CNTRL,"isdigit",DIGIT, "isgraph",GRAPH,"islower",LOWER, "isprint",PRINT,"ispunct",PUNCT, "isspace",SPACE,"isupper",UPPER, "isxdigit",XDIGIT }; static int numfunc = sizeof(isfuncs)/sizeof(FUNC_TABLE); main(){ int ch, count, i, test_result; int mark=0x2a; /* to mark characters */ char input_func_name[9]; printf("Enter function's name of special attribute ASCII" " character\nthat you want to find out: "); gets(input_func_name); /* Search table for function name and pointer */ for (i=0;i=numfunc) { printf("unknown functions:%s\n",input_func_name); exit(0); } /* Now go over entire ASCII table and mark the */ /* characters that satisfy requested test */ printf("Those marked with a %c satisfy %s\n" ,mark,input_func_name); for (count=0,ch=0;ch<=0x7f;ch++) { printf("%#04x ",ch); /* Print character -- if printable */ if (isprint(ch)) { printf(" %c",ch); } else { printf(" "); } /* Perform the test and put a mark if test succeeds */ switch (isfuncs[i].funcnum) { case CNTRL: test_result=iscntrl(ch); break; case DIGIT: test_result=isdigit(ch); break; case GRAPH: test_result=isgraph(ch); break; case LOWER: test_result=islower(ch); break; case PRINT: test_result=isprint(ch); break; case PUNCT: test_result=ispunct(ch); break; case SPACE: test_result=isspace(ch); break; case UPPER: test_result=isupper(ch); break; case XDIGIT: test_result=isxdigit(ch); break; } if (test_result!=0) { printf("%c ",mark); } else { printf(" ",ch); } count++; if (count==6) { printf("\n"); count=0; } } printf("\n"); } /* clearerr.c */ #include main(){ FILE *fp; char fname[80]; printf("Enter an existing file name:"); gets(fname); /* Open the file */ if ((fp = fopen(fname, "r")) == NULL) { printf("Failed to open : %s\n", fname); } /* Try to write a message to the file... */ printf("Attempt to write a message to the file...\n"); fputs("Test string", fp); if (ferror(fp) != 0) { printf("Error occured!\n"); clearerr(fp); /* Clear the error */ printf("Error cleared.\n"); } } /* clock.c */ #include #include main(){ int i,count=500000000; double a,b,c,d; unsigned t_used; clock_t t_start,t_end; t_start=clock(); for (i=0;i #include #define RadianToDegree 57.29578 main(){ double v1, cos_val; for (v1 = 0; v1 <= 360; v1 += 30){ cos_val = cos(v1/RadianToDegree); printf("Cosine (%3.0f) = %5.1f\n", v1, cos_val); } } /* cosh.c */ #include #include #define RadianToDegree 57.29578 main(){ double v1, cosh_val; for (v1=0; v1<=360; v1+=30){ cosh_val = cosh(v1/RadianToDegree); printf("Hyperbolic cosine (%3.0f) = %6.2f\n", v1, cosh_val); } } /* ctime.c 日期/時間 */ #include #include main(){ time_t bintime; bintime=time(NULL); printf("Today's date and time: %s\n",ctime(&bintime)); } /* difftime.c 時間差 */ #include #include main(){ time_t t_start,t_end; int i; t_start=time(NULL); _sleep(5000); t_end=time(NULL); printf("The program ran in %6.2f seconds\n", difftime(t_end, t_start)); } /* div.c */ #include main(){ char s[10]; int numer, denom; div_t div_result; printf("請輸入分子: "); gets(s); numer = atoi(s); printf("請輸入分母: "); gets(s); denom = atoi(s); div_result=div(numer, denom); printf("%d/%d\n", numer, denom); printf("商 = %d\n餘數 = %d\n", div_result.quot, div_result.rem); } /* exit.c */ #include void fun(); main(){ FILE *fp; char FileName[81]; atexit(fun); printf("Please input an existing file name: "); gets(FileName); /* Open file */ if ((fp = fopen(FileName, "w+")) == NULL) { printf("File not found!\a\n"); abort(); } /* Output data to buffer. */ fprintf(fp, "The function \"exit\" will flush the buffers!\n"); fprintf(fp, "So, you can see this message in this file!\n"); printf("The function \"exit\" flushed the buffers!\n"); exit(0); } void fun(){ printf( "The function \"fun\" is called " ); printf( "when the main program stops!\n" ); } /* exp.c (e^x)*/ #include #include main(){ char s[10]; double v0, v1; printf("請輸入一整數: "); gets(s); v0 = atof(s); v1 = exp(v0); printf("exp(%.1f) = %.2f\n", v0, v1); } /* fabs.c 絕對值 */ #include #include main(){ char s[10]; double v0, v1; printf("請輸入一浮點數: "); gets(s); v0 = atof(s); v1 = fabs(v0); printf("此數的絕對值為 %g\n", v1); } /* fclose.c */ #include main(){ FILE *fp; char s[81]; if ((fp = fopen("fclose.c", "r")) == NULL) { printf("File open failure 失敗.\n"); } printf("=== Top of File ===\n"); while (fgets(s, 80, fp) != NULL) printf(s); fclose(fp); printf("=== File is closed. ===\n"); } /* feof.c */ #include main(){ FILE *fp; char s[81]; if ((fp = fopen("feof.c", "r")) == NULL) { printf("Cannot open feof.c\n"); exit(0); } printf("=== Top of File ===\n"); while (fgets(s, 80, fp)) printf(s); if (feof(fp) != 0) printf("=== End of File ===\n"); else printf("\nFailed to read file."); } /* ferror.c */ #include main(){ FILE *fp; fp = fopen("test", "w"); /* force to occur an error condition */ getc(fp); if (ferror(fp) != 0) { /* Display an error message */ printf("Error to read from file 'test'\n"); /* Cleear error state */ clearerr(fp); } fclose(fp); } /* fflush.c */ #include main(){ FILE *fp; int result; fp = fopen("test", "w"); printf("Write message 'This is a test.' to file...\n"); fputs("This is a test.", fp); printf("Flush the data to file 'test'....\n"); result = fflush(fp); if (result == 0) printf("File was successfully flushed.\n"); else printf("Failed to flush the file.\n"); fclose(fp); } /* fgetc.c */ #include main(){ FILE *fp; int ch; if ((fp = fopen("fgetc.c", "r")) == NULL) { printf("Cannot open file fgetc.c\n"); exit(0); } printf("=== Content of file fgetc.c: ===\n"); while ((ch = fgetc(fp)) != EOF) putchar(ch); printf("=== End of File fgetc.c ===\n"); fclose(fp); } /* fgetpos.c */ #include main(){ FILE *fp; fpos_t curpos; if ((fp = fopen("fgetpos.bak", "w")) == NULL) { printf("Error to create file.\n"); exit(0); } /* Get current file pointer */ if (fgetpos(fp, &curpos) == 0) printf("Current file pointer is at %d\n", curpos); else printf("Error occured!\n"); printf("Now write the message 'This is a test.\\n' to file...\n"); /* Write message to the file */ fputs("This is a test.\n", fp); /* Get current file pointer */ fgetpos(fp, &curpos); printf("Current file pointer is at %d\n", curpos); fclose(fp); } /* fgets.c */ #include main(){ FILE *fp; char s[81]; if ((fp = fopen("fgets.c", "r")) == NULL) { printf("File open failure 失敗.\n"); exit(0); } printf("=== Content of the file fgets.c ===\n"); while (fgets(s, 81, fp) != NULL) printf(s); fclose(fp); printf("=== End of File ===\n"); } /* floor.c 下捨入 */ #include #include main(){ char s[10]; double v1, floor_val; printf("請輸入一浮點數: "); gets(s); v1 = atof(s); floor_val = floor(v1); printf("floor(%g) = %g\n", v1, floor_val); } /* fmod.c */ #include #include main(){ char s[10]; double numerator, denominator, remainder; // 分子,分母,餘數 printf("請輸入分子: "); gets(s); numerator = atof(s); // 分子 printf("請輸入分母: "); gets(s); denominator = atof(s); // 分母 remainder = fmod(numerator, denominator); printf("(%g/%g)的餘數 = %g\n", numerator, denominator, remainder); } /* fopen0.c */ #include #include main(){ FILE *fp, *fp2; char s[81]; if ((fp = fopen("fopen0.c", "r")) == NULL) { printf("File 'fopen0.c' open failure 失敗.\n"); exit(0); } if ((fp2 = fopen("fopen0.bak", "w")) == NULL) { printf("Cannot create 'fopen0.bak'.\n"); exit(0); } printf("\nCopying file fopen0.c to fopen0.bak......\n"); while (fgets(s, 81, fp) != NULL) fputs(s, fp2); fclose(fp); fclose(fp2); printf("\nCompleted.\n"); } /* fopen1.c */ #include main(){ FILE *fp; char s[17]; int length; int i; if ((fp = fopen("fopen0.c", "rb")) == NULL) { printf("Cannot not open file 'fopen.c'.\n"); exit(0); } while ((length = fread(s, 1, 16, fp)) != 0) { for (i = 0; i < 16; i++) if (i < length) printf("%02X ", s[i]); else printf(" "); printf(" "); for (i = 0; i < length; i++) printf("%c", (s[i] < 32) ? '.' : s[i]); printf("\n"); } } /* fprintf.c */ #include main(){ FILE *fp, *fp2; int i = 200; char ch = 'A'; float f = 3.1415f; char *str = "This is a test."; char s[81]; if ((fp2 = fopen("test", "w")) == NULL) { printf("File create failure 建立失敗.\n"); exit(0); } printf("write '%d', '%c', '%f', and '%s' to file 'test'...\n", i, ch, f, str); fprintf(fp2, "%d, %c, %f, %s", i, ch, f, str); printf("Close file 'test'.\n"); fclose(fp2); printf("Reopen file 'test' for reading...\n"); fp = fopen("test", "r"); printf("The results of last writing...\n"); fgets(s, 81, fp); puts(s); fclose(fp); } /* fputc.c */ #include main(){ char *str = "This is a test."; int i; for (i = 0; str[i] != '\0'; i++) fputc(str[i], stdout); printf("\n"); } /* fputs.c */ #include main(){ char *str = "This is a test."; fputs(str, stdout); printf("\n"); } /* fread.c */ main(){ FILE *fp; char s[1024]=""; if ((fp = fopen("fread.c", "rb")) == NULL) { printf("Cannot open file 'fread.c'.\n"); exit(0); } printf("First 1024 characters in file 'fread.c'...\n"); fread(s, sizeof(char), 1024, fp); /* Add a null character to the end oif string buffer */ s[1023] = '\0'; printf("%s", s); fclose(fp); } /* free.c 放 */ #include #include main(){ char *s; if ( ( s = ( char * )malloc( 256 ) ) == NULL ){ printf( "Memory allocated failed!\a\n" ); abort(); } printf( "Memory allocated!\n" ); printf( "Please input a string: " ); gets(s); printf( "This string is \"%s\".\n", s ); free(s); printf( "Memory released!\n" ); } /* freopen.c */ #include main(){ FILE *fp; char s[81]; /* Redirect screen to a file */ if (freopen("screen.out", "w", stdin) == NULL) { fprintf(stderr, "Error to redirecting stdout.\n"); exit(0); } printf("Write message....\n"); /* This message will go to a file. */ fprintf(stdin, "This is a test to screen.out file...\n"); /* Close the standard output fp */ printf("-----\n"); printf("close the file\n"); fclose(stdin); printf("-----\n\n"); /* Get the message from screen.out file */ fp = fopen("screen.out", "r"); fgets(s, 81, fp); printf("The message you wrote to print is :\n"); printf("%s", s); } /* frexp.c */ #include #include main(){ int exponent; double number, mantissa; printf("請輸入一浮點數: "); scanf("%lf", &number); mantissa = frexp(number, &exponent); printf("%g 是 %g 乘以 2 的 %d 次方\n", number, mantissa, exponent); } /* fscanf.c */ #include main(){ FILE *fp; char token[81]; if ((fp = fopen("fscanf.c", "r")) == NULL) { printf("Cannot open file 'fscanf.c'.\n"); exit(0); } printf("All tokens in the file 'fscanf.c' are:\n"); while (fscanf(fp, "%s", token) != EOF) printf("%-20s", token); fclose(fp); } /* fseek.c 移至 */ #include long filesize(char *filename); main(){ printf("The size of file %s is %ld\n", "fseek.c", filesize("fseek.c")); } long filesize(char *filename){ FILE *fp; long length; if ((fp = fopen(filename, "rb")) == NULL) { printf("File %s doesn't exist.\n", filename); exit(0); } // 0:SEEK_SET,1:SEEK_CUR,2:SEEK_END /* Move file indicator to the end of the file */ fseek(fp, 0L, SEEK_END); /* get the length of the file */ length = ftell(fp); return length; } /* fsetpos.c */ #include main(){ FILE *fp; fpos_t curpos; char s[20]; if ((fp = fopen("test", "w")) == NULL) { printf("Cannot create file 'test'.\n"); exit(0); } printf("Get current file location...\n"); fgetpos(fp, &curpos); printf("Write message 'This is a test 1' to file 'test'...\n"); fputs("This is a test 1", fp); printf("Move file indicator to last file location...\n"); if (fsetpos(fp, &curpos) != 0) { printf("Cannot setting file pointer.\n"); exit(0); } printf("Write message 'THIS IS A TEST 2' to file 'test'...\n"); fputs("THIS IS A TSET 2", fp); fclose(fp); fp = fopen("test", "r"); printf("Content of the file 'test':\n"); fgets(s, 20, fp); printf("%s\n",s); fclose(fp); } /* ftell.c 目前位置 */ main(){ FILE *fp; if ((fp = fopen("test", "w")) == NULL) { printf("Cannot create file 'test'\n'"); exit(0); } printf("Write message 'This is a test.' to file...\n"); fprintf(fp, "This is a test."); printf("The file pointer is at byte %ld\n", ftell(fp)); fclose(fp); } /* fwrite.c */ #include struct stdnt { char name[10]; int score; }; main(){ FILE *fp; struct stdnt student; strcpy(student.name, "Jerry"); student.score = 80; if ((fp = fopen("test", "r+b")) == NULL) { printf("Cannot create file 'test'.\n"); exit(0); } printf("Write the record to file 'test'...\n"); fwrite(&student, sizeof(struct stdnt), 1, fp); fseek(fp, 0L, SEEK_SET); fread(&student, sizeof(struct stdnt), 1, fp); printf("The contents of the file 'test' are...\n"); printf("Student name: %s\n", student.name); printf("Student score: %d\n", student.score); fclose(fp); } /* getc.c */ #include main(){ FILE *fp; int ch; if ((fp = fopen("getc.c", "r")) == NULL) { printf("Cannot open file getc.c\n"); exit(0); } printf("=== Content of file getc.c: ===\n"); while ((ch = getc(fp)) != EOF) putchar(ch); printf("=== End of File getc.c ===\n"); fclose(fp); } /* getchar.c */ #include main(){ char s[81]; int ch; int i; printf("Please enter a line:\n"); for (i = 0; i < 80 && (ch = getchar()) != '\n'; i++) s[i] = ch; s[i] = '\0'; printf("The line you entered is: %s\n", s); } /* getenv.c */ #include main(){ printf("Current path=%s\n", getenv("PATH")); } /* gets.c */ #include main(){ char s[81]; printf("Please enter a line:\n"); gets(s); printf("The line you entered is: %s\n", s); } /* gmtime.c */ #include #include main(){ time_t current_time; struct tm *current_tm; time(¤t_time); current_tm=gmtime(¤t_time); printf("Greenwich Mean Time = %s\n", asctime(current_tm)); } /* isalnum.c 字母或數字 */ #include #include main(){ int c, count; printf("Alphnumeric:\n\n"); for (count=0,c=0;c<=0x7f;c++) { if (isprint(c)&&(isalnum(c) != 0)) { printf(" %#05d ", c); printf(" %c ", c); count++; } if (count == 6) { printf("\n"); count = 0; } } printf("\n"); } /* isalpha.c 字母 */ #include #include main(){ int c, count; printf("Alphabetic:\n\n"); for (count=0,c=0;c<=0x7f;c++) { if (isprint(c) && (isalpha(c) != 0)) { printf(" %#05d ", c); printf(" %c ", c); count++; } if (count == 6) { printf("\n"); count = 0; } } printf("\n"); } /* labs.c */ #include #include main(){ long v1,v2; printf("請輸入一長整數: "); scanf("%ld", &v1); v2 = labs(v1); printf("此數的絕對值為 %ld\n", v2); } /* ldexp.c */ #include #include main(){ double mantissa, ldexp_val; int exponent; printf("計算 (mantissa*2^exp)\n"); printf("Mantissa: "); gets(s); mantissa = atof(s); printf("Exponent: "); gets(s); exponent= atoi(s); ldexp_val = ldexp(mantissa, exponent); printf("%g 乘以 2 的 %d 次方 = %g\n", mantissa, exponent, ldexp_val); } /* ldiv.c */ #include main(){ long numer, denom; ldiv_t ldiv_result; printf("請輸入分子: "); scanf("%d", &numer); printf("請輸入分母: "); scanf("%d", &denom); ldiv_result = ldiv(numer, denom); printf("%ld/%ld\n", numer,denom); printf("商 = %ld\n餘數 = %ld\n", ldiv_result.quot, ldiv_result.rem); } /* localeco.c */ #include main(){ struct lconv *Locale ; Locale = localeconv(); printf("Locale informations :\n"); printf("Decimal Point = %s\n", Locale->decimal_point ); printf("Thousands Separator = %s\n", Locale->thousands_sep ); printf("Grouping = %s\n", Locale->grouping ); printf("Positive sign position = %d\n", Locale->p_sign_posn ); printf("Negative sign position = %d\n", Locale->n_sign_posn ); } /* localtim.c */ #include #include main(){ time_t current_time; struct tm *current_tm; time(¤t_time); printf("1970年1月1日00:00:00至現在經過 %ld 秒 \n", current_time); current_tm=localtime(¤t_time); printf("Local Time = %s\n",asctime(current_tm)); } /* log.c */ #include #include #include main(){ double x, log_val; printf("計算某數的自然對數值\n"); printf("請輸入一數值: "); scanf("%lf", &x); log_val = log(x); if (errno != EDOM && errno != ERANGE) { printf("log(%g)=%g\n", x, log_val); } } /* log10.c */ #include #include #include main(){ double x, log10_val; printf("計算某數以10為底的對數值\n"); printf("請輸入一數值: "); scanf("%lf", &x); log10_val = log10(x); if (errno != EDOM && errno != ERANGE) { printf("log10(%g)=%g\n", x, log10_val); } } /* longjmp.c */ #include #include jmp_buf RetBuff; void Call_jmp(jmp_buf); main(){ int v1; printf("Set up CallFunction status by calling the "); printf("function \"setjmp\"..\n"); v1 = setjmp(RetBuff); printf("\n.......Value is %d...\n", v1); if (v1 !=0) { printf("CallFunction by call the function "); printf("\"longjmp\".\n"); exit(0); } printf("Now calling the function \"longjmp\"..\n"); Call_jmp(RetBuff); } /* Function "Call_jmp" will call "longjmp" */ void Call_jmp(jmp_buf RetBuff){ longjmp(RetBuff, 1); } /* malloc.c */ #include #include main(){ char *MemBlk; if ((MemBlk = (char *)malloc(256)) == NULL){ printf("Memory allocated failed!\a\n"); abort(); } printf("Memory allocated!\n"); printf("Please input a string: "); gets(MemBlk); printf("This string is \"%s\".\n", MemBlk); free(MemBlk); printf("Memory has been collected!\n"); } /* memchr.c */ #include main(){ char s[81] = {"Apple computer"}; memchr(s, 'c', 80); printf("The first character address is %#x\n", s); printf("The address of 'c' is %#x\n", memchr(s, 'c', 80)); } /* memcmp.c */ #include main(){ char s1[] = "Microsoft Visual C++ 2008", s2[] = "ANSI C/C++"; int RetValue; printf("First string is \"%s\".\n", s1); printf("Second string is \"%s\".\n", s2); RetValue = memcmp(s1, s2, 7); if (!RetValue) printf("First string = Second string.\n"); else if (RetValue < 0) printf("First string < Second string.\n"); else printf("First string > Second string.\n"); } /* memcpy.c */ #include main(){ char source[81] = "ANSI C Bible"; char dest[81] = "dummy"; printf("Before copy:\n"); printf(" source = \"%s\".\n", source); printf(" destination = \"%s\".\n", dest); memcpy(dest, source, 81); printf("After copy:\n"); printf(" source = \"%s\".\n", source); printf(" destination = \"%s\".\n", dest); } /* memmove.c */ #include main(){ char s[80] = "ANSI C Bible"; printf("Before move:\n"); printf(" s = \"%s\".\n", s); memmove(s + 5, s, sizeof(s)); printf("After move:\n"); printf(" s = \"%s\".\n", s); } /* memset.c */ #include main(){ char s[11] = "computer"; printf("Before set:\n"); printf(" s = \"%s\".\n", s); memset(s, 'C', 10); s[10] = '\0'; printf("After set:\n"); printf(" s = \"%s\".\n", s); } /* mktime.c */ #include #include char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Unknown"}; main(){ struct tm time_check; int year, month, day; printf("Year: "); scanf("%d", &year); printf("Month: "); scanf("%d", &month); printf("Day: "); scanf("%d", &day); time_check.tm_year = year - 1900; time_check.tm_mon = month - 1; time_check.tm_mday = day; time_check.tm_hour = 0; time_check.tm_min = 0; time_check.tm_sec = 1; time_check.tm_isdst = -1; if (mktime(&time_check) == -1) time_check.tm_wday = 7; printf("Today is %s\n", wday[time_check.tm_wday]); } /* modf.c */ #include #include main(){ double v, intpart, fract; printf("請輸入一浮點數: "); scanf("%lf", &v); fract = modf(v, &intpart); printf("%g 的整數部份為 %g, 小數部份為 %g\n" ,v, intpart, fract); } /* perror.c */ main(){ char filename[11]; FILE *fp; tmpnam(filename); printf("Attempting to open '%s'...\n", filename); if ((fp = fopen(filename, "r")) == NULL) perror("Error to open the specified file.\n"); else { printf("File open is successful.\n"); fclose(fp); } } /* pow.c */ #include #include main(){ double x, y, v; printf("請輸入兩數,計算 x^y\n"); printf("X: "); scanf("%lf", &x); printf("Y: "); scanf("%lf", &y); v = pow(x, y); printf("%g 的 %g 次方為 %g\n", x, y, v); } /* printf.c */ #include main(){ char *s = "This is a test."; int i = 100; float f = 3.1415926f; printf("s = %s\n", s); printf("Address of msg : %Fp\n", s); printf("The first character of msg is : %c\n", s[0]); printf("i = %3d (in decimal 十進)\n", i); printf("i = %3o (in octal 八進)\n", i); printf("i = %3x (in hexadecimal 十六進)\n", i); printf("f = %-11f (in the floating point format)\n", f); printf("f = %-11e (in the scientific format)\n", f); } /* putc.c */ main(){ char *s = "This is a test."; // char s[16]; int i; for (i=0; s[i] != '\0'; i++) putc(s[i], stdout); printf("\n"); } /* putchar.c */ main(){ char *s = "This is a test."; int i; for (i=0; s[i] != '\0'; i++) putchar(s[i]); } /* puts.c */ #include main(){ char *s = "This is a test."; puts(s); printf("Over\n"); } /* qsort.c 快速排序法 */ #include int a[]={999,15,17,143,122,144,578,200,395,466,333,11}; int compare(const int *elem1, const int *elem2){ return(*elem1 - *elem2); } main(){ int i; printf("Source sequence: 排序前\n"); for (i=0;i<=11;i++) printf("%d\n",a[i]); qsort((void *)a, 12, sizeof(a[0]), compare); printf("\nSorting...\n\n"); printf("Target sequence: 排序後\n"); for (i=0;i<=11;i++) printf("%d\n",a[i]); } /* raise.c */ #include main(){ printf("Raise the signal SIGINT..\n"); raise(SIGINT); } /* rand.c */ #include main(){ int i; printf("產生五個隨機rand亂數:\n"); for (i=0;i<5;i++) { printf("%d\n", rand()); } } /* realloc.c */ #include #include main(){ char *s; s = (char *)malloc(81); if (s == NULL) { printf("Allocated memory failed!\a\n"); abort(); } printf("Memory allocated.\n"); printf("Please input a string: "); gets(s); printf("This string is \"%s\".\n", s); /* Enlarge the memory. */ s = (char *)realloc(s, 256); if (s == NULL) { printf("Memory adjusted failed!\a\n"); abort(); } printf("Memory adjusted succeed.\n"); printf("The memory adjusted also contains \"%s\".\n", s); free(s); } /* remove.c */ #include main(){ char filename[80]; printf("Please enter a filename to delete: "); gets(filename); if (remove(filename) == 0) printf("File deleted Successfully: %s\n", filename); else{ switch (errno){ case ENOENT: printf("Cannot find file: %s\n", filename); break; case EACCES: printf("Permission denied.\n"); break; default : printf("Errno : %d\n", errno); } } } /* rename.c 改名 */ #include #include #include main(){ char oldname[80], newname[80]; printf("Please enter a filename to rename: "); gets(oldname); /* Checks for file existence */ if (access(oldname, 0) != 0) { printf("File: %s doesn't exist.\n", oldname); exit(0); } printf("Rename %s as: ", oldname); gets(newname); /* Checks for file existence */ if (access(newname, 0) == 0) { printf("File : %s already exists 存在.\n", newname); exit(0); } if (rename(oldname, newname) == 0) printf("Successfully renamed %s as %s\n", oldname, newname); else{ switch (errno) { case ENOENT : printf("No such file 檔案不存在.\n"); break; case EACCES : printf("Permission denied 授權被拒.\n"); break; default : printf("Error no: %d\n", errno); } } } /* rewind.c 回起點 */ #include main(){ FILE *fp; char s[31]; char *msg1 = "This is the message 1."; char *msg2 = "This is the message 2."; if ((fp = fopen("test", "w+")) == NULL) { printf("Cannot create 建立 file 'test'.\n"); exit(0); } printf("Write message 1 '%s' to file 'test'...\n", msg1); fprintf(fp , msg1); printf("Rewind the file pointer to the beginning of the file...\n"); rewind(fp); printf("Write message 2 '%s' to file 'test'...\n", msg2); fprintf(fp , msg2); rewind(fp); printf("The last content of file is:\n"); fgets(s, 31, fp); printf("%s\n",s); } /* scanf.c */ #include main(){ int n; char str[80], junk[80]; float fnumber; double dnumber; printf("Enter an integer: "); scanf("%d", &n); printf("The integer you entered is:\n"); printf(" %4d: in decimal \n", n); // 十進 printf(" %4x: in hexadecimal\n", n); // 十六進 printf(" %4o: in octal \n\n", n); // 八進 gets(junk); /* 吃掉不必要的輸入 */ printf("Enter a floating point number: "); scanf("%f", &fnumber); printf("The floating point number you entered is %f\n\n", fnumber); gets(junk); /* 吃掉不必要的輸入 */ printf("Enter a double precision floating point number: "); scanf("%lf", &dnumber); printf("The double precision floating point number you entered " "is %f\n\n", dnumber); gets(junk); /* 吃掉不必要的輸入 */ printf("Enter 5 characters: "); scanf("%5c", str); str[5] = '\0'; printf("The 5 characters you entered are : %s\n\n", str); gets(junk); /* 吃掉不必要的輸入 */ printf("Enter a string (Allowed characters: a to z).\n=>"); scanf("%[a-z]", str); gets(junk); /* 吃掉不必要的輸入 */ printf("The string you entered is: %s", str); printf("\n\nEnter a string (Except characters: a to z).\n=>"); scanf("%[^a-z]", str); gets(junk); /* 吃掉不必要的輸入 */ printf("The string you entered is: %s\n", str); } /* setbuf.c */ #include main(){ FILE *fp; char s[BUFSIZ]; printf("Buffer size is %d\n\n", BUFSIZ); if ((fp = fopen("setbuf.c", "r")) == NULL) { printf("Cannot open file 'test.dat'.\n"); exit(0); } setbuf(fp, s); printf("Reading a character from file 'test.dat'...\n"); getc(fp); s[BUFSIZ-1] = '\0'; printf("The content of the buffer: \n"); printf("%s\n", s); fclose(fp); } /* setjmp.c */ #include #include jmp_buf RetBuff; void Call_jmp(jmp_buf ); main(){ int v; printf("Set up CallFunction status by calling the "); printf("function \"setjmp\"..\n"); v = setjmp(RetBuff); printf("\n.......Value is %d...\n", v); if (v !=0) { printf("CallFunction by call the function "); printf("\"longjmp\".\n"); exit(0); } printf("Now calling the function \"longjmp\"..\n"); Call_jmp(RetBuff); } /* Function "Call_jmp" will call "longjmp" */ void Call_jmp(jmp_buf RetBuff){ longjmp(RetBuff, 1); } /* setlocal.c */ #include main(){ printf("Old locale was %s\n", setlocale(LC_MONETARY, "C")); } /* setvbuf.c */ #include main(){ FILE *fp; char s[400]; if ((fp = fopen("setvbuf.c", "r")) == NULL) { printf("Cannot open file 'setvbuf.c'.\n"); exit(0); } if (setvbuf(fp, s, _IOFBF, 399) != 0) { printf("Error to set buffer.\n"); fclose(fp); exit(0); }; printf("Reading a character from file 'setvbuf.c'...\n"); getc(fp); printf("The content of the buffer : \n"); s[399] = '\0'; puts(s); fclose(fp); } /* signal.c */ #include #include void SIGFPE_Handler(int); main(){ int a, b; if (signal(SIGFPE, SIGFPE_Handler) == SIG_ERR) { perror("Set up signal handler error!"); exit(1); } printf("If you divided some integer by 0, it can "); printf("start \nthe signal handler..\n"); printf("Now we can input two integers for division.\n"); scanf("%d %d", &a, &b); printf("%d / %d = %d\n", a, b, a/b); } /* The handler for floating-point error. */ void SIGFPE_Handler(int signum){ printf("Signal number: %d\n", signum); perror("Division error: \a\a\a"); abort(); } /* sin.c */ #include #include #define RadianToDegree 57.29578 main(){ double v1,sin_val; for (v1 = 0; v1<=360; v1+=30){ sin_val = sin(v1/RadianToDegree); printf("Sine (%3g) = %8.2f\n", v1, sin_val); } } /* sinh.c */ #include #include #define RadianToDegree 57.29578 main(){ double v1, sinh_val; for (v1 = -1; v1 <= 1; v1 += 0.2){ sinh_val = sinh(v1/RadianToDegree); printf("Hyperbolic sine(%4.1f) = %8.4f\n", v1, sinh_val); } } /* sprintf.c */ #include main(){ char s[80]; sprintf(s, "%d 以十六進位表示為 %#x\n", 100, 100); // 0x64 puts(s); sprintf(s, "%d 以 八進位表示為 %#o\n", 100, 100); // 0144 puts(s); } /* sqrt.c 平方根(square root) */ #include #include main(){ double x, sqrt_val; printf("計算某一數值的平方根: \n"); printf("請輸入一數值: "); scanf("%lf", &x); sqrt_val = sqrt(x); printf("SQRT(%g) = %g\n", x, sqrt_val); } /* sqrtl.c */ #include #include main(){ char *s; long double x,v; printf("Enter the number that you want to evaluate its square root\n"); printf("Number:"); gets(s); x=_atold(s); v=sqrtl(x); printf("SQRT(%Lg) = %Lg\n",x,v); } /* srand.c */ #include #include main(){ int i; time_t t; srand((unsigned) time(&t)); // srand(time(NULL)); printf("產生十個 0 到 32767 隨機亂數:\n"); for (i=0; i<10; i++) { printf("%d\n", rand()); } } /* sscanf.c */ #include main(){ char *raw_data = "Mary 90 88 87 80"; char name[10]; int score[4]; printf("Raw data : %s\n", raw_data); printf("Use sscanf function to scan each of the data item...\n"); sscanf(raw_data, "%s %d %d %d %d", name, &score[0], &score[1], &score[2], &score[3]); printf("Name : %s\n", name); printf("Chinese : %d\n", score[0]); printf("English : %d\n", score[1]); printf("Maths : %d\n", score[2]); printf("Computer : %d\n", score[3]); } /* strcat.c 文字合併 */ #include #include main(){ char s1[20], s2[80]; printf("Enter two strings which you want to concatenate 合併\n"); printf("First string: "); gets(s1); printf("Second string: "); gets(s2); strcat(s1, s2); printf("Concatenated string: %s\n", s1); } /* strchr.c 字串中找尋字元 */ #include #include main(){ char s[20], *ptr; int c; printf("請輸入一字串: "); gets(s); printf("要找尋的字元: "); scanf("%c",&c); printf("陣列第一個元素的位址為 %x \n", s); ptr=strchr(s,c); if(ptr!=NULL) printf("第一次出現字元 %c 是在 %x\n", c, ptr); else printf("找不到此字元\n", c); } /* strcmp.c 文字比較 */ #include #include main(){ char s1[20], s2[20]; int n; printf("Enter two strings which you want to compare with \n"); printf("First string: "); gets(s1); printf("Second string: "); gets(s2); n=strcmp(s1,s2); if (n>0) printf("%s > %s", s1, s2); else if (n<0) printf("%s < %s", s1, s2); else printf("%s = %s", s1, s2); printf("\n"); } /* strcoll.c */ #include #include main(){ char s1[20],s2[20]; int n; printf("Enter two strings which you want to compare with\n"); printf("First string: "); gets(s1); printf("Second string: "); gets(s2); n=strcoll(s1,s2); if (n>0) printf("%s > %s", s1, s2); else if (n<0) printf("%s < %s", s1, s2); else printf("%s = %s", s1, s2); printf("\n"); } /* strcpy.c 文字複製 */ #include #include main(){ char s1[80], s2[80]; printf("Enter source string: "); gets(s2); strcpy(s1, s2); printf("Source string is copied to destination string\n"); printf("Destination string is: %s\n", s1); } /* strcspn.c */ #include #include main(){ char s1[80], s2[80]; size_t n; printf("Enter one string: "); gets(s1); printf("Enter other string which you want to search: "); gets(s2); n = strcspn(s1, s2); printf("Character strings differ at position: %d\n", n); } /* strerror.c */ #include #include main(){ int i; for (i=0; i<=10; i++) printf("String error #%2d = %s\n", i, strerror(i)); } /* strftime.c */ #include #include main(){ struct tm *currenttime; time_t bintime; char str[80]; time(&bintime); currenttime=localtime(&bintime); strftime(str,80,"Current time: %I:%M:%S %p ", currenttime); printf("%s\n",str); } /* strlen.c */ #include #include main(){ char s[20]; printf("請輸入一字串: "); gets(s); printf("字串 %s 的長度為 %d\n", s, strlen(s)); } /* strncat.c 文字合併 */ #include #include main(){ char source[20],dest[20]; size_t maxlen; printf("Enter two strings which you want to concatenate\n"); printf("First string: "); gets(dest); printf("Second string: "); gets(source); printf("How many char of second string want to concatenate: "); scanf("%d",&maxlen); strncat(dest, source, maxlen); printf("Concatenated string: %s", dest); printf("\n"); } /* strncmp.c 比較 */ #include #include main(){ char s1[20],s2[20]; size_t maxlen; int n; printf("輸入要比較的字串\n"); printf("字串1: "); gets(s1); printf("字串2: "); gets(s2); printf("要比較多少個字元: "); scanf("%d", &maxlen); n=strncmp(s1, s2, maxlen); if (n>0) printf("%s > %s", s1, s2); else if (n<0) printf("%s < %s", s1, s2); else printf("%s = %s", s1, s2); printf("\n"); } /* strncpy.c 複製 */ #include #include main(){ char dest[20], source[20]; size_t maxlen; printf("Enter source string: "); gets(source); printf("How many characters do you want to copy: "); scanf("%d", &maxlen); strncpy(dest, source, maxlen); dest[maxlen]='\0'; printf("Destination string is: %s\n", dest); } /* strnset.c */ #include main(){ char s[20]; int ch; size_t n; printf("Enter one string: "); gets(s); printf("Enter character you want the string set to: "); scanf("%c",&ch); printf("How many character you want to set to: "); scanf("%d",&n); strnset(s,ch,n); printf("Converted string is: %s\n",s); } /* strpbrk.c */ #include #include main(){ char s1[20], s2[20], *position; printf("Enter one string: "); gets(s1); printf("Enter one string which you want to search: "); gets(s2); position = strpbrk(s1, s2); printf("The address of s1 is %x\n", s1); printf("%s 中的字元出現在: %x\n", s2, position); } /* strrchr.c */ #include #include main(){ char string[20], *ptr; int c; printf("請輸入一字串: "); gets(string); printf("輸入要找尋的字元: "); scanf("%c",&c); printf("陣列第一個元素的位址為 %x \n", string); ptr=strrchr(string, c); if (ptr) printf("最後出現字元 %c 是在 %x\n", c, ptr); else printf("找不到此字元\n", c); } /* strset.c */ #include main(){ char string[20]; int ch; printf("Enter one string: "); gets(string); printf("Enter character you want the string set to: "); scanf("%c",&ch); strset(string,ch); printf("Converted string is: %s\n",string); } /* strspn.c */ #include #include main(){ char s1[20], s2[20]; size_t length; printf("Enter one string: "); gets(s1); printf("Enter other string which you want to search: "); gets(s2); length = strspn(s1, s2); printf("Character where strings different is at position: %d\n", length); } /* strstr.c */ #include #include main(){ char s1[20], s2[20], *ptr; printf("請輸入一字串: "); gets(s1); printf("請輸入要找尋的字串: "); gets(s2); printf("%s的位址是%x\n", s1, s1); ptr = strstr(s1, s2); if (ptr) printf("%s出現於: %x\n", s2, ptr); else printf("%s在%s中找不到\n",s2, s1); } /* strtod.c */ #include main(){ char s[20], *stop_at; double v; printf("請輸入一浮點數字串: "); gets(s); v=strtod(s, &stop_at); printf("轉換後的浮點數為 %g\n", v); printf("轉換後停在: %s\n", stop_at); } /* strtok.c */ #include #include main(){ char s1[20], *s2=" ", *ptr; printf("請輸入一字串: "); gets(s1); ptr=strtok(s1, s2); while (ptr!=NULL) { printf("子字串為: %s\n", ptr); ptr=strtok(NULL,s2); } } /* strtol.c */ #include main(){ char s[20], *stop_at; long v; int radix; printf("請輸入一長整數字串: "); gets(s); printf("請入基底數(base): "); scanf("%d", &radix); v = strtol(s, &stop_at, radix); printf("以%2d為基底的數為 %ld\n", radix, v); printf("轉換後停在: %s\n", stop_at); } /* strtoul.c */ #include #include #include main(){ char s[20], *stop_at; unsigned long v; int radix; printf("請輸入一長整數字串: "); gets(s); printf("請入基底數: "); scanf("%d", &radix); v = strtol(s, &stop_at, radix); printf("以%2d為基底的數為 %lu\n", radix, v); printf("轉換後停在: %s\n", stop_at); } /* strxfrm.c */ #include main(){ char dest[20], source[20]; size_t maxchr, length; printf("Enter source string: "); gets(source); printf("How many characters do you want to copy: "); scanf("%d", &maxchr); length=strxfrm(dest, source, maxchr); dest[maxchr] = '\0'; printf("Destination string is: %s\n", dest); } /* system.c (DOS command) */ #include main(int argc, char *argv[]){ if (argc != 2) { printf("Usage: SYSTEM command\n"); abort(); } if (system(argv[1]) == -1) printf("Command error!\a\n"); } /* tan.c */ #include #include #define RadianToDegree 57.29578 main(){ double v1, tan_val; for (v1=0; v1<=360; v1+=15){ tan_val = tan(v1/RadianToDegree); printf("Tangent (%4.1f) = %5.2f\n", v1, tan_val); } } /* tanh.c */ #include #include #define RadianToDegree 57.29578 main(){ double v1, tanh_val; for (v1=-1; v1<=1; v1 +=.1){ tanh_val = tanh(v1/RadianToDegree); printf("Hyperbolic tangent (%4.1f) = %7.3f\n", v1, tanh_val); } } /* time.c */ #include #include main(){ time_t current_time; current_time=time(NULL); printf("%d seconds since January 1, 1970\n", current_time); } /* tmpfile.c 暫存檔 */ #include main(){ FILE *tmp; if ((tmp = tmpfile()) == NULL) { printf("Cannot create temporary file.\n"); exit(0); } else printf("Temporary file is successfully opened.\n"); printf("Write a message :\n'You cannot see this message at any files.'" "\nto temporary file...\n"); fprintf(tmp, "You cannot see this message at any files."); printf("Close temoprary file...\n"); fclose(tmp); } /* tmpnam.c 產生暫存檔名稱 */ #include main(){ char tmpname[3][L_tmpnam]; tmpnam(tmpname[0]); tmpnam(tmpname[1]); tmpnam(tmpname[2]); printf("Temporary name 0: %s\n", tmpname[0]); printf("Temporary name 1: %s\n", tmpname[1]); printf("Temporary name 2: %s\n", tmpname[2]); printf("Temporary name in internal static object : %s\n", tmpnam(NULL)); } /* tolower.c */ #include #include main(){ char s[20]; int i; printf("Enter a string: "); gets(s); for (i=0; (s[i]!=' ') && (s[i]!='\0'); i++) s[i] = tolower(s[i]); printf("Converted string is: %s\n", s); } /* toupper.c */ #include #include main(){ char s[20]; int i; printf("Enter a string: "); gets(s); for (i=0; (s[i]!=' ') && (s[i]!='\0'); i++) s[i] = toupper(s[i]); printf("Converted string is: %s\n", s); } /* ungetc.c */ #include main(){ int ch; printf("Please enter a character: "); ch = getchar(); printf("put back the character to the buffer of stdin...\n"); ungetc(ch, stdin); printf("The character got from the buffer is: %c\n", getchar()); } /* va_arg.c 可改變參數數目 */ #include #include /* Function Prototyping */ int Add(int, ...); main(){ int total=0; total=Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -999); printf( "1+2+3+,...+10= %d\n", total); } /* The function "Add" will add the all arguments up. */ int Add(int i, ...){ int sum = 0; va_list argp; va_start(argp, i); // Initialize the pointer - argp while (i != -999) { sum += i; i = va_arg(argp, int); } va_end(argp); return(sum); } /* vfprintf.c */ #include #include int myfprintf(FILE *fp, char *format, ...){ va_list arglist; int numbers; va_start(arglist, format); numbers = vfprintf(fp, format, arglist); va_end(arglist); return numbers; } main(){ int i = 200; char ch = 'A'; float f = 3.1415f; char *str = "This is a test."; printf("write '%d', '%c', '%f', and '%s' to stdout...\n", i, ch, f, str); myfprintf(stdout, "%d, %c, %f, %s", i, ch, f, str); printf("\n"); } /* vprintf.c */ #include #include int myvprintf(char *format, ...){ va_list arglist; int numbers; va_start(arglist, format); numbers = vprintf(format, arglist); va_end(arglist); return numbers; } main(){ int i = 200; char ch = 'A'; float f = 3.1415f; char *str = "This is a test."; printf("write '%d', '%c', '%f', and '%s' to stdout...\n", i, ch, f, str); myvprintf("%d, %c, %f, %s", i, ch, f, str); printf("\n"); } /* vsprintf.c */ #include #include int myvsprintf(char *format, ...){ va_list arglist; int numbers; char s[100]; va_start(arglist, format); numbers = vsprintf(s, format, arglist); va_end(arglist); puts(s); return numbers; } main(){ myvsprintf("The number %d in hexadecimal is %#x.\n", 100, 100); }