跳过正文

丙加·第1章·答案

·124 字·1 分钟·

第一题(12 处错误 + 1 处习惯不佳):

inculde studioh   // 错误1:井应为#
                        // 错误2:inculde → include
                        // 错误3:studio → stdio
                        // 错误4:。h → .h
                        // 错误5:中文引号应为尖括号或英文双引号(但stdio.h建议用<>)
INT mian(){           // 错误6:INT → int(大小写)
                        // 错误7:mian → main
                        // 错误8:同样是中文符号问题,()→ (void) 或 ()(但C中建议写void)
print("Hello World\n")  // 错误9:print → printf
                        // 习惯1:printf一行应该缩进。
                        // 错误10:缺少分号 ;
    /* 打印语句         // 错误11:注释未闭合,应补上 */
                        // 错误12:没写return 0; 虽然能编译,但这是很不好的习惯
}

习惯不佳:main 函数没有 return 0;(虽然某些编译器允许,但不规范)。


第二题(2 处错误;两种改法):

#include <iostream>
int main() {
    cout << "Hello World" << endl;  // 错误1:cout和endl未指定命名空间(缺少std::)
                                    // 错误2:缺少闭合大括号 }
    return 0;
}

两种改法:

  1. 使用 std:: 前缀(推荐):

    #include <iostream>
    int main() {
        std::cout << "Hello World" << std::endl;
        return 0;
    }
    
  2. 使用 using namespace std;(教学可用,项目慎用):

    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello World" << endl;
        return 0;
    }
    

命令提示符@CommandPrompt-Wang
作者
命令提示符@CommandPrompt-Wang