/*
* 01_基本数据类型.c
*
* Created on: 2015年7月3日
* Author: zhong
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h> //数学
#define Pi 3.1415926 //定义符号常量,预编译指令,编译后将由3.1415926代替引用Pi的地方
const float pi=3.1415926; //常变量和变量差不多,只是运行期间,数值不能变
//整型数据
void int_data_type(){
printf("-------int------------------\n");
signed short a= -10; //signed 有符号的,可以省略,默认是有符号的 ,范围:-2^(2*8-1)--2^(2*8-1)-1 = -2^15-2^15-1 = -32768-32767
unsigned short b=-10; //unsigned 无符号的,只能放正数,如果赋予负值,会得到错误的结果,但也可以输出
//范围:0-^2*2*8-1 = 0-2^16-1 = 0-65535
long l=10l; //因为long和int都 是4bit,所以不用long
l=10L;
printf("%d\n",a);
printf("%u\n",b); //指定用无符号十进制数的格式输出
//sizeof()是测量类型或者变量长度的运算符
printf("int=%d\n",sizeof(short)); //2 bit
printf("int=%d\n",sizeof(int)); //4 bit
printf("int=%d\n",sizeof(long)); //4 bit
printf("int=%d\n",sizeof(long long)); //8 bit
}
//字符型数据 :字符是按其代码(整型)存储的,因此c99将字符型作为整数类型的一种
void char_data_type(){
printf("-------char----------------\n");
printf("char=%d\n",sizeof(char)); //1 bit
char a='a'; //97 0110000001
printf("%d %c\n",a,a); //97 a
unsigned char c=253;// ? 默认char是有符号的, 0-127
printf("%d\n",c);
}
//浮点型数据:表示具有小数实数 --实数以指数形式存放在存储单元中
void float_data_type(){
printf("----------float----------------------\n");
printf("float=%d ",sizeof(float)); //4bit float能得到6位有效数字
printf("double=%d ",sizeof(double)); //8bit double能得到15位有效数字 --c语言中进行浮点数的运算针float型数据自动转换为double型,进行运算
printf("long double=%d\n",sizeof(long double)); //12bit
float f=3.12159f; //表示一个float
double a=1.2f; //表示double
long double d=1.23L;//表示long double
d=1.23l;
}
//运算符
void operational_character(){
printf("----运算符---------------------------\n");
int a=10;
int b=11;
printf("%d\n",+a); //- 正号运算符 output:a的值
printf("%d\n",-a); //- 负号运算符 output:-10 a的算术负值
printf("%d\n",a*a); //* 乘法运算符 output:100
printf("%d\n",b/a); // / 除法运算符 output:1 a/b的商 11/10=1 两个整数相除结果为整数
float f=11.0f;
float g=10.0f;
printf("%f\n",f/g); //output:1.100000 两个实数相除结果是双精度实数
printf("%d\n",b%a); //% 求余运算符 output:10 10/11=商1余1 a/b的余数 要求参数运算的对象是整数,结果也是整数 8%3=2
//除了这个%运算符,其运算符操作数都 可以是任何算术类型
printf("%d\n",a+b); //+ 加法运算符
printf("%d\n",a-b); //+ 加法运算符
//自增运算符 自减运算法 ,只能用于变量,而不能用于常量或者表达式 如:5++,(a+b)++是不合法的
//用途:自增减 常用于循环语句,使变量自动加1,也用于指针变量,使指针指向下一地址
int i=10;
printf("++i=%d\n",++i); //output :11 先增加,后获得值
i=10;
printf("--i =%d\n",--i); //output :9 先自减,后获得值
i=10;
printf("i++ =%d\n",i++); //output :10 先获得值,后相加
i=10;
printf("i-- =%d\n",i--); //output :10 先获得值,后自减
}
void compute_area(){
printf("求三角形的面积 \n");
double a,b,c,s,area;
a=3;
b=4;
c=5;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));//开均号函数
printf("a=%f,b=%f,c=%f ",a,b,c);
printf("area=%f\n",area);
}
//将秒转换成分种和秒
void test(){
int count_s=60; //500s
if(count_s<60){
printf("共%d秒",count_s);
}else{
int m=count_s/60;
int s=count_s%60;
printf("共%d分,%d秒",m,s);
}
}
int main1() {
// int_data_type();
// char_data_type();
// float_data_type();
// operational_character();
// compute_area();
test();
// system("pause");
}
C语言学习笔记:01_基本数据类型
最新推荐文章于 2022-08-29 15:53:48 发布