Thread: argument data type confusion

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    argument data type confusion

    Hi,

    suppose if i write a test program like
    Code:
    void function1(unsigned int var1);
    int main(void)
    {
    function1(-3);
    }
    
    void function1(unsigned int var1)
    {
    printf("%d", var1);
    }
    The output is -3. how it happens the argument is unsigned but iam passing signed but still prints the signed value. My bigger question is how the arguments are handled if the passing parameters are different types compared to declaration. Can someone please explain me in detail.

    Thanks and regards,
    Satya

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    %d tells printf() to treat the corresponding argument as if it is a signed int. Providing an argument that is not signed int results in undefined behaviour.

    It is the programmer's responsibility to ensure that the types specified in the format string match the supplied arguments. You have not fulfilled your responsibility.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i didn't read the page fully , but it offers an explanation to your question here
    .....
    in particular,the conversion part of the article
    Last edited by africanwizz; 06-12-2014 at 03:58 AM. Reason: meh..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2013, 12:28 PM
  2. constructor argument confusion
    By Blacky Ducky in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2011, 02:58 PM
  3. Replies: 5
    Last Post: 01-24-2011, 05:37 AM
  4. data type of passed argument
    By fran1942 in forum C Programming
    Replies: 1
    Last Post: 01-08-2011, 12:05 AM
  5. Help with template data type argument
    By TriKri in forum C++ Programming
    Replies: 6
    Last Post: 07-12-2008, 11:21 AM