I want to know, if have a command that find which is the type of that it's in the variable , if it's a char or int. For example to make a program that, if anything that isn't a number is entered, show an error to user, because I am using an int variable in the scanf, but if the user type a char, the program continue, I want do it stop, when an type char are typed
Asked
Active
Viewed 68 times
-1
-
1Have you googled your question? – Kick Buttowski Jun 07 '17 at 01:19
-
1Possible duplicate https://stackoverflow.com/questions/1478932/check-if-user-inputs-a-letter-or-number-in-c – B. Wolf Jun 07 '17 at 01:21
-
@KickButtowski yes, I searched, but, I didn't find in the way that i wanted it – Morpheus Curriculum Vitæ Jun 07 '17 at 01:27
-
If you use one of the `scanf()` family of functions, you simply check the value the function returns: it is the number of successful conversions. So, if you try to read `int x;` from standard input, you can use e.g. `if (scanf("%d", &x) == 1) { /* Thank you for the value of x */ } else { /* Bad user! Not an integer! */ }` – Nominal Animal Jun 07 '17 at 01:29
-
@NominalAnimal thanks man, it worked – Morpheus Curriculum Vitæ Jun 07 '17 at 01:35
-
@KickButtowski Google isn't an authority on programming; unfortunately, in the arena of C programming specifically, Google returns results which are *invalid* and *non-portable*. Please don't make that recommendation again. – autistic Jun 07 '17 at 05:39
-
@user8120919 All of that information and more can be found within [the `fscanf` manual](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html). In the future, please keep in mind that C programmers **are expected** to read manuals in order to avoid undefined behaviour. The Open Group has some nice manuals; they're usually fairly in sync with the C standard manuals, owing to the adoption of C as a system language for Opengroup/POSIX-compliant UNIX systems. – autistic Jun 07 '17 at 05:42
-
@Seb ok, i'll see this manuals, thanks – Morpheus Curriculum Vitæ Jun 08 '17 at 10:40
1 Answers
0
Try using isdigit(int c). It returns a non-zero value if c in a digit, otherwise il resturs 0. For more information type 'man isdigit'

ddannel
- 51
- 3
-
I have searched this function before, but when I tested, was having errors, I searched more now and from what I understood, have to use char for this function, not int, which was that I was using – Morpheus Curriculum Vitæ Jun 08 '17 at 10:54
-
No, @MorpheusCurriculumVitæ, you have to use an `int` which stores an `unsigned char` value or `EOF`. What did I write about manuals earlier? You can [find the `isdigit` manual from the OpenGroup standards here](http://pubs.opengroup.org/onlinepubs/9699919799/functions/isdigit.html). It says, and I quote, "The *c* argument is an **int**, the value of which the application shall ensure is a character representable as an **unsigned char** or equal to the value of the macro EOF. If the argument has any other value, the behavior is undefined." The emphasis is not mine. – autistic Jun 09 '17 at 04:46
-
@ddannel See the above comment for my protest against your sentence *"It returns a non-zero value if c in a digit, otherwise il resturs 0."* – autistic Jun 09 '17 at 04:48