2025-08-18 20:01:21
This commit is contained in:
@@ -2,4 +2,54 @@
|
||||
title = 'C'
|
||||
date = 2024-09-20T04:17:50Z
|
||||
draft = false
|
||||
+++
|
||||
+++
|
||||
|
||||
<!-- 笔试题有的时候不给正常输入,只给plain text -->
|
||||
## Format Specifier
|
||||
|
||||
Common specifiers:
|
||||
```
|
||||
%d int
|
||||
%u unsigned int
|
||||
%c char
|
||||
%s string (char s[])
|
||||
%p void*
|
||||
%ld long
|
||||
%lld long long (64bits)
|
||||
%f float/double
|
||||
%lf float/double(usually used in scanf)
|
||||
```
|
||||
We can extend the format specifiers:
|
||||
``` C
|
||||
printf("%5d", 33); // width " 33"
|
||||
printf("%.2f", 3.14159); // precision "3.14"
|
||||
printf("%8.2f", 3.14159);// " 3.14"
|
||||
printf("%-5d", 33); //Alignment "33 "
|
||||
```
|
||||
|
||||
## IO
|
||||
Read or print via terminal:
|
||||
|
||||
``` C
|
||||
int x;
|
||||
scanf("%d", &x);
|
||||
printf("%d", x);
|
||||
```
|
||||
|
||||
|
||||
## Type Conversion
|
||||
String to int, float....
|
||||
``` c
|
||||
int i = atoi("123");
|
||||
double d = atof("3.14");
|
||||
|
||||
long l = strtol("123", NULL, 10); // safer, auto detec errors
|
||||
double d2 = strtod("3.14", NULL);
|
||||
```
|
||||
|
||||
Numbers to string:
|
||||
``` c
|
||||
int i = 123;
|
||||
char buf[20];
|
||||
sprintf(buf, "%d", i);
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user