C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Point Out Errors (Q.No. 2)
2.
Point out the error in the program?
#include<stdio.h>

int main()
{
    char ch;
    int i;
    scanf("%c", &i);
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}
Error: suspicious char to in conversion in scanf()
Error: we may not get input for second scanf() statement
No error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
23 comments Page 1 of 3.

Shekhar said:   7 years ago
As shown in first, scanf we can convert integer input into character using ASCII alue but second scanf get the character input using %d format specifier it cannot get converted into interger value because of precision concept in c, character consumes 1 byte and integer 4 bytes- in c we can easily store 1byte into 4bytes but we cannot stores 4bytes into 1byte.
(3)

Mishat said:   3 years ago
@All.

#include<stdio.h>

int main()
{
char ch;
int i;
scanf("\n%d", &i);
scanf("\n%c", &ch);
printf("%d %c", i, ch);
getch();
return 0;
}

In the case of integer it is %d and for character it ic %c.
(2)

Dhruv gupta said:   7 years ago
But in {B} option printf give output in ASCII value.

So, I think option A is correct.
(2)

Madhu Yadav said:   7 years ago
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c", &i);
scanf("%d", &ch);
printf("%c %d", i, ch);
return 0;
}
in printf("%c %d", ch, i) statement-

You can store the character value inside integer value because character is of 1 byte and integer is 2 byte but you can not store integer value inside character value.

Therefore if we change this statement printf("%c %d", ch, i) we should write it as
printf("%c %d", i, ch);

I compiled and run, there was no error.
(2)

ALERSHNAN said:   7 years ago
I GOT THE CORRECT ANSWER, option (B).

REASON: We should be knowing that char datatype can contain alphabet, "NUMBER", empty space etc. Thus in the first scanf, character specifier is used to get a "NUMBER" of integer datatype 'i'. And in the second scanf, integer specifier is used to get a character of char datatype 'ch' which is not possible and showing compilation error.
(1)

Ivon said:   9 years ago
But why I use CodeBlock (GCC compiler) to get it compiled and each scan runs smoothly, just getting wrong value, but it just got value for each place!

Rutuja Revadekar said:   3 years ago
I thought the error was in printf statement.

Pooja said:   5 years ago
I couldn't get the answer can you explain properly?

Prahlad said:   8 years ago
When I'm compiling in ubuntu I'm just getting a warning not an even error and how can the answer be error?

Please help me.

Nidipa said:   8 years ago
Here they did not flush the buffer, so it can not ready to get the next input.

When I tried the code using the following method:
scanf("%c\n", &i);
scanf("%d", &ch);
printf("%c %d", ch, i);

It gets the two inputs from the user.


Post your comments here:

Your comments will be displayed after verification.