How to get Integer and float input without `scanf()` in c?

user8418601

How can I assign a integer and float values given by user to a variable or array without using scanf()?

Like we have getchar,fgetc,fgets...etc for char and string, Is there any function for floats and integers ?

user7122093

There aren't functions to read integers and floats but you can use fgets with strtol for integers and strtof for floats:

// floats:
char str_f[20];
float f;

fgets (str_f, 20, stdin);
f = strtof(str_f, NULL);

// integers:
char str_i[20];
int i;

fgets(str_i, 20, stdin);
i = strtol(str_i, NULL, 0);

You can also use atoi for integers but it's not recommended because atoi doesn't detect errors and it's considered obsolete.

If you want to detect errors you can use the following code:

// floats:
char *endptr_f;
char str_f[20];
float f;

fgets (str_f, 20, stdin);
f = strtof(str_f, &endptr_f);

if (*endptr_f != '\n' || str_f[0] == '\n' || endptr_f == str_f)
{
    printf("ERROR: \"%s\" is an invalid float!\n", str_f);
}

// integers:
char *endptr_i;
char str_i[20];
int i;

fgets(str_i, 20, stdin);
i = strtol(str_i, &endptr_i, 0);

if (*endptr_i != '\n' || str_i[0] == '\n' || endptr_i == str_i)
{
    printf("ERROR: \"%s\" is an invalid integer!\n", str_i);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get integer input in an array using scanf in C?

From Dev

How to double a float/integer within a user input string in C?

From Dev

C scanf in loop continues automaticly without input

From Dev

How to print decreasing odd numbers starting from scanf input integer in C?

From Dev

How to print decreasing odd numbers starting from scanf input integer in C?

From Dev

How to avoid spaces, enter key or non-numeric input in C ? scanf reads integer value

From Dev

Checking if number is an integer in C/C++ (without scanf/gets/etc)

From Dev

How to let scanf() keep reading integer inputs unless a character is input?

From Dev

C-Checking if input (float) is purely integer or float

From Dev

C-Checking if input (float) is purely integer or float

From Dev

How to create optional input for scanf in C?

From Dev

How to print the char, integer, float and double values without using format specifiers in c

From Dev

C++ Input to a vector<float> without ending it with a non-float

From Dev

char Input in C by scanf

From Dev

C - How to check if the number is integer or float?

From Dev

How to get scanf to work in an if statement objective c

From Dev

Scanf() taking 0 instead of Float keyboard input

From Dev

How to convert Integer into float without changing the type to string in javascript

From Dev

scanf check in while loop to restrict integer input

From Dev

Using Scanf() for taking input for both string and integer

From Dev

How to get Integer input from Java Scanner

From Dev

How to get an input as either a letter or an integer?

From Dev

How do I get a UITextField input as an Integer

From Dev

How to get Integer of BigDecimal without separator

From Dev

How can you recognize with scanf in C that the input is not a number?

From Dev

get the integer representation value of a float type variable in C

From Java

How to use printf() and scanf() in C without going to the next line?

From Dev

How to read 2 lines of integer input in C?

From Dev

How to get a common integer in python from a float list?

Related Related

  1. 1

    How to get integer input in an array using scanf in C?

  2. 2

    How to double a float/integer within a user input string in C?

  3. 3

    C scanf in loop continues automaticly without input

  4. 4

    How to print decreasing odd numbers starting from scanf input integer in C?

  5. 5

    How to print decreasing odd numbers starting from scanf input integer in C?

  6. 6

    How to avoid spaces, enter key or non-numeric input in C ? scanf reads integer value

  7. 7

    Checking if number is an integer in C/C++ (without scanf/gets/etc)

  8. 8

    How to let scanf() keep reading integer inputs unless a character is input?

  9. 9

    C-Checking if input (float) is purely integer or float

  10. 10

    C-Checking if input (float) is purely integer or float

  11. 11

    How to create optional input for scanf in C?

  12. 12

    How to print the char, integer, float and double values without using format specifiers in c

  13. 13

    C++ Input to a vector<float> without ending it with a non-float

  14. 14

    char Input in C by scanf

  15. 15

    C - How to check if the number is integer or float?

  16. 16

    How to get scanf to work in an if statement objective c

  17. 17

    Scanf() taking 0 instead of Float keyboard input

  18. 18

    How to convert Integer into float without changing the type to string in javascript

  19. 19

    scanf check in while loop to restrict integer input

  20. 20

    Using Scanf() for taking input for both string and integer

  21. 21

    How to get Integer input from Java Scanner

  22. 22

    How to get an input as either a letter or an integer?

  23. 23

    How do I get a UITextField input as an Integer

  24. 24

    How to get Integer of BigDecimal without separator

  25. 25

    How can you recognize with scanf in C that the input is not a number?

  26. 26

    get the integer representation value of a float type variable in C

  27. 27

    How to use printf() and scanf() in C without going to the next line?

  28. 28

    How to read 2 lines of integer input in C?

  29. 29

    How to get a common integer in python from a float list?

HotTag

Archive