有趣的C++语言问题 测试你对C++语言的熟悉程度

· · 休闲·娱乐

本文基于matrix67大佬的 《有趣的C语言问题 测试你对C语言的熟悉程度》

这篇文章是极好的,但对于一个学C++的蒟蒻来说,看C语言像赤石一样又熟悉又难受,原文章代码的格式也有一点问题

况且虽然C++兼容了部分C的语法,但还有许多已不适用,故写下这篇文章

为确保趣味性,本文中代码均不使用代码高亮

原C语言特性,在C++中也适用部分

你相信么?下面这个程序输出的两行东西不一样!

#include<iostream>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
using namespace std;
int main(){
    cout<<h(f(1,2))<<endl;
    cout<<g(f(1,2))<<endl;
    return 0;
}

下面的程序看似完全正确。你能看出它为什么通不过编译吗? 看出问题前不要去试着编译,不然你会后悔你没看出来这个低级的语法错误。

#include<iostream>
using namespace std;
void OS_Solaris_print(){
    cout<<"Solaris - Sun Microsystems"<<endl;
}
void OS_Windows_print(){
    cout<<"Windows - Microsoft"<<endl;
}
void OS_HP-UX_print(){
    cout<<"HP-UX - Hewlett Packard"<<endl;
}
int main(){
    int num;
    cout<<"Enter the number (1-3):"<<endl;
    cin>>num;
    switch(num){
        case 1:
            OS_Solaris_print();
            break;
        case 2:
            OS_Windows_print();
            break;
        case 3:
            OS_HP-UX_print();
            break;
        default:
            cout<<"Hmm! only 1-3 :-)"<<endl;
            break;
    }
    return 0;
}

为什么下面这个程序的输出不是NONE?看你多久才能看出来。

#include<iostream>
using namespace std;
int main(){
    int a=10;
    switch(a){
        case '1':
            cout<<"ONE"<<endl;
            break;
        case '2':
            cout<<"TWO"<<endl;
            break;
        defa1ut:
            cout<<"NONE"<<endl;
    }
    return 0;
}

下面这个程序输出什么?

#include<cstdio>
using namespace std;
int main(){
    int i=43;
    printf("%d\n",printf("%d",printf("%d",i)));
    return 0;
}

下面这个程序输出什么?

#include<iostream>
using namespace std;
int main(){
    int i;
    i=10;
    cout<<"i : "<<i<<endl;
    cout<<"sizeof(i++) is: "<<sizeof(i++)<<endl;
    cout<<"i : "<<i<<endl;
    return 0;
}

下面这个程序输出什么?

#include<cstdio>
#include<cstdlib>
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
using namespace std;
int main(){
    /* The powers of 10 */
    int pot[] = {
        0001,
        0010,
        0100,
        1000
    };
    int i;
    for(i=0;i<SIZEOF(pot);++i){
        PrintInt(pot[i]);       
    }
    return 0;
  }

下面这个程序输出什么?

#include<cstdio>
using namespace std;
int main(){
    int a=3,b=5;
    printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
    printf(&a["WHAT%c%c%c  %c%c  %c !\n"], 1["this"],
        2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
    return 0;
}

下面这个程序输出什么?

#include<cstdio>
using namespace std;
int main(){
    int i=23;
    printf("%d %d\n",i++,i++);
    return 0;
}

下面这个程序输出什么?

#include<iostream>
using namespace std;
int main(){
    int i=6;
    if(((++i<7)&&(i++/6))||(++i<=9));
    cout<<i<<endl;
    return 0;
}

下面这段代码是否合法?

#include<cstdio>
#define PrintInt(expr) printf("%s : %dn",#expr,(expr))
using namespace std;
int max(int x, int y){
    (x > y)? return x : return y;
}
int main(){
    int a=10,b=20;
    PrintInt(a);
    PrintInt(b);
    PrintInt(max(a,b));
    return 0;
}

怎样用printf函数打印"I can print %"?别忘了百分号是用于格式化输出的。

不用任何比较运算符,写一个程序找出三个数中的最小数。

不用+号,(用位运算)实现加法运算。

最有趣的一个问题:不用分号,写一个Hello World程序。 这是有可能的,而且办法非常简单,只用到了最基本的语法规则。 实在想不出来再看答案吧(棍母):

.

::::info[答案]

#include<cstdio>
int main(){
    if(printf("Hello World")){}
}

::::

C++特有特性

暂时懒得写了