Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:18:36+00:00 2026-05-13T09:18:36+00:00

I have this sample code for converting 32 bit integers to ip addresses. #include

  • 0

I have this sample code for converting 32 bit integers to ip addresses.


#include <stdio.h>
int main()
{
 unsigned int c ;
 unsigned char* cptr  = (unsigned char*)&c ;
 while(1)
 {
  scanf("%d",&c) ;
  printf("Integer value: %u\n",c);
  printf("%u.%u.%u.%u \n",*cptr, *(cptr+1), *(cptr+2), *(cptr+3) );
 }
}

This code gives incorrect output for input 2249459722 .
But when i replace

scanf("%d",&c) ;

by

scanf("%u",&c) ;

The output comes out to be correct.

P.S : I know about inet_ntop and inet_pton.
I expect answers other than suggesting those.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:18:36+00:00Added an answer on May 13, 2026 at 9:18 am

    You are coding ‘sinfully‘ (making a number of mistakes which will hurt you sooner or later – mostly sooner). First off, you are assuming that the integer is of the correct endian-ness. On some machines, you will be wrong – either on Intel machines or on PowerPC or SPARC machines.

    In general, you should show the actual results you get rather than just saying that you get the wrong result; you should also show the expected result. That helps people debug your expectations.


    Here’s my modified version of your code – instead of requesting input, it simply assumes the value you specified.

    #include <stdio.h>
    int main(void)
    {
        unsigned int c = 2249459722;
        unsigned char* cptr  = (unsigned char*)&c;
        printf("Integer value:  %10u\n", c);
        printf("Integer value:  0x%08X\n", c);
        printf("Dotted decimal: %u.%u.%u.%u \n", *cptr, *(cptr+1), *(cptr+2), *(cptr+3));
        return(0);
    }
    

    When compiled on my Mac (Intel, little-endian), the output is:

    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    

    When compiled on my Sun (SPARC, big-endian), the output is:

    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 134.20.8.10 
    

    (Using GCC 4.4.2 on the SPARC, I get a warning:

    xx.c:4: warning: this decimal constant is unsigned only in ISO C90
    

    Using GCC 4.2.1 on Mac – with lots of warnings enabled (gcc -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Werror) – I don’t get that warning, which is interesting.) I can remove that by adding a U suffix to the integer constant.


    Another way of looking at the problems is illustrated with the following code and the extremely fussy compiler settings shown above:

    #include <stdio.h>
    
    static void print_value(unsigned int c)
    {
        unsigned char* cptr  = (unsigned char*)&c;
        printf("Integer value:  %10u\n", c);
        printf("Integer value:  0x%08X\n", c);
        printf("Dotted decimal: %u.%u.%u.%u \n", *cptr, *(cptr+1), *(cptr+2), *(cptr+3));
    }
    
    int main(void)
    {
        const char str[] = "2249459722";
        unsigned int c = 2249459722;
    
        printf("Direct operations:\n");
        print_value(c);
    
        printf("Indirect operations:\n");
        if (sscanf("2249559722", "%d", &c) != 0)
            printf("Conversion failed for %s\n", str);
        else
            print_value(c);
        return(0);
    }
    

    This fails to compile (because of the -Werror setting) with the message:

    cc1: warnings being treated as errors
    xx.c: In function ‘main’:
    xx.c:20: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘unsigned int *’
    

    Remove the -Werror setting and it compiles, but then shows the next problem that you have – the one of not checking for error indications from functions that can fail:

    Direct operations:
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations:
    Conversion failed for 2249459722
    

    Basically, the sscanf() function reports that it failed to convert the string to a signed integer (because the value is too large to fit – see the warning from GCC 4.4.2), but your code was not checking for the error return from sscanf(), so you were using whatever value happened to be left in c at the time.

    So, there are multiple problems with your code:

    • It assumes a particular architecture (little-endian rather than recognizing that big-endian also exists).
    • It doesn’t compile cleanly when using a compiler with lots of warnings enabled – for good reason.
    • It doesn’t check that functions that can fail actually succeeded.

    Alok’s Comment

    Yes, the test on sscanf() is wrong. That’s why you have code reviews, and also why it helps to post the code you are testing.

    I’m now a bit puzzled – getting consistent behaviour that I can’t immediately explain. With the obvious revision (testing on MacOS X 10.6.2, GCC 4.2.1, 32-bit and 64-bit compilations), I get one not very sane answer. When I rewrite more modularly, I get a sane answer.

    + cat yy.c
    #include <stdio.h>
    
    static void print_value(unsigned int c)
    {
        unsigned char* cptr  = (unsigned char*)&c;
        printf("Integer value:  %10u\n", c);
        printf("Integer value:  0x%08X\n", c);
        printf("Dotted decimal: %u.%u.%u.%u \n", *cptr, *(cptr+1), *(cptr+2), *(cptr+3));
    }
    
    int main(void)
    {
        const char str[] = "2249459722";
        unsigned int c = 2249459722;
    
        printf("Direct operations:\n");
        print_value(c);
    
        printf("Indirect operations:\n");
        if (sscanf("2249559722", "%d", &c) != 1)
            printf("Conversion failed for %s\n", str);
        else
            print_value(c);
        return(0);
    }
    
    
    + gcc -o yy.32 -m32 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes yy.c
    yy.c: In function ‘main’:
    yy.c:20: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘unsigned int *’
    
    
    + ./yy.32
    Direct operations:
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations:
    Integer value:  2249559722
    Integer value:  0x86158EAA
    Dotted decimal: 170.142.21.134 
    

    I do not have a good explanation for the value 170.142.21.134; but it is consistent on my machine, at the moment.

    + gcc -o yy.64 -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes yy.c
    yy.c: In function ‘main’:
    yy.c:20: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘unsigned int *’
    
    
    + ./yy.64
    Direct operations:
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations:
    Integer value:  2249559722
    Integer value:  0x86158EAA
    Dotted decimal: 170.142.21.134 
    

    Same value – even in 64-bit instead of 32-bit. Maybe the problem is that I’m trying to explain undefined behaviour, which is more or less by definition unexplainable (inexplicable).

    + cat xx.c
    #include <stdio.h>
    
    static void print_value(unsigned int c)
    {
        unsigned char* cptr  = (unsigned char*)&c;
        printf("Integer value:  %10u\n", c);
        printf("Integer value:  0x%08X\n", c);
        printf("Dotted decimal: %u.%u.%u.%u \n", *cptr, *(cptr+1), *(cptr+2), *(cptr+3));
    }
    
    static void scan_value(const char *str, const char *fmt, const char *tag)
    {
        unsigned int c;
        printf("Indirect operations (%s):\n", tag);
        fmt = "%d";
        if (sscanf(str, fmt, &c) != 1)
            printf("Conversion failed for %s (format %s \"%s\")\n", str, tag, fmt);
        else
            print_value(c);
    }
    
    int main(void)
    {
        const char str[] = "2249459722";
        unsigned int c = 2249459722U;
    
        printf("Direct operations:\n");
        print_value(c);
        scan_value(str, "%d", "signed");
        scan_value(str, "%u", "unsigned");
    
        return(0);
    }
    

    Using the function argument like this means GCC cannot spot the bogus format any more.

    + gcc -o xx.32 -m32 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes xx.c
    
    
    + ./xx.32
    Direct operations:
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations (signed):
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations (unsigned):
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    

    The results are consistent here.

    + gcc -o xx.64 -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes xx.c
    
    
    + ./xx.64
    Direct operations:
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations (signed):
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134 
    Indirect operations (unsigned):
    Integer value:  2249459722
    Integer value:  0x8614080A
    Dotted decimal: 10.8.20.134
    

    And these are the same as the 32-bit case. I’m officially bemused. The main observations remain accurate – be careful, heed compiler warnings (and elicit compiler warnings), and don’t assume that “all the world runs on Intel chips” (it used to be “don’t assume that all the world is a VAX”, once upon a long time ago!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 491k
  • Answers 491k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to change the StoreGeneratedPattern value for the property… May 16, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer As I can see so far, your ContentValues keys are… May 16, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer It says to go into all the subdirectories below tmp,… May 16, 2026 at 10:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm having a bit of a problem with converting the result of a MySQL
I have been considering using the code example shown at Shell Style Drag and
In the opengl section of Hillegass' otherwise fine book, we're given sample code for
I have many source/text file, say file.cpp or file.txt . Now, I want to
Im still pretty new so bear with me on this one, my question(s) are
I have a nontrivial service object developed with TDD. It started with a simple
I have a VERY complex service host which consists of multiple DUPLEX services here.
i have the following map of the world: <!-- copyright (c) 2009 Google inc.
I have been using a stored procedure for more than 1.5 years. But I've
This is a newbie question in R. I am downloading yahoo finance monthly stock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.