-1

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;
}
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It should be `i <= 6`. Check your array extents. – Bib Jul 16 '22 at 18:43
  • can you explain what's meaning add * because i don't think it's a pointer – user3943 Jul 16 '22 at 18:56
  • A `char` is a single character (technically speaking for C, a `char` is something that can be stored in a byte). Something like `"Monday"` is a C-string of 6 characters plus the null terminator `\0`, so it's a sequence of `char`. You can't correctly store multiple characters in a single `char`, so it's represented by something like `char*`. Also see [string literals](https://en.cppreference.com/w/c/language/string_literal) - this is from cppref but C++ took its basic string literals from C. – wkl Jul 16 '22 at 19:05
  • 1
    If you did not get compiler warnings for this code, fix your compiler settings. If you ignored the warnings fix that before posting a question - or ask about the warnings. You could also set warnings to errors by compiler switch to force you to correct all warnings. The quality and semantic correctness of your code will improve. Warnings are a development aid, not an annoyance to be ignored or disabled. – Clifford Jul 16 '22 at 19:32
  • _"i don't think it's a pointer"_ : You think wrong. When you assign a string literal you are actually assigning a pointer to the first character of the string. The string literal is also `const` so strictly you should have `const char*` - attempting to modifying a string literal through `string[i]` results in undefined behaviour, `const` prevents that. C allows the array of to be non-const for historical reasons. Depending on your compiler you can probably achieve stronger type agreement via compiler switches - or even compile as C++ with stronger type agreement rules. – Clifford Jul 16 '22 at 19:42

2 Answers2

2

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

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;
}
wkl
  • 77,184
  • 16
  • 165
  • 176