数据类型

数据类型
一.空类型(void),字符型,数值型(实型:单精度实型(小数点后面有6位)和双精度实型(小数点后面有15位)和整型:短整型(有无符号) 整型(一定不比短整型短) 长整型())

1.各类型所占字节:
int 4个字节
float:4个字节
double:8个
char:1个
枚举型

1.#include <stdio.h>
int main ()
int i = a ;
{
printf( “int %d\n”,sizeof(a或int) );
printf(“double%d\n”,sizeof(double));
printf(“float%d\n”,sizeof(float));
printf(“short%d\n”,sizeof(short));
printf(“long%d\n”,sizeof(long));
return 0;
}
2.该程序的结果为计算各种数据类型的所占字节
内存以字节为单位,每一个字节都有一个地址(一个字节等于8个位)
字节序分为大端字节序:高字节放低地址,低字节放高地址
小端字节序 :高字节放高地址,低字节放低地址
3.(1)char
无符号 11111111 = 2^8-1=255
00000000=0
有符号 01111111 = 2^7-1=127
11111111 =-127
(2)int
无符号 2^32 -1
0
有符号2^31-1
-2^31
2.strlen 函数
其功能是求字符串长度 ,遇到‘\0’结束 (\0表示的一个字符对应的数字是0)
#include <stdio.h>
#include<string.h> 有strlen函数时
int main()
char a [1000];
int i;
for(i = 0;i<1000;i++)
{
a[i] = -1 - i;
}
printf("%d\n",strlen(a));
return 0;
}
此程序结果为255.当i的值为127时,a[127]的值为-128,而-128是char类型数据能表示的最小负数当i继续增加时,a[128]的值肯定不能是-129,而程序会减1使回到127再逐次减1,当strlen遇到0时便会停止。
3.int i=-20;
unsigned int j = 10;
i+j =?
#include <stdio.h>
int main()
{int i= - 20;
unsigned int j =10;(当j=20时,程序运行的结果将会是0)
printf(”%u\n“,i+j);一定是%u
return 0;}
则其结果为4294967286
这里涉及到了补码问题
-20
10000000 00000000 00000000 00010100
先转换为反码
反码 111111111 111111111 11111111 11101011
补码 为在反码基础上加1
得到结果为4294967286
4.常量与变量以及只读变量
变量:整型:123l 表示 长整形 123u:表示无符号常量 0123:表示八进制 ox123:表示十六进制
实型
字符:字母+单引号 (\n:表示换行(表示一个字符
\o表示结束
\t 使字符前突出一个”tab“(空格))
字符串:用双引号表示(“a”就是“a\0”) 如china表示6个字符
枚举
只读变量:不能通过变量c去修饰对应内存的值,可以通过其他方式去修改,比如地址
#include <stdio.h>
#define b 20
int main()
{ int a = 10; 变量
a=100;
b=100; 常量不能被赋值
const int c=10; 只读变量
c=100; 不能通过变量c修改对应内存的值,可以通过其他方式去修改,比如地址
int p = (int)&c;
*p = 100;
printf("%d\n",c);
return 0;}
5.运算符
5.1 算术运算符 /:取整
%:取余
==:判断
=:赋值
5.2逻辑运算符
& :与(两个为1才为1)
| :或(只要一个1便是1)
5.3位运算符
<<:左移一位便相当于乘以2
左移两位便相当于乘以4

:右移一位便相当于除以2
#include <stido.h>
int main()
{ int i,count = 0;
char ch;
scanf("%c",&ch); 从标准缓冲区获取数据
for(i=0;i<8;i++)
{ if (ch & 1== 1)
{count = count + 1;}
ch = ch >> 1;
}
printf("%d\n" , count);
return 0;}
5.4三目运算符
#include <stdio.h>
int main()
{
int a = 1,b = 2;
int c;
c=(a>b)?a:b;
printf("%d\n",c);
return 0;}
5.5逗号运算符
当遇到 3+5,6+8时 整个表达式的值为:逗号后面的表达式2的值表达式
6.自加自减 (编译时要用g++代替gcc)
#include <stdio.h>
int main()
{int a=4;
a += a++;(或为a+=++a;++a+=a;++a+=a++;++a+=++a)
printf("%d\n,a)
return 0;}
程序运行的结果为:9 (或为 10 10 11 12)
7.\n换行
1.换行
2.刷新缓冲区
#include <stdio.h>
int main()
{
int i;
for (i = 0;i<5;i++)
{printf(“helloworld!\n”);
sleep(1);
}
return 0;} 结果为每隔一秒就输出一个helloword并换行但是若不加\n,则会等5秒钟一行内直接输出helloworld
或者
#include <stdio.h>
int main()
{int i;
for (i = 0; i<5; i++)
{printf(“helloworld!”)
fflush(stdout); 手动刷新缓冲区
sleep(1);
}
return0;}
8.printf (“格式控制字符串”,输出列表) 格式输出函数
%后面跟各种格式字符,以说明输出数据的类型,形式,长度,小数位数
scanf(”%d“,&a)格式输入函数(要注意选地址&a)
#include
int main()
{int a = 100;
float b = 1.111111;
char ch = ‘a’;
char ptr = “helloworld”;
printf("%d\n",a);整形输出
printf("%u\n",a);无符号输出
printf("%o\n",a);八进制输出
printf("%x\n",a);十六进制输出
printf("%f\n",b);输出float类型数据
printf("%c\n",ch);输出字符
printf("%s\n",ptr);输出字符串
printf("%p\n,&a);输出地址
printf("%10d\n",a);输出字段宽度为10且右对齐
printf("%-10d\n",a);输出字段宽度为10且左对齐
printf("%6.3f\n",b);输出占6列且3位为小数
return 0;}
9.优先级(

10.基本if语句
注:1.if(a=0)与if(a0)代码运行时都不会报错,但是这两者意义完全不同,所以为了避免发生错误
常常将if(a
0)写成if(0a)
2.字符可以用”
“判断而字符串不可以
简单计算器
#include <stdio.h>
int main()
{ int num1,num2,result;
char ch;
printf (“Please input …\n”);
scanf("%d%c%d",&num1,&ch,&num2);
if (’+’ == ch)
{result = num1 +num2;}
else if (’-’ == ch)
{result = num1 - num2 ;}
else if (’’ == ch)
{result = num1 * num2;}
else if (’/’ == ch)
{result = num1 / num2;}
printf(“result is %d\n”,result);
11.switch 语句 (break为关键字)
匹配到一个break 就跳出重新switch(),若没有break 则便会一直执行
简单计算器
#include <stdio.h>
int main()
{{ int num1,num2,result;
char ch;
switch(ch)
{
case 1 ‘+’:
result = num1 +num2;
break;
case 2 ‘-’:
result = num1 -num2;
break;
case 3 '
’:
result = num1 * num2;
break;
case 4 ‘/’:
result = num1 / num2;
break;
defult:
printf(“error!\n”);
}
printf(“result=%d\n”,result);
return 0;}
12.while 与 do…while
#include <stdio.h>
int main ()
{
while(1);
int a=10;
while(a–)
{
printf(“helloworld!\n”);
} … while先判断后执行
a=10;
do{
printf(helloworld!\n");
}while(a–);

return 0;} …do while 先执行后判断
#include <stdio.h>
int main ();
char ch;
int count =0;
scanf("%c",&ch);
while(1)
{
if(ch & 1 == 1)
{
count ++;

}
ch = ch >>1;
if (ch == 0)
{break;}
}
printf("%d\n",count);
result 0;}…当循环次数不确定时
13.for语句
for(1,2,3)第一个表达式仅执行一次,相当于i的初始化,再对2进行执行,再执行循环体,再执行3,再执行2
#include <stdio.h>
int main ()
{
int i;
for(i=0,printf(“first\n”);i<5,printf(“second\n”);i++,printf(“third\n”))
{
printf("%d\n",i);
if (5==i)
{break;}
}
result 0;}
14.break 与 countinue 的区别
break:跳出循环仅为跳出这一层的循环,对外循环无效
countinue:结束本次循环,继续下一次循环
#include <stdio.h>
int main()
{int i;
for(i=0; i<5;i++)
{if (3 == i)
{break;}…跳出循环
(若为continue则结束本次循环,继续下一次循环 )

printf(“hellloworld!\n”);
}

for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if(2 == j)
{break;}
printf(“helloworld!\n”);
}
}
return 0;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值