1

scanf's loop is not working for index (which is 2). In the printf loop, I am getting garbage output for two indexes. I don't know what is happening here.

#include <stdio.h>
int main() {
    int i;
    char name[3];
    float price[3];
    int pages[3];

    printf("Enter names, prices and pages of 3 books:\n ");
    for (i = 0; i <=2; i++) {
        scanf("%c%f%d", &name, &price, &pages);
    }
    printf("what you entered:\n");

    for (i = 0; i <=2; i++) {
    printf("%c %f %d\n", name[i], price[i], pages[i]);
    }
    return 0;
}

This program is from the book "Let us C", first page of the structure chapter.

My real output (what I am getting) is:

Enter names, prices and pages of 3 books:

a 100 200 // given by me  
b 100 200

// but not able to give third index values

what you entered:

b 100.000000 200

84227675241280636545541341184\.000000 0

0\.000000 70

This is a simple program and according to me, I should get like the below output.

My output is what I am expecting:

 Enter names, prices and pages of 3 books:

a 100 200  
b 100 200

c 100 200  

what you entered:

a 100.000000 200  
b 100.000000 200

c 100.000000 200
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Please always check the value returned by `scanf`: the number of items converted. – Weather Vane Mar 09 '23 at 15:40
  • 2
    Also, only the first element is being entered. Suggest `scanf(" %c%f%d", &name[i], &price[i], &pages[i]);`. Also note the space before `%c`. – Weather Vane Mar 09 '23 at 15:42
  • @WeatherVane `name` is not an array of strings, it's just one string. – Barmar Mar 09 '23 at 15:43
  • @Barmar do you mean that `name` is *supposed* to be an array of strings? Why is that addressed to me, and not the OP? – Weather Vane Mar 09 '23 at 15:44
  • 1
    @WeatherVane Oh right, just noticed that they're using `%c`, not `%s` for it. – Barmar Mar 09 '23 at 15:45
  • OT: the loops are more idiomatic as `i < 3;` instead of `i <= 2;`. One reason for this, is the hard-coded "magic numbers" `2` and `3` which would be better as the *same* magic number as they refer to the same limit. – Weather Vane Mar 09 '23 at 15:47

2 Answers2

2

You should to put to scanf() an address for the next element of array at each iteration, but you pass address to first element every time instead of that. Try to write:

scanf("%c%f%d", &name[i], &price[i], &pages[i]);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
re1neke
  • 21
  • 3
  • 3
    Also need a space before `%c` to skip whitespace. See https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why – Barmar Mar 09 '23 at 15:46
2

The array name is declared like

char name[3];

The expression &name has the type char ( * )[3] while the conversion specifier c expects a corresponding argument of the type char *.

Also entering a character using the conversion specifier c you need to skip white space characters as for example the new line character '\n' stored in the input buffer after pressing the key Enter.

So you need to write

printf("Enter names, prices and pages of 3 books:\n ");
for (i = 0; i <=2; i++) {
    scanf( " %c%f%d", name + i, price + i, pages + i );
}

In this call the expression name + i is equivalent to &name[i]. The same is valid for other two arrays.

Pay attention to the leading space in the format string. It allows to skip white space characters.

Also it is a bad style of programming to use magic numbers like 3 and 2. Instead you could introduce a named constant like for example

enum { N = 3 };
char name[N];
float price[N];
int pages[N];

printf("Enter names, prices and pages of %d books:\n", N);
for ( i = 0; i < N; i++) {
    scanf( " %c%f%d", name + i, price + i, pages + i );
}
//...
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335