0

I'm trying to read and print array of characters using scanf. This is my source code. May I please know what is the error in this code?

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

void main()
{
    int i;
    char str[20];
    printf( "Enter\n" );
    for ( i=0; str[i] != '\0'; i++ )
        scanf( "%s", &str[i] );
    for ( i=0; str[i] != '\0'; i++ )
        scanf( "%s\n", str[i] );
}
6
  • will you please tell us what error you are getting when you run this code? Commented Mar 28, 2014 at 13:19
  • 1
    A tip: your char str[20] is an array of chars, not strings Commented Mar 28, 2014 at 13:22
  • 3
    I would suggest you to read some basic C book. Commented Mar 28, 2014 at 13:22
  • @JoachimPileborg: I just shouldn't post that close to the weekend. I always screw it up on Fridays. ;-) Commented Mar 28, 2014 at 13:35
  • 1
    @user3472728: Teacher's question. When execution reaches the for loop for the first time, what's the value of str[i]? Commented Mar 28, 2014 at 13:38

2 Answers 2

3

You have at least three problems: The first is that str is a single string, not an array of strings, that would have been e.g.

char str[20][20];

The second problem is that you apparently try to print the "strings" with scanf.

The third problem is that you are using the array str as it was initialized. Local variables are not initialized, and they values are indeterminate. That means that the contents of str will seem to be random. Using uninitialized local variables, like you do in your first loop, leads to undefined behavior.


One way of fixing (parts) of the code could be like

char strings[20][20];

int i;
for (i = 0; i < 20; ++i)
{
    char *p = fgets(strings[i], sizeof(strings[i]), stdin);
    if (p == NULL)
        break;  /* Error reading, or "end of file" */

    /* The fgets function can leave the newline in the buffer, remove it */
    if (strings[i][strlen(strings[i]) - 1] == '\n')
        strings[i][strlen(strings[i]) - 1] = '\0';
}

for (int j = 0; j < i; ++j)
    printf("String #%d: '%s'\n", i + 1, strings[i]);
Sign up to request clarification or add additional context in comments.

3 Comments

how can I read an "array of char" using scanf in a similar way that we use for reading "array of numbers"? Can you please suggest modifications in my source code.
and the 4th problem is you haven't ordered that C programming book yet.
I want the source code without string functions i.e using basic functions.
0
#include <stdio.h>

int main(){
    int i;
    char str[20][32];
    printf("Enter\n");
    for (i=0; i < 20; ++i)
        scanf("%31s", str[i]);
    for (i=0; i < 20; ++i)
        printf("%s\n", str[i]);
    return 0;
}

#include <stdio.h>

int main(){
    int i, pos;
    char *str[20];
    char array[20*32];

    printf("Enter\n");
    for (pos=i=0; i < 20; ++i){
        int len;
        if(EOF!=scanf("%31s%n", &array[pos], &len)){
            str[i] = &array[pos];
            pos += len + 1;
        } else {
            str[i] = NULL;
            break;
        }
    }
    printf("\n");
    for (i=0; i < 20 && str[i] != NULL; ++i)
        printf("%s\n", str[i]);
    return 0;
}

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.