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

HARSHIT TIWARI

I was writing a simple C program to take inputs from user for different variables using scanf() as follows:

#include <stdio.h>
int main(){
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d\n",a);
    printf("%d\n",b);
    return 0;
}

The output for this is coming completely fine, as expected:

input: 10 23
output: 10
        23

But then I also tried to take input for a string array, as follows(here char c[2] is a string array):

#include <stdio.h>
int main(){
    int a, b;
    char c[2];
    scanf("%d %d %s", &a, &b, c);
    printf("%d\n",a);
    printf("%d\n",b);
    printf("%s\n",c);
    return 0;
}

And now the output was something unexpected:

input: 10 23 AM
output: 10
        0
        AM

Here, as it can be seen that the value being printed for variable b is coming to be 0, instead of the expected 23. How did taking input for a string array change the value of variable b?

Can someone help figure out, what wrong(of course silly mistake) have I done?emphasized text

Adrian Mole

Your char c[2]; variable is not a large enough array to hold a 2-character string plus the required nul-terminator. If you know that the input will be no more than 2 characters, changing to char c[3]; should be sufficient; however, for added safety, you can include a limit on how many characters will be read by the scanf call, using a format specifier like %2s (or, more generally, for an array such as char buff[n]; use a value of n - 1 in that format specifier).

As it stands, you have undefined behaviour – and this may include overwriting the value given for b (with the zero that should be the nul-terminator).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

error in taking input string after integer in java

From Dev

Taking input in integer /string and store it in array

From Dev

Read a string as an input using scanf

From Dev

Using element of list both as string and integer

From Dev

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

From Dev

Scanf() taking 0 instead of Float keyboard input

From Dev

taking string and integer in structure variable

From Dev

Taking input and using it for calculation

From Dev

Taking input using bufferedreader

From Dev

taking input using LocalDate

From Dev

selective input using scanf

From Dev

Difficulty using scanf for input

From Dev

Using string in switch statement, by taking input from user

From Dev

Why can't I read the input string using this scanf function?

From Dev

AT&T assembly + C functions. Using Scanf for string input

From Dev

segmentation fault using scanf with integer

From Dev

scanf check in while loop to restrict integer input

From Dev

using scanf to check input parameters

From Dev

Checking if an input is a double using scanf?

From Dev

Checking if an input is a double using scanf?

From Dev

using scanf to check input parameters

From Dev

Stop console input using scanf()

From Dev

Discord command taking an integer or a string in the same argumente

From Dev

scanf vs cin: string as integer processing

From Dev

scanf problems involving string input and string comparison

From Dev

Both scanf("%c", x) and x=getchar aren't waiting for input

From Dev

How to check if a value entered is an integer or string when using input

From Dev

How to check if a value entered is an integer or string when using input

From Dev

Haskell - Validate integer input from string using readMaybe

Related Related

  1. 1

    error in taking input string after integer in java

  2. 2

    Taking input in integer /string and store it in array

  3. 3

    Read a string as an input using scanf

  4. 4

    Using element of list both as string and integer

  5. 5

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

  6. 6

    Scanf() taking 0 instead of Float keyboard input

  7. 7

    taking string and integer in structure variable

  8. 8

    Taking input and using it for calculation

  9. 9

    Taking input using bufferedreader

  10. 10

    taking input using LocalDate

  11. 11

    selective input using scanf

  12. 12

    Difficulty using scanf for input

  13. 13

    Using string in switch statement, by taking input from user

  14. 14

    Why can't I read the input string using this scanf function?

  15. 15

    AT&T assembly + C functions. Using Scanf for string input

  16. 16

    segmentation fault using scanf with integer

  17. 17

    scanf check in while loop to restrict integer input

  18. 18

    using scanf to check input parameters

  19. 19

    Checking if an input is a double using scanf?

  20. 20

    Checking if an input is a double using scanf?

  21. 21

    using scanf to check input parameters

  22. 22

    Stop console input using scanf()

  23. 23

    Discord command taking an integer or a string in the same argumente

  24. 24

    scanf vs cin: string as integer processing

  25. 25

    scanf problems involving string input and string comparison

  26. 26

    Both scanf("%c", x) and x=getchar aren't waiting for input

  27. 27

    How to check if a value entered is an integer or string when using input

  28. 28

    How to check if a value entered is an integer or string when using input

  29. 29

    Haskell - Validate integer input from string using readMaybe

HotTag

Archive