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;
}
Discussion:
23 comments Page 1 of 3.
Madhu Yadav said:
6 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.
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.
(1)
NITESH KHATRI said:
1 decade ago
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.
Nilay Vishwakarma said:
1 decade ago
@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.
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.
Nidipa said:
7 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.
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.
ALERSHNAN said:
6 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.
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.
Shivam said:
9 years ago
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 :).
[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 :).
Shekhar said:
6 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.
(2)
Mishat said:
2 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.
#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)
Dinesh said:
10 years ago
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
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
Himanshu Bansal said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers