免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: flw
打印 上一主题 下一主题

超强 Hello World [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
41 [报告]
发表于 2007-04-20 15:49 |只看该作者
原帖由 langue 于 2007-4-20 15:38 发表
如果能再把 ANSI C 里所有的特性都用上,那这可真能算是名副其实的教学代码了。

呵呵,下面这个是水木上一位大侠刚才写给我的:
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>

  4. inline void foo(){}
  5. struct bar
  6. {
  7.     int x;
  8.     int y;
  9.     int z[4];
  10. };

  11. struct bar b = { .x = 1, .y = 2, { [2]= 1} };

  12. bool test(bar b)
  13. {
  14.     return true;
  15. }

  16. int main(void)
  17. {
  18.     int n;
  19.     scanf("%d", &n);
  20.     int a[n];
  21.     foo();
  22.     test((struct bar){ .x = 1, .y = 2});
  23.     int32_t x = 1;
  24.     return x;
  25. }
复制代码

[ 本帖最后由 flw 于 2007-4-20 15:57 编辑 ]

论坛徽章:
0
42 [报告]
发表于 2007-04-20 15:53 |只看该作者
还真没有见过这个语法...长见识了...
不过可惜没有变长数组的例子

  1. int n;
  2.     scanf("%d", &n);
  3.     int a[n];
复制代码

论坛徽章:
0
43 [报告]
发表于 2007-04-20 15:55 |只看该作者
原帖由 flw 于 2007-4-20 15:49 发表

呵呵,下面这个是水木上一位大侠刚才写给我的:
  1. // C99 风格的注释

  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #pragma (c9x,on)

  6. inline void foo(){}
复制代码

...


果然 C99。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
44 [报告]
发表于 2007-04-20 15:59 |只看该作者
总算成精华了。吼吼~

论坛徽章:
0
45 [报告]
发表于 2007-04-20 16:03 |只看该作者
原帖由 flw 于 2007-4-20 15:49 发表

呵呵,下面这个是水木上一位大侠刚才写给我的:
[code]#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

inline void foo(){}
struct bar
{
    int x;
    int y;
...


这代码有什么用? 是用来展示c99支持的新语法?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
46 [报告]
发表于 2007-04-20 16:07 |只看该作者
原帖由 zx_wing 于 2007-4-20 16:03 发表

这代码有什么用? 是用来展示c99支持的新语法?

对头。
如果某个编译器不能把这段代码编译过去,说明它不支持 C99 或者支持程度不够。

论坛徽章:
0
47 [报告]
发表于 2007-04-20 16:13 |只看该作者
没有吧?况且就算是被攻击也很正常。习惯了。

习惯...了......

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
48 [报告]
发表于 2007-04-20 16:21 |只看该作者

  1. #include <string.h>
  2. #include <stdio.h>

  3. struct t_option {
  4.         const char *long_name;
  5.         int short_name;
  6.         int phased;
  7. };

  8. int f_getopt_long(struct t_option *ops, const char *name) {
  9.         int index;

  10.         index = 0;
  11.         while (ops[index].long_name != NULL) {
  12.                 if (!ops[index].phased && strcmp(ops[index].long_name, name) == 0) {
  13.                         ops[index].phased = 1;
  14.                         return index;
  15.                 }
  16.                 index++;
  17.         }
  18.         return -1;
  19. }

  20. int f_getopt_short(struct t_option *ops, int name) {
  21.         int index;

  22.         index = 0;
  23.         while (ops[index].long_name != NULL) {
  24.                 if (!ops[index].phased && ops[index].short_name == name) {
  25.                         ops[index].phased = 1;
  26.                         return index;
  27.                 }
  28.                 index++;
  29.         }
  30.         return -1;
  31. }

  32. int f_getopt(int argc, const char *argv[], struct t_option *cmds, struct t_option *ops) {
  33.         int index;
  34.         int cmd;
  35.         int retval;

  36.         cmd = -1;
  37.         for (index = 1; index < argc; index++) {
  38.                 if (argv[index][0] != '-') {
  39.                         break;
  40.                 }
  41.                 if (argv[index][1] == '-') {
  42.                         retval = f_getopt_long(cmds, &argv[index][2]);
  43.                         if (retval == -1 && ops != NULL) {
  44.                                 retval = f_getopt_long(ops, &argv[index][2]);
  45.                         }else {
  46.                                 cmd = retval;
  47.                         }
  48.                         if (retval == -1) {
  49.                                 break;
  50.                         }
  51.                 }
  52.                 if (argv[index][2] != '\0') {
  53.                         break;
  54.                 }
  55.                 retval = f_getopt_short(cmds, argv[index][1]);
  56.                 if (retval == -1 && ops != NULL) {
  57.                         retval = f_getopt_short(ops, argv[index][1]);
  58.                 }else {
  59.                         cmd = retval;
  60.                 }
  61.                 if (retval == -1) {
  62.                         break;
  63.                 }
  64.         }
  65.         return cmd;
  66. }

  67. int main(int argc, const char *argv[]) {
  68.         struct t_option cmds[] = {
  69.                 "help", 'h', 0,
  70.                 "version", 'v', 0,
  71.                 "traditional", 't', 0,
  72.                 "next_generation", 'n', 0,
  73.                 NULL, 0, 0,
  74.         };

  75.         switch (f_getopt(argc, argv, cmds, NULL)) {
  76.         default:
  77.         case 0:
  78.         printf( "``hello'' is a greeting program which wrote by flw.\n"
  79.                 "\n"
  80.                 "Usage: hello [OPTIONS]\n"
  81.                 "       -h, --help             display this message then exit.\n"
  82.                 "       -v, --version          display version information then exit.\n"
  83.                 "\n"
  84.                 "       -t, --traditional      output a greeting message with traditional format.\n"
  85.                 "       -n, --next-generation  output a greeting message with next-generation format.\n"
  86.                 "\n"
  87.                 "Report bugs to <flw@cpan.org>\n"
  88.               );

  89.         break;
  90.         case 1:
  91.         printf( "hello - flw's hello world. 0.8 version\n" );
  92.         break;
  93.         case 2:
  94.         printf( "hello, world\n" );
  95.         break;
  96.         case 3:
  97.         printf(
  98.                 "+---------------+\n"
  99.                 "| Hello, world! |\n"
  100.                 "+---------------+\n"
  101.               );
  102.         break;
  103.         }
  104.         return 0;
  105. }
复制代码

论坛徽章:
0
49 [报告]
发表于 2007-04-20 16:52 |只看该作者

有创意!

试了下: test.c
#gcc -o test.o test.c
# ./test.o
Hello, world!
# ./test.o -h
``hello'' is a greeting program which wrote by flw.

Usage: hello [OPTIONS]
       -h, --help             display this message then exit.
       -v, --version          display version information then exit.

       -t, --traditional      output a greeting message with traditional format.
       -n, --next-generation  output a greeting message with next-generation format.

Report bugs to <flw@cpan.org>
# ./test.o -v
hello - flw's hello world. 0.8 version
# ./test.o -t
hello, world
# ./test.o -n
+---------------+
| Hello, world! |
+---------------+

论坛徽章:
0
50 [报告]
发表于 2007-04-20 20:06 |只看该作者
flw (市长姐姐)   
版主-法王
美女市长梅贝尔

久仰,怎么换性别了,到底是男是女啊??????
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP