Chanchhunneng chrea
Chanchhunneng chrea

Reputation: 27

How to print the days of the week using a structure member?

I want to print the days of the week in this program but it doesn't work, what can I do to fix it ?

#include<stdio.h>
struct month{
    int date[12];
    char day[7];
}mon;
main()
{   int i;

    strcpy(mon.day[0],"Sunday");
    strcpy(mon.day[1],"Monday");
    strcpy(mon.day[2],"Tuesday");
    strcpy(mon.day[3],"Wednesday");
    strcpy(mon.day[4],"Thursday");
    strcpy(mon.day[5],"Friday");
    strcpy(mon.day[6],"Saturday");

    for(i=0;i<7;i++)
    {
        printf("Day %d is %c\n",i+1,mon.day[i]);
    }

}

Upvotes: 1

Views: 969

Answers (3)

KL-Yang
KL-Yang

Reputation: 401

For constants like day and month, better constructed like the following, unless you are practicing with struct.

#include<stdio.h>
int main()
{   
    const char *day[] = {"Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday" };
              //^^^^^ array of strings.
    for(int i=0;i<7;i++)
        printf("Day %d is %s\n",i+1,day[i]);
    return 0;
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134376

If you try to compile your program, your compiler will give you certain warnings. Like

  • Problem 1:

    source_file.c:10:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
     main()
     ^
    

    The proper signature of main() is int main(void), for usual hosted environments.

  • Problem 2:

    source_file.c: In function ‘main’:
    source_file.c:13:5: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
         strcpy(mon.day[0],"Sunday");
         ^
    source_file.c:13:5: warning: incompatible implicit declaration of built-in function ‘strcpy’
    source_file.c:13:5: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
    

    You'd need to include string.h header file which contains the declaration for strcpy().

  • Problem 3:

    source_file.c:13:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
         strcpy(mon.day[0],"Sunday");
    

    This indicates, the first argument of strcpy() expects a char *, but you're passing a char, casted to an int.

    This is the most important point here, as you'd see, you need to pass a buffer which can contain the content from the copied string. Thu's, you'd need

  • day to be a 2-D array

  • use day[i] to store the content
  • use %s to print the content thereof.

Upvotes: 2

Blaze
Blaze

Reputation: 16876

You are trying to store strings in char. However, a string is an array of char. Instead of this:

char day[7];

You need something such as:

char day[7][16];

Now you have 16 char to fit in a weekday, and you have that seven times, one for each day. Also, your print is wrong. To print strings, you need %s, not %c. %c is just for one single char:

printf("Day %d is %s\n", i + 1, mon.day[i]);

Another way of doing it is to have an array of char* instead of char and only storing the address of the strings rather than copying the strings:

char *day[7];

Then store the addresses like this:

mon.day[0] = "Sunday";
mon.day[1] = "Monday";
mon.day[2] = "Tuesday";
...

And print it the same way, with %s.

Upvotes: 4

Related Questions