C Programming - Input / Output - Discussion

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;
}

[A]. Error: suspicious char to in conversion in scanf()
[B]. Error: we may not get input for second scanf() statement
[C]. No error
[D]. None of above

Answer: Option B

Explanation:

No answer description available for this question.

Shiv Bhajan said: (Sep 23, 2012)  
Why it will not get input for the second scanf () ? please Explain it.

Pratham said: (Oct 7, 2012)  
You will not get a chance to supply a character for 2nd scanf().

Nitesh Khatri said: (Oct 18, 2012)  
Because this is the peculiarity of scanf(). After supplying data for the int i variable we would hit the enter key. What scanf() does it assigns the value what we gave to the variable i and keeps the enter key unread in the keyboard buffer (standard i/o buffer) , so when its time to supply another value the 2nd scanf() statement will read the enter key from the buffer thinking that user has entered the enter key. To AVOID this problem. WE USE THE FUNCTION flush (stdin). It is designed to remove or 'flush out' any data remaining in the buffer.

Khushi said: (Dec 24, 2012)  
No its not the case.

It will take input but the values that will get stored will be unexpected and it may lead to segmentation fault as well.

Shoana Carvalho said: (Mar 12, 2013)  
What I guess is segmentation fault occurs only when there is buffer overflow comes into existence because of too many function calls!

Rup said: (Mar 12, 2013)  
'i' is an integer type so %d should be used and 'ch' is a character type so %c or %s should be used.

Himanshu Bansal said: (Nov 17, 2013)  
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.

Nilay Vishwakarma said: (Nov 23, 2013)  
@Khushi.
There can't be any segmentation fault in this program
ch would store ASCII 10 (return or line feed).

Try cleaning buffer before entering next value since <return> would be in buffer and gets stored in next scanf.

@Himanshu Bansal.
Your point is almost correct, theoretically. However,we can store integer value inside character value, but there would be a slight ,malfunction. The least significant bits of the integer would be accepted as character.

@Rup.
The apple has fallen very far dude.

Dinesh said: (Dec 15, 2013)  
I executed this program in Dev c++.

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

Program compiled and executed successfully (Without any warning and errors).

Input provided : 65
Output : i==A

Shivi said: (Jul 19, 2014)  
Because %d is a format specifier of int type but ch is a char data type.

Shivam said: (Apr 22, 2015)  
Nice question!

[B]. Error: We MAY not get input for second scanf() statement.

"may".

-If you enter a character for first scanf, you are allowed to enter a value for second scanf.

-If you enter an integer for first scanf, you are not allowed to enter a value for second scanf.

Answer is simple just try think harder in memory size differences between int and char :).

Ivon said: (Dec 21, 2015)  
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!

Sowmiya said: (Jul 20, 2016)  
The 'i' is an integer type. So %d should be used and 'ch' is a character type so %c or %s should be used.

Afzal said: (Aug 20, 2016)  
@Nitesh Khatri. I understood your explanation.

But, is Shivam's explanation is right?

Does it depend on the Type of data ?

Nidipa said: (Nov 3, 2016)  
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.

Prahlad said: (Mar 1, 2017)  
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.

Madhu Yadav said: (Jul 31, 2017)  
#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.

Alershnan said: (Aug 15, 2017)  
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.

Shekhar said: (Dec 3, 2017)  
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.

Dhruv Gupta said: (May 12, 2018)  
But in {B} option printf give output in ASCII value.

So, I think option A is correct.

Pooja said: (May 3, 2020)  
I couldn't get the answer can you explain properly?

Rutuja Revadekar said: (Jul 22, 2021)  
I thought the error was in printf statement.

Mishat said: (Aug 26, 2021)  
@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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.