Why Scanf is not run properly ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why Scanf is not run properly ?

#include<stdio.h> void Bubble_Sort(char A[],int size) { int i,round; char temp; for(round=0;round<size;round++) { for(i=0;i<round-1;i++) { if(A[i]>A[i+1]) { temp=A[i]; A[i]=A[i+1]; A[i+1]=temp; } } } for(i=0;i<size;i++) { printf("%c\t",A[i]); } } main() { char A[100]; int i,size; printf("\nEnter Your Size Of Array"); scanf("%d",&size); for(i=0;i<size;i++) { scanf("%c",&A[i]); } Bubble_Sort(A,size); }

8th Aug 2021, 7:34 AM
Sumit Kumar
Sumit Kumar - avatar
6 Answers
+ 4
It probably doesn't run right because when you scan for characters, spaces and new line characters ALSO count as characters. They're stored but not visible. Look at the spaces i give in the call to scanf(" %c ", &A[i]);
8th Aug 2021, 9:35 AM
Slick
Slick - avatar
+ 1
works fine here w/ chars. idk what you're trying to do //input: 3 a b c //output: Size: 3 Array values: a b c https://code.sololearn.com/cgkjdo6n7Mk4/?ref=app
8th Aug 2021, 9:32 AM
Slick
Slick - avatar
+ 1
Yeah, you'll have to look through my codes and make some changes for yourself though. Mine works fine, probably because i sort and display in different functions. If they are in functions at all
8th Aug 2021, 9:53 AM
Slick
Slick - avatar
0
I just changed all the character variables to integer variables and made it scan for integers. //input: 3 2 1 3 //output: Size: 3 Array values: 1 2 3 https://code.sololearn.com/cLC12UjYhYL1/?ref=app
8th Aug 2021, 9:22 AM
Slick
Slick - avatar
0
If we use char type value Why Scanf is not run properly?
8th Aug 2021, 9:27 AM
Sumit Kumar
Sumit Kumar - avatar
0
Input = 3 b d a Output = a b d i am thinking to do this ! can you do ??
8th Aug 2021, 9:38 AM
Sumit Kumar
Sumit Kumar - avatar