📅  最后修改于: 2023-12-03 14:40:27.030000             🧑  作者: Mango
本篇文章介绍23个C程序输出相关的问题,包括基本的数据类型,控制流语句,以及函数等。希望能为您提供帮助。
#include <stdio.h>
int main() {
int n = 123;
printf("%d\n", n);
return 0;
}
输出:123
#include <stdio.h>
int main() {
float f = 123.456;
printf("%f\n", f);
return 0;
}
输出:123.456001
(由于浮点数在计算机中是近似存储的,因此输出的值可能略有误差)
#include <stdio.h>
int main() {
char c = 'A';
printf("%c\n", c);
return 0;
}
输出:A
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
printf("%s\n", str);
return 0;
}
输出:Hello, world!
#include <stdio.h>
#include <stdbool.h>
int main() {
bool b = true;
printf("%d\n", b);
return 0;
}
输出:1
#include <stdio.h>
int main() {
printf("%%\n");
return 0;
}
输出:%
#include <stdio.h>
int main() {
int n = 123;
printf("%p\n", &n);
return 0;
}
输出:0x7ffee5a5a5ac
(地址每次运行可能不同)
#include <stdio.h>
int main() {
float f = 123.456;
printf("%10.2f\n", f);
return 0;
}
输出: 123.46
#include <stdio.h>
int main() {
printf("Hello, \\\"world\\\"!\n");
return 0;
}
输出:Hello, "world"!
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3;
printf("%d, %d, %d\n", a, b, c);
return 0;
}
输出:1, 2, 3
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
输出:0 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int main() {
int n = 123456;
if (n > 1000) {
printf("%d\n", n);
}
return 0;
}
输出:123456
#include <stdio.h>
int main() {
int n = 2;
switch (n) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid\n");
}
return 0;
}
输出:Tuesday
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
输出:0 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 10);
printf("\n");
return 0;
}
输出:0 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int main() {
int a[] = {1, 2, 3};
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
输出:1 2 3
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(1, 2);
printf("%d\n", sum);
return 0;
}
输出:3
#include <stdio.h>
void print_hello(char *name) {
printf("Hello, %s!\n", name);
}
int main() {
char *name = "World";
print_hello(name);
return 0;
}
输出:Hello, World!
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n = 5;
int result = factorial(n);
printf("%d! = %d\n", n, result);
return 0;
}
输出:5! = 120
#include <stdio.h>
int main() {
int n = 123;
int *ptr = &n;
printf("%p\n", ptr);
return 0;
}
输出:0x7ffee5a5a5ac
(地址每次运行可能不同)
#include <stdio.h>
void print_binary(unsigned char c) {
for (int i = 7; i >= 0; i--) {
if (c & (1 << i)) {
printf("1");
} else {
printf("0");
}
}
}
int main() {
unsigned char c = 0b10101010;
print_binary(c);
printf("\n");
return 0;
}
输出:10101010
#include <stdio.h>
int main() {
int n = 0xabcd;
printf("%x\n", n);
return 0;
}
输出:abcd