0

The clang-tidy program shows me a hint and I don't know what it means:

#include <stdio.h>

int main() {
    // Initializing:
    char userName[12];
    int userDOB;
    // Developing:
    printf("-------------------------------------------------------------\n");
    // Taking the userName:
    printf("PLEASE ENTER YOUR USER-NAME:");
    scanf("%s", userName);
    // Taking the userDOB:
    printf("PLEASE ENTER YOUR USER-DOB:");
    scanf("%i", &userDOB);
    printf("--------------------------\n");
    // Printing data:
    printf("USER-NAME: %s \n", userName);
    printf("USER-DOB: %i \n", userDOB);
    printf("-------------------------------------------------------------\n");
    return 0;
}

The hint is:

Clang-Tidy: 'scanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead

This occurs in the scanf of the userDOB.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ABDO-AR
  • 160
  • 3
  • 9
  • 3
    The suggestion is to read the input for `userDOB` as a string, and use `strtol` to convert to an integer. This will have much more validation of the input to make sure that it's really a valid integer. And even if you continue to use `scanf` to read the strings, you really should check what it [*returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value). – Some programmer dude Aug 13 '21 at 17:39
  • 2
    See [What can I use for input conversion instead of scanf?](https://stackoverflow.com/questions/58403537) – Steve Summit Aug 13 '21 at 17:49
  • 2
    I'm a little bit surprised that clang-tidy is yapping at you about that, but what it's telling you is something that very many C programmers have concluded: despite its superficial attraction, `scanf` is very nearly completely useless. Most experienced C programmers don't use it, ever, for anything. – Steve Summit Aug 13 '21 at 17:51
  • 2
    See also: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html. See also https://stackoverflow.com/questions/2430303/disadvantages-of-scanf. – Steve Summit Aug 13 '21 at 17:56
  • scanf is good for reading text files containing fixed-format records. – stark Aug 13 '21 at 18:00
  • 1
    @stark ... _no it isn't_. That's the point of all those links Steve posted. It's broken as specified and should not be used at all. – zwol Aug 13 '21 at 18:12
  • THANKS FOR ALL ❤️♥️❤️ – ABDO-AR Aug 14 '21 at 23:54

0 Answers0