复习c语言数据类型,运算符与表达式以及循环语句,控制结构复习

int 4个字节

float 4个字节

double 8个字节

char 1个字节

程序:


#include <stdio.h>
#include <string.h>
int main()

{
  printf("int is %d\n",sizeof(int));
  printf("short is %d\n",sizeof(short));
  printf("char is %d\n",sizeof(char));
  printf("float is %d\n",sizeof(float));
  printf("double is %d\n",sizeof(double));

return 0;

}

结果:

int is 4
short is 2
char is 1
float is 4
double is 8
注:unsigned char类型可以表示0-255范围的数字;最大1111 11111最小0000 0000

有符号char a最大表示0111 1111等于127最小1111 1111为-127  还有-0和正0,-0为-128共256种

无符号16位最大0-65535

有符号16位最大-32767到+32767

二。

strlen与sizeof一样都是关键字strlen遇到\0结束计算长度(\0为一个字符表示结束标志不算在strlen计算范围内)

变量 a指一块内存叫a,字符a放在一块内存里面。

注:\0对应Int0

程序:

#include <stdio.h>
#include<string.h>
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));


}

结果:不会出现正128和负的129到0结束计算

#include <stdio.h>
#include<string.h>
int main()
 {
   char *ptr="hello\0world";
   printf("%d\n",strlen(ptr));
   char a='a';
   char b='\0';
   printf("%d\n",b);

   char c[10]={1,2,3,4,5,6,0,7,8,9};
   printf("%d\n",strlen(c));


   
 
 }

运行结果:5 0 6注意字符'\0'对应整型0;

三:混合运算:

#include <stdio.h>
int main()
{
 int i=-20;
 unsigned int j=10;
 printf("%d\n",i+j) ;
     printf("%u\n",i+j);
};

其中%d输出会自动将无符号整数转换成有符号的数得到-10;

%u才是-20取补码的相加的结果;

混合运算规则:几个变量转换成其中空间最大的类型,一般无符号更大;

四:const

修饰只读变量

如 const int a不能通过a变量改变a对应空间值,但可以用别的方法

#include <stdio.h>
int main()
{
 const int a=1;
 int*p=(int*)&a;
*p=2;
//a=2;
printf("%d\n",a);
return 0;

注:volatile防止编译被优化  010与10不一样 03与3也不一样

ps: \n换行 \t水平制表(加空格)

wall打开警告

六:强制类型转换:

#include <stdio.h>
int main()
{

  const int b=3;
  int*p=(int*)&b;//将b类型从 const int转换成int*类型
printf("%d",b);
 char a=129;
 printf("%d",a);

}

七:运算符:

#include <stdio.h>
int main()
{
     char ch,i,count=0;
 printf("please input one\n");
 scanf ("%d",&ch);
 for(i=0;i<8;i++)
  {
       if(ch&1==1)
       {
         count++;
         
       }
       ch>>=1;
  
  
  }
 printf("%d\n",count);


}//输入一个8位二进制数计算1的个数,与1想与1*1=1,1*0=0,右移指令改变输入数字最后一位与1比较。

条件运算符?


#include <stdio.h>
int main()
{
  int num1 ,num2, max,min;
  printf("please input two number\n");
  scanf("%d%d",&num1,&num2);
  max=(num1>num2) ? num1:num2;
  min=(num1<num2) ? num1:num2;//num1<num2成立时输出num1,不成立时输出num2
printf("the max is%d\nthe min is %d\n",max,min);
return 0;

}

运算符优先级:

函数符号() 数组下标[]> 单目运算符> 算数运算符> 移位运算符 >关系运算符 >逻辑运算符 >三目运算符

#include <stdio.h>
int main()
{

    int a=1;
    printf("%d",a++);
    printf("%d\n",++a);
    printf("%d",++a);
    printf("%d",a++);
   int c=4;
   c += c++;//等于9
   printf("%d\n",c);
 //  c += ++c;
  // printf("%d\n",c);等于10
  // ++c +=c;等于10
    //   printf("%d\n",c);
  // ++c +=c++;等于11
  // printf("%d\n",c);
 //  ++ a+= ++a等与12
   //    printf("%d\n",c);
 


}

运行结果:

1
3
4
4
9
八:printf输出

%d或%i按十进制有符号整数输出,正数的符号省略 %u按十进制无符号整数输出 %o按八进制无符号整数输出(不输出前导0) %x或X按十六进制无符号整数输出(不输出前导符0x) %c按字符型数据输出 %s按字符串数据输出 %f按小数形式输出(6位小数) %e或E按指数形式输出实数        %p输出地址

#include <stdio.h>
main()
{
  int a=1,b=1,c=16;
  char ch='a';
  char *str="hello";
  float d=1.23;
printf("%d %i %o %x %p\n",a,a,a,b,c,&a);
printf("%c\n",ch);
printf("%s\n",str);
printf("%7.3f\n",d);//小数点前面保留7位,后面保留3位小数点

}

九:if语句

#include <stdio.h>
main()
{
 float num1,num2,result;
 char opt ;
 printf("please input number\n");
 scanf("%f%c%f",&num1,&opt,&num2);//运算符也算字符
/*if(opt=='+')
{
  result=num1+num2;

}
else if(opt=='-')
{
  result=num1-num2;

}

else if(opt=='*')
{

  result=num1*num2;

}

else 
{
  result=num1/num2;
      

}
printf("%3.3f\n",result);//保留3位小数点

}*/
if();

else if();

else();

十 :switch语句:

switch(opt)
{
  case '+':
      result=num1+num2;
      break;
  case '-':
      result=num1-num2;
      break;
  case '*':
      result=num1*num2;
      break;
  case '/':
      result=num1/num2;
      break;
    default:
      printf("error\n");
      break;
}
      printf("result is%3.3f\n",result);

注意:一定要加break

十一:continue与break的区别

#include <stdio.h>

int main()
{
    int i, j;
    
    for (j = 0; j < 3; j++)
    {
        for (i = 0; i < 5; i++)
        {
            if (i == 2)
            {
                break;     //跳出一层循环i=2时跳出里面的for循环不影响外面的for循环,所以执行6次Printf

        //continue跳出本次循环执行i=3,continue下面语句不执行所以执行12次

        }
            printf("helloworld!\n");
        }
    }

    for (i = 0; i < 10000; i++)
    {
        for (j = 0; j < 10; j++)
        {
            
        }
    }                       //跨切10000次
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10000; j++);
    }//跨切10次效率更高

十二:逗号运算符

语句a,语句b  只执行逗号后面的

#include <stdio.h>

int main()
{
    int i;

    for (i = 0, printf("first\n"); i < 5, printf("second\n"); i++, printf("third\n"))//去掉second\n就能运行
    {
        printf("helloworld!\n");
    }

    return 0;
}

十二:while语句

#include <stdio.h>

int main()
{
    
    /*while (1);//当while语句里面为1进入死循环
    {
        printf("hello\n");
    }*/

    int x = 5;
    while (x--)//当while语句里面为0跳出循环
    {
        printf("hellowrold!\n");
    }

    x = 5;

    do//先执行循环体在判断
    {
        printf("hello\n");
    }while (x--);

    return 0;
}
结果:

hellowrold!
hellowrold!
hellowrold!
hellowrold!
hellowrold!//5次
hello
hello
hello
hello
hello
hello//多执行一次x=0时先执行循环体在判断
 

猜你喜欢

转载自blog.csdn.net/hujiaqi2018/article/details/81153632