免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1065 | 回复: 2
打印 上一主题 下一主题

预编译的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-06 22:13 |只看该作者 |倒序浏览
今日在看《C++ Primer 3rd Edition》第一篇,遇到一个问题

我以为应该先输出条件编译里的cout,结果却是先输出Hello World!
百思不得其解,难道预编译有什么规定?


源码如下:
#include "stdafx.h"
#include <iostream.h>
#define DEBUG

void read()    { cout << "read()\n";    }
void sort()    { cout << "sort()\n";    }
void compact() { cout << "compact()\n"; }
void write()   { cout << "write()\n";   }

int main(int argc, char* argv[])
{

#ifdef DEBUG
        cout << "Beginning execution of main()\n";
#endif

        printf("Hello World!\n");
        int iterations = 0;
        bool continue_loop = true;

        while ( continue_loop != false )
        {
                iterations++;

                cout << "the while loop has executed "
                          << iterations << " times\n";

                if ( iterations == 5 )
                          continue_loop = false;
        }
           read();
           sort();
           compact();
                write();

        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2006-05-07 09:30 |只看该作者
你可以通过跟踪调试发现cout << "Beginning execution of main()\n";先执行,为什么会先出现"Hello, world"? 你把上句写成cout << "Beginning execution of main()"<<endl;就知道了

论坛徽章:
0
3 [报告]
发表于 2006-05-07 21:50 |只看该作者
一语惊醒梦中人
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP