Stdio

返回

#include <stdio.h> int main(void) { char st[80] = "B456"; int ch, dt; sscanf(st, "%c%d", &ch, &dt); printf("ch=%c dt=%d\n", ch, dt); return 0; } /* char st[80] = 'B456'; error C2075: 'st' : 配列の初期化には中カッコ ({}) が必要です。 运行结果 ch=B dt=456 */ #include <stdio.h> int main(void) { char st[80]; double n; sprintf(st, "%c%c%d", 33,66,123); puts(st); return 0; } /* 运行结果 !B123 */ #include <stdio.h> int main(void) { int idt; long ldt; float fdt; double ddt; char sdt[80]; scanf("%d", &idt); printf("Your input int number is %d\n",idt); scanf("%ld", &ldt); printf("Your input long number is %ld\n",ldt); scanf("%f", &fdt); printf("Your input float number is %f\n",fdt); scanf("%lf", &ddt); printf("Your input double number is %lf\n",ddt); scanf("%s", sdt); printf("Your input char is %s\n",sdt); return 0; } /* 运行结果 3 Your input int number is 3 99 Your input long number is 99 3.14 Your input float number is 3.140000 3.14 Your input double number is 3.140000 This_is_the_test. Your input char is This_is_the_test. */ // 拥有混合数据型数据的函数 #include <stdio.h> #include <stdarg.h> void unidisp(char *fmt, ...); int main(void) { unidisp("iiii", 101, 202, 303,404); unidisp("idsids", 99, 22.22, "aaaa", 33, 44.44, "bbbb"); return 0; } void unidisp(char *fmt, ...) { va_list ap; va_start(ap,fmt); while(*fmt) { if (*fmt == 'i') printf("%d\n", va_arg(ap,int)); if (*fmt == 'd') printf("%f\n", va_arg(ap,double)); if (*fmt == 's') printf("%s\n", va_arg(ap,char *)); ++fmt; } va_end(ap); } /* 运行结果 C:\VC6prg\c2pro>list17_7 101 202 303 404 99 22.220000 aaaa 33 44.440000 bbbb */

如何取得日期和时间

#include <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("It's %s", ctime(&now)); return 0; } /* C:\>test080811 It's Mon Aug 11 16:30:01 2008 */

shift

#include <stdio.h> int main(void) { short int a; unsigned short int u; int dt; a = 0xAAAA; printf("First value is %04hX\n", a); a = a << 1; printf("Left shift is %04hX\n", a); a = 0xAAAA; a = a >> 1; printf("Sign right shift is %04hX\n", a); u = 0xAAAA; u = u >> 1; printf("No sign right shift is %04hX\n", u); dt = 88888888; printf("First value is %d\n", dt); dt = dt << 2; printf("Left 2 shift is %d\n", dt); dt = dt >> 1; printf("Right 1 shift is %d\n", dt); dt = -88888888; printf("First value is %d\n", dt); dt = dt << 2; printf("Left 2 shift is %d\n", dt); dt = dt >> 1; printf("Right 1 shift is %d\n", dt); return 0; } /* First value is AAAA Left shift is 5554 Sign right shift is D555 No sign right shift is 5555 First value is 88888888 Left 2 shift is 355555552 Right 1 shift is 177777776 First value is -88888888 Left 2 shift is -355555552 Right 1 shift is -177777776 */
返回