📜  C 程序的输出 | 29套(1)

📅  最后修改于: 2023-12-03 14:39:41.783000             🧑  作者: Mango

C 程序的输出

如果你是一名C程序员,那么你一定需要掌握一些关于C程序输出的知识。本文将介绍29个关于C程序输出的示例。让我们开始吧!

示例1

输出一个字符串:

#include <stdio.h>

int main() {
    printf("Hello World!");
    return 0;
}

输出:Hello World!

示例2

输出一个变量:

#include <stdio.h>

int main() {
    int a = 10;
    printf("%d", a);
    return 0;
}

输出:10

示例3

输出多个变量:

#include <stdio.h>

int main() {
    int a = 10;
    float b = 3.14;
    char c = 'A';
    printf("%d %f %c", a, b, c);
    return 0;
}

输出:10 3.140000 A

示例4

输出一个整数和一个浮点数,不同的输出方式:

#include <stdio.h>

int main() {
    int a = 10;
    float b = 3.14;
    printf("%d\n", a);
    printf("%.2f\n", b);
    return 0;
}

输出:

10
3.14
示例5

使用转义序列输出特殊字符:

#include <stdio.h>

int main() {
    printf("This is a \"double quoted\" string\n");
    printf("This is a 'single quoted' character\n");
    printf("This is a \\ backslash\n");
    printf("This is a \n new line\n");
    printf("This is a \t tab character\n");
    return 0;
}

输出:

This is a "double quoted" string
This is a 'single quoted' character
This is a \ backslash
This is a 
 new line
This is a     tab character
示例6

使用printf来输出指针:

#include <stdio.h>

int main() {
    int a = 10;
    int *ptr = &a;
    printf("Address of a = %p\n", ptr);
    printf("Value of a = %d\n", *ptr);
    return 0;
}

输出:

Address of a = 0x7ffee08fba48
Value of a = 10
示例7

输出一个十六进制数:

#include <stdio.h>

int main() {
    int x = 255;
    printf("%x\n", x);
    return 0;
}

输出:ff

示例8

输出一个八进制数:

#include <stdio.h>

int main() {
    int x = 8;
    printf("%o", x);
    return 0;
}

输出:10

示例9

使用printf输出一个字符数组:

#include <stdio.h>

int main() {
    char str[] = "Hello World!";
    printf("%s", str);
    return 0;
}

输出:Hello World!

示例10

使用scanf读取用户输入的整数:

#include <stdio.h>

int main() {
    int a;
    printf("Enter an integer: ");
    scanf("%d", &a);
    printf("You entered: %d", a);
    return 0;
}

输出:

Enter an integer: 10
You entered: 10
示例11

使用scanf读取用户输入的浮点数:

#include <stdio.h>

int main() {
    float a;
    printf("Enter a floating-point number: ");
    scanf("%f", &a);
    printf("You entered: %f", a);
    return 0;
}

输出:

Enter a floating-point number: 3.14
You entered: 3.140000
示例12

使用scanf读取字符串:

#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);
    printf("You entered: %s", str);
    return 0;
}

输出:

Enter a string: Hello World!
You entered: Hello
示例13

使用fgets读取字符串:

#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    printf("You entered: %s", str);
    return 0;
}

输出:

Enter a string: Hello World!
You entered: Hello World!
示例14

使用putchar输出一个字符:

#include <stdio.h>

int main() {
    putchar('A');
    return 0;
}

输出:A

示例15

使用puts输出一个字符串:

#include <stdio.h>

int main() {
    puts("Hello World!");
    return 0;
}

输出:

Hello World!
示例16

使用getchar读取一个字符:

#include <stdio.h>

int main() {
    int ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: %c", ch);
    return 0;
}

输出:

Enter a character: A
You entered: A
示例17

让用户输入多个整数,并计算它们的和:

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("Enter number of integers: ");
    scanf("%d", &n);
    for (i = 1; i <= n; ++i) {
        int x;
        printf("Enter integer %d: ", i);
        scanf("%d", &x);
        sum += x;
    }
    printf("Sum of %d integers is %d", n, sum);
    return 0;
}

输出:

