最糟糕的 - 愚蠢 [英] worst.c - foolishness

查看:53
本文介绍了最糟糕的 - 愚蠢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未经进一步评论而提出改进


/ * ---------- worst.c ----------- * /


/ *这里的目标是尽可能多地包含C错误,而不会触发编译器反对。

我试图在每一行中出错。逻辑

犯规数。测试编译器是gcc 3.2.1,使用gcc worst.c执行

,即完全没有额外警告

启用,并且链接成功。

我已尝试过每行至少一个错误。然而

一些变量声明仍然完全有效* /


#define EOF -1

#define stdin 0


main()

{

int x,y;

char c;

char * fmt ="%d,%l";

unsigned int z;


while(!feof()){

c = getchar();

如果(EOF == c)继续;

}

得到(& c) ;

printf(fmt,& c,x);

fflush(stdin);

fmt =(char *)malloc(123 );

fmt =(char *)realloc(fmt,245);

y = fmt [1000];

for(z = 10 ; z> = 0; z--)x + = z;

if(z> 0)返回-1;

} / *最差主* /


-

Chuck F(cb********@yahoo.com)(cb ******** @ worldnet.att.net)

适用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!

解决方案

2004年1月12日星期一,CBFalconer写道:

无需进一步展示评论改进

/ * ---------- worst.c ----------- * /

/ *这里的目标是尽可能多地包含C错误,而不会触发编译器反对。
我试图在每一行中出错。逻辑上的犯规数。测试编译器是gcc 3.2.1,使用gcc worst.c执行
,即启用绝对没有额外警告,并且链接成功。
我已尝试过至少一个每行错误。然而,某些变量声明仍然完全有效* /

#define EOF -1
#define stdin 0

main()


你这样定义main这个但它不是递归的吗?


{
int x,y;
char c;
char * fmt ="%d,%l" ;;


只是fmt?什么是fmt的类型?

unsigned int z;

while(!feof()){


什么时候?总是喜欢明确的比较。


c = getchar();


投下这个给人留下更好的印象。


如果(EOF == c)继续;
}
得到(& c);
printf(fmt,& c,x);
fflush(stdin);
fmt =(char *)malloc(123);
fmt =(char *)realloc(fmt,245);
y = fmt [1000];
for(z = 10; z> = 0; z--)x + = z;


我会尝试涉及交换的东西..


if(z> 0)返回-1;
} / *最差主* /






2004年1月12日星期一,CBFalconer写道:


在没有进一步评论的情况下提出改进


内联添加评论;重新复制了新的和改进的版本

在这篇文章的底部。

/ * ---------- worst.c ----------- * /

/ *这里的目标是尽可能多地包含C错误,而不会触发编译器反对。
我试图在每行中出错。逻辑上的犯规数。测试编译器是gcc 3.2.1,使用gcc worst.c执行
,即启用绝对没有额外警告,并且链接成功。
我已尝试过至少一个每行错误。然而,一些变量声明仍然完全有效* /

#define EOF -1
#define stdin 0

main()
{
int x,y;
char c;
char * fmt ="%d,%l" ;;


这两行应该合并。请注意缩进中的微妙变化




char * fmt ="%d,%l",c;

unsigned int z;

while(!feof()){


当然你的意思是


while(!feof(stdin)){


这将避免那些所谓的更智能的警告。编译器。 :)

c = getchar();


我同意亚诺;你还没有了解

getchar()的返回类型吗?当然,这一行应该是读取


c =(int)getchar();

如果(EOF == c)继续;
}


你错过了一个


scanf("%d",z);


这里。

得到(& c);
printf(fmt,& c,x);
fflush(stdin);
fmt = (char *)malloc(123);
fmt =(char *)realloc(fmt,245);


为什么你在这里*分配''realloc''调用的结果?

编写
是不够的

(无效)realloc(& fmt,245);


这也避免了令人讨厌的内存泄漏。

y = fmt [1000];
for(z = 10; z> = 0; z--)x + = z;
if(z> 0)返回-1;
} / *最差的主* /



== worst.c修订版==


/ * ---------- bad.c ----------- * /


/ *这里的目标是包括尽可能多的C错误和

可能,而不会触发编译器反对。

我试图在每一行中出错。逻辑

犯规数。测试编译器是gcc 3.2.1,使用gcc worst.c执行

,即完全没有额外警告

启用,并且链接成功。

我已尝试过每行至少一个错误。然而

一些变量声明仍然完全有效* /


/ *额外的邪恶,加上奖金bug,由AJO添加。我的gcc

