1
#include<stdio.h>

int main(){
  char name[3];
  float price[3];
  int i,page[3];

  printf("enter the name price and book\n");

  for(i=0;i<3;i++){

   scanf("%s",name[i]);
   printf("enter character:\n");
     }
   for(i=0;i<3;i++) {
   scanf("%f",&price[i]);
    printf("enter floating point number:\n");
     }
   for(i=0;i<3;i++) {
   scanf("%d",&page[i]);
   printf("enter digit:\n");
     }
   printf("\n");
   for(i=0;i<3;i++) {
   printf("%s\n",name[i]);
     }
   for(i=0;i<3;i++) {
   printf("%f\n",&price[3]);
     }
   for(i=0;i<3;i++){
   printf("%d\n",&page[i]);
     }
    
   return 0;

}

When I was trying this code, I thought this was quite simple to do but I realized, there is something which I am missing in the code. The main problem with this code is it is not scanning the values of price and pages. I don't understand where I am mistaken.

So, please correct my code so that it will print the values of name price and pages.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

1

There are a number of issues in your code. First and foremost, the %s specifier (for both the scanf and printf functions) expects a string argument (that is, a nul-terminated array of char for printf or an array sufficiently large to hold the input characters plus that terminator, for scanf); however, you are attempting to read (and print) a single char value in each of the relevant for loops.

To fix this, use the %c format specifier, instead of %s. However, when you use this, the newline character that is generated when you press the Enter key will be left in the input buffer, and that will be read as the actual char input on the next iteration of the first for loop. To clear any such newline (or, indeed other whitespace) from the input before the real input, add a space in the format string before the %c. Also, when using this, you will need to pass the address of each name element: scanf(" %c", &name[i]);.

Further, the printf function takes the actual values of the variables to be output, rather than their addresses – so remove the & from the arguments in your printf calls. (Also, and I assume it's a typo, the price[3] expression should be price[i] – the former attempts to access an out-of-bounds element of the price array.)

Another issue is that, in each of your input loops, you call the scanf function before you display the relevant prompt. In the code below, I have reversed your printf and scanf lines in each of those input loops.

Here's a possible fixed version:

#include<stdio.h>
int main()
{
    char name[3];
    float price[3];
    int i, page[3];
    printf("enter the name price and book\n");
    for (i = 0; i < 3; i++) {
        printf("enter character:\n");
        scanf(" %c", &name[i]);
    }
    for (i = 0; i < 3; i++) {
        printf("enter floating point number:\n");
        scanf("%f", &price[i]);
    }
    for (i = 0; i < 3; i++) {
        printf("enter digit:\n");
        scanf("%d", &page[i]);
    }
    printf("\n");
    for (i = 0; i < 3; i++) {
        printf("%c\n", name[i]);
    }
    for (i = 0; i < 3; i++) {
        printf("%f\n", price[i]);
    }
    for (i = 0; i < 3; i++) {
        printf("%d\n", page[i]);
    }
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • i had another doubt in the concept of pointers , that is an array of 5 elements A[5]={1,2,3,4,5} and now we assign the value of first element in pointer *p=a, then what is the value of *(a+0) , *(a+1) and *p i didnt understand the difference between them please clarify this. – user17725027 Mar 04 '22 at 03:13
  • @user17725027 If `p` points to the first element of `a` then `*(p+i)` is the same as `a[i]`. In fact, the C Standard even says that the array indexing expression is equivalent to the pointer addition one. – Adrian Mole Mar 04 '22 at 07:43
  • Its clear thank you – user17725027 Mar 04 '22 at 17:48
  • i was trying to write a code for printing of last word from a sentence , we should take input of a sentence and print last word in it without using any library functions in C except scanf and printf. if possible please give me a source code that will helps me. – user17725027 Mar 08 '22 at 15:11