Enter number of integers: 3
Enter integer 1: 10
Enter integer 2: 20
Enter integer 3: 30
Sum of 3 integers is 60
示例18

计算两个整数的最大公约数:

#include <stdio.h>

int main() {
    int a, b, i, gcd;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    for (i = 1; i <= a && i <= b; ++i) {
        if (a % i == 0 && b % i == 0) {
            gcd = i;
        }
    }
    printf("GCD of %d and %d is %d", a, b, gcd);
    return 0;
}

输出:

Enter two integers: 12 16
GCD of 12 and 16 is 4
示例19

计算一个整数的阶乘:

#include <stdio.h>

int main() {
    int n, i, fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);
    for (i = 1; i <= n; ++i) {
        fact *= i;
    }
    printf("Factorial of %d is %d", n, fact);
    return 0;
}

输出:

Enter an integer: 5
Factorial of 5 is 120
示例20

计算一个整数的平方和:

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("Enter an integer: ");
    scanf("%d", &n);
    for (i = 1; i <= n; ++i) {
        sum += i * i;
    }
    printf("Sum of squares of first %d integers is %d", n, sum);
    return 0;
}

输出:

Enter an integer: 5
Sum of squares of first 5 integers is 55
示例21

判断一个数是奇数还是偶数:

#include <stdio.h>

int main() {
    int n;
    printf("Enter an integer: ");
    scanf("%d", &n);
    if (n % 2 == 0) {
        printf("%d is even", n);
    } else {
        printf("%d is odd", n);
    }
    return 0;
}

输出:

Enter an integer: 10
10 is even
示例22

判断一个数是否为质数:

#include <stdio.h>

int main() {
    int n, i, flag = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    for (i = 2; i <= n / 2; ++i) {
        if (n % i == 0) {
            flag = 1;
            break;
        }
    }
    if (n == 1) {
        printf("1 is neither prime nor composite.");
    } else {
        if (flag == 0) {
            printf("%d is a prime number.", n);
        } else {
            printf("%d is not a prime number.", n);
        }
    }
    return 0;
}

输出:

Enter a positive integer: 7
7 is a prime number.
示例23

反转一个字符串:

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = 0;
    int i, n = strlen(str);
    for (i = 0; i < n / 2; ++i) {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
    printf("Reverse of the string is: %s", str);
    return 0;
}

输出:

Enter a string: Hello World!
Reverse of the string is: !dlroW olleH
示例24

把一个字符串转换为大写:

#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    int i, n = strlen(str) - 1;
    for (i = 0; i < n; ++i) {
        putchar(toupper(str[i]));
    }
    return 0;
}

输出:

Enter a string: Hello World!
HELLO WORLD!
示例25

把一个字符串转换为小写:

#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    int i, n = strlen(str) - 1;
    for (i = 0; i < n; ++i) {
        putchar(tolower(str[i]));
    }
    return 0;
}

输出:

Enter a string: Hello World!
hello world!
示例26

把一个字符串转换为数字:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[100];
    printf("Enter a number: ");
    fgets(str, sizeof(str), stdin);
    int num = atoi(str);
    printf("The number is %d", num);
    return 0;
}

输出:

Enter a number: 123
The number is 123
示例27

使用sprintf把一个数字转换为字符数组:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = 123;
    char str[100];
    sprintf(str, "%d", num);
    printf("The string is %s", str);
    return 0;
}

输出:

The string is 123
示例28

使用qsort对一个整数数组进行排序:

#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void * a, const void * b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr[] = {50, 20, 10, 40, 30};
    int n = sizeof(arr) / sizeof(arr[0]);
    qsort(arr, n, sizeof(int), cmpfunc);
    int i;
    for (i = 0; i < n; ++i) {
        printf("%d ", arr[i]);
    }
    return 0;
}

输出:

10 20 30 40 50
示例29

使用fgets读取一个文本文件:

#include <stdio.h>

int main() {
    FILE *fp;
    char str[100];
    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        printf("Unable to open file.");
        return 1;
    }
    while (fgets(str, 100, fp) != NULL) {
        printf("%s", str);
    }
    fclose(fp);
    return 0;
}

输出:

Hello World!
This is a test file.