0

I was trying to write a code in c which converts a day month year format to dayRD day of Month Year

For example: 1 12 2020 must be converted like 1st day of december 2020.

I wrote the code as ı write down the page but the output is not the output that ı want.

#include<stdio.h>
int main() {
int day,month,year;
printf("Please enter date in a format of day month year");
scanf("%d%d%d",&day,&month,&year);

int dayconvert(day) {
    switch (day) {
        case 1:printf("%dst day of",day);
        break;
        case 2:printf("%dnd day of",day);
        break;
        case 3:printf("%drd day of",day);
        break;
        case 4:printf("%dth day of",day);
        break;
        case 5:printf("%dth day of",day);
        break;
        case 6:printf("%dth day of",day);
        break;
        case 7:printf("%dth day of",day);
        break;
        case 8:printf("%dth day of",day);
        break;
        case 9:printf("%dth day of",day);
        break;
        case 10:printf("%dth day of",day);
        break;
        case 11:printf("%dst day of",day);
        break;
        case 12:printf("%dnd day of",day);
        break;
        case 13:printf("%drd day of",day);
        break;
        case 14:printf("%dth day of",day);
        break;
        case 15:printf("%dth day of",day);
        break;
        case 16:printf("%dth day of",day);
        break;
        case 17:printf("%dth day of",day);
        break;
        case 18:printf("%dth day of",day);
        break;
        case 19:printf("%dth day of",day);
        break;
        case 20:printf("%dth day of",day);
        break;
        case 21:printf("%dst day of",day);
        break;
        case 22:printf("%dnd day of",day);
        break;
        case 23:printf("%drd day of",day);
        break;
        case 24:printf("%dth day of",day);
        break;
        case 25:printf("%dth day of",day);
        break;
        case 26:printf("%dth day of",day);
        break;
        case 27:printf("%dth day of",day);
        break;
        case 28:printf("%dth day of",day);
        break;
        case 29:printf("%dth day of",day);
        break;
        case 30:printf("%dth day of",day);
        break;
        
            
    }
    
}

int monthconvert(month) {
    switch (month) {
        case 1:printf("January");
        break;
        case 2:printf("February");
        break;
        case 3:printf("March");
        break;
        case 4:printf("April");
        break;
        case 5:printf("May");
        break;
        case 6:printf("June");
        break;
        case 7:printf("July");
        break;
        case 8:printf("August");
        break;
        case 9:printf("September");
        break;
        case 10:printf("October");
        break;
        case 11:printf("November");
        break;
        case 12:printf("December");
        break;
        
    }
}

printf("%d %d %d",dayconvert(day),monthconvert(month),year);

}

The output is January1st day of10 2020 when we enter 1 12 2020 values to day,month,year

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 5
    Your functions don't return anything. Your compiler should warn you about that. I think you need to work out whether you want the functions to return a result or to print the result. – kaylum Feb 07 '22 at 21:48
  • 1
    I don't believe nested functions are legal in standard C, though g++ seems to allow it. – Fred Larson Feb 07 '22 at 21:51
  • 2
    There are several misunderstandings imaginable. To find out what you misunderstood how, please explain in English what you think your functions are doing and what they are returning. What you think the last printf is going to print, seeing that you give a format specifier for three numbers. I do not see how you expect an output of "1st day of december 2020" by only outputting three numbers.... I mean "numbers" as opposed to "words which have a number-like meaning". – Yunnosch Feb 07 '22 at 21:57
  • Note also that 11, 12, and 13 should all have a "th" suffix. There is no "11st", "12nd", and "13rd". – Fred Larson Feb 07 '22 at 22:01
  • ı am explaining. first you see that there is a function that is named dayconvert.it is printing days in format of 1st 2nd 3rd... day of. monthconvert function does print the month for the number for example january for 1,february for 2... – Onur Yağcı Feb 07 '22 at 22:02
  • SSidenote. You'd better use `if` / `else` in `dayconvert`. If wold make the code much shorter – Jabberwocky Feb 07 '22 at 22:07
  • @FredLarson: The C standard **allows** nested functions in conforming C code. It does not **define** nested functions in conforming C code. The C standard is not a walled city that residents can never leave. It is an open plain with supporting services that anybody can use and plenty of places where people can build their own. C implementations are free to add their own extensions. – Eric Postpischil Feb 07 '22 at 22:11
  • Thanks for the explanation of waht the function does in your mind. It prints things. Now, when do you want that printing to happen and in which order? Why don't you call the functions in that order? Why do they return something? What is it that they return? Where do they actually return that? They pretend, in their prototype to return numbers. What are those numbers? What dou you want to do with them? They are numbers. Why do you output them? Why do you have a separate output at all, if you expect the functions to do the printing? – Yunnosch Feb 07 '22 at 22:20
  • @EricPostpischil: If you say so. All I know is I got `warning: ISO C forbids nested functions [-Wpedantic]` on the posted code. Perhaps newer C standards are more open. – Fred Larson Feb 07 '22 at 22:21
  • @FredLarson: It is just a misworded compiler message. The C standard has always allowed extensions such as this. – Eric Postpischil Feb 07 '22 at 22:24
  • You can combine lines like: `case 24: case 25: case 26: printf("%dth day of",day); break;` – i486 Feb 07 '22 at 22:50
  • 1
    @FredLarson — no; the C standard still does not, and I trust never will, define how nested functions work. If people want Pascal, they can find it. – Jonathan Leffler Feb 07 '22 at 23:13
  • Note that some months have 31 days, but your code doesn't seem to cover them. Your switch statement is decidedly non-minimal. – Jonathan Leffler Feb 07 '22 at 23:13

1 Answers1

2

at the end you do this

 printf("%d %d %d",dayconvert(day),monthconvert(month),year);

there is no guarantee what order these function calls are made. In fact you have shown that printf calls monthconvert, then dayconvert.

Monthconvert prints out the month name, then dayconvert prints out the day name.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • but ı wrote dayconvert before monthconvert in printf how it can be? – Onur Yağcı Feb 07 '22 at 22:12
  • 1
    @OnurYağcı because the order of evaluation of arguments to a function is not defined, the compiler can evaluate in any order it likes. If there are side effects (like your functions printing) it can lead to surprises (like you are surprised) – pm100 Feb 07 '22 at 22:19
  • @OnurYağcı https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c – pm100 Feb 07 '22 at 22:20
  • 2
    @OnurYağcı If those functions `monthconvert` and `dayconvert` are returning numbers for your final `printf` call to print, the functions shouldn't also print outputs of their own. It's just too confusing if they do. And if those functions didn't print anything, it wouldn't matter what order they got called in. – Steve Summit Feb 07 '22 at 22:27