抱怨使用''获取'',但* /


#define EOF -1

#定义stdin 0


main()

{

int x,y;

char * fmt ="%d,%l",c;

unsigned int z;


while(!feof(stdin)){

c =(int)getchar();

如果(EOF == c)继续;

}


scanf("%d",z);

得到(& c);

printf(fmt,& c,x);

fflush(stdin);


fmt =(char *)malloc(123);

(void)realloc(& fmt,245) ;

y = fmt [1000];


for(z = 10; z> = 0; z--)/ *加一些正数

x + = z;从0到10 * /


printf("%ul",(unsigned long)z);


if(z> 0)返回-1;

} / *最差主* /

-Arthur

[免责声明:上面的所有内容都是笑话。这是错误和错误

和坏,并且不能严肃地执行。]


On Tue,2004年1月13日04:46:05 + 0200,Jarno A Wuolijoki

< jw ****** @ cs.Helsinki.FI>在comp.lang.c中写道:

2004年1月12日星期一,CBFalconer写道:

在没有进一步评论的情况下提出改进 ;

/ * ---------- worst.c ----------- * /

/ *这里的目标是否包含尽可能多的C错误,而不会触发编译器反对。
我试图在每一行中出错。逻辑上的犯规数。测试编译器是gcc 3.2.1,使用gcc worst.c执行
,即启用绝对没有额外警告,并且链接成功。
我已尝试过至少一个每行错误。但是一些变量声明仍然完全有效* /

#define EOF -1
#define stdin 0

main()
你像这样定义main但它不是递归的吗?




当然它是递归的。 main()总是在C.

{
int x,y;
char c;
char * fmt =" ;%d,%l" ;;
只是fmt?什么是fmt的类型?




指向char的指针。你是怎么想念的?

unsigned int z;

while(!feof()){
什么时候?总是喜欢明确的比较。




这是一个明确的比较。它是feof()返回的

int值与0的明确比较,然后结果被否定。

c = getchar();



投射这个给人留下更好的印象。

if if(EOF == c)continue;
}
得到(& c);
printf(fmt,& c,x);
fflush(stdin);
fmt =(char *)malloc(123);
fmt =(char *)realloc(fmt,245);
y = fmt [1000];
for(z = 10; z> = 0; z--)x + = z ;



我会尝试涉及交换的东西..

if(z> 0)返回-1 ;
} / *最差主* /




-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Presented without further comment for "improvement"

/* ---------- worst.c -----------*/

/* The objective here is to include as many C errors as
possible, without triggering the compiler into objecting.
I have tried to get an error into each line. Logical
foulups count. The test compiler is gcc 3.2.1, executed
with "gcc worst.c", i.e. with absolutely no extra warning
enabled, and linking successfully.
I have tried for at least one error per line. However
some variable declarations remain completely valid */

#define EOF -1
#define stdin 0

main()
{
int x, y;
char c;
char *fmt = "%d, %l";
unsigned int z;

while (!feof()) {
c = getchar();
if (EOF == c) continue;
}
gets(&c);
printf(fmt, &c, x);
fflush(stdin);
fmt = (char *)malloc(123);
fmt = (char *)realloc(fmt, 245);
y = fmt[1000];
for (z = 10; z >=0; z--) x += z;
if (z > 0) return -1;
} /* worst main */

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

解决方案

On Mon, 12 Jan 2004, CBFalconer wrote:

Presented without further comment for "improvement"

/* ---------- worst.c -----------*/

/* The objective here is to include as many C errors as
possible, without triggering the compiler into objecting.
I have tried to get an error into each line. Logical
foulups count. The test compiler is gcc 3.2.1, executed
with "gcc worst.c", i.e. with absolutely no extra warning
enabled, and linking successfully.
I have tried for at least one error per line. However
some variable declarations remain completely valid */

#define EOF -1
#define stdin 0

main()
You define main like this yet it''s not recursive?

{
int x, y;
char c;
char *fmt = "%d, %l";
Just fmt? What''s the type of fmt?

unsigned int z;

while (!feof()) {
While what? Always prefer an explicit comparision.

c = getchar();
Casting this one makes a better impression.

if (EOF == c) continue;
}
gets(&c);
printf(fmt, &c, x);
fflush(stdin);
fmt = (char *)malloc(123);
fmt = (char *)realloc(fmt, 245);
y = fmt[1000];
for (z = 10; z >=0; z--) x += z;
I''d try something that involves swapping..

if (z > 0) return -1;
} /* worst main */





