我以前[color=red]临摹[/color]的一个 hello world。
大家看了以后不要说我无聊,我无聊是因为有人无聊在先:
http://packages.debian.org/unstable/devel/hello
/*
* $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
int
main( int argc, char *argv[] )
{
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ "traditional", 0, 0, 't' },
{ "next-generation", 0, 0, 'n' },
{ 0, 0, 0, 0 }
};
c = getopt_long( argc, argv, "hvtn",
long_options, &option_index );
if (c == -1)
break;
switch (c) {
case 'h':
printf( "``hello'' is a greeting program which wrote by flw.\n"
"\n"
"Usage: hello [OPTIONS]\n"
" -h, --help display this message then exit.\n"
" -v, --version display version information then exit.\n"
"\n"
" -t, --traditional output a greeting message with traditional format.\n"
" -n, --next-generation output a greeting message with next-generation format.\n"
"\n"
"Report bugs to <flw@cpan.org>\n"
);
break;
case 'v':
printf( "hello - flw's hello world. 0.8 version\n" );
break;
case 't':
printf( "hello, world\n" );
break;
case 'n':
printf(
"+---------------+\n"
"| Hello, world! |\n"
"+---------------+\n"
);
break;
default:
break;
}
}
if ( optind < argc ){
fprintf( stderr,
"Too many arguments\n"
"Try `hello --help' for more information.\n"
);
exit(1);
}
if ( optind == 1 ){
printf( "Hello, world!\n" );
}
exit (0);
}
flw 回复于:2007-04-20 10:12:28
这个程序是我根据 Debian 的 hello 包的 man 手册所描述的功能,自己实现的。
Sworder 回复于:2007-04-20 10:13:15
靠过一次了,我再来靠一次。。。
我靠。
Edengundam 回复于:2007-04-20 10:14:31
还用了getopt.....
厉害
yuanchengjun 回复于:2007-04-20 10:16:48
flw发疯了……
wjianc 回复于:2007-04-20 10:31:08
flw 是 M 还是 F ?
flw 回复于:2007-04-20 10:33:14
引用:原帖由 wjianc 于 2007-4-20 10:31 发表
flw 是 M 还是 F ?
既不是 father,也不是 mother,事实上我还没有孩子。
wjianc 回复于:2007-04-20 10:49:34
引用:原帖由 flw 于 2007-4-20 10:33 发表
既不是 father,也不是 mother,事实上我还没有孩子。
:oo :lol
xiao_pk 回复于:2007-04-20 10:54:31
哈哈
cugb_cat 回复于:2007-04-20 10:54:49
引用:原帖由 flw 于 2007-4-20 10:33 发表
既不是 father,也不是 mother,事实上我还没有孩子。
以前一直以为flw是female,后来听说是male
cugb_cat 回复于:2007-04-20 10:58:33
刚才试了一下,debian的stable源里就有这个程序,apt-get install hello
还有好多选项呢,哈哈,有意思~~
converse 回复于:2007-04-20 11:05:12
这个帖子再次向我证明这个世界解决了温饱问题之后没事做的人大把大把的是~~
hblee 回复于:2007-04-20 11:11:47
真牛哦
wjianc 回复于:2007-04-20 11:13:32
引用:原帖由 converse 于 2007-4-20 11:05 发表
这个帖子再次向我证明这个世界解决了温饱问题之后没事做的人大把大把的是~~
支持。。。。第1次写叫叫解决温饱问题。。。解决后还有新版本,那就不好说了。。。;P
flw 回复于:2007-04-20 11:14:40
引用:原帖由 converse 于 2007-4-20 11:05 发表
这个帖子再次向我证明这个世界解决了温饱问题之后没事做的人大把大把的是~~
以你之水平,居然没看出来这个程序源码的实用性?
太让我失望了。
flw 回复于:2007-04-20 11:15:25
不成,我得建议版主加个精华。
Edengundam 回复于:2007-04-20 11:16:47
引用:原帖由 flw 于 2007-4-20 11:14 发表
以你之水平,居然没看出来这个程序源码的实用性?
太让我失望了。
不要重复造轮子 :em14::em14:
Sworder 回复于:2007-04-20 11:26:24
引用:原帖由 flw 于 2007-4-20 12:14 发表
以你之水平,居然没看出来这个程序源码的实用性?
太让我失望了。
一个控制台类程序的标准化范例,其实是一个很好的程序外壳。
套在哪里都能用,插在哪里都是一棵好苗苗。
值得设个精。。。:em11:
[ 本帖最后由 Sworder 于 2007-4-20 12:27 编辑 ]
flw 回复于:2007-04-20 11:27:34
楼上两位都错了。
zx_wing 回复于:2007-04-20 11:31:04
引用:原帖由 flw 于 2007-4-20 10:11 发表
我以前[color=red]临摹[/color]的一个 hello world。
大家看了以后不要说我无聊,我无聊是因为有人无聊在先:
http://packages.debian.org/unstable/devel/hello
/*
* $Id$
*/
#include <stdio. ...
正好不少人在问怎么解析参数,这个例子正好
flw 回复于:2007-04-20 11:33:29
引用:原帖由 zx_wing 于 2007-4-20 11:31 发表
正好不少人在问怎么解析参数,这个例子正好
你得到它了。
基于这个理由,我希望版主设精。
Sworder 回复于:2007-04-20 11:35:29
以后再有人发贴说自己是初学者,想学编程的话,就把他拉到这里来盯着首贴看吧。
yxm0513 回复于:2007-04-20 11:46:41
GNU也有哦
http://directory.fsf.org/GNU/hello.html
flw 回复于:2007-04-20 11:48:56
引用:原帖由 yxm0513 于 2007-4-20 11:46 发表
GNU也有哦
http://directory.fsf.org/GNU/hello.html
然。Debian 那个 hello 就是从 GNU 的移植的。
不仅有,而且从 92 年的 1.0 版到 06 年的 2.2 版,一直都有人维护。
所以说,不要小看了 Hello world。
flw 回复于:2007-04-20 11:53:41
刚看了一下 GNU 的 hello world,发现我的还是不够档次啊。没有考虑到多国语言的支持问题。
另外文档也不如人家的齐全。还有兼容性的问题,唉…… 看来还是差远了。
ngzyl 回复于:2007-04-20 12:05:19
支持加精,好帖!
建议把gnu的也贴出来
cugb_cat 回复于:2007-04-20 12:05:32
引用:原帖由 flw 于 2007-4-20 11:48 发表
然。Debian 那个 hello 就是从 GNU 的移植的。
不仅有,而且从 92 年的 1.0 版到 06 年的 2.2 版,一直都有人维护。
所以说,不要小看了 Hello world。
我刚装上的那个hello程序是2.1.1版的~~
Edengundam 回复于:2007-04-20 12:19:36
引用:原帖由 flw 于 2007-4-20 11:27 发表
楼上两位都错了。
我的意思就是解析命令行去用getopt而不是自己去辛苦处理....
可惜...不知道为什么...还是有开源软件自己实现一遍getopt.
loveface 回复于:2007-04-20 12:35:57
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ "traditional", 0, 0, 't' },
{ "next-generation", 0, 0, 'n' },
{ 0, 0, 0, 0 }
};
为什么有个死循环呢?
Edengundam 回复于:2007-04-20 12:38:44
引用:原帖由 loveface 于 2007-4-20 12:35 发表
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0 ...
再看下花括号..........:em06:
不过我还是习惯K&R风格...
niexining 回复于:2007-04-20 13:32:23
关于getopt的示例程序。
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int opt;
while((opt = getopt(argc, argv, "if:lr")) != -1) {
switch(opt) {
case 'i':
case 'l':
case 'r':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
for(; optind < argc; optind++)
printf("argument: %s\n", argv[optind]);
exit(0);
}
来源:http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764543733,descCd-download_code.html
Beginning Linux Programming, 2nd Edition
by Richard Stones, Neil Matthew, Alan Cox
ISBN: 978-0-7645-4373-9
zhoujianxin 回复于:2007-04-20 14:05:20
参数解析很经典。
更让我明白一件事情,有些人是不能够惹的。会导致全部神经失调
flw 回复于:2007-04-20 14:09:52
引用:原帖由 zhoujianxin 于 2007-4-20 14:05 发表
参数解析很经典。
更让我明白一件事情,有些人是不能够惹的。会导致全部神经失调
还有些人不惹他他都会随地大小便。
decwang 回复于:2007-04-20 14:25:27
呵呵,任何程序要想一次编译通过几乎是不可能的,尤其是当你的代码超过10行的时候
chedan 回复于:2007-04-20 14:32:07
apt-get install hello
==================
回去装上玩玩,体会一下计算机高人证明1+1=2的有益尝试。
nully 回复于:2007-04-20 15:11:40
简单的问题复杂化,这不应该成为自由软件炫耀的资本。
现在明白gnome/qt各种各样的程序,甚至小小的一个游戏启动速度都这么慢的原因。
tonymark974 回复于:2007-04-20 15:21:05
作为参数解析的例子还不错嘛
hongst 回复于:2007-04-20 15:28:59
有点放屁脱裤的嫌疑,不过初学者做为教程是不错的
Edengundam 回复于:2007-04-20 15:33:56
可怜的flw...又被攻击了...
flw 回复于:2007-04-20 15:35:36
引用:原帖由 Edengundam 于 2007-4-20 15:33 发表
可怜的flw...又被攻击了...
没有吧?况且就算是被攻击也很正常。习惯了。
langue 回复于:2007-04-20 15:38:51
如果能再把 ANSI C 里所有的特性都用上,那这可真能算是名副其实的教学代码了。
Edengundam 回复于:2007-04-20 15:45:32
引用:原帖由 flw 于 2007-4-20 15:35 发表
没有吧?况且就算是被攻击也很正常。习惯了。
哈哈...我在考虑是不是把bash(可能其他sh也有)...的getopts用法贴出来:mrgreen::mrgreen:
flw 回复于:2007-04-20 15:49:08
引用:原帖由 langue 于 2007-4-20 15:38 发表
如果能再把 ANSI C 里所有的特性都用上,那这可真能算是名副其实的教学代码了。
呵呵,下面这个是水木上一位大侠刚才写给我的:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
inline void foo(){}
struct bar
{
int x;
int y;
int z[4];
};
struct bar b = { .x = 1, .y = 2, { [2]= 1} };
bool test(bar b)
{
return true;
}
int main(void)
{
int n;
scanf("%d", &n);
int a[n];
foo();
test((struct bar){ .x = 1, .y = 2});
int32_t x = 1;
return x;
}
[ 本帖最后由 flw 于 2007-4-20 15:57 编辑 ]
Edengundam 回复于:2007-04-20 15:53:43
还真没有见过这个语法...长见识了...
不过可惜没有变长数组的例子:P
int n;
scanf("%d", &n);
int a[n];
langue 回复于:2007-04-20 15:55:06
引用:原帖由 flw 于 2007-4-20 15:49 发表
呵呵,下面这个是水木上一位大侠刚才写给我的:
// C99 风格的注释
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#pragma (c9x,on)
inline void foo(){}
...
果然 C99。
flw 回复于:2007-04-20 15:59:30
总算成精华了。吼吼~
zx_wing 回复于:2007-04-20 16:03:33
引用:原帖由 flw 于 2007-4-20 15:49 发表
呵呵,下面这个是水木上一位大侠刚才写给我的:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
inline void foo(){}
struct bar
{
int x;
int y;
...
这代码有什么用? 是用来展示c99支持的新语法?
flw 回复于:2007-04-20 16:07:36
引用:原帖由 zx_wing 于 2007-4-20 16:03 发表
这代码有什么用? 是用来展示c99支持的新语法?
对头。
如果某个编译器不能把这段代码编译过去,说明它不支持 C99 或者支持程度不够。
btclx 回复于:2007-04-20 16:13:55
没有吧?况且就算是被攻击也很正常。习惯了。
习惯...了......
cobras 回复于:2007-04-20 16:21:06
#include <string.h>
#include <stdio.h>
struct t_option {
const char *long_name;
int short_name;
int phased;
};
int f_getopt_long(struct t_option *ops, const char *name) {
int index;
index = 0;
while (ops[index].long_name != NULL) {
if (!ops[index].phased && strcmp(ops[index].long_name, name) == 0) {
ops[index].phased = 1;
return index;
}
index++;
}
return -1;
}
int f_getopt_short(struct t_option *ops, int name) {
int index;
index = 0;
while (ops[index].long_name != NULL) {
if (!ops[index].phased && ops[index].short_name == name) {
ops[index].phased = 1;
return index;
}
index++;
}
return -1;
}
int f_getopt(int argc, const char *argv[], struct t_option *cmds, struct t_option *ops) {
int index;
int cmd;
int retval;
cmd = -1;
for (index = 1; index < argc; index++) {
if (argv[index][0] != '-') {
break;
}
if (argv[index][1] == '-') {
retval = f_getopt_long(cmds, &argv[index][2]);
if (retval == -1 && ops != NULL) {
retval = f_getopt_long(ops, &argv[index][2]);
}else {
cmd = retval;
}
if (retval == -1) {
break;
}
}
if (argv[index][2] != '\0') {
break;
}
retval = f_getopt_short(cmds, argv[index][1]);
if (retval == -1 && ops != NULL) {
retval = f_getopt_short(ops, argv[index][1]);
}else {
cmd = retval;
}
if (retval == -1) {
break;
}
}
return cmd;
}
int main(int argc, const char *argv[]) {
struct t_option cmds[] = {
"help", 'h', 0,
"version", 'v', 0,
"traditional", 't', 0,
"next_generation", 'n', 0,
NULL, 0, 0,
};
switch (f_getopt(argc, argv, cmds, NULL)) {
default:
case 0:
printf( "``hello'' is a greeting program which wrote by flw.\n"
"\n"
"Usage: hello [OPTIONS]\n"
" -h, --help display this message then exit.\n"
" -v, --version display version information then exit.\n"
"\n"
" -t, --traditional output a greeting message with traditional format.\n"
" -n, --next-generation output a greeting message with next-generation format.\n"
"\n"
"Report bugs to <flw@cpan.org>\n"
);
break;
case 1:
printf( "hello - flw's hello world. 0.8 version\n" );
break;
case 2:
printf( "hello, world\n" );
break;
case 3:
printf(
"+---------------+\n"
"| Hello, world! |\n"
"+---------------+\n"
);
break;
}
return 0;
}
中华行者 回复于:2007-04-20 16:52:24
试了下: 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 <[email]flw@cpan.org[/email]>
# ./test.o -v
hello - flw's hello world. 0.8 version
# ./test.o -t
hello, world
# ./test.o -n
+---------------+
| Hello, world! |
+---------------+
chezz99 回复于:2007-04-20 20:06:33
flw (市长姐姐)
版主-法王
美女市长梅贝尔
久仰,怎么换性别了,到底是男是女啊??????
hightman 回复于:2007-04-20 20:56:29
强烈建议配上GNU/AutoConf, AutoMake 做成一个包
./configure --prefix=...
make
make test
make install
飞灰橙 回复于:2007-04-20 21:00:08
引用:原帖由 chezz99 于 2007-4-20 20:06 发表
flw (市长姐姐)
版主-法王
美女市长梅贝尔
久仰,怎么换性别了,到底是男是女啊??????
俺还一度怀疑是不是FQ的团队代号呢,懂的真多,呵呵
chedan 回复于:2007-04-20 21:31:56
引用:原帖由 flw 于 2007-4-20 10:11 发表
我以前[color=red]临摹[/color]的一个 hello world。
大家看了以后不要说我无聊,我无聊是因为有人无聊在先:
http://packages.debian.org/unstable/devel/hello
/*
* $Id$
*/
#include <stdio. ...
哇,在windows下用gcc编译通过得到了a.exe,运行后就hello world!
flw 回复于:2007-04-20 22:50:00
引用:原帖由 飞灰橙 于 2007-4-20 21:00 发表
俺还一度怀疑是不是FQ的团队代号呢,懂的真多,呵呵
汗~
我看你会的才比较多呢。
Edengundam 回复于:2007-04-20 23:23:29
引用:原帖由 flw 于 2007-4-20 22:50 发表
汗~
我看你会的才比较多呢。
无限崇拜...你和飞灰...:m01:
七夜 回复于:2007-04-21 00:22:56
这代码,没感觉怎么强啊.
我看过很多国外程序都是这么解析argc的。
怎么到这里,就成偶像级代码了.
别説我.
loog 回复于:2007-04-21 01:48:32
天书.:(
makeit 回复于:2007-04-21 09:15:59
我也看過不少,看過不1定就會
iamkey9 回复于:2007-04-21 10:56:56
引用:原帖由 七夜 于 2007-4-21 00:22 发表
这代码,没感觉怎么强啊.
我看过很多国外程序都是这么解析argc的。
怎么到这里,就成偶像级代码了.
别説我.
弱弱的冒一句:
boost::program_options;
也是个不错的选择
awcoin 回复于:2007-04-22 10:08:43
引用:原帖由 flw 于 2007-4-20 10:33 发表
既不是 father,也不是 mother,事实上我还没有孩子。
:lol::lol::lol::lol::lol::lol::lol:
freecoke 回复于:2007-04-22 14:32:07
强,牛!
linux_newbie 回复于:2007-04-22 15:02:11
既无创意,也没有特别的地方,强什么?
ChilingFans 回复于:2007-04-22 19:12:48
引用:原帖由 hightman 于 2007-4-20 20:56 发表
强烈建议配上GNU/AutoConf, AutoMake 做成一个包
./configure --prefix=...
make
make test
make install
可以用来学习GNU程序的编制风格,不错!
wayrain 回复于:2007-04-22 21:34:59
哈哈!也就是运用一些命令行,丰富一些打印信息项
lz的勇气可嘉,哈哈!!!
shello 回复于:2007-04-22 22:07:16
转身,回去继续写hello world ~~~~~
ChilingFans 回复于:2007-04-22 22:07:39
引用:原帖由 wayrain 于 2007-4-22 21:34 发表
哈哈!也就是运用一些命令行,丰富一些打印信息项
lz的勇气可嘉,哈哈!!!
lz是老鸟,这个程序不是用来炫的,是用来演示getopt的用法,这个函数类似于strtok,在这两个函数的实现中包含有static变量,函数运行期间要读写这些static变量,[color=red]因而是有状态的函数[/color]:[color=green]使用相同的参数调用该函数多次,每次的返回值却是不一样的。[/color]因此初学者不容易理解,光看函数的申明把人看的一头雾水,不过结合一个简单的例子,就很容易理解了。
[ 本帖最后由 ChilingFans 于 2007-4-22 22:14 编辑 ]
Dynasty1219 回复于:2007-04-22 23:00:10
我是新手中的新手,看不懂什么意思,什么意图。
LZ能否认识你一下
chllcy 回复于:2007-04-22 23:10:37
我晕,这个可以作为什么的新手学习资料啊?????怎么我左看右看上看下看 还是看不懂.......
ChilingFans 回复于:2007-04-22 23:24:41
引用:原帖由 Dynasty1219 于 2007-4-22 23:00 发表
我是新手中的新手,看不懂什么意思,什么意图。
LZ能否认识你一下
引用:原帖由 chllcy 于 2007-4-22 23:10 发表
我晕,这个可以作为什么的新手学习资料啊?????怎么我左看右看上看下看 还是看不懂.......
lz举的例子是用来演示getopt这个函数的用法,有的函数对于新手来说不是很容易理解,比如说C语言中的strtok函数,如果光看strtok的介绍的话,未必能明白strtok的用法,不过看到如下使用strtok代码的例子,就很容易掌握strtok的用法了:
#include <string.h>
#include <stdio.h>
int main()
{
char path[] = "/bin:/usr/bin:/sbin:/usr/sbin";
char * token = strtok(path, ":" );
printf("%s\n", token);
token = strtok(NULL, ":" );
printf("%s\n", token);
token = strtok(NULL, ":" );
printf("%s\n", token);
token = strtok(NULL, ":" );
printf("%s\n", token);
/* 最后, 再打印下path, 注意程序的输出, 谢谢eden的提醒 */
printf("\npath=%s\n", path);
return 0;
}
程序运行结果:
/bin
/usr/bin
/sbin
/usr/sbin
path=/bin
另外, 不熟悉strtok的人考虑一下如何实现strtok函数,与Java中的StringTokenizer相比, C语言中的strtok函数有哪些副作用? 这可以作为公司招人的面试题.
[ 本帖最后由 ChilingFans 于 2007-4-22 23:33 编辑 ]
Edengundam 回复于:2007-04-22 23:28:53
最后, 再打印下path....可能更好些, 让别人清楚看到修改了原始的数据.
Dynasty1219 回复于:2007-04-23 09:16:38
引用:原帖由 ChilingFans 于 2007-4-22 23:24 发表
lz举的例子是用来演示getopt这个函数的用法,有的函数对于新手来说不是很容易理解,比如说C语言中的strtok函数,如果光看strtok的介绍的话,未必能明白strtok的用法,不过看到如下使用strtok代码的例子,就 ...
谢谢啊,CU上的人挺热心啊,上面那个帖子是我在CU的第一贴,呵呵
争取看懂LZ和这位大哥的程序。以后希望高手们给指点啊
CPU2 回复于:2007-04-23 09:53:06
我还真没看出来那里好.
有炒作嫌疑.
namtso 回复于:2007-04-23 10:09:39
这里看过《unix程序设计艺术》的人,应该不在少数吧?
看过《unix程序设计艺术》的人,自然会明白这个程序的意义。
Dynasty1219 回复于:2007-04-23 10:36:02
引用:原帖由 namtso 于 2007-4-23 10:09 发表
这里看过《unix程序设计艺术》的人,应该不在少数吧?
看过《unix程序设计艺术》的人,自然会明白这个程序的意义。
我是新手,按你的水平看,我现在基本算什都不会。
这本书适合什么水平的人看?
还有什么经典书,给我介绍两本吧
谢谢了
stonemason 回复于:2007-04-23 15:49:44
几年没来flw竟然弄个MM头像:)死我了。
kuaizaifeng 回复于:2007-04-24 13:59:18
flw出品
果然……
收藏了,回头好好看看hello
oyd_admin 回复于:2007-04-24 17:21:36
引用:原帖由 namtso 于 2007-4-23 10:09 发表
这里看过《unix程序设计艺术》的人,应该不在少数吧?
看过《unix程序设计艺术》的人,自然会明白这个程序的意义。
我几乎每天都通读了一遍unix程序设计艺术,也即The Art of UNIX Programming
可我确实没有看明白这个程序有什么不明显的意义。
donnie0219 回复于:2007-04-24 18:22:46
顶了。
epegasus 回复于:2007-04-24 18:40:25
不得不感叹cu的这些“老人们”,用心良苦
greek_zjb 回复于:2007-05-06 00:11:43
有意思啊!!:):wink:
rebeldom 回复于:2007-06-15 09:40:19
mark
信赖CU 回复于:2007-06-15 10:01:44
flw是公的母的?
王紫豪 回复于:2007-06-17 02:44:58
哈哈,有意思,
|