【语法基础】1.1 变量、输入输出、表达式和顺序语句

1.1 变量、输入输出、表达式和顺序语句

#include <cstdio>       //printf,scanf
#include <iostream>     //cin,cout 不用判断数据的输入输出类型

using namespace std;	//命名空间

int main()	//函数的入口
{  
    
    cout<<"Hello World!"<<endl;
    
    return 0;	

}
常用变量类型及范围:
类型关键字注释大小
布尔型boolfalse/true1byte
字符型char‘c’,’ ', '/n’一个字符用单引号1byte
整型int-2147483648~214783647;-231 ~231-14byte
浮点型float6-7位有效数字4byte
双浮点型double15-16位有效数字8byte
long long-263 ~263-18byte
long double18-19位有效数字16byte

B->Byte 字节

b->bit 位

1Byte=8bit

1.变量的定义:
//变量的定义

   int a, b=2, c=b;
   float d=1.5, e=1, f=1.234e2;
   bool g = true, h=false;
   char j = 'a', k = 'b';

   long long l=1000000000000ll;
   long double m =123.45;
2.变量的输入输出:
#include <iostream>     

using namespace std;	

int main()	
{  
    
    int a,b;	//定义两个变量 
    cin >> a >> b;	//输入
    cout << a + b << endl;	//输出 

    return 0;	

}
输出多个变量:
#include <iostream>   

using namespace std;	

int main()	
{  
    
   int a,b;	//定义两个变量 
   cin >> a;
   cin >> b;	//输入

   cout << a + b;
   cout << ' ';
   cout << a * b;
   cout << endl;	//输出 
    

    return 0;	

}
使用scanf,printf进行输入输出:
#include <cstdio>       
#include <iostream>     

using namespace std;	

int main()	//函数的入口
{  
    
   int a,b;	//定义两个变量 

   scanf("%d%d", &a, &b);	// %f读入浮点数类型 
   							// %d读入整数类型 
   							// &取地址符号 
   							// \n是回车 
   printf("a + b = %d\na * b = %d\n", a+b, a*b);
   							// 输出保留3位小数 %.3f 
   
    return 0;	

}


//浮点数的输入输出
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

    float a, b;//定义两个变量
    scanf("%f%f",&a,&b);
    printf( "a + b = %.1f\na * b = %.2f\n", a + b,a * b);
    
    return 0;

}
//char的输入输出
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

    char a, b;	//定义两个变量
    cin >> a >> b;
    printf( "%c %c\n", a, b);
    
    return 0;

}
//double的输入输出
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

    double a, b;	//定义两个变量
    scanf("%lf%lf", &a, &b);
    printf( "%lf %lf\n", a, b);
    
    return 0;

}
//long long的输入输出
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

    long long a, b;	//定义两个变量
    scanf("%lld%lld", &a, &b);
    printf( "%lld %lld\n", a, b);
    
    return 0;

}

scanf,printf输出效率比较高

cin,cout 不用判断数据的输入输出类型,cin会过滤空格

%c会读入空格需要过滤一下

#include 中对某个数字开平方 sqrt(x);

/*
int: %d
float: %f
double: %lf
char %c
long long %lld
*/
3.表达式
整数的加减乘除四则运算:

5.png

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{

	int a = 6;
	int c = a ++;	//a先给c然后再加1
	cout << a << ' ' << c << endl;
	
	int b = 6;
	int d = ++ b;	//b先加1再赋值给d
	cout << b << ' ' << d << endl;
	
	return 0;

}

image-20220329210104977

简写运算:
b += a; b = b + a;
b -= a; b = b - a;
b *= a; b = b * a;
b /= a; b = b / a;
b %= a; b = b % a;
变量的强制转换:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    float x = 123.12;

    int y = (int)x;
    
    cout << x << ' ' << y << endl;
    
    return 0;

}
顺序语句:略
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值