Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Promotions ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 8 Topic 1076 of 1134
Post > Topic >>

Re: Promotions while delivering parameters

by Kenneth Brody <kenbrody@[EMAIL PROTECTED] > Apr 16, 2008 at 04:56 PM

Magi Su wrote:
> 
> Hello everyone!
> 
> I am a student learning Expert C Programming. One interesting topic I
have
> found is that, which the book told me, when we use K&R style declaration
> and definition of functions:
[... K&R style versus prototypes ...]
> I have written a program to try it out:
> 
> ------------------promote1.c----------------------
> void fun1(int,char);
> void fun2();
> 
> int main(void)
> {
>         asm(" #FUNCTION 1 CALL ");
>         fun1(1,'c');
>         asm(" #FUNCTION 2 CALL ");
>         fun2(1,'c');
>         asm(" #FUNCTION CALL FINISHED ");
>         return 0;
> }
[...]
> From the selected part of assemble file, I found that both function
call,
> fun1 and fun2, have promoted the 'char' to 'int'. Could anyone explain
why?

Actually, what is happening here is that you are passing two ints
in the calls.  The parameter 'c', while looking like a char, is in
reality, an int.

Try the same code, passing a "real" char:

    int i = 1;
    char c = 'c';
    fun1(i,c);
    fun2(i,c);

and see the difference.

On my particular compiler, I get:

        movsx   eax, BYTE PTR _c$[ebp]
        push    eax
        mov     ecx, DWORD PTR _i$[ebp]
        push    ecx
        call    _kandr
        add     esp, 8

versus

        mov     dl, BYTE PTR _c$[ebp]
        push    edx
        mov     eax, DWORD PTR _i$[ebp]
        push    eax
        call    _ansi
        add     esp, 8

Note that the call to kandr() sign-extends c, converting it to an
int.  (In this case, chars are signed.)  The call to ansi(), on
the other hand, simply loads the 1-byte char and pushes it on the
stack.


-- 
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        | www.hvcomputer.com | #include              |
| kenbrody/at\spamcop.net | www.fptech.com     |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@[EMAIL PROTECTED]
>
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 8 Posts in Topic:
Promotions while delivering parameters
Magi Su <magisu@[EMAIL  2008-04-13 02:41:49 
Re: Promotions while delivering parameters
Francis Glassborow <fr  2008-04-16 16:56:02 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-18 16:11:36 
Re: Promotions while delivering parameters
Francis Glassborow <fr  2008-04-21 13:20:21 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-16 16:56:33 
Re: Promotions while delivering parameters
Brian Inglis <Brian.In  2008-04-18 16:11:55 
Re: Promotions while delivering parameters
Kenneth Brody <kenbrod  2008-04-16 16:56:45 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-18 16:11:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:53:36 CDT 2008.