Linux学习日志 DAY5

C语言数据类型与基本语句
一.
整型int:在大部分机器上占4个字节,TC环境下占两个字节。
short(int) :2个字节。 long:4个字节。
单精度实型float:单精度浮点数,4个字节。
双精度实型double:双精度浮点数,8个字节。
字符型char:字符,一个字节。

二.
for语句
#include
#include
int main()
{
char a[1000];
int i;
for(i=0;i<1000;i++)
{
a[i]=-1-i;
printf("%d",a[i]);
}
printf("\n");
printf("%d\n",strlen(a)); //strlen:求字符长度,遇到“\0”结束
return 0;
}

运行结果即从-1到-128再到128到0依次循环你1000次,结果为255,因为strlen遇到了0。

格式说明符的含义
%d或%i按十进制有符号整数输出,正数的符号省略
%u按十进制无符号整数输出
%o按八进制无符号整数输出(不输出前导0)
%x或X按十六进制无符号整数输出(不输出前导符0x)
%c按字符型数据输出
%s按字符串数据输出
%f按小数形式输出(6位小数)
%e或E按指数形式输出实数
%%输出%本身
%g或G选用%f或%e格式中输出宽度较短的一种格式,不输出无意义的0

示例:
int main()
{
int a=100;
float b=1.11111;
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);
printf("%c\n",ch);
printf("%s\n",ptr);
printf("%p\n",&a);

printf("%10d\n",a);
printf("%6.3f\n",b);

return 0;

}
输出结果:
100
100
144
64
1.111110
helloworld!
0xbfd73cd4
100 //长度为10
1.111 // 6.3 :长度为6,小数点后保留三位

#include
int main ()

{
char ch;
int i,count=0;

scanf("%c",&ch);

for(i=0;i<8;i++)
{
    if (ch&1==1)  //按位和1相与,结果为1计数一次
    {
        count=count+1;
    }

ch=ch>>1;
}
printf("%d\n",count);

return 0;

}

结果:
输入a 输出3
输入b 输出3
输入c 输出4
输入r 输出4

你可能感兴趣的:(Linux学习日志 DAY5)