gcc编译器 [英] gcc compiler

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

问题描述

你好,


我是C的新手,现在我遇到了以下问题。

当我编译程序hulp.c(代码在下面)与命令

gcc -O -o hulp hulp.c,

运行程序输入1 2 3 4 1 2 3 4 1 2 3 4 1 2

给出输出

12341234123412

00041234123412.

我不明白怎么可能价值

macht [0],macht [1]和macht [2]不会保留1 2 3.


它变得更多对我来说很神秘,因为当我使用

gcc -o hulp hulp.c来编译东西时

使用与上面相同的输入运行程序现在给出了

预期产量:

12341234123412

12341234123412.


这怎么可能? -O在编译器

命令中有什么影响?在我阅读的手册中,这与优化有关,但

它在这里做了什么?


如果我替换''char''的类型''inleeslg''到''int'',输入相同的

输入如上所述两个编译器给出预期输出:

12341234123412

12341234123412.

如何解释这个?


我真的希望有人可以帮助我...


下面可以找到代码。

文件的第一行facortest是2 1 1 2 3 4 5 6 7 8 9 10 11

0。


提前谢谢,


An

************************************** ************ *******

#include< stdio.h>


main()

{FILE * fp;

short i;

char inleeslg;

char macht [14];

长pbas;

char e;


fp = fopen(" Factortest"," r");

if((fp = fopen(" Factortest"," r"))== NULL)printf(" Kan datafile niet

openen \ n");

else {

printf(" Type 14 small numbers:\ n");

for(i = 0; i< 14 ; i ++)

{scanf("%d",& macht [i]);

}

/ *打印输入* /

for(i = 0; i< 14; i ++)

{printf("%d",macht [i]);

}

printf(" \ n");


fscanf(fp,"%d%d%d",&的PBA,急症,&安培; inleeslg); <无线电通信/>

/ *再次打印输入* /

for(i = 0; i< 14; i ++)

{printf(" ;%d",macht [i]);

}

printf(" \ n");

}

fclose(fp);

}

*********************** *************************** ****************

解决方案



2004年7月9日星期五,An写道:


我是新来的C,现在我遇到了以下问题。
当我用命令编译程序hulp.c(代码如下)时使用命令
gcc -O -o hulp hulp.c,
运行带输入的程序1 2 3 4 1 2 3 4 1 2 3 4 1 2
给出输出
12341234123412
00041234123412。
我不明白怎么可能值为
macht [0],macht [1]和macht [2]不能保留1 2 3.


你用零覆盖它们,那是'这就是原因。见下文。


< snip>这怎么可能? -O在编译器
命令中有什么影响?在我阅读的手册中,这与优化有关,但
它在这里做了什么?


优化。由于您的代码开头是错误的,* any *

优化是正确的。所以它做了一些优化,改变了你的程序从错误到错误的行为 - 不是问题!

编写正确的代码,突然优化器有很多较少的回旋余地。 :)


#include< stdio.h>

main()


int main(void)是首选,并将在C99​​(最近的

标准化)工作。

