0

I am programming a STM32F103C8T6 microcontroller with the STM32CubeIDE, Version: 1.11.2, using the included gcc/g++ compiler. I just made a little program to control some leds and turn them on and off again. I let the variable tickCounter just count up and compare it with different time points. For example i have the condition

if(tickCounter - wasSwitchedOnAt > TIME_OFF_DELAY_NO_MILLISECS)

Above i declared the type with

    using tick_count_t = uint32_t;

    tick_count_t tickCounter = 0U;
    tick_count_t wasSwitchedOnAt = 0U;

and my constants like this

#define TIME_OFF_DELAY_NO_MILLISECS (30000U)

Now to my problem: why is the compiler throwing a warning when i change the tick_count_t to uint16_t? The whole warning is as follows:

../Src/main.cpp:118:42: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]

The biggest value which i compare it to are the given 30000U.

The compiler is not warning me when i try using tick_count_t = uint64_t; but when i try uint16_t, which would be the correct, smallest type to use imo, the compiler throws the signedness warning. Has anyone an idea why this is happening? I also tried to explicitly cast the value to my type like this if((tick_count_t) (tickCounter - wasSwitchedOnAt) > TIME_OFF_DELAY_NO_MILLISECS) and no error gets thrown. What is also a conundrum for me is that i have conditions without the subtraction like 'if(buttonPressedForXTicks > TIME_ON_DELAY_PERMANENT_MODE)' . Here the compiler does not throw an error when i use the uint16_t. I have no clue what is happening. Can anyone tell me what is going on? Thanks in advance!

sepp2k
  • 363,768
  • 54
  • 674
  • 675
bLead
  • 1
  • 2
  • C and C++ are very different languages. Don't tag both unless you're specifically asking about their differences. – molbdnilo Mar 22 '23 at 10:47
  • [C++ Why is the uint16_t being implicitly cast to int here?](https://stackoverflow.com/q/68566946/995714), [Does uint16_t get promoted to int? And is it safe to typecast uint16_t to uint32_t?](https://stackoverflow.com/q/75724292/995714) – phuclv Mar 22 '23 at 10:48

0 Answers0