Home > database >  output weekdays using char string and for loop
output weekdays using char string and for loop

Time:07-17

I try to output weekdays using a char string and a for loop but the output is (null)

#include <stdio.h>

int main()
{
    int i;
    char string[7];

    string[0] = "Monday";
    string[1] = "Tuesday";
    string[2] = "Wednesday";
    string[3] = "Thursday";
    string[4] = "Friday";
    string[5] = "Saturday";
    string[6] = "Sunday";


    for (i = 0; i <= 7; i  )
    {
      printf ("%s", string[i]);
    }
    return 0;
}

CodePudding user response:

The compiler should tell you all you need to know. At https://onlinegdb.com/TTMwKS8GJ:

main.c: In function ‘main’:
main.c:12:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   12 |     string[0] = "Monday";
      |               ^
main.c:13:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   13 |     string[1] = "Tuesday";
      |               ^
main.c:14:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   14 |     string[2] = "Wednesday";
      |               ^
main.c:15:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   15 |     string[3] = "Thursday";
      |               ^
main.c:16:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   16 |     string[4] = "Friday";
      |               ^
main.c:17:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   17 |     string[5] = "Saturday";
      |               ^
main.c:18:15: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   18 |     string[6] = "Sunday";
      |               ^
main.c:23:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   23 |       printf ("%s", string[i]);
      |                ~^   ~~~~~~~~~
      |                 |         |
      |                 char *    int
      |                %d

string[] is an array of 7 characters not 7 strings. You are trying to assign character strings to single characters. C is permissive in that it will let you do that with an implicit cast; but it makes no sense semantically.

What the compiler does not tell you about, but which will cause a run-time error, is the out-of-bounds array access: string[7] is not a valid array element.

If you did not get similar warnings, fix your compiler settings. If you got warnings, but you ignored them you should either address them before posting the question or ask about the warnings if you do not understand them.

Fixing the errors and improving the "style":

#include <stdio.h>

int main( void )
{
    const char* const string[] = { "Monday",
                                   "Tuesday",
                                   "Wednesday",
                                   "Thursday",
                                   "Friday",
                                   "Saturday",
                                   "Sunday" } ;

    for( int i = 0; 
         i < sizeof(string) / sizeof(*string); 
         i   )
    {
        printf( "%s\n", string[i] ) ;
    }
    
    return 0 ;
}

This is now semantically correct and also const correct, devoid of magic numbers and has no temporarily unitialised variables.

CodePudding user response:

Your code has some issues and would cause segfaults, so it's interesting that whatever compiler you're using builds something that outputs null.

A couple issues:

  1. char string[7]; should be char* string[7]; - you're trying to store pointers-to-cstrings and not individual chars. For more correctness based on your code, you're also trying to just store const char*.
  2. Your loop condition should be i < 7 (or i <= 6) instead of i <= 7 because you have 7 items in your list, so your original loop condition would make it try to access an 8th item.

Here's corrected code that should work.

#include <stdio.h>

int main()
{
    int i;

    // or const char* string[7]
    char* string[7];

    string[0] = "Monday";
    string[1] = "Tuesday";
    string[2] = "Wednesday";
    string[3] = "Thursday";
    string[4] = "Friday";
    string[5] = "Saturday";
    string[6] = "Sunday";


    for (i = 0; i < 7; i  )
    {
        printf("%s\n", string[i]);
    }
    return 0;
}
  •  Tags:  
  • c
  • Related