#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n"<<endl;
system("pause");
return 0;
}
C++的基础框架如上:
若有多个文件,有且仅有1个main函数,若有多个main函数 就会出错
变量的定义及输出:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//1.短整型(-32768 ^ 32767)
short num1 = 10;
//2.整型
int num2 = 10;
//3.长整型
long num3 = 10;
//4.长长整型
long long num4 = 10;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
cout << "num3=" << num3 << endl;
cout << "num4=" << num4 << endl;
system("pause");
}
#include <iostream>
using namespace std;
int main()
{
//1.短整型(2)(-32768 ^ 32767)
short num1 = 10;
//语法:sizeof(数据类型/变量)
cout << "short占用内存空间为:" << sizeof(short) << endl;
cout << "short占用内存空间为:" << sizeof(num1) << endl;
//2.整型(4)
int num2 = 10;
cout << "int占用内存空间为:" << sizeof(int) << endl;
//3.长整型(4)
long num3 = 10;
cout << "long占用内存空间为:" << sizeof(long) << endl;
//4.长长整型(8)
long long num4 = 10;
cout << "long long占用内存空间为:" << sizeof(long long) << endl;
system("pause");
}
#include <iostream>
using namespace std;
int main()
{
//1.单精度float
//2.双精度double
//默认情况下,输出一个小数,会显示出6位有效数字
float f1 = 3.14f;
cout << "f1=:" << f1 << endl;
double d1 = 3.14;
//语法:sizeof(数据类型/变量)
cout << "f2=:" << d1 << endl;
//统计float和double占用内存空间
//4个字节
cout << "f1oat 占用的内存空间为:" << sizeof(float) << endl;
//8个字节
cout << "double 占用的内存空间为:" << sizeof(double) << endl;
//科学计数法
float f2 = 3e2;//3*10^2
cout << "f2=:" << f2 << endl;
float f3 = 3e-2;//3*0.1^2
cout << "f3=:" << f3 << endl;
system("pause");
}
#include <iostream>
using namespace std;
int main()
{
//1.字符型变量创建方式
//2.字符型变量所占内存大小
char ch = ‘a‘;
cout << ch << endl;
//2、字符型变量所占内存大小
cout << "char字符型变量所占内存:" << sizeof(char) << endl;
//3、字符型变量常见错误
//char ch2 = "b"; //创建字符型变量时候,要用单引号
//char ch2 = ‘abcdef‘;//创建字符型变量时候,单引号内只能有一个字符
//4、字符型变量对应ASCII编码
//a - 97
//A - 65
cout << (int)ch << endl;
system("pause");
}
#include <iostream>
using namespace std;
int main()
{
//转义字符
//换行符\n
cout << "hello world\n";
// 反斜杠 cout << "\\" <<endl;
//水平制表符\t 可以整齐输出数据
cout << "aaaa\t helloworld" << endl;
cout << "aa\t helloworld" << endl;
cout << "aaaaaa\t helloworld" << endl;
system("pause");
}
#include <iostream>
using namespace std;
#include <string>//用C++风格字符串时候,要包含这个头文件
int main()
{
// 1、C风格字符串
// 注意事项 char字符串名 []
// 注意事项2 等号后面 要用双引号 包含起来字符串
char str[] = "hello world";
cout << str << endl;
// 2、C++风格字符串
//包含一个头文件 #include <string>
string str2 = "hello world";
cout << str2 << endl;
system("pause");
}
#include <iostream>
using namespace std;
#include <string>//用C++风格字符串时候,要包含这个头文件
int main()
{
//1.创建bool数据类型
bool flag = true;//true代表真
cout << flag << endl;
flag = false;//false代表假
cout << flag << endl;
//本质上 1代表真 0代表假
cout << "bool类型所占内存空间: " << sizeof(bool) << endl;
system("pause");
}
#include <iostream>
using namespace std;
#include <string>//用C++风格字符串时候,要包含这个头文件
int main()
{
// 1.整型
int a = 0;
cout << "请给整型变量a幅值" << endl;
cin >> a;
cout <<"整型变量a= "<< a << endl;
// 2.浮点型
float f = 3.14f;
cout << "请给浮点型变量f幅值" << endl;
cin >> f;
cout << "浮点型变量f= " << f << endl;
// 3.字符型
char ch = ‘a‘;
cout << "请给字符型变量ch幅值" << endl;
cin >> ch;
cout << "字符型变量ch= " << ch << endl;
// 4.字符串型
string str = "hello";
cout << "请给字符串型变量str幅值" << endl;
cin >> str;
cout << "字符串型变量str= " << str << endl;
// 5.布尔型
bool flag = false;
cout << "请给布尔型变量flag幅值" << endl;
cin >> flag; //只要是非0的值 都为真
cout << "布尔型变量flag= " << flag << endl;
system("pause");
}
原文:https://www.cnblogs.com/jgg54335/p/14459220.html