c++容易忘记的基础知识


强制类型转换
#include<stdio.h>
int main(){
double r=12.56;
int a=3,b=5;
printf("%d\n",(int)r);
printf("%d\n",a/b);
printf("%.1f",(double)a/(double)b);
    return 0;
}
12
0
0.6


宏定义,末尾不加分号
#include<stdio.h>
#define pi 3.14
int main(){
double r=3;
printf("%f\n",2*pi*r);
    return 0;
}
18.840000

能加括号的地方要加括号
#define ADD(a,b) ((a)+(b))
三目运算符
#include<stdio.h>
int main(){
    int a=3,b=5;
    int c= a>b ?7:11;
    printf("%d\n",c);
    return 0;
}
定义无限大数的写法
const int INF=0x3fffffff;


scanf格式控制(char数组不加&,除此之外都要加)
scanf("格式控制",变量地址)
scanf("%d",&n);
常见的数据类型变量
int %d scanf("%d",&n);
long long %lld scanf("%lld",&n);
float     %f    scanf("%f",&fl);
double    %lf   scanf("%lf",&db);
char      %c    scanf("%c",&c);
字符串  char数组   %s scanf("%s",str);

scanf对%d的输入跳过空格,而%s空格会被读入

   int a, b;
   scanf("%d%d",&a,&b);

   char str[10];
   scanf("%s",str);
   printf("%s",str);
   3 4
abcd efg
Abcd

Printf格式控制与scanf基本相同,
除了double   %f  printf(%f”,db);


常见的格式控制
int a=123;
double c= 12.3456;
printf("%5d\n",a);
printf("%05d\n",a);
printf("%.2f",c);
  123
00123
12.35
起别名;
typedef long long LL;
向上取整 和向下取整  要加#include<math.h>
double db1=-5.2,db2=5.2;
    printf("%.0f %.0f\n",floor(db1),ceil(db1));
switch语句
    int a=1,b=2;
    switch (a+b)
    {
    case 2:
        printf("%d\n",a);
        break;
    case 3:
        printf("%d\n",b);
        break;
    default:
    printf(" sad story\n");
        
    }


数组大小必须是常量,不能是变量
  int a[10];
  char str[10000];

二维数组
int a[5][6]={{3,1,2},{8,4},{},{1,2,3,4,5}};
剩下部分默认为0;
要注意把10^6大小的数组定义在主函数的外面。因为系统栈的大小很小。
对数组中每一个元素赋予相同的值,
#include<stdio.h>
#include<string.h>
int a[5]={1,2,3,4,5};
int main(){
    memset(a,-1,sizeof(a));
    return 0;
}

 char str[5][5];
  for (int i = 0; i < 3; i++)
  {
   for (int j = 0; j < 3; j++)
   {
       str[i][j]=getchar();
   }
   getchar();//把输入每行末尾的换行符给吸收掉
  }`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值