程序清单2.4_nogood.c_程序_《C Primer Plus》P26

// nogood.cpp : 定义控制台应用程序的入口点。
//
/*
    时间:2018年05月31日 21:02:59
    代码:程序清单2.4_nogood.c_程序_《C Primer Plus》P26
    目的:认识代码的基本错误_花括号/声明/注释/分号
*/
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
(
    int n, int n2, int n3;
    /* 该程序含有几个错误
    n = 5;
    n2 = n * n;
    n3 = n2 * n2;
    printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)
    return 0;
)

/* 
    在VS2010中运行结果:
---------------------------------------
    生成失败,无法运行.
---------------------------------------
*/

 以上代码主要有四种错误:

  1. 函数体用使用花括号{};而非();

  2. 声明:在一行内声明,声明关键用一次就好;

  3. 注释/*要有始有终 */

  4. 结束语句用分号;


修正为如下代码:

// nogood.cpp : 定义控制台应用程序的入口点。
//
/*
    时间:2018年05月31日 21:02:59
    代码:程序清单2.4_nogood.c_程序_《C Primer Plus》P26
    目的:认识代码的基本错误_花括号/声明/注释/分号
*/
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{    //此处务必使用花括号(左)
    int n, n2,n3;    
    /* 等价于:
    int n;
    int n2;
    int n3;
    */
    n = 5;
    n2 = n * n;
    n3 = n2 * n2;
    printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3); //结束语句用分号;
    getchar();

    return 0;
}    //此处务必使用花括号(右)

/* 
    在VS2010中运行结果:
---------------------------------------
n = 5, n squared = 25, n cubed = 625
---------------------------------------
*/


猜你喜欢

转载自blog.51cto.com/13555061/2122613
今日推荐