That is one of the major difficulty to write embedded C.
It is important to understand how and where the variables are stored.
Whenever possible, define the variable with shortest length as possible.
There is no need to use an entire 1 byte for flag, you could just use 1 bit instead.
Good programming skill helps too.
For example,
int A,B,C;
A=1;B=1
C=A+B;
printf("%d", C);
we can actually modify the code to
printf("%d", A+B);
this will save 1 byte of RAM, as the C is just for temporay use and can be obmitted (if you dont need to use it in later of your program)
If some part of the code is to be used frequently, it is always better to create a function for that. This helps to save RAM as well.
Never define variable to store constant value. Define it as constant, and it will be stored in ROM.
However, the giant killer of RAM is actually... Array. For instance, the buffer array. So, examine your code carefully and try to reduce the variable declared.