{FILE * fp;
短i;
char inleeslg;
char macht [14];
long pbas;
char e;

fp = fopen(" Factortest"," r");
if((fp = fopen(Factortest,r))== NULL)printf(Kan datafile niet
openen \ n);


双呐!首先,打开Factortest。文件*两次*在这里;

这是不明智的,虽然我不认为这是一个错误,严格来说。

第二个问题是你的字符串文字超出其行。字符串
C中的
不能跨越线条。如有必要,可以使用较窄的C代码边距。


if(fp == NULL)printf(" Kan datafile niet openen!\ n");


else {
printf(" Type 14 small numbers:\ n");
for(i = 0; i< 14; i ++)
{scanf( "%d",&安培;马赫特[I]);


这是一个错误。 ''macht''是一个char数组,而不是int。 "%d"是int的

格式说明符。你的意思是


{

int tmp;

scanf("%d",& tmp);

macht [i] = tmp; / *带有可选的错误检查* /

}
/ *打印输入* /
(i = 0; i< 14; i ++)
{ printf("%d",macht [i]);
}
printf(" \ n");

fscanf(fp,"%d%) d%d,& pbas,& e,& inleeslg);


这是另一个错误。 ''pbas''是一个很长的int。 e和inleeslg是

chars。它们都不是整数,这就是fscanf(%d)所期望的。

''long int''的正确格式说明符是%ld;两个字符

可以使用我上面给你的临时变量解决方案读取。

/ *再次打印输入* /
for(i = 0; i< 14; i ++)
{printf("%d",macht [i]);


请注意,这不是*一个bug,因为''macht [i]''是一个char,

它作为一个int传递''printf''。但是,学习什么时候

安全依赖整数促销是一项棘手的工作。

}
printf(" \ n");
}
fclose(fp);


最后但并非最不重要的,


返回0;

}



一个< an ********* @ student.kuleuven.ac.be>写道:

我是C的新手,现在我遇到了以下问题。
当我用命令编译程序hulp.c(代码如下)时
gcc -O -o hulp hulp.c,
运行程序输入1 2 3 4 1 2 3 4 1 2 3 4 1 2
给出输出
12341234123412
00041234123412。
我不明白为什么有可能将机器[0],机器[1]和机器[2]的值保持不变1 2 3.
It对我来说更加神秘,因为当我用gcc -o hulp hulp.c编译东西时
使用上面相同的输入运行程序现在给出了
预期的输出:
12341234123412
12341234123412.
这怎么可能? -O在编译器
命令中有什么影响?在我阅读的手册中,这与优化有关,但
它在这里做了什么?
char inleeslg;
char macht [14];
long pbas;
char e;


但后来你有了
fscanf(fp,"%d%d%d"& pbas,& e,& inleeslg) ;


你承诺fscanf()函数,所有''pbas'','e'和''inlesslg''

是整数,但它们不是'' T。但是fscanf()并不知道这一点,并且在某些情况下,它可能会写入其他一些

变量的内存。它在手册中有详细说明,但标题不同于您可能正在寻找的标题。你调用所谓的未定义的

行为然后所有的赌注都关闭了。你的程序似乎可以工作,它可以运行
但是会给出错误的结果,它可能会崩溃甚至重新格式化你的
硬盘(幸运的是,这也不会发生)现在经常出现;-)。通过

玩优化选项产生的可执行文件

会略有不同,你会看到上面提到的两个结果:
:在一个案例中程序似乎工作,在另一个案例中它也运行

但是给你一些奇怪的输出。

如果我替换类型''char''' 'inleeslg''到''int'',输入与上面相同的输入两个编译器给出预期输出:
12341234123412
12341234123412.
如何解释?


一旦你也制作了''pbas''和''e''整数(或更正格式

字符串,你传递给fscanf())你的程序不再调用未定义的

行为,它将运行得非常好并且永远和一天给出正确的

结果。

#include< ; stdio.h中>
main()


制作


int main()





int main(无效)


否则一旦开始使用
$ b就会遇到麻烦$ b C99兼容编译器。

printf(Type 14 small numbers:\ n);
for(i = 0; i< 14; i ++)
{ scanf("%d"& macht [i]);




使用scanf()读入用户输入基本上不可能获得

正确(只是让用户输入错误,程序将会爆炸)。更好的查找,例如fgets(),读完整行并

然后将它拆解成你想要的部分。


BTW,白色空间变得相当cheep所以你被允许

使用更多它来代码缩进,人们

不得不阅读它会很感激...


问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


< blockquote>在''comp.lang.c''中, ********* @ student.kuleuven.ac.be (An)写道:

当我用命令编译程序hulp.c(代码如下)时
gcc -O -o hulp hulp.c,
运行程序输入1 2 3 4 1 2 3 4 1 2 3 4 1 2 br />给出输出
12341234123412
00041234123412。
我不明白
macht [0],macht [1]和macht [2]的值是多少? ]不要留下1 2 3.

这对我来说更加神秘,因为当我使用
gcc -o hulp hulp.c编译东西时运行程序与上面相同的输入现在给出了
预期输出:
12341234123412
12341234123412.


听起来有一个未定义的行为(UB)你的代码。

这怎么可能? -O在编译器
命令中有什么影响?在我阅读的手册中,这与优化有关,但它在这里做了什么?

如果我将''inleeslg''的类型''char''替换为'' int'',使用与上面相同的
输入两个编译器给出预期输出:
12341234123412
12341234123412.
如何解释?


典型的UB。任何事情都可能发生。

我真的希望有人可以帮助我...

下面可以找到代码。
文件的第一行" ; facortest"是2 1 1 2 3 4 5 6 7 8 9 10 11
0。

********************** **************************** *******
#include< stdio.h>

main()
{FILE * fp;
简短的我;
char inleeslg;
char macht [14];
long pbas;
char e;

fp = fopen(" Factortest"," r");
if((fp = fopen(" Factortest"," r"))) == NULL)printf(" Kan datafile niet
openen \ n");


停止! UB。你不能两次打开文件。其余的代码注定要失败。

else {
printf(" Type 14 small numbers:\ n");
for(i = 0; i< 14 ; i ++)
{scanf("%d",& macht [i]);


UB:"%d"期望int的地址。你提供了一个字母的地址。

}
/ *打印输入* /
为(i = 0; i< 14; i ++)
{printf ("%d",macht [i]);
}
printf(" \ n");

fscanf(fp,"%d%d %d",&安培;的PBA,急症,&安培; inleeslg);


UB:"%d"期望int的地址。你提供了一个char的地址,

一个int和一个long。

/ *再次打印输入* /
for(i = 0; i< 14; i ++)
{printf("%d",macht [i]);
}
printf(" \ n");
} fclose(fp);
}
*********************************** *************** ****************




-

-ed-在这里收到我的电子邮件: http://marreduspam.com/ad672570

C语言常见问题解答: http://www.eskimo.com/~scs/C-faq/top.html

C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99

FAQ de fclc: http: //www.isty-info.uvsq.fr/~rumeau/fc lc /


Hello,

I''m new to C and now I encountered the following problem.
When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don''t understand how it is possible that the value of
macht[0],macht[1]and macht[2] don''t remain 1 2 3.

It gets even more mysterious to me, since when I compile things using
gcc -o hulp hulp.c
running the program with the same input as above now gives the
expected output:
12341234123412
12341234123412.

How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here?

If I replace the type ''char'' of ''inleeslg'' to ''int'', with the same
input as above BOTH compilers give the expected output:
12341234123412
12341234123412.
How can this be explained?

I really hope someone can help me out...

Here below one can find the code.
The first line of the file "facortest" is 2 1 1 2 3 4 5 6 7 8 9 10 11
0 .

Thank you in advance,

An
************************************************** *******
#include <stdio.h>

main()
{FILE *fp;
short i;
char inleeslg;
char macht[14];
long pbas;
char e;

fp=fopen("Factortest","r");
if ((fp=fopen("Factortest","r"))==NULL) printf("Kan datafile niet
openen \n");
else{
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht[i]);
}
/*Printing the input */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
}
printf("\n");

fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);

/*Printing the input again */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
}
printf("\n");
}
fclose(fp);
}
************************************************** ****************

解决方案


On Fri, 9 Jul 2004, An wrote:


I''m new to C and now I encountered the following problem.
When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don''t understand how it is possible that the value of
macht[0],macht[1]and macht[2] don''t remain 1 2 3.
You are overwriting them with zeroes, that''s why. See below.

<snip> How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here?
It optimizes. Since your code was wrong to begin with, *any*
optimization is correct. So it makes some optimizations that change
the behavior of your program from wrong to wrong --- not a problem!
Write correct code, and suddenly the optimizer has a lot less leeway. :)

#include <stdio.h>

main()
int main(void) is preferred, and will work in C99 (the most recent
standardization).
{FILE *fp;
short i;
char inleeslg;
char macht[14];
long pbas;
char e;

fp=fopen("Factortest","r");
if ((fp=fopen("Factortest","r"))==NULL) printf("Kan datafile niet
openen \n");
Double whoops! First, you open the "Factortest" file *twice* here;
that''s unwise, although I don''t think it''s a bug, strictly speaking.
The second problem is that your string literal overruns its line. Strings
in C can''t span lines. Use a narrower margin for C code if necessary.

if (fp == NULL) printf("Kan datafile niet openen!\n");

else{
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht[i]);
Here is a bug. ''macht'' is an array of char, not int. "%d" is the
format specifier for int. You mean something like

{
int tmp;
scanf("%d", &tmp);
macht[i] = tmp; /* with optional error-checking */
}
/*Printing the input */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
}
printf("\n");

fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);
Here is another bug. ''pbas'' is a long int. ''e'' and ''inleeslg'' are
chars. None of them are ints, which is what fscanf("%d") is expecting.
The correct format specifier for ''long int'' is "%ld"; the two chars
can be read in using the temporary-variable solution I gave you above.
/*Printing the input again */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
Note that this is *not* a bug, since while ''macht[i]'' is a char,
it is passed as an int to ''printf''. However, learning when it is
safe to rely on the integer promotions is a tricky business.
}
printf("\n");
}
fclose(fp);
And last but not least,

return 0;
}



An <an*********@student.kuleuven.ac.be> wrote:

I''m new to C and now I encountered the following problem.
When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don''t understand how it is possible that the value of
macht[0],macht[1]and macht[2] don''t remain 1 2 3. It gets even more mysterious to me, since when I compile things using
gcc -o hulp hulp.c
running the program with the same input as above now gives the
expected output:
12341234123412
12341234123412. How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here? char inleeslg;
char macht[14];
long pbas;
char e;
but later you have
fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);
You are promising the fscanf() function that all ''pbas'', ''e and ''inlesslg''
are integers but they aren''t. But fscanf() doesn''t know that and, under
certain circumstances, it probably writes over the memory of some other
variable. It''s spelled out in the "manual", but under a different heading
than you probably were looking for. You invoke so-called "undefined
behaviour" and then all bets are off. Your program can seem to work, it
can run but give wrong results, it can just crash or even reformat your
harddisk (luckily, that doesn''t happen too often nowadays anymore;-). By
playing around wit the optimization options the resulting executables
will be slightly different and you see two of the above mentioned conse-
quences: in one case the program seems to work, in the other it also runs
but gives you some strange output.
If I replace the type ''char'' of ''inleeslg'' to ''int'', with the same
input as above BOTH compilers give the expected output:
12341234123412
12341234123412.
How can this be explained?
Once you also have made ''pbas'' and ''e'' ints (or corrected the format
string you pass to fscanf()) your program does not invoke undefined
behaviour anymore and it will run perfectly well and give correct
results forever and a day.
#include <stdio.h> main()
Make that

int main()

or

int main( void )

otherwise you''re going to get into trouble once you start using a
C99 compliant compiler.
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht[i]);



Using scanf() to read in user input is basically impossible to get
right (just have the user make a typing error and the program will
blow up). Better look up e.g. fgets(), read in the whole line and
then disassemble it into the pieces you''re looking for.

BTW, white space has become rather cheep so you are allowed to
use lots more of it for the indentation of your code, people
having to read it will be grateful...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


In ''comp.lang.c'', an*********@student.kuleuven.ac.be (An) wrote:

When I compile the program hulp.c (the code is below) with the command
gcc -O -o hulp hulp.c,
running the program with input 1 2 3 4 1 2 3 4 1 2 3 4 1 2
gives output
12341234123412
00041234123412.
I don''t understand how it is possible that the value of
macht[0],macht[1]and macht[2] don''t remain 1 2 3.

It gets even more mysterious to me, since when I compile things using
gcc -o hulp hulp.c
running the program with the same input as above now gives the
expected output:
12341234123412
12341234123412.
Sounds like there is an Undefined Behaviour (UB) in your code.
How is this possible? What is the influence of the -O in the compiler
command? In the manual I read this has to do with optimization, but
what does it do here?

If I replace the type ''char'' of ''inleeslg'' to ''int'', with the same
input as above BOTH compilers give the expected output:
12341234123412
12341234123412.
How can this be explained?
Typical UB. Anything can happen.
I really hope someone can help me out...

Here below one can find the code.
The first line of the file "facortest" is 2 1 1 2 3 4 5 6 7 8 9 10 11
0 .

************************************************** *******
#include <stdio.h>

main()
{FILE *fp;
short i;
char inleeslg;
char macht[14];
long pbas;
char e;

fp=fopen("Factortest","r");
if ((fp=fopen("Factortest","r"))==NULL) printf("Kan datafile niet
openen \n");
Stop! UB. You can''t open a file twice. The rest of the code is doomed.
else{
printf("Type 14 small numbers:\n");
for(i=0;i<14;i++)
{scanf("%d",&macht[i]);
UB : "%d" expects the address of an int. You provided the address of a char.
}
/*Printing the input */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
}
printf("\n");

fscanf(fp,"%d %d %d",&pbas,&e,&inleeslg);
UB : "%d" expects the address of an int. You provided the address of a char,
an int and a long.
/*Printing the input again */
for(i=0;i<14;i++)
{printf("%d",macht[i]);
}
printf("\n");
}
fclose(fp);
}
************************************************** ****************



--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


这篇关于gcc编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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