Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4260082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:52:20+00:00 2026-05-21T05:52:20+00:00

I wrote the following code to read a character array and print it. #include<stdio.h>

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:52:21+00:00Added an answer on May 21, 2026 at 5:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following is my code to write some hardcoded int values(1,2,3,4,5) into a file,
I'm new to Java. I wrote the following code: import java.io.*; import java.lang.*; public
Based on my reading, the following code: string aggregate = give + 'n'; Should
I need to write the following line of code: ((eq item #\) (do-this...)) But
I'm trying to write code to read a binary file into a buffer, then
Valgrind detect an invalid write of size 1 in this piece of code. In
I have some old (~1995) legacy fortran code which is compiled with g77 compiler
It seems that GHC is at least inconsistent in the character encoding it decides
I've asked this question on Wikipedia's Reference Desk , but got no answer. On

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.