By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

can u tell this will accept or not?

P: n/a
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!!

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
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!!


Nov 15 '05 #2

P: n/a
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.

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #3

P: n/a
On 2005-10-26, Netocrat <ne******@dodo.com.au> wrote:
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.
Nov 15 '05 #4

P: n/a
On Wed, 26 Oct 2005 13:59:07 +0000, Jordan Abel wrote:
On 2005-10-26, Netocrat <ne******@dodo.com.au> wrote:
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.

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #5

P: n/a
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.
--
Rich Gibbs
ri*****@gmail.com

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.