c++的基本语句
#include<iostream>
#include<string.h>
#define x 3 //两种定义方法
const int y = 4;
using namespace std;
int main(){
/*基本输出输入的用法*/
int z;
cin>>z;
cout<<endl<<z<<endl;
//上面定义#define 的用法
cout<<x<<" "<<y;
/*sizeof的基本用法*/
cout<<"long long的所占的字符空间为"<<sizeof(long long)<<endl;
cout<<"long的所占的字符空间为"<<sizeof(long)<<endl;
cout<<"int的所占的字符空间为"<<sizeof(int)<<endl;
cout<<"short的所占的字符空间为"<<sizeof(short)<<endl;
cout<<endl;
long num1 = 10;
cout<<"num1的所占的字符空间为"<<sizeof(num1)<<endl;
/*sizeof算数组长度的用法*/
int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,0};
double c[5]={1.1,2.2,3.3,4.4,5.5};
double n;
n=sizeof(c)/sizeof(c[0]);/*有时侯可能会报错,这个时候要加
(int)(sizeof(c)/sizeof(c[o]))*/
printf("len(a)=%d\n",sizeof(a)/sizeof(a[0]));
printf("len(b)=%d\n",sizeof(b)/sizeof(b[0]));
printf("len(c)=%lf\n",n);
cout<<endl;
/*strlen的用法*/
/*strlen 是库函数,参数是一个指针*/
/*只能以char*(字符串)做参数,而且要想得到的结果正确必须包含'\0'*/
/*strlen是不会计算'\0'之后的长度的*/
//必须包含头文件事#include<string,h>
char str[12] = "hello word";
printf("sizeof=%d\n",sizeof(str));
cout<<endl;
cout<<"str的长度为:"<<sizeof(str)/sizeof(str[0]);
//这里可以看到出来sizeof只能取出数组的长度,并不能取出数组的实际个数
cout<<endl;
printf("strlen=%d\n",strlen(str));
cout<<endl;
/*补充科学计数法的使用*/
float s1 = 3e2;//
float s2 = 3e-2;//
cout<<"s1的大小为"<<s1<<endl;
cout<<"s2的大小为"<<s2<<endl;
/*如果只有一个system那么当结束的时候就要按两次回车*/
cout<<"当程序运行到第一次的时候结束";
system("pause");
cout<<"当程序运行到第二次的时候结束";
system("pause");
return 0;
}
/*注意short短整型在-32767~32768 之间
int在二的三十一次方
long
long long*/
补充一下ld returned 1 exit status报错的原因
(1)程序正在运行,无法编译,上次运行的窗口未关闭。程序窗口重复运行没有及时关闭,存在多个打开窗口,得一个个都关闭了再编译。(2)一个项目有多个 xx.c 文件,将多余的xx.c文件改成头文件即可。 (3)有函数拼写错误,如:printf拼写成prntf。scanf()写成scan()等。(4)main()函数拼写错误
本文介绍了C++中基本语句的应用,包括输入输出操作、宏定义的使用、类型大小查询、数组长度获取及字符串长度测量等核心概念。此外,还讲解了科学计数法的表示方法,并列举了一些常见的编译错误。

被折叠的 条评论
为什么被折叠?



