escape % in printf

Anything that doesn't fit elsewhere.
Post Reply
pfortier

escape % in printf

Post by pfortier »

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: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: escape % in printf

Post by hines »

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: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: escape % in printf

Post by hines »

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