3

I wrote the following code to read a character array and print it.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void read_array(char a[],int n);
void print_array(char a[],int n);
int main(void)
{
    char a[100];
    int n;
    printf("\nEnter n:");
    scanf("%d",&n);
    printf("\nEnter the characters:");
    read_array(a,n);
    printf("\nThe array now is: ");
    print_array(a,n);
    getch();
    return 0;
}

void read_array(char a[],int n)
{
     int i;
     for(i=0;i<n;i++)
         scanf("%c",&a[i]);

}
void print_array(char a[],int n)
{
     int i;
     for(i=0;i<n;i++)                
        printf("a[%d]=%c\n",i,a[i]);
}

Input:

Enter n:15  
Enter the characters:xxxxx     xxxxx  

Output:

The array now is:  
a[0]=    
a[1]=x    
a[2]=x    
a[3]=x    
a[4]=x    
a[5]=x    
a[6]=    
a[7]=    
a[8]=    
a[10]=    
a[11]=x    
a[12]=x   
a[13]=x    
a[14]=x    

Where in my input a[5] through a[9] are blank characters. So how come in the output a[0]=(a blank)?

5
  • Are you sure it's blank? Or does your output have a blank line between a[0]= and a[1]= Commented Apr 5, 2011 at 18:31
  • Rolled back. Input and output belong in quote blocks, not code blocks. Commented Apr 5, 2011 at 18:32
  • 1
    @GEOCHET, says who? What if the I/O doesn't make sense without fixed-width formatting? Not to mention some highlighting comes for free with code blocks. Meta also seems to disagree with you: meta.stackexchange.com/questions/52502 Commented Apr 5, 2011 at 22:45
  • 1
    @carl: Syntax highlighting is useless on non code blocks and <pre> works just fine for fixed width. Commented Apr 6, 2011 at 15:18
  • I have to go with @GEOCHET here, syntax highlighting is usually annoying for non-code blocks. However, pre tag was required here since the spacing is important in all those x's (so I added it). Commented Jun 10, 2011 at 0:22

3 Answers 3

2

The first character you're reading in is the newline you typed to enter the 15. Use fgets() and sscanf() - you'll be much happier.

Sign up to request clarification or add additional context in comments.

3 Comments

doesn't scanf skip over all the whitespaces.
@user567797, that depends on how you use it, I guess. That's the normal use case, though. You can just use getch() for your second set of input if that makes it easier.
Actually, you don't need *scanf here (for the string anyway, you still need it for the number) since fgets will give you a string. And, if you're looking for a robust input method, see stackoverflow.com/questions/5130723/…
1

In the scanf function for getting the values of character use getche or getchar function. This will allow you to capture all the characters including new line. you can skip the first character and copy the rest.

Comments

0

While taking input in case of char array using scanf, it also captures the enter key that you presses while entering the input on a new line, so this problem is happening.

You may use getchar if you want each character to be present as input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.