0

This is driving me crazy. I have a code which is outputting a weird value while using division:

#define NANOSECONDS_PER_SECOND   1000000000
uint64 CurrentTimeInNanoSecs; 

    uint64 GetTimeInNanoSecs( )
   {
      printf("\n%X", (CurrentTimeInNanoSecs >> 32) ); 
      printf("\n%X", (CurrentTimeInNanoSecs & 0xFFFFFFFF) ); 
      return ( CurrentTimeInNanoSecs );
   }

void GetCurrentTimeInSecs()
{
  uint32 Time = GetTimeInNanoSecs() / NANOSECONDS_PER_SECOND;
  printf("%X", time);
}

void main()
{
GetCurrentTimeInSecs();
}

On init, I see the prints as follows: 0x00000000 0x3016DC6B 0x00000198

I am not sure what is happening. Can someone pls help.

  • 3
    Can you please [edit] your question to include a [mcve]? Hard-code values if needed, but please try to make sure it actually replicates the problem you have (and also include how you check the values). – Some programmer dude Sep 07 '20 at 08:28
  • Did you remember to declare `GetTimeInNanoSecs` before calling it? The posted code doesn't show it. Without declaring it, you should have gotten a warning, and of course it won't work since it will implicitly treat the return type as `int`. – Tom Karzes Sep 07 '20 at 08:31
  • 1
    It is impossible to tell anything at all from this gibberish pseudo code... please post valid C code. – Lundin Sep 07 '20 at 08:32
  • There are at least two seriously weird looking constructs, from point of view of a C compiler. The return value data type `func` (from a function returning nothing) and the strange comment-like `--- this is a...`. In this situation, answering is too much based on guessing and assumptions. Please take Somes hint and demonstrate your problem wih a MRE. – Yunnosch Sep 07 '20 at 08:32
  • Updated the question. I apologize for the giberrish code earlier – darkfall94 Sep 07 '20 at 08:58
  • Please study and apply the concept of making a [mre]. The shown code still does not match it. I doubt that it compiles without warnings curently and this is not about warnings. – Yunnosch Sep 07 '20 at 09:14
  • Shifting a 64 bit value evaluates to a 64 bit value. Does your compiler warn if you pass an `uint64` to `printf("%X")`? What is the size of `int` on your system? – Gerhardh Sep 07 '20 at 09:27
  • `CurrentTimeInNanoSecs` is `uint64` but `%X` waits for an `unsigned int` so you're getting UB here – phuclv Sep 07 '20 at 12:41
  • Is `uint64` an equivalent of the standard `uint64_t` ? `I see the prints as follows: 0x` Are you really seeing `0x` in the output? – KamilCuk Sep 07 '20 at 13:10

2 Answers2

0

Beware that printf format specifiers must agree with the datatype that is passed, otherwise data may be misinterpreted and print as gibberish!

The correct way to print a uint64_t is with printf("%" PRIu64 "\n", x);, where PRIu64 is defined in inttypes.h. Note: Pre-C11, you may need to define __STDC_FORMAT_MACROS to get PRIu64, as described in this SO post.

More generally, see the table in https://en.cppreference.com/w/cpp/io/c/fprintf of the expected types for each specifier. There's also a good printf format string article on Wikipedia about it.

I recommend compiling with warnings turned on. Use command line option -Wall, if using clang or gcc. Most compilers should give warnings on the posted code like:
format '%X' expects argument of type 'unsigned int', but argument has type 'uint64_t'

Pascal Getreuer
  • 2,906
  • 1
  • 5
  • 14
0

My bad: I know I posted the code as:

printf("\n%X", (CurrentTimeInNanoSecs >> 32) );

but in reality I wrote it as:

printf("\n%X", (CurrentTimeInNanoSecs & 0xFFFFFFFF >> 32) );

So my upper 32 bits were always zero and I was misinterpreting the results :/

Thank you to the community though.

  • SO works best when the author provides feedback to questions from comments. You still pass wrong parameter type into `printf` and should get some warning from your compiler. – Gerhardh Sep 09 '20 at 13:58