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.


|