I'm trying to convert an inputted character to an integer by using strtol. Here's part of the source code:
char option;
char *endptr;
printf("=========================================Login or Create Account=========================================\n\n");
while(1) {
printf("Welcome to the Bank management program! Would you like to 1. Create Account or 2. Login?\n>>> ");
fgets(&option, 1, stdin);
cleanStdinBuffer();
option = strtol(&option, &endptr, 10);
In the strtol function, I'm getting a warning saying:
Clang-Tidy: Narrowing conversion from 'long' to signed type 'char' is implementation-defined
Can someone tell me what I'm doing wrong?
Clang-Tidy is warning you about the implicit conversion you are doing here where you are assign the
longreturn value ofstrtolto achar:If this is intentional and you are sure the value will be in the [-128,127] range that isn't necessarily an issue (it's just a warning), but even then I would advice to explicitly cast the return-type of
strtol, useint8_tinstead ofcharand not reuse theoptionvariable for the return value. In other words:If it wasn't intentional I would recommend you to simply use
longas type for the variable you assign the return value of strtol, so:What Clang-tidy doesn't warn you about is that the first argument to
strtolshould be a pointer to a char buffer containing a 0-terminated string, not a pointer to a single char. This is also an issue for fgets. There are two ways to solve this, either:Make option a
chararray of at least two chars,Use fgetc instead and modify your code into something like this:
I think the latter looks much cleaner.