can u tell this will accept or not?

V

venkatesh

hai
i am declaring an function as
like
void convert(char a);//function prototype


then
when i am calling the function like
int i=20;
convert(i);

this will work or produce errors please specify ?
thanking you!!
 
R

Ravi Uday

There is no problem with the one below and will work as long as 'i' is
initialized with a value less than CHAR_MAX defined in <limits.h>.
But why would you want to do that ?

- Ravi
 
N

Netocrat

hai
i am declaring an function as
like
void convert(char a);//function prototype

then
when i am calling the function like
int i=20;
convert(i);

this will work or produce errors please specify ?
thanking you!!
There is no problem with the one [above] and will work as long as 'i' is
initialized with a value less than CHAR_MAX defined in <limits.h>. But
why would you want to do that ?[/QUOTE]

According to the standard it's undefined behaviour.

It may happen to behave as you describe on some systems (especially
little-endian ones such as Intel/AMD) but highly likely it will never
behave as you expect on big-endian machines. In any case it's not
portable and should be avoided.
 
J

Jordan Abel

hai
i am declaring an function as
like
void convert(char a);//function prototype

then
when i am calling the function like
int i=20;
convert(i);

this will work or produce errors please specify ?
thanking you!!
There is no problem with the one [above] and will work as long as 'i' is
initialized with a value less than CHAR_MAX defined in <limits.h>. But
why would you want to do that ?

According to the standard it's undefined behaviour.

It may happen to behave as you describe on some systems (especially
little-endian ones such as Intel/AMD) but highly likely it will never
behave as you expect on big-endian machines. In any case it's not
portable and should be avoided.[/QUOTE]

You would be correct, if not for the fact that there is a prototype
in scope. The argument is converted to a char and passed to the
function, exactly as expected.
 
N

Netocrat

On Wed, 26 Oct 2005 17:11:44 +0530, Ravi Uday wrote:
[quotes re-ordered]
venkatesh wrote:

hai
i am declaring an function as
like
void convert(char a);//function prototype

then
when i am calling the function like
int i=20;
convert(i);

this will work or produce errors please specify ?
thanking you!!

There is no problem with the one [above] and will work as long as 'i' is
initialized with a value less than CHAR_MAX defined in <limits.h>. But
why would you want to do that ?

According to the standard it's undefined behaviour.

It may happen to behave as you describe on some systems (especially
little-endian ones such as Intel/AMD) but highly likely it will never
behave as you expect on big-endian machines. In any case it's not
portable and should be avoided.

You would be correct, if not for the fact that there is a prototype
in scope. The argument is converted to a char and passed to the
function, exactly as expected.

Right, I forgot about the prototype.
 
R

Rich Gibbs

Netocrat said the following, on 10/26/05 09:31:
On Wed, 26 Oct 2005 17:11:44 +0530, Ravi Uday wrote:
[quotes re-ordered]
venkatesh wrote:

hai
i am declaring an function as
like
void convert(char a);//function prototype

then
when i am calling the function like
int i=20;
convert(i);

this will work or produce errors please specify ?
thanking you!!

There is no problem with the one [above] and will work as long as 'i' is
initialized with a value less than CHAR_MAX defined in <limits.h>. But
why would you want to do that ?


According to the standard it's undefined behaviour.

What part of it do you think produces undefined behavior? When the
'convert' function is called, 'i' is converted to a char, according to
the function prototype.
It may happen to behave as you describe on some systems (especially
little-endian ones such as Intel/AMD) but highly likely it will never
behave as you expect on big-endian machines. In any case it's not
portable and should be avoided.

Whether the program behaves as the OP expected is another question
entirely, and one that we can't answer, since we haven't been told what
it's intended to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top