C++与C的日常回忆总结
c的写法:scanf printf
- #include <stdio.h>
- int main(){
- printf("输出")//输出
- scanf("%d",&a);//输入 %d整型 %c字符 %s字符串
- return 0;
- }
c++的写法: cout<< cin>>
- #include <iosstream>
- using namespace std;
- int main(){
- cout << "Hello, World! I am " << 18 << " Today!" << endl;//输出
- cin >> number;//输入
- return 0;
- }
-
用面向对象的思路对比C/C++写法的
C的写法:
-
typedef struct point3d{ - float x; float y;float z;
- }Point3d;
- void Point3d_print(const Point3d* 3d);
- Point3d a;
- a.x = 1 ; a.y = 2 ; a.z=3;
- Point3d_print(&a);
-
C++的写法
-
class Point3d { - public:
- Point3d(float x,float y,float z);
- print();
- private:
- float x; float y;float z;
- };
- Point3d a(1,2,3);
- a.print();
-
c的定义宏 调试宏
-
#include <stdio.h>
int main(){
printf("%s,%s,%dxxx\n",__FUNCTION__,__FILE__,__LINE__);
return 0;
}
运行:
main,001.c,3xxx
__FUNCTION__,__FILE__,__LINE__
函数名 文件名 行号
c一般的基础 输出 10.00000
-
#include <stdio.h>
int main(){
printf("%f",10/3.0*3);
return 0;
}
c一般的基础 没有输出 被我骗了?
-
int main(){
int a= 7;
int b = 6;
if ( a == 6 )
if ( b == 7 )
printf("A");
else
printf("B");
return 0;
}
C++的写法
-
class Point3d { - public:
- Point3d(float x,float y,float z);
- print();
- private:
- float x; float y;float z;
- };
- Point3d a(1,2,3);
- a.print();