开发者

Symbolic Constant in Xcode

I'm learning C through The C Programming Language 2nd Edition and it refers to symbolic constants where you use #define before main() to assign a label to a value.

This is the program I am trying to use:

#include <stdio.h>

#define LOWER 0
#define UPPER 300
#define STEP 20

main()
{
    int fahr;

    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
    {
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
    }
}

Printing a table of Fahrenheit Celsius conversions. However This code when compiled in xcode using the c tool gives me the response unable to read unknown load command referring to the line starting with for. I've tried retyping the program but it still hasn't worked.

Any help would be much apprecia开发者_运维知识库ted.


The first line should be

#include <stdio.h>


The code is perfectly fine. There's something wrong with your XCode setup (may be related: unable to read unknown load command.

cristi:tmp diciu$ cat test.c
#include <stdio.h>

#define LOWER    0
#define UPPER    300
#define STEP 20

main()
{
    int fahr;

    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
cristi:tmp diciu$ gcc test.c
cristi:tmp diciu$ ./a.out 
  0  -17.8
 20   -6.7
 40    4.4
[..]


Works for me in XCode - the only warning/error I got was

Control reaches end of non-void function

As defining main() defaults to int it should return something, eg. 0 for a successful program. Convention suggests that 0 means a program runs correctly and anything else is an error.

Better to define

int main()
{ 
    /* code */ 
    return 0;
}

as your main function. But this is a digression - see diciu's answer for a potential explanation of your problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