笔试之语言特性代码记录
来源:互联网 发布:ubuntu 64位安装wine 编辑:程序博客网 时间:2024/05/16 18:29
#include <stdio.h>#include <sys/types.h>#include <unistd.h>int main(){ int ret = 0; int i = 0; for( i = 0; i < 2; i++ ) { fork(); printf( "a\n" ); } return ret;}// 输出6个a
#include <stdio.h>#include <iostream>using namespace std;class A{ public: int &b; //有这个sizeof(A)为16,在64位机器上,没有该引用,则为4 int a;};int main(){ int ret = 0; cout << sizeof(int*) << " " << sizeof(A) << " " << sizeof(int) << endl; // 8 16 4 // unsigned int a = 1;// printf( "%d\n", a * (-2) );// cout << a * -2 << endl; unsigned int a = 0x1FFFFFF7; unsigned char b = a; char c = a; char *p = ( char * )&a; long long data = 0xf1f2f3f4f8f0f5f7; cout << sizeof(long long) << endl; printf( "%x, %x, %x, %x\n", b, c, *p, data ); long long a1 = 1, b1 = 2, c1 = 3; printf( "%d, %d, %d\n", a1, b1, c1 ); return ret;}/*long long data = 0xf1fffffffffff5f7;8f7, fffffff7, fffffff7, fffff5f7%x总是输出4个字节,所以当不足4个字节时,如b,c,*p会补,对应无符号位补0,有符号位补1,对于超过4个字节的,也是将4个字节入栈;*/
1. #define定义的宏只在本文件有效 //myheader.h#ifndef MYFILE#define MYFILEvoid test();#endif// use1.cpp#include <iostream>using namespace std;//#include "myheader.h"#define MAX 10void func(){ #ifdef MYFILE cout << "MYFILE is defined" << endl; #else cout << "MYFILE is not defined" << endl; //虽然在use1.cpp中包含了#include "myheader.h",但是该文件中没有,故MYFILE并没有定义 #endif return;}// use2.cpp#include "myheader.h"#include <iostream>using namespace std;extern void func();int main(){ func(); //cout << MAX << endl; //会出现:error: ‘MAX’ was not declared in this scope return 0;}g++ use1.cpp use2.cpp -o use./useMYFILE is not defined故下面代码是为了防止同一个文件包含一个头文件多次#ifndef MYFILE#define MYFILE#endif===========================#include <iostream>using namespace std;int cnt = 0;int show(){ cnt = 9; return 0;}int main(){ cout << show() << ', ' << cnt << endl; // 输出 0, 0 return 0;}
#include "myheader.h"#include <iostream>using namespace std;extern void func();extern const int v1;extern const int v2;class Test{ private: static int a;};int Test::a = 10;// static int Test::a = 10; //编译出错void f( const int &a ) //可以编译通过,但若去掉const则编译出错{ cout << "test f(const int&)" << endl; return;}//int mal( int a, int b = 2, int c = 3 );int mal( int a, int b = 2, int c = 4 ){ //int test() //{ // return 0; //} cout << a + b + c << endl; return 0;}template <typename T> T add( T x, T y ){ return x + y;}class A{ public: int a; int &b;};// 分解质因数,任何数都可以用若干个质数相乘得到void prim( int m, int n ){ if( m >= n ) { while( m % n != 0 ) { ++n; } cout << n << endl; m /= n; prim( m, n ); } return;}typedef int* PINT;typedef int *PINT1;// PINT等价于PINT1typedef int fun( int *, int );int testfun( int *p, int a ){ return a;}class ConstRef{ public: ConstRef( int ii ) : i(ii), ci(i), ri(ii){} void show() { cout << ci << ' ' << ri << endl; ri = 8; cout << ci << ' ' << ri << endl; } private: int i; const int ci; int &ri;};int main(){ int hah = 0; int &p33 = hah; cout << sizeof(int) << ' ' << sizeof(const int) << ' ' << sizeof(p33) << endl; cout << sizeof(ConstRef) << endl; ConstRef obj(3); obj.show(); int t1 = 9; const PINT p = &t1; const int *p2 = &t1; const PINT1 p3 = &t1; *p3 = 3; *p = 10; //编译通过 //*p2 = 20; //编译出错 //p = &t1; //p3 = &t1; //编译出错 assignment of read-only variable ‘p3’ cout << t1 << endl; int *p1; //*p1 = 9; int data = 27; while( cin >> data ) { prim( data, 2 ); cout << "===============================" << endl; } cout << "sizeof(A)=" << sizeof(A) << endl; cout << "sizeof(int) = " << sizeof(int) << endl; //add( 4.3, 2 ); add<float>( 4.3, 3 ); func(); //cout << MAX << endl; //cout << v1 << endl; //访问出错 cout << v2 << endl; const char *pp; //(**pp) = 'c'; cout << mal( 2 ) << endl; f(1); return 0;}
#include <iostream>using namespace std;//#include "myheader.h"#define MAX 10const int v1 = 10; //c++中只能在当前文件中使用extern const int v2 = 20; //c++可以在其他文件中使用// c中const变量为外部链接,而c++中为内部链接void func(){ #ifdef MYFILE cout << "MYFILE is defined" << endl; #else cout << "MYFILE is not defined" << endl; #endif return;}
0 0
- 笔试之语言特性代码记录
- (记录)笔试题之语言和程序设计
- 笔试之c语言
- JavaScript之语言特性
- PHP5.3新特性之语言特性
- PHP5.3新特性之语言特性
- c语言笔试之汇总
- 《代码之美》记录
- Go语言学习之语言特性
- 笔试记录
- 笔试记录
- C语言记录之九
- C语言笔试题之3
- C语言笔试题之3
- 笔试必考-C语言之sizeof详解
- J2SE 5.0专题 之 语言特性
- J2SE 5.0专题 之 语言特性
- J2SE 5.0专题 之 语言特性
- class<T>和 class<?>类型 有什么区别
- 本地访问阿里云故障问题
- WebView启动支付宝客户端支付失败
- 1934. 移动小球
- Mesos+Marathon对比Kubernetes
- 笔试之语言特性代码记录
- mysql 5.7.10使用客户端mysql-font 连接异常 提示:The'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is dis
- jQuery事件:bind、delegate、on的区别
- ElasticSearch的update_by_query方法
- redis.conf配置详细解析
- JPEG算法解密(三)
- 深入Struts学习系列(一)
- Hello Kitty在线数据库泄露 330万粉丝信息受到影响
- Spring Boot