Using scanf to read uint8_t data

I wrote a simple program to read hours and minutes, and then add them together. But it is not added, but currentHrMinprints only the value of minutes. However, if getCurrentDate(&dateParams)called after printing currentHrMin, there is no problem. I can’t find out what is wrong with my code. Maybe a stupid question. I am using the MinGW C compiler.

#include <stdio.h>
#include <stdint.h>

#define BCD_TO_DEC(num) ((((num)&0xF0)>>4)*10+((num)&0x0F))
#define DEC_TO_BCD(num) ((((num)/10) << 4) | ((num) % 10))


struct RTC_TIME
{
    uint8_t hours;
    uint8_t minutes;
    uint8_t seconds;
    uint8_t twelveHourFormat:1; //1 = 12 hour format, 0=24 hour format.
    uint8_t AM_0_PM_1:1;
    uint8_t hours24Format;
    uint8_t alarm1State:1;
    uint8_t alarm2State:1;
};

struct RTC_DATE
{
    uint8_t date;
    uint8_t month;
    uint8_t dayOfWeek;
    uint8_t year;
};


void getCurrentTime(struct RTC_TIME* time)
{
    printf("Enter Hour: ");
    scanf("%d",&(time->hours));
    printf("Enter Min: ");
    scanf("%d",&(time->minutes));
}

void getCurrentDate(struct RTC_DATE* date)
{
    printf("Enter Month: ");
    scanf("%d",&(date->month));
}

int ar1[5]= {0x1253,0x1034,0x0804,0x1112,0x0409};

int main(void)
{
    struct RTC_DATE dateParams;
    struct RTC_TIME timeParams;

    getCurrentTime(&timeParams);
    getCurrentDate(&dateParams);
    uint16_t currentHrMin = timeParams.hours*60 + timeParams.minutes;
    printf("Current hour minute = %d\n",currentHrMin);

    return(0);

}
+4
source share
1 answer

After power on, #include <inttypes.h>change:

scanf("%d",&(time->hours));

:

scanf("%" SCNu8, &(time->hours));

in all your scanf so that you read for uint8_t instead of int.


, , , %d, int, 32. , time->hours, "" .


, - :

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
main.c:32:16: warning: format specifies type 'int *' but the argument has type
      'uint8_t *' (aka 'unsigned char *') [-Wformat]
    scanf("%d",&(time->hours));
           ~~  ^~~~~~~~~~~~~~
           %s
main.c:34:16: warning: format specifies type 'int *' but the argument has type
      'uint8_t *' (aka 'unsigned char *') [-Wformat]
    scanf("%d",&(time->minutes));
           ~~  ^~~~~~~~~~~~~~~~
           %s
main.c:40:16: warning: format specifies type 'int *' but the argument has type
      'uint8_t *' (aka 'unsigned char *') [-Wformat]
    scanf("%d",&(date->month));
           ~~  ^~~~~~~~~~~~~~
           %s
3 warnings generated.

Wall , .

+8

Source: https://habr.com/ru/post/1677371/


All Articles