C 程序:展示基本算术运算和类型转换作用
原文:https://www.studytonight.com/c/programs/important-concepts/basic-arithmetic-operations
这里我们有两个简单的程序来展示各种基本的算术运算,以及在我们的程序中显式使用和不使用类型转换的情况下,C 语言是如何处理类型转换的。
没有类型转换的算术运算
下面是一个不用类型转换就能完成基本算术运算的程序。
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, add, subtract, multiply;
float divide;
printf("Enter two integers: \n");
scanf("%d%d", &a, &b);
add = a+b;
subtract = a-b;
multiply = a*b;
divide = a/b;
printf("\nAddition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
输出:
具有类型转换的算术运算
C 语言确实隐式处理类型转换,但是用户也可以在他们的程序中处理它。
下面是一个使用类型转换执行基本算术运算的简单程序。
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, add, subtract, multiply, remainder;
float divide;
printf("Enter two integers: \n");
scanf("%d%d", &a, &b);
add = a+b;
subtract = a-b;
multiply = a*b;
divide = a/(float)b;
remainder = a%b;
printf("\n\nAddition of the numbers = %d\n", add);
printf("\nSubtraction of 2nd number from 1st = %d\n", subtract);
printf("\nMultiplication of the numbers = %d\n", multiply);
printf("\nDividing 1st number from 2nd = %f\n", divide);
printf("\nRemainder on Dividing 1st number by 2nd is %d\n", remainder);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}