473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

without declare parameter [double square(parameter)] return 0 in main

#include <stdio.h>
double square(); /*without declare

main()
{
double s;
s = square(2);
printf("%g\n", s);
}

double square(double x)
{
return x*x;
}

========result=========
0
=====================
Sep 30 '08 #1
5 3109
In article <94**********************************@k30g2000hse. googlegroups.com>,
Wa********@gmail.com <Wa********@gmail.comwrote:
>double square(); /*without declare
> s = square(2);
>double square(double x)
Unless you have a prototype in scope (rather than just a declaration),
the compiler does not know the type of the argument, so you must
give it an argument of the right type. (More precisely, of a type
that will be promoted to the right type, so in this case a float
would be ok.)

I would have thought a compiler could spot this kind of error: the
function call is ok given the declaration, but when it reaches the
function definition, the compiler can see that it has been called as
if the argument type were int. But gcc doesn't complain. I suggest
using compiler options to warn about all non-prototype function
declarations. (For gcc, that's -Wstrict-prototypes.)

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Sep 30 '08 #2
Wa********@gmail.com wrote:
#include <stdio.h>
double square(); /*without declare
Your comment got truncated; I'm not sure exactly what you're saying.
main()
You appear to be using a C90 compiler; implicit int has been removed in
C99. You should make that "int main(void)", even though this isn't
necessary in C90.
{
double s;
s = square(2);
In C99, with no declaration of square() in scope, that's a constraint
violation.

In C90, with no declaration in scope, using square() in this fashion
would cause square() to be treated as if it takes one integer argument,
and returns an int.
printf("%g\n", s);
}

double square(double x)
In reality, you have defined square() as taking one double argument and
returning a double. Because of the mismatch, the behavior of your
program is undefined. That is the single worst thing the C standard can
say about any program. It means that your program could do anything. It
could print a message saying "This was a mistake". It could print
3.14159. Realistically, there are many compilers where it might produce
a memory violation when square attempts to read a double variable from a
location that only has enough room for an int, or when it attempts to
write a double variable into a location that only has enough room for an
an it.
{
return x*x;
}

========result=========
0
=====================
That is also legal behavior for such a program.

Moral: never rely upon implicit int. That's why implicit int was dropped
in C99. Always use function prototypes, if possible.
Sep 30 '08 #3
James Kuyper <ja*********@verizon.netwrites:
Wa********@gmail.com wrote:
>#include <stdio.h>
double square(); /*without declare

Your comment got truncated; I'm not sure exactly what you're saying.
>main()

You appear to be using a C90 compiler; implicit int has been removed
in C99. You should make that "int main(void)", even though this isn't
necessary in C90.
>{
double s;
s = square(2);

In C99, with no declaration of square() in scope, that's a constraint
violation.

In C90, with no declaration in scope, using square() in this fashion
would cause square() to be treated as if it takes one integer
argument, and returns an int.
> printf("%g\n", s);
}

double square(double x)

In reality, you have defined square() as taking one double argument
and returning a double. Because of the mismatch, the behavior of your
program is undefined. That is the single worst thing the C standard
can say about any program. It means that your program could do
anything. It could print a message saying "This was a mistake". It
could print 3.14159. Realistically, there are many compilers where it
might produce a memory violation when square attempts to read a double
variable from a location that only has enough room for an int, or when
it attempts to write a double variable into a location that only has
enough room for an an it.
>{
return x*x;
}

========result=========
0
=====================

That is also legal behavior for such a program.

Moral: never rely upon implicit int. That's why implicit int was
dropped in C99. Always use function prototypes, if possible.
Good advice, but I think a clarification (to the OP) might help. It
reads as though the use of implicit ints are in some way connected to
the OP's problem when they are not.

By writing double square(); the OP has provided a declaration (but not
a prototype) for the square function so both C90 and C99 will be happy
provided the call is valid. When a call is made to a function without
a prototype, the arguments are converted by a fiddly set of rules
called the default argument promotions and it is this (rather than
implicit int) that is being incorrectly relied on. Had the OP
written:

s = square(2.0);

then all would have been well.

But, as I say, your advice is sound: always use prototype, if possible.

--
Ben.
Sep 30 '08 #4
Ben Bacarisse wrote:
James Kuyper <ja*********@verizon.netwrites:
>Wa********@gmail.com wrote:
>>#include <stdio.h>
double square(); /*without declare
Your comment got truncated; I'm not sure exactly what you're saying.
....
Good advice, but I think a clarification (to the OP) might help. It
reads as though the use of implicit ints are in some way connected to
the OP's problem when they are not.
My apologies. The OP made the mistake of asking his question in the
subject line, without repeating it in the body of his message. My news
reader truncated the long subject line about halfway through. I glanced
at the truncated subject line, but didn't pay much attention, and
mistakenly thought he was asking why the program worked incorrectly if
the declaration were commented out. I didn't realize that he was asking
only about why it misbehaved when the declaration was not missing, but
only lacked a parameter.

The example code contains a broken comment that might have corrected my
misunderstanding, if it had been complete.
Sep 30 '08 #5
On Sep 30, 8:24*pm, James Kuyper <jameskuy...@verizon.netwrote:
Ben Bacarisse wrote:
James Kuyper <jameskuy...@verizon.netwrites:
WanHong...@gmail.com wrote:
#include <stdio.h>
double square(); * /*withoutdeclare
Your comment got truncated; I'm not sure exactly what you're saying.
...
Good advice, but I think a clarification (to the OP) might help. *It
reads as though the use of implicit ints are in some way connected to
the OP's problem when they are not.

My apologies. The OP made the mistake of asking his question in the
subject line,withoutrepeating it in the body of his message. My news
reader truncated the long subject line about halfway through. I glanced
at the truncated subject line, but didn't pay much attention, and
mistakenly thought he was asking why the program worked incorrectly if
the declaration were commented out. I didn't realize that he was asking
only about why it misbehaved when the declaration was not missing, but
only lacked a parameter.

The example code contains a broken comment that might have corrected my
misunderstanding, if it had been complete.
Thanks, i slove the problem.
Oct 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Guruz | last post by:
hi C gurus do anyone of u know how to write a program in C without main and still create a executable out of it. Remember, I said no main() function not in -->include files -->libraries -->no...
16
by: leeaby | last post by:
Hi, I will appreciate your assistance. Can we write a c code which do not contain main() I have heard that this is possible. Is it really possible? Thanks for your help in advance lee
6
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
4
by: koolbijay | last post by:
hello plz can u help me in providing the source code for the following programmes 1.c programme without main() function 2.how to get size of a datatype without using sizeof operator
7
by: AshokG | last post by:
Hi, If you use just throw without parameter should preserve the complete stack trace and the exception information. for example: 1. private void Bar() 2. { 3. try
17
by: mariz | last post by:
hi , i m a new member to this group . i start from the beginig - main(). Can any one give me an idea for how to write a program in C without using main() ? ie while looking source code it wont...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.