哪些你容易忽略的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\n",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\n",sizeof(a));

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

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

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

    return 0;

}

输出:

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
更多的基础知识介绍请点击这里
posted @ 2017-08-09 10:36  小小心脏  阅读(233)  评论(0编辑  收藏  举报