Difference between printf and cout

When I run the following code:

  TTree* t=...
  long a=t->GetEntries();
  printf("%l\n",a);                                                                                      
  cout<<a<<endl;

I get the following output:

25168
13634.3

I expect these to be the same for any tree. What’s going on?

With ROOT 6 I get:

$ root
   ----------------------------------------------------------------
  | Welcome to ROOT 6.11/03                    http://root.cern.ch |
  |                                   (c) 1995-2017, The ROOT Team |
  | Built for macosx64                                             |
  | From heads/master@v6-11-02-517-g2e7682e, Oct 30 2017, 08:55:01 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'     |
   ----------------------------------------------------------------

root [0] long a=ntuple->GetEntries();
root [1]  cout<<a<<endl;
25000
root [2] printf("%l\n",a); 
ROOT_prompt_2:1:11: warning: invalid conversion specifier '\x0a' [-Wformat-invalid-specifier]
printf("%l\n",a);
        ~~^

root [3] printf("%ld\n",a); 
25000
root [4] 

%l is not a valid format string on its own, l only modifies another. You want %li, for long integer. Read more details at http://www.cplusplus.com/reference/cstdio/printf/

Thanks for your reply. I didn’t see the warning. This resolves the issue.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.