Question about %HI format specifier in printf, etc.

Discussion to talk about software related topics only.
Post Reply
jaypdx
Posts: 29
Joined: Wed Oct 15, 2014 6:06 pm

Question about %HI format specifier in printf, etc.

Post by jaypdx »

I see both %I and %HI format specifiers in the Netburner sample code (i.e. examples\Ethernet\ManualConfig\src\main.cpp). When I use %HI I get a warning "use of 'H' length modifier with 'I' type character has either no effect or undefined behavior". But if I just use %I it says that's for IPADDR6 (I'm printing an IPADDR4). This is on NNDK 3.3.0. Are these specifiers documented someplace? Should I just ignore the warning?
User avatar
pbreed
Posts: 1081
Joined: Thu Apr 24, 2008 3:58 pm

Re: Question about %HI format specifier in printf, etc.

Post by pbreed »

%hI for an IPADDR4 STORED in an IPADDR4.
%I for an IPADDR4 stored in an IPADDR (IPADDR6)

So what is the actual TYPE of the variable you are using in the printf?

The code ties to make the distinction between IPV6 and IPV4 disappear...
IE You should not care....

You should use IPADDR and even if you are only ever putting IPV4 addresses in it...
You should only ever use an IPADD4 if you have a legacy structure or flash storage that saves it as a 32bit DWORD.


IPADDR ia=AsciiToIp("10.1.2.3");

ia is an IPADDR (aka IPADDR6) it is holding an IPV4 address...

If you use it in a network call udp, tcp connect etc....
the packets that are generated on the wire will be IPv4 packets.

if you want to printf it use %I as its an IPADDR, its type is not an IPADD4 type , even though its current contents hold an IPV4 address.
When it prints out using %I it will print out in IPv4 format because it is an IPV4 address...
jaypdx
Posts: 29
Joined: Wed Oct 15, 2014 6:06 pm

Re: Question about %HI format specifier in printf, etc.

Post by jaypdx »

My code is:

IPADDR4 EthernetIP;
EthernetIP = InterfaceIP(GetFirstInterface());
iprintf("IP address: %HI\n", EthernetIP);

InterfaceIP(), as defined in netinterface.h, returns an IPADDR4. If I define EthernetIP as an IPADDR I get an error. All I'm trying to here is get the IP address of my ethernet interface and print it to the console or to a string (via sprintf). Should I be doing this another way?

I should ignore the warning, right?:

../src/main.cpp: In function 'void UserMain(void*)':
../src/main.cpp:165:13: warning: use of 'H' length modifier with 'I' type character has either no effect or undefined behavior [-Wformat=]
iprintf("IP address: %HI\n", EthernetIP);
^~~~~~~~~~~~~~~~~~~
User avatar
TomNB
Posts: 541
Joined: Tue May 10, 2016 8:22 am

Re: Question about %HI format specifier in printf, etc.

Post by TomNB »

The show interface example goes through all this in detail: \nburn\examples\ShowInterfaces\. It also has utility files you can copy to your design if you wish.
Post Reply