0

I just started learning array in C language.
1. Tryed to print out item names and prices, but getting error, While compiling no error gives, but when i run it gives error.
What did i do wrong here ?

Here is my code

   #include<stdio.h>

    int main(void){

        float p[7]={0.55,0.8,1,1.2,0.95,0.4,0.6};
        int i=1;

        printf("%s%10s\n","#Item","ItemName");
            for(i=1;i<8;i++){

                printf("%s%13f0.2\n",i,p[i]);
    }

        return 0;

    }

2.How i can list names? i tried with char but didnt worked well.

char n[10]={'Water','Cola','Fanta','Chokolate','Biscuit','Gum','Candy'};

this gives errl4 14 C:\Users\Erdene\Desktop\turshilt.c

[Warning] character constant too long for its type

if i change n[100] how can i list with Numbering and pricing?

like listing numbers and next to numbers Item and prices.
any advices? thanks

2
  • 1
    Arrays in C are indexed starting at 0 not at 1. So you're array index is out of bounds when it hits the value 7. It should go from 0 to 6. Commented Apr 12, 2014 at 0:54
  • 1
    enable all warnings, and you'll see something about multibyte character constant Commented Apr 12, 2014 at 1:15

3 Answers 3

2
float p[7]={0.55,0.8,1,1.2,0.95,0.4,0.6};

Arrays in C are indexed starting with 0, so your for loop should be

for(i=0;i<7;i++) { ... }

For the other part of your question:

char n[10]={'Water','Cola','Fanta','Chokolate','Biscuit','Gum','Candy'};

you should use double quotes for the strings (single quotes are for character constants). n should be an array of ten pointers, not ten characters, and it's good practice to make the array size the same as the number of initializers:

char *n[7]={"Water","Cola","Fanta","Chokolate","Biscuit","Gum","Candy"};
Sign up to request clarification or add additional context in comments.

1 Comment

I just realize that printf("%s%13f0.2\n",i,p[i]); part is wrong, printf("%s%13 0.2f\n",i,p[i]); is also wrong. How i can use that 13 and 0.2 together ?
1

You have to understand that there is a difference between an array of char type values, which is what you have, and an array of strings.

You denote a single char value using a number, or single quotes like so

char bar = 65;
char foo = 'A';

Both of these are the same value. char can only store one character of information, the size is restricted by a byte.

When you're declaring a string of chars use double quotes

char string[100] = "Hello!";

As an alternative to what Jim Lewis has posted, you can use a constant size array, if you know that at compile time. In this instance, you are required to specify all but the outer-most level size.

// This will store as many 19 letter words, as you specify in the declaration
char strings[][20] = {"One", "word", "two", "words"};
// The index starts at zero
printf("%s\n", strings[0]); // -> One
printf("%s\n", strings[1]); // -> word
printf("%s\n", strings[2]); // -> two
printf("%s\n", strings[3]); // -> words
printf("%s\n", strings[4]); // -> ERROR - out of bounds

Comments

0

Read the other answers to explain why. Read the example below for a full example of how to do this.

#include <stdio.h>

int main(void){

    // array sizes do not need to be explicitly set when initialized this way
    char *n[]={"Water","Cola","Fanta","Chokolate","Biscuit","Gum","Candy"};
    float p[]={0.55,0.8,1,1.2,0.95,0.4,0.6};
    int i;
    // calculate array length, so can change array length without breaking code
    int len = sizeof(n)/sizeof(*n);

    // tinker with printf if you want to make the columns wider, etc.
    printf("%s%11s%9s\n", "Item #", "Item Name", "Price");
    for(i = 0; i < len; i++) {
        printf("%6d  %-10s%8.2f\n", i + 1, n[i], p[i]);
    }
    return 0;
}

2 Comments

yea its working well, but how that printf("%6d %-10s%8.2f\n", i + 1, n[i], p[i]); printing perfect ? %-10s means to lefthand side 10 Isnt it ? but n[i] shows right under the ItemName how?
Header has %s%11s for "Item #" and "Item Name". "Item #" is printed as-is, 6 characters. "Item Name", whose length is 9, is printed right-justified in 11 spaces, providing 2 blank spaces before the text. As a result, the text "Item Name" starts in column 9. The detail line has "%6d %-10s", so likewise, n[i] is printed starting in column 9. The header could be rewritten as "%6s %9s %5s" to make it easier to see how they line up.

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.