使用c写的第一个hello world!

来源:互联网 发布:linux升级ruby版本 编辑:程序博客网 时间:2024/05/16 06:39

我第一次编写的helloworld.c是这样的:

#include <stdio.h>int main(void){    printf('Hello World!\n');}

然后就报错了:

andymini:c andy$ vim helloworld.c andymini:c andy$ gcc helloworld.c helloworld.c:5:9: warning: multi-character character constant [-Wmultichar]        printf('Hello World!\n');               ^helloworld.c:5:9: warning: character constant too long for its typehelloworld.c:5:9: warning: incompatible integer to pointer conversion passing 'int' to parameter of      type 'const char *' [-Wint-conversion]        printf('Hello World!\n');               ^~~~~~~~~~~~~~~~/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/stdio.h:257:36: note:       passing argument to parameter hereint      printf(const char * __restrict, ...) __printflike(1, 2);                                       ^helloworld.c:5:9: warning: format string is not a string literal (potentially insecure)      [-Wformat-security]        printf('Hello World!\n');               ^~~~~~~~~~~~~~~~4 warnings generated.andymini:c andy$ clearandymini:c andy$ lltotal 40drwxr-xr-x   5 andy  admin   170  4  1 22:28 ./drwxrwxr-x  24 root  admin   816  4  1 22:26 ../-rwxr-xr-x   1 andy  admin  8456  4  1 22:28 a.out*-rw-r--r--   1 andy  admin    66  4  1 22:27 helloworld.c-rw-r--r--   1 andy  admin     8  4  1 22:28 test1.phpandymini:c andy$ ./a.out Segmentation fault: 11andymini:c andy$ vim helloworld.c andymini:c andy$ gcc helloworld.c andymini:c andy$ lltotal 40drwxr-xr-x   5 andy  admin   170  4  1 22:29 ./drwxrwxr-x  24 root  admin   816  4  1 22:26 ../-rwxr-xr-x   1 andy  admin  8456  4  1 22:29 a.out*-rw-r--r--   1 andy  admin    66  4  1 22:29 helloworld.c-rw-r--r--   1 andy  admin     8  4  1 22:28 test1.phpandymini:c andy$ ./a.out Hello World!andymini:c andy$ 

后面看原来是双引号的问题,改成了这样:

#include <stdio.h>int main(void){    printf("Hello World!\n");}

就可以了!

另外发现vim可以:n test1.php 进行新文档的编写,只要是用:e test1.php或者:e helloworld.c就可以切换回刚刚的文件了,非常方便!

迫不及待的想多写点C代码了~~~

0 0