Thread: Array to store integers and string

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    Array to store integers and string

    I want to store 7 integers and 7 string in array

    Here is program to store 7 integers

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       
       int date[8]= {1,2,3,4,5,6,7};
       
       for ( i = 0; i < 7; i++)
    	   printf ("Date  %d\n", date[i]);
       
       return 0;
    }

    Here is program to store 7 strings

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       
       char days[8][9] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
       
       for ( i = 0; i < 7; i++)
    	   printf ("Day %s \n", days[i]);
       
       return 0;
    }

    I have few doubts

    What is meaning of this line
    Code:
    int date[8]= {1,2,3,4,5,6,7};
    I have declared and initialize array

    Does it line mention that 7 integers has been stored ?


    Code:
        char days[8][9] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    Does it line mention that 7 string has been stored ?

    If we want to store multiple variables then we use array. I think both lines are storing integers and strings

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Does it line mention that 7 integers has been stored ?
    The rule is that if you have fewer initialisers than the declared size, the remainder will be filled with zero (or '\0' for chars, 0.0 for floats or NULL for pointers).

    So
    Code:
    int date[8]= {1,2,3,4,5,6,7,0};
    and
    char days[8][9] = {
        "Monday\0\0\0", 
        "Tuesday\0\0", 
        "Wednesday",
        "Thursday\0", 
        "Friday\0\0\0", 
        "Saturday\0", 
        "Sunday\0\0\0",
        "\0\0\0\0\0\0\0\0\0"};
    Incidentally, you need 10 characters to store "Wednesday" as a proper string.

    > If we want to store multiple variables then we use array.
    > I think both lines are storing integers and strings
    You would begin with a structure.
    Code:
    struct day {
      int dayNumber;
      char dayName[10];
    };
    
    // Now create an array of the structure and initialise
    // with the data we want.
    struct day days[7] = {
      { 1, "Monday" },
      { 2, "Tuesday" },
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    > Does it line mention that 7 integers has been stored ?
    The rule is that if you have fewer initialisers than the declared size, the remainder will be filled with zero (or '\0' for chars, 0.0 for floats or NULL for pointers).
    Please look at following programs

    Code:
    #include <stdio.h>
    int main()
    {
    
       int date[8]= {1,2,3,4,5,6,7};
    
       return 0;
    }
    Code:
    #include <stdio.h>
    int main()
    {
       int i;
    
       int date[8]= {1,2,3,4,5,6,7};
        
       for ( i = 0; i < 7; i++)
           printf("%d \n "date[i]);
    
    
       return 0;
    }


    In first program I am not using printf statement and loop and second program i am using printf statement and loop

    I know the printf statment is use to print on screen and for loop to repeat process 8 time

    Is my first program store the 1,2,3,4,5,6,7 value ?

    I think yes, because if we want to store integer or string we have to declare and initialize variable. I am doing both things here

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I know the printf statment is use to print on screen and for loop to repeat process 8 time
    No, for ( i = 0; i < 7; i++) loops 7 times, not 8 times.

    Try making the loop < 8, then you'll see that 0 is the last value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-02-2014, 05:30 PM
  2. How to store a user input string into dynamic array?
    By sethwb in forum C++ Programming
    Replies: 4
    Last Post: 04-25-2011, 04:48 AM
  3. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  4. store function in string array? possible?
    By sappy in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2005, 12:30 PM
  5. Extract numbers as integers from a string array
    By DarthC in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2002, 05:09 PM

Tags for this Thread