1

I am trying to code a program that shows the Easter day when the user inputs the year between 1991 and 2099. All are good but when I am trying to run the program, it says there is an error: assignment to expression with array type. How can I fix this? and what is the wrong with my code?

#include <stdio.h>
#include <stdlib.h>

int main()
{
char month[50];
int year, day;
printf("Please enter a year between 1990 and 2099: ");
scanf("%d", &year);


int a = year-1900;
int b = a%19;
int c = (7*b+1)/19;
int d = (11*b+4-c)%29;
int e = a/4;
int f = (a+e+31-d)%7;
int g = 25 - (d + f);

if (g <= 0 )
{
    month = "March";
    day = 31 + g;
}
else
{
    month = "April";
    day = g;
}
    printf("The date is %s %d", month, day);

}
5
  • 1
    You don't assign to strings with =, you use strcpy(). Commented Oct 25, 2021 at 17:31
  • 3
    You could also change it to a pointer variable: char *month. Commented Oct 25, 2021 at 17:31
  • Thank you, it runs now Commented Oct 25, 2021 at 17:36
  • It's best to avoid rolling your own calendar functions. Calendars are weird. In this case it will probably barely suffice, though. Commented Oct 25, 2021 at 17:45
  • Always check the return value of scanf etc., and abort (e.g. abort();) on failure. Commented Oct 25, 2021 at 18:04

1 Answer 1

2

Arrays do not have the assignment operator. Arrays are non-modifiable lvalues.

To change the content of an array use for example the standard string function strcpy.

#include <string.h >

//...

strcpy( month, "March" );

An alternative approach is to declare the variable month as a pointer. For example

char *month;

or (that is better)

const char *month;

In this case you may write

month = "March";
Sign up to request clarification or add additional context in comments.

4 Comments

"lvalue" literally means "can be used on the LHS of an assignment". Did you mean "rvalue"?
@ikegami No according to the C Standard arrays are non-modifiable lvalues.
Ok, but how odd
@ikegami The origin of the terms refers to where they appear in assignments, language details sometimes make those meanings not apply. An lvalue is an expression that denotes a memory location, while an rvalue is a value without an associated location (it can be a temporary value from an expression).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.