【C语言系列】基础语法案例分析(初级篇)
时间:2022-05-04
本文章向大家介绍【C语言系列】基础语法案例分析(初级篇),主要内容包括1、C语言打印一条语句、2、C语言打印用户输入的一个整数、3、C语言实现两个整数相加、4、C语言实现两个小数相乘、5、C语言查找字符的ASCII值、6、C语言根据用户输入的整数做商和余数、7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度、8、C语言获取关键字long的长度范围、9、C语言交换数值、10、C语言检查数值是奇数还是偶数、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
本文是该系列的第一篇,都是一些相对初级的算法,很适合刚开始学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",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient=dividend/divisor; /* Computes quotient */
remainder=dividend%divisor; /* Computes remainder */
printf("Quotient = %dn",quotient);
printf("Remainder = %d",remainder);
return 0;
}
输出:
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度
基本语法是:
temp = sizeof(operand);
/* Here, temp is a variable of type integer,i.e, sizeof() operator
returns integer value. */
源代码:
/* 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 bytesn",sizeof(a));
printf("Size of float: %d bytesn",sizeof(b));
printf("Size of double: %d bytesn",sizeof(c));
printf("Size of char: %d byten",sizeof(d));
return 0;
}
输出:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
注:可能会由于系统的不同出来的结果也不尽相同。
8、C语言获取关键字long的长度范围
源代码:
#include <stdio.h>
int main(){
int a;
long int b; /* int is optional. */
long long int c; /* int is optional. */
printf("Size of int = %d bytesn",sizeof(a));
printf("Size of long int = %ld bytesn",sizeof(b));
printf("Size of long long int = %ld bytes",sizeof(c));
return 0;
}
输出:
Size of int = 4 bytes
Size of long int = 4 bytes
Size of long long int = 8 bytes
9、C语言交换数值
源代码:
#include <stdio.h>
int main(){
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a; /* Value of a is stored in variable temp */
a = b; /* Value of b is stored in variable a */
b = temp; /* Value of temp(which contains initial value of a) is stored in variable b*/
printf("nAfter swapping, value of a = %.2fn", a);
printf("After swapping, value of b = %.2f", b);
return 0;
}
输出:
Enter value of a: 1.20
Enter value of b: 2.45
After swapping, value of a = 2.45
After swapping, value of b = 1.2
10、C语言检查数值是奇数还是偶数
源代码:
/*C program to check whether a number entered by user is even or odd. */
#include <stdio.h>
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
输出1:
Enter an integer you want to check: 25
25 is odd.
输出2:
Enter an integer you want to check: 12
12 is even.
也可以用条件运算符解决:
/* C program to check whether an integer is odd or even using conditional operator */
#include <stdio.h>
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);
return 0;
}
修改自:http://www.codeceo.com/article/100-c-programming-code.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 掘金15W沸点简单分析(一)
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
- 基于AOP和ThreadLocal实现日志记录
- 搭建prometheus+grafana监控SpringBoot应用入门
- 掘金15W沸点简单分析(二)
- 老生常谈SpringAop日志收集与处理做的工具包
- 线程间通信wait---notify
- Ubuntu安装Oracle Java8
- CentOS7安装Git
- RocketMQ-环境搭建(单master模式-ubuntu版)
- 使用Web.xml配置SpringMvc(同时使用xml配置文件)
- 第七节:Activiti6.0——Task的变量使用
- 第八节:Activiti6.0——启动流程相关
- 第九节:Activiti6.0——ReceiveTask接收信号、中间信号捕获事件和中间消息捕获事件的流程推进
- SpringBoot——全局异常捕获和自定义异常