Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
Why are the strings not printing in my Array?
#include<stdio.h>
int main()
{
    char arr[100], i;
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    printf("\nEnter the Strings in the Array\n");
    for(i=0; i<n; i++)
    {
        scanf("%s", &arr[i]); 
    }

    for(i=0; i<n; i++)
    {
        printf("%s", arr[i]);
    }
    return 0;
} 

Why does the execution of this code just stop after taking the input of string variables? Why isn't it printing the string in array?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You are overwriting the same string every time but starting one deeper in the array on every iteration. You can see the problem more easily when the array has a size of 4.

  char arr[4], i;
  int n = 4;

  printf("\nEnter the Strings in the Array\n");
  for(i=0; i<n; i++)
  {
      scanf("%s", &arr[i]); 
  }

Now the first time when you enter a string more than 3 characters your application will fault. The second time more than 2 characters because you start in the same string +1 byte. The address of arr + 1.

The third time only 1 bytes is still valid, etc.

Change your array into something like char arr[100][100]; Then you have 100 char arrays of array of 100 big. Also remove ampersand from the scanf as at that point arr[i] already is an address.

scanf("%s", arr[i]);

Same as:

scanf("%s", &arr[i][0]);
over 4 years ago · Santiago Trujillo Report

0

Source:

#include<stdio.h>
int main(){
    //stores the size of the array
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    //n represents number of strings you want to store
    char arr[n][100], i;
    printf("\nEnter the Strings in the Array\n\n");
    for(i=0; i<n; i++){
        scanf("%s", arr[i]); 
    }

    printf("\nOutput:\n\n");
    for(i=0; i<n; i++){
        printf("%s\n", arr[i]);
    }
    return 0;
} 

Output:

$ gcc array.c && ./a.out 
Enter the size of the Array: 3

Enter the Strings in the Array

first
second
third

Output:

first
second
third

If you want to scan spaces also :

Source:

#include<stdio.h>
int main(){
    //stores the size of the array
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    //n represents number of strings you want to store
    char arr[n][100], i;
    printf("\nEnter the Strings in the Array\n\n");
    for(i=0; i<n; i++){
        //flushs the previous buffer
        getchar();
        //It will read spaces now
        scanf("%[^\n]s", arr[i]); 
    }

    printf("\nOutput:\n\n");
    for(i=0; i<n; i++){
        printf("%s\n", arr[i]);
    }
    return 0;
} 

Output :

$ gcc array.c && ./a.out 
Enter the size of the Array: 3

Enter the Strings in the Array

first string
second 
third string...

Output:

first string
second
third string...
over 4 years ago · Santiago Trujillo Report

0

The line

char arr[100];

will only allocate space for a single string of up to 100 characters (99 characters plus the terminating null character). If you want to allocate space for 100 strings of 100 characters each, you should write this instead:

char arr[100][100];

Another problem is that using scanf with the %s conversion format specifier will only read a single word, not a whole line of input. If you want to read a whole line of input instead, you should use the fgets function instead.

Here is an example program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRINGS 100
#define MAX_STRING_LENGTH 100

int main( void )
{
    char strings[MAX_STRINGS][MAX_STRING_LENGTH];
    int n;
    int c;

    //read number of strings
    printf( "Enter the number of strings: " );
    if ( scanf( "%d", &n ) != 1 )
    {
        printf( "input error!\n" );
        exit( EXIT_FAILURE );
    }

    //verify that number of strings is acceptable value
    if ( ! ( 0 <= n && n <= MAX_STRINGS ) )
    {
        printf( "Input must be between 0 and %d.\n", MAX_STRINGS );
        exit( EXIT_FAILURE );
    }

    //discard remainder of input line
    do
    {
        c = getchar();
    }
    while ( c != EOF && c != '\n' );

    //input all strings
    for ( int i = 0; i < n; i++ )
    {
        //prompt user for input
        printf( "Enter string #%d: ", i + 1 );

        //attempt to read one line of input
        if ( fgets( strings[i], sizeof *strings, stdin ) == NULL )
        {
            printf( "input error!\n" );
            exit( EXIT_FAILURE );
        }

        //remove newline character from input
        strings[i][strcspn(strings[i],"\n")] = '\0';
    }

    //output all strings
    printf( "\nOutput:\n\n" );
    for( int i = 0; i < n; i++ )
    {
        printf( "%s\n", strings[i] );
    }
}

This program has the following behavior:

Enter the number of strings: 3
Enter string #1: This is a test.
Enter string #2: This is another test.
Enter string #3: This is yet another test.

Output:

This is a test.
This is another test.
This is yet another test.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!