why a simple program can't produce a correct output?

hi,

I have a programe test.c like below:

#include <stdio.h>

void main()
{
int a;

scanf( “%d”, a );
printf("%d",a);

}

Then i use the command
cc test.c

then i execute the a.out and input 1 ,but the system give a message such as
:

a.out terminated(SIGSEGV) at :
%1 memory fault a.out

Please help me ,thanks!

ycao <ycao@mail.ipp.ac.cn> wrote in article <a6sk0l$u8$1@inn.qnx.com>…

hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );

Try
scanf("%d", &a);

Read documentation careful. Best regards. Eduard.

I’m sorry! It’s my mistake!

But the value output is so strange,not the value i input!

For example ,if i input is 1,2,3…
The output are all 40584 :frowning:

“ed1k” <ed1k@yahoo.com> wrote in message
news:01c1cc14$bf1c4e00$106fa8c0@ED1K…

ycao <> ycao@mail.ipp.ac.cn> > wrote in article <a6sk0l$u8$> 1@inn.qnx.com> >…
hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );

Try
scanf("%d", &a);

Read documentation careful. Best regards. Eduard.

This program should work as you expect, no?

#include <stdio.h>

void main()
{
int a;

scanf( “%d”, &a );
printf("%d",a);

}

You had passed uninitialized ‘a’ value to scanf(), instead of address of variable ‘a’. So, scanf()
tried to write into wrong memory place and OS SIGSEGVed the your program. But if you pass address
of ‘a’ variable to scanf() (use ampersand to resolve address), the scanf() should correct fill in
the ‘a’ variable. So, if you meant the program (first version) was not SIGSEGVed for some reason
(can imagine it under DOS, but hardly under QNX), your output does not depend from input, because
uninitialized ‘a’ is still uninitialized ‘a’ :wink:
Is your problem solved? BTW, it only scanf() needs address of variable, don’t use ampersand in
printf(), unless you want to print out the address.
Regards,
Eduard.


ycao <ycao@mail.ipp.ac.cn> wrote in article <a6smn5$2nd$1@inn.qnx.com>…

I’m sorry! It’s my mistake!

But the value output is so strange,not the value i input!

For example ,if i input is 1,2,3…
The output are all 40584 > :frowning:

Mario Charest <goto@nothingness.com> wrote in article <a6sp01$4f2$1@inn.qnx.com>…
<…>

By the way main should always be declared as returning an int.

Is it demand of QNX? C doesn’t require the main() to be such spiteful :wink:

“ycao” <ycao@mail.ipp.ac.cn> wrote in message
news:a6smn5$2nd$1@inn.qnx.com

I’m sorry! It’s my mistake!

But the value output is so strange,not the value i input!

For example ,if i input is 1,2,3…
The output are all 40584 > :frowning:

You probably made another mistake, post the source code again.
By the way main should always be declared as returning an int.

“ed1k” <ed1k@yahoo.com> wrote in message
news:01c1cc1e$b22c08c0$106fa8c0@ED1K…

Mario Charest <> goto@nothingness.com> > wrote in article
a6sp01$4f2$> 1@inn.qnx.com> >…

By the way main should always be declared as returning an int.

Is it demand of QNX? C doesn’t require the main() to be such spiteful > :wink:

ANSI C demands afair.

// wbr

Yeah… You’re right :wink: Just now I looked throught ISO draft… Old good books say nothing on
this, obviously.
Eduard.

Ian Zagorskih <ianzag@mail.ru> wrote in article <a6spvr$537$1@inn.qnx.com>…

“ed1k” <> ed1k@yahoo.com> > wrote in message
news:01c1cc1e$b22c08c0$106fa8c0@ED1K…
Mario Charest <> goto@nothingness.com> > wrote in article
a6sp01$4f2$> 1@inn.qnx.com> >…

By the way main should always be declared as returning an int.

Is it demand of QNX? C doesn’t require the main() to be such spiteful > :wink:

ANSI C demands afair.

// wbr
\

BTW, a programmed called test not producing the right output
under QNX4 is an old gotcha, as there is a test builtin command.
Best to call the final program something else.
In this case it wasn’t the problem, but be aware…

On Fri, 15 Mar 2002 19:01:14 +0800, “ycao” <ycao@mail.ipp.ac.cn> wrote:

hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );
printf("%d",a);

}

Then i use the command
cc test.c

then i execute the a.out and input 1 ,but the system give a message such as
:

a.out terminated(SIGSEGV) at :
%1 memory fault a.out

Please help me ,thanks!


\

Alex Cellarius wrote:

BTW, a programmed called test not producing the right output
under QNX4 is an old gotcha, as there is a test builtin command.
Best to call the final program something else.
In this case it wasn’t the problem, but be aware…

This is not particular to QNX, it is a feature of the shell. Almost all Unix
shells have a built-in called “test”.

On Fri, 15 Mar 2002 19:01:14 +0800, “ycao” <> ycao@mail.ipp.ac.cn> > wrote:
hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );
printf("%d",a);

}

Then i use the command
cc test.c

then i execute the a.out and input 1 ,but the system give a message such as
:

a.out terminated(SIGSEGV) at :
%1 memory fault a.out

Please help me ,thanks!


\

He said he executed the program as a.out.

As someone said, you should probibly repost the source code. This is too
simple not to work. Although I have always found scanf() to be about the
crappiest function undcer ANY OS.


Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net


“Dean Douthat” <ddouthat@faac.com> wrote in message
news:3C96430C.B4D29730@faac.com

Alex Cellarius wrote:

BTW, a programmed called test not producing the right output
under QNX4 is an old gotcha, as there is a test builtin command.
Best to call the final program something else.
In this case it wasn’t the problem, but be aware…

This is not particular to QNX, it is a feature of the shell. Almost all
Unix
shells have a built-in called “test”.



On Fri, 15 Mar 2002 19:01:14 +0800, “ycao” <> ycao@mail.ipp.ac.cn> > wrote:
hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );
printf("%d",a);

}

Then i use the command
cc test.c

then i execute the a.out and input 1 ,but the system give a message
such as
:

a.out terminated(SIGSEGV) at :
%1 memory fault a.out

Please help me ,thanks!



\

Um… don’t you need to link?

ycao wrote:

hi,

I have a programe test.c like below:

#include <stdio.h

void main()
{
int a;

scanf( “%d”, a );
printf("%d",a);

}

Then i use the command
cc test.c

then i execute the a.out and input 1 ,but the system give a message such as
:

a.out terminated(SIGSEGV) at :
%1 memory fault a.out

Please help me ,thanks!

“Keith Vasilakes” <kvasilak@cyberoptics.com> wrote in message
news:3C989AE5.9BF8D488@cyberoptics.com

Um… don’t you need to link?


Then i use the command
cc test.c

No cc will take care of calling the compiler and the linker. In this case
because -o (output) isn’t specify cc will create a.out.

  • Mario