zoukankan      html  css  js  c++  java
  • 哪些你容易忽略的C语言基础知识

    1、C语言打印一条语句

    源代码:

    /* C Program to print a sentence. */

    #include <stdio.h>

    int main()

    {

       printf("C Programming"); /* printf() prints the content inside quotation */

       return 0;

    }

     输出:

    C Programming			

    2、C语言打印用户输入的一个整数

    #include <stdio.h>

    int main()

    {

        int num;

        printf("Enter a integer: "); 

        scanf("%d",&num);  /* Storing a integer entered by user in variable num */

        printf("You entered: %d",num);

        return 0;

    }

    输出:

    Enter a integer: 25
    You entered: 25

    3、C语言实现两个整数相加

    /*C programming source code to add and display the sum of two integers entered by user */

    #include <stdio.h>

    int main( )

    {

        int num1, num2, sum;

        printf("Enter two integers: ");

        scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */

        sum=num1+num2;      /* Performs addition and stores it in variable sum */

        printf("Sum: %d",sum);  /* Displays sum */

        return 0;

    }

    输出:

    Enter two integers: 12
    11
    Sum: 23

    4、C语言实现两个小数相乘

    /*C program to multiply and display the product of two floating point numbers entered by user. */

    #include <stdio.h>

    int main( )

    {

        float num1, num2, product;

        printf("Enter two numbers: ");

        scanf("%f %f",&num1,&num2);        /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */

        product = num1*num2;  /* Performs multiplication and stores it */

        printf("Product: %f",product);

        return 0;

    }

    输出:

    Enter two numbers: 2.4
    1.1
    Product: 2.640000

    5、C语言查找字符的ASCII值

    /* Source code to find ASCII value of a character entered by user */

    #include <stdio.h>

    int main(){

        char c;

        printf("Enter a character: ");

        scanf("%c",&c);        /* Takes a character from user */

        printf("ASCII value of %c = %d",c,c);

        return 0;

    }

    输出:

    Enter a character: G
    ASCII value of G = 71

    6、C语言根据用户输入的整数做商和余数

    /* C Program to compute remainder and quotient  */

    #include <stdio.h>

    int main(){

        int dividend, divisor, quotient, remainder;

        printf("Enter dividend: ");

        scanf("%d",&dividend);

        printf("Enter divisor: ");

        scanf("%d",&divisor);

        quotient=dividend/divisor;           /*  Computes quotient */

        remainder=dividend%divisor;          /* Computes remainder */

        printf("Quotient = %d ",quotient);

        printf("Remainder = %d",remainder);

        return 0;

    }

    输出:

    Enter dividend: 25
    Enter divisor: 4
    Quotient = 6
    Remainder = 1

    7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度

    基本语法:

    /* This program computes the size of variable using sizeof operator.*/

    #include <stdio.h>

    int main(){

        int a;

        float b;

        double c;

        char d;

        printf("Size of int: %d bytes ",sizeof(a));

        printf("Size of float: %d bytes ",sizeof(b));

        printf("Size of double: %d bytes ",sizeof(c));

        printf("Size of char: %d byte ",sizeof(d));

        return 0;

    }

    输出:

    Size of int: 4 bytes
    Size of float: 4 bytes
    Size of double: 8 bytes
    Size of char: 1 byte
    更多的基础知识介绍请点击这里
  • 相关阅读:
    OpenCV 在android studio 中的用法
    Python中用requests处理cookies的3种方法
    Jmeter利用正则表达式提取器提取登录cookie供下一步使用
    vue项目 el-tree的界面自定义 实现增删改查操作
    PostgreSQL高可用方案-patroni+etcd+vipmanager(二)
    PostgreSQL高可用方案-patroni+etcd+vipmanager(一)
    【转载】Linux下PostgreSQL主备环境搭建和切换
    一些自定义 PostgreSQL 随机数据生成器 —— Some self-defined PostgreSQL random data generators
    ClickHouse 简单使用(六)
    ClickHouse 简单使用(五)
  • 原文地址:https://www.cnblogs.com/gzgb/p/7323907.html
Copyright © 2011-2022 走看看