Hellp with type promotion

Discussion in 'C Programming' started by manochavishal@gmail.com, Feb 28, 2006.

  1. Guest

    HI,
    I read that every char in expression is promoted to int.

    So as function arguments are expressions so type promotion should take
    place for arguments passed to function.

    so

    *****
    int main(void)
    {
    char c = 'C';
    test(c);

    return 0;
    }

    void test(char c)
    {
    printf("Size of char is %d",sizeof(c));

    }
    *******


    should print -Size of char is 4 - as the char c is promoted to type
    int.

    But it prints 1

    Why??

    Cheers
    Vishal
    , Feb 28, 2006
    #1
    1. Advertising

  2. Ravi Uday Guest

    wrote:

    > HI,
    > I read that every char in expression is promoted to int.
    >
    > So as function arguments are expressions so type promotion should take
    > place for arguments passed to function.
    >
    > so
    >
    > *****
    > int main(void)
    > {
    > char c = 'C';
    > test(c);
    >
    > return 0;
    > }
    >
    > void test(char c)
    > {
    > printf("Size of char is %d",sizeof(c));


    sizeof - The sizeof operator yields the size (in bytes) of its operand,
    so in your case the operand is c which is of type 'char'
    and on your system size of char is 1 so you get 1 when you print that !

    >
    > }
    > *******
    >
    >
    > should print -Size of char is 4 - as the char c is promoted to type
    > int.


    No it doesnt

    - ravi


    >
    > But it prints 1
    >
    > Why??
    >
    > Cheers
    > Vishal
    >
    Ravi Uday, Feb 28, 2006
    #2
    1. Advertising

  3. Guest

    Hi,

    it does promote to int type.

    If we dont explicitly declare the function prototype, the function
    argument character is promoted to int.



    void test();
    int main(void)
    {


    int n;
    char c = 'C';
    test(c);

    printf("\nsize in main %d",sizeof(c));

    return 0;

    }
    void test(c)
    {
    printf("size in func %d",sizeof(c));
    }


    This prints:

    size in func 4
    size in main 1

    So if we dont tell compiler explicitly in the prototype the type of
    arguments they are promoted.

    Great!!

    Cheers
    Vishal.
    , Feb 28, 2006
    #3
  4. Richard Bos Guest

    "" <> wrote:

    > I read that every char in expression is promoted to int.


    Yes.

    > So as function arguments are expressions so type promotion should take
    > place for arguments passed to function.


    No. This is only true for old-style declarations, a function without a
    declaration, or the variable arguments of a variadic function.

    Richard
    Richard Bos, Feb 28, 2006
    #4
  5. Robin Haigh Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > it does promote to int type.
    >
    > If we dont explicitly declare the function prototype, the function
    > argument character is promoted to int.
    >
    >
    >
    > void test();
    > int main(void)
    > {
    >
    >
    > int n;
    > char c = 'C';
    > test(c);
    >
    > printf("\nsize in main %d",sizeof(c));
    >
    > return 0;
    >
    > }
    > void test(c)
    > {
    > printf("size in func %d",sizeof(c));
    > }
    >
    >
    > This prints:
    >
    > size in func 4
    > size in main 1
    >
    > So if we dont tell compiler explicitly in the prototype the type of
    > arguments they are promoted.



    But here's the odd thing. If you change the call

    test(c);

    to

    test();

    so that you don't pass an argument at all, you still get the same output

    --
    RSH
    Robin Haigh, Feb 28, 2006
    #5
  6. wrote:
    > HI,
    > I read that every char in expression is promoted to int.
    > So as function arguments are expressions so type promotion should take
    > place for arguments passed to function.
    > so
    >
    > *****
    > int main(void)
    > {
    > char c = 'C';
    > test(c);
    >
    > return 0;
    > }
    >
    > void test(char c)
    > {
    > printf("Size of char is %d",sizeof(c));
    >
    > }
    > *******
    >
    >
    > should print -Size of char is 4 - as the char c is promoted to type
    > int.
    >
    > But it prints 1
    >
    > Why??
    >


    The real question is why you either
    1) have your diagnostics turned off or
    2) are ignoring them.
    These are the diagnostics gcc produces for your code:

    a.c: In function 'main':
    a.c:4: warning: implicit declaration of function 'test'
    a.c: At top level:
    a.c:10: warning: conflicting types for 'test'
    a.c:4: warning: previous implicit declaration of 'test' was here
    a.c: In function 'test':
    a.c:11: warning: implicit declaration of function 'printf'
    a.c:11: warning: incompatible implicit declaration of built-in function
    'printf'
    a.c:11: warning: format '%d' expects type 'int', but argument 2 has type
    'long unsigned int'
    Martin Ambuhl, Feb 28, 2006
    #6
  7. "Robin Haigh" <> wrote in news:du1qg6$7jd$1
    @newsg3.svr.pol.co.uk:

    >
    > <> wrote in message
    > news:...


    >> void test();

    ....

    >> void test(c)
    >> {
    >> printf("size in func %d",sizeof(c));
    >> }
    >>

    ....
    > But here's the odd thing. If you change the call
    >
    > test(c);
    >
    > to
    >
    > test();
    >
    > so that you don't pass an argument at all, you still get the same
    > output


    How is that odd? (Except that the code looks like it is from the 80s).

    You have circumvented the prototype system. When func is called, it is
    set up to receive one int argument. The value might be crap, but that
    doesn't really matter because sizeof does not evaluate its operand.

    Of course, IMHO, anyone who writes code like this in this day and age
    deserves whatever they get.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)
    A. Sinan Unur, Feb 28, 2006
    #7
  8. Default User Guest

    wrote:

    > int main(void)
    > {
    > char c = 'C';
    > test(c);
    >
    > return 0;
    > }
    >
    > void test(char c)
    > {
    > printf("Size of char is %d",sizeof(c));
    >
    > }


    1. This code is not legal. You don't have a declaration for test, and
    the definition is different than the default declaration you would get
    with a pre-C99 compiler. Also, printf() is a variadic function a
    requires a prototype. The best way is to include <stdio.h>

    2. Even if there were a legal implicit declaration or an old-style
    declaration, that wouldn't matter. Except for variable-length arrays,
    sizeof is performed at compile time. What the arguments get promoted to
    at run-time have no bearing on what's printed there. It will be
    sizeof(char), which is guaranteed to be 1, always.



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Feb 28, 2006
    #8
  9. Default User Guest

    wrote:

    > Hi,
    >
    > it does promote to int type.
    >
    > If we dont explicitly declare the function prototype, the function
    > argument character is promoted to int.
    >
    >
    >
    > void test();
    > int main(void)
    > {
    >
    >
    > int n;
    > char c = 'C';
    > test(c);
    >
    > printf("\nsize in main %d",sizeof(c));
    >
    > return 0;
    >
    > }
    > void test(c)
    > {
    > printf("size in func %d",sizeof(c));
    > }
    >
    >
    > This prints:
    >
    > size in func 4
    > size in main 1
    >
    > So if we dont tell compiler explicitly in the prototype the type of
    > arguments they are promoted.


    Has nothing to do with promotion. You didn't give c a type in the
    definition of test(). Therefore the implementation provided one for
    you. The equivalent definition was:

    void test(c)
    int c;
    {
    }




    So you got sizeof(int).



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Feb 28, 2006
    #9
  10. "" <> writes:
    > I read that every char in expression is promoted to int.
    >
    > So as function arguments are expressions so type promotion should take
    > place for arguments passed to function.
    >
    > so
    >
    > *****
    > int main(void)
    > {
    > char c = 'C';
    > test(c);
    >
    > return 0;
    > }
    >
    > void test(char c)
    > {
    > printf("Size of char is %d",sizeof(c));
    >
    > }
    > *******
    >
    >
    > should print -Size of char is 4 - as the char c is promoted to type
    > int.
    >
    > But it prints 1


    There are several problems with your program:
    Calling test() with no visible declaration.
    Missing "#include <stdio.h>".
    No newline at the end of your output.
    Incompatible type for "%d" format (it expects an int; you gave it
    a size_t).

    Leaving that aside, the parameter "c" within your "test" function is
    effectively a local variable of type char. sizeof(c) yields the size
    of that variable, which is 1 byte by definition.

    There are a number of rules about when integer promotions and default
    argument promotions are applied (and I actually don't know all the
    rules myself). But applying sizeof to a variable doesn't involve any
    promotions; it simply yields the size of the variable.

    The fact that the variable happens to be a parameter is irrelevant.
    On the function call, the value of the argument (the variable "c" in
    main) is used to initialize the parameter object (the parameter "c" in
    test(); this would be easier if they had different names). But the
    value assigned to it doesn't affect its size.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 28, 2006
    #10
  11. On 28 Feb 2006 04:27:07 -0800, ""
    <> wrote:

    > HI,
    > I read that every char in expression is promoted to int.
    >

    Not quite all. As the operand to any computational operator, yes. Not
    as a full-expression (though that's useless) or an operand of sizeof
    or & (address) or of the comma operator; and not necessarily as the
    RHS of an assignment, or passing to a prototyped function or returning
    from (any) function which are both as-if assignment.

    Also note this applies only to the value of a char _object_ (variable)
    or a value (number) cast to char. A character constant aka literal
    like 'x' has type int _already_, before any promotion or conversion
    -- in C <OT> but not in C++. </>

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Mar 6, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigMan

    Type promotion rules?

    BigMan, Jun 12, 2005, in forum: C++
    Replies:
    7
    Views:
    6,995
    BigMan
    Jun 14, 2005
  2. John Smith

    data type promotion

    John Smith, Sep 6, 2004, in forum: C Programming
    Replies:
    2
    Views:
    265
    John Smith
    Sep 6, 2004
  3. KOFKS
    Replies:
    2
    Views:
    276
    KOFKS
    Jun 6, 2006
  4. Zhaohui

    hellp about local timestamp

    Zhaohui, Dec 7, 2005, in forum: C++
    Replies:
    3
    Views:
    383
  5. fulio pen

    Please hellp again with a code

    fulio pen, Aug 22, 2005, in forum: Javascript
    Replies:
    0
    Views:
    69
    fulio pen
    Aug 22, 2005
Loading...

Share This Page