Hello!

I enter the month number, number 1 - and this is January (I just have the 'y' character). How can I output the entire string Yanvar without using an array?

#include <stdio.h> int main() { int m1; char c; scanf("%d", &m1); c = m1 == 1 ? 'y' : m1 == 2 ? 'f' : m1 == 3 ? 'm' : 'o'; printf("%d -> %cn", m1, c); } 

    3 answers 3

     char* month = "Yanvar"; printf("%s", month); 
    • accept! =) - marioxxx
    • one
      @marioxxx, but note that month is an array . Simply, he is so syntactically disguised. Usually such arrays are called string constants . I wonder how the original (completely) looked like your task? Do not take the trouble to publish. - avp
    • month is a pointer to an array - Yury Shadchnev

    If you want without an array of strings, then stupidly through a splitter

     switch (m1) { case (1) : {puts ("January"); break;} case (2) : ....... ........... default: { puts ("Wrong number"); break;} } 
    • 2
      Exactly, the author apparently wanted something like that. You can even instead of puts () set the pointer to the name of the month, and type after the switch. - avp
    • You can too. May reduce file size slightly. - skegg
    • case is also an option. instead of puts how to use a pointer? - marioxxx
    • one
      What? )))))) - skegg
    • one
      For example, so char * p; switch (m1) {case 1: p = "Jan"; break; case 2: p = "Feb"; break; ...... default: p = "???"; } puts (p); Not so urgent you needed answers. - avp

    Print character by puthchar ():

     void main() { putchar('Y'); putchar('a'); putchar('n'); putchar('v'); putchar('a'); putchar('r'); putchar('\n'); } 
    • and the remaining months what?) - marioxxx