Integer to IP Address - C

后端 未结 9 769
陌清茗
陌清茗 2020-12-07 17:45

I\'m preparing for a quiz, and I have a strong suspicion I may be tasked with implementing such a function. Basically, given an IP address in network notation, how can we ge

9条回答
  •  爱一瞬间的悲伤
    2020-12-07 18:27

    You actually can use an inet function. Observe.

    main.c:

    #include 
    
    main() {
        uint32_t ip = 2110443574;
        struct in_addr ip_addr;
        ip_addr.s_addr = ip;
        printf("The IP address is %s\n", inet_ntoa(ip_addr));
    }
    

    The results of gcc main.c -ansi; ./a.out is

    The IP address is 54.208.202.125

    Note that a commenter said this does not work on Windows.

提交回复
热议问题