escape % in printf

Anything that doesn't fit elsewhere.
Post Reply
pfortier
Posts: 47
Joined: Thu Apr 20, 2006 8:02 am
Location: University of Ottawa

escape % in printf

Post by pfortier » Wed Oct 22, 2014 11:59 am

The following webpage http://www.mekong.net/tech/printf.htm indicates that the format specifier %% will print one % but in hoc it prints 2 of them. So printf("aaa %%g bbb") will print:
aaa %%g bbb

If this is a deviation from the standard, could it be fixed?

Escaping backslashes works well, so printf("aaa %%g bbb\\n") will print:
aaa %%g bbb\n

hines
Site Admin
Posts: 1577
Joined: Wed May 18, 2005 3:32 pm

Re: escape % in printf

Post by hines » Thu Oct 23, 2014 1:59 am

The fix is at http://www.neuron.yale.edu/hg/neuron/nr ... 3444e22186

This fix was applied to the trunk (NEURON -- VERSION 7.4) and NEURON -- Release 7.3 continues to have the old behavior. Consider

Code: Select all

$ cat temp.hoc
printf("%%\n")
printf("abc%%def\n")

printf("%\n")
printf("abc%def", 2)
printf("abc%def")
The 7.3 gives

Code: Select all

%%
	3 
abc%%def
	9 
%
	2 
abc2ef	6 
nrniv: printf not enough arguments
And 7.4 with the fix gives

Code: Select all

%
	2 
abc%def
	8 
%
	2 
abc2ef	6 
nrniv: printf not enough arguments

hines
Site Admin
Posts: 1577
Joined: Wed May 18, 2005 3:32 pm

Re: escape % in printf

Post by hines » Wed Nov 05, 2014 1:20 am

Sorry. I botched the previous fix which introduced much more serious problems. See:
http://www.neuron.yale.edu/hg/neuron/nr ... 8d1dbd1346

Post Reply