V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
W1nd
V2EX  ›  程序员

Mac下gcc和vim配合使用出现一些问题,望能指点迷津

  •  
  •   W1nd · 2013-06-03 11:30:33 +08:00 · 3862 次点击
    这是一个创建于 3970 天前的主题,其中的信息可能已经有所发展或是发生改变。
    C语言新手,正在学习C语言中,我的电脑是MacBook Pro,所以我使用了vim+gcc,gcc是从Xcode里安装了Command tools,因为我在V2EX里提问过,大家是这么建议我的,VIM是一个强大的命令行工具!我现在使用VIM编写,书看的是C程序设计语言,学到数组这里,我发现里面的范例编译之后,打开a.out或者在终端里输入./a.out没有出现应该有的结果,而是使得终端进入了a.out进程。但是我在学习前面一开始的华氏温度与摄氏温度对照表敲出来的重新编译运行没有任何问题,我在思考是不是getchar的问题,不能在终端中运行?因为我在学习到了getchar了以后写的代码编译出来都无法成功运行,我想让指点指点,不知道我的猜想是错是对?望不要说现在让我用Xcode,因为我既然我现在已经在使用Vim+gcc,就有个有始有终吧!
    第 1 条附言  ·  2013-06-04 02:55:51 +08:00
    #include <stdio.h>
    main()
    {
    int c, i, nwhite, nother;
    int ndigit[ 10 ];

    nwhite = nother = 0;
    for ( i = 0; i < 10; ++i)
    ndigit[ i ] = 0;
    while ( ( c = getchar() ) != EOF)
    if ( c >= '0' && c <= '9' )
    ++ndigit[ c - '0' ];
    else if ( c == ' ' || c == '\n' || c== '\t' )
    ++nwhite;
    else
    ++nother;
    printf( "digits =" );
    for ( i = 0; i < 10; ++i )
    printf( " %d", ndigit[ i ] );
    printf( ", white space = &d, other = &d", nwhite, nother);
    }
    这是书中的代码:统计各个数字、空白字符(空格符、制表符及换行符)以及所有其他字符出现次数的程序。
    我敲好以后编译然后直接打开a.out出现的是这样的:
    https://copy.com/FPXigpyAKC9y
    如果是使用终端打开的话是这样的:
    https://copy.com/ktbrhnDTO202
    这是刚开始学时第一项:华氏温度与摄氏温度的倒序
    正常运行
    https://copy.com/YmB2QvNxrNQ8
    13 条回复    1970-01-01 08:00:00 +08:00
    hector
        1
    hector  
       2013-06-03 15:53:59 +08:00
    看了半天没懂是什么意思,我的是mvim,配置文件 https://github.com/myourys/Vimrc
    然后一键编译运行
    tywtyw2002
        2
    tywtyw2002  
       2013-06-03 17:56:02 +08:00
    贴出来代码呀? 可能是溢出了吧 或者其他的
    Ricepig
        3
    Ricepig  
       2013-06-03 18:09:19 +08:00 via iPhone
    貌似gcc在osx上编译以后不是.out哇,看看有别的可执行文件?
    kqz901002
        4
    kqz901002  
       2013-06-03 20:01:44 +08:00
    @Ricepig 直接对c文件进行编译不指定输出的确是out文件
    sqbing
        5
    sqbing  
       2013-06-03 20:42:35 +08:00
    @kqz901002 呵呵
    pagict
        6
    pagict  
       2013-06-03 21:16:56 +08:00
    贴代码
    swulling
        7
    swulling  
       2013-06-03 21:18:40 +08:00
    和vim有啥关系
    ivanlw
        8
    ivanlw  
       2013-06-04 03:34:31 +08:00   ❤️ 1
    getchar是接受输入的吧?你运行程序后就一直等待你输入,直到EOF吧
    mikewoo
        9
    mikewoo  
       2013-06-04 06:01:04 +08:00   ❤️ 1
    统计字数的 你不输入字怎么统计呢?在等你输入呢,直到遇到EOF(Ctr+d)
    还有有几处%d 你写成了&d了
    W1nd
        10
    W1nd  
    OP
       2013-06-04 12:55:09 +08:00
    @mikewoo 抱歉,提问的时候思考的不充分,忘记说我输入字了也是毫无反应,https://copy.com/OqsmhvxmDEWV这是我重新修改以后执行的,输入过也没有用。
    贴一下我重新修改的代码
    #include <stdio.h>
    main()
    {
    int c, i, nwhite, nother;
    int ndigit[ 10 ];

    nwhite = nother = 0;
    for ( i = 0; i < 10; ++i)
    ndigit[ i ] = 0;
    while ( ( c = getchar() ) != EOF)
    if ( c >= '0' && c <= '9' )
    ++ndigit[ c - '0' ];
    else if ( c == ' ' || c == '\n' || c== '\t' )
    ++nwhite;
    else
    ++nother;
    printf( "digits =" );
    for ( i = 0; i < 10; ++i )
    printf( " %d", ndigit[ i ] );
    printf( ", white space = %d, other = %d", nwhite, nother);
    }
    mikewoo
        11
    mikewoo  
       2013-06-04 13:14:34 +08:00   ❤️ 1
    @W1nd 最后以EOF结束? 就是输入完了按control+d
    W1nd
        12
    W1nd  
    OP
       2013-06-04 13:52:58 +08:00
    @mikewoo 非常感谢啊!有效了,在终端里EOF一定要Control+D,原来是这样!
    xff1874
        13
    xff1874  
       2013-06-04 14:09:05 +08:00
    别用vim,改sublime text2
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2838 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:26 · PVG 23:26 · LAX 08:26 · JFK 11:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.