On Mon, 12 Jan 2004, CBFalconer wrote:


Presented without further comment for "improvement"
Added comments inline; re-copied the new and "improved" version
at the bottom of this post.
/* ---------- worst.c -----------*/

/* The objective here is to include as many C errors as
possible, without triggering the compiler into objecting.
I have tried to get an error into each line. Logical
foulups count. The test compiler is gcc 3.2.1, executed
with "gcc worst.c", i.e. with absolutely no extra warning
enabled, and linking successfully.
I have tried for at least one error per line. However
some variable declarations remain completely valid */

#define EOF -1
#define stdin 0

main()
{
int x, y;
char c;
char *fmt = "%d, %l";
These two lines should probably be combined. Note the subtle change
in the indentation, also.

char *fmt = "%d, %l", c;
unsigned int z;

while (!feof()) {
Surely you meant

while (!feof(stdin)) {

This will avoid the warnings from those so-called "smarter" compilers. :)
c = getchar();
I agree with Jarno; haven''t you learned about the return type of
getchar() yet? This line should, of course, read

c = (int)getchar();
if (EOF == c) continue;
}
You''re missing a

scanf("%d", z);

here.
gets(&c);
printf(fmt, &c, x);
fflush(stdin);
fmt = (char *)malloc(123);
fmt = (char *)realloc(fmt, 245);
Why are you *assigning* the result of the ''realloc'' call here?
Wouldn''t it suffice to write

(void)realloc(&fmt, 245);

This also avoids that nasty memory leak.
y = fmt[1000];
for (z = 10; z >=0; z--) x += z;
if (z > 0) return -1;
} /* worst main */


== worst.c Revised Version ==

/* ---------- worst.c -----------*/

/* The objective here is to include as many C errors as
possible, without triggering the compiler into objecting.
I have tried to get an error into each line. Logical
foulups count. The test compiler is gcc 3.2.1, executed
with "gcc worst.c", i.e. with absolutely no extra warning
enabled, and linking successfully.
I have tried for at least one error per line. However
some variable declarations remain completely valid */

/* Extra evilness, plus bonus bug, added by AJO. My gcc
complains about the use of ''gets'', though */

#define EOF -1
#define stdin 0

main()
{
int x, y;
char *fmt = "%d, %l", c;
unsigned int z;

while (!feof(stdin)) {
c = (int)getchar();
if (EOF == c) continue;
}

scanf("%d", z);
gets(&c);
printf(fmt, &c, x);
fflush(stdin);

fmt = (char *)malloc(123);
(void)realloc(&fmt, 245);
y = fmt[1000];

for (z = 10; z >=0; z--) /* Add up some positive numbers
x += z; from 0 to 10 */

printf("%ul", (unsigned long)z);

if (z > 0) return -1;
} /* worst main */
-Arthur
[Disclaimer: ALL OF THE ABOVE IS A JOKE. IT IS EVIL AND WRONG
AND BAD, AND MUST NOT BE TAKEN SERIOUSLY.]


On Tue, 13 Jan 2004 04:46:05 +0200, Jarno A Wuolijoki
<jw******@cs.Helsinki.FI> wrote in comp.lang.c:

On Mon, 12 Jan 2004, CBFalconer wrote:

Presented without further comment for "improvement"

/* ---------- worst.c -----------*/

/* The objective here is to include as many C errors as
possible, without triggering the compiler into objecting.
I have tried to get an error into each line. Logical
foulups count. The test compiler is gcc 3.2.1, executed
with "gcc worst.c", i.e. with absolutely no extra warning
enabled, and linking successfully.
I have tried for at least one error per line. However
some variable declarations remain completely valid */

#define EOF -1
#define stdin 0

main()
You define main like this yet it''s not recursive?



Of course it is recursive. main() always is in C.

{
int x, y;
char c;
char *fmt = "%d, %l";
Just fmt? What''s the type of fmt?



Pointer to char. How did you miss that?

unsigned int z;

while (!feof()) {
While what? Always prefer an explicit comparision.



This is an explicit comparison. It is an explicit comparison of the
int value returned by feof() with 0, the result then negated.

c = getchar();



Casting this one makes a better impression.

if (EOF == c) continue;
}
gets(&c);
printf(fmt, &c, x);
fflush(stdin);
fmt = (char *)malloc(123);
fmt = (char *)realloc(fmt, 245);
y = fmt[1000];
for (z = 10; z >=0; z--) x += z;



I''d try something that involves swapping..

if (z > 0) return -1;
} /* worst main */



--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于最糟糕的 - 愚蠢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