简体   繁体   中英

Is there any problem with my usage of printf function?

I have been given the following task to perform in C: -

The year is divided into four seasons: spring, summer, fall and winter. While the exact dates that the seasons change vary a little bit from year to year because of the way that the calendar is constructed, we will use the following dates for this exercise:

Season First day
Summer March 20
Spring June 21
Fall September 22
Winter December 21

Create a program that reads a month and day from the user. The user will enter the name of the month as a string, followed by the day within the month as an integer.

Then your program should display the season associated with the date that was entered.

Note: Enter First three letter for month example: Jan for january, Feb for Feburary ans so on....and first letter of the month should be capital.

I wrote a mini test code to check the first condition, that is Mar 20.

Here is my code: -

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[]) {
int date;
char month[3];

printf("Enter the month\n");
scanf("%s",month);

printf("Enter the date\n");
scanf("%d",&date);

int temp;
temp= strcmp(month,"Mar");
printf("output is %d\n",temp);
return 0;
}

The problem which I am getting is that while giving input as Mar and 20 , I am getting output: - "output is 20" . Whereas i am suppose to get output as 0 in my test code.

One more observation is that if I change the date to any random number, I am getting that as the output, whereas i am suppose to get the value which is stored in Temp, ie 0.

I am getting correct output if i remove variable date completely from the code. At that time I am getting 0.

The use of your printf function is correct, your printing an integer using the correct conversion specifier for it '%d', as documented on https://linux.die.net/man/3/printf

What may not be fine is the variable 'month'. It is a char buffer with 3 bytes for storage. You are using it to stored null terminated strings, so it can only store 2 ASCII characters + the null terminator.

Another concern is how the scanf function is used for getting the month string. If you do not specify a field width, things can get ugly, see https://linux.die.net/man/3/scanf for further info.

Remember that C strings include a 0-valued terminator, so to store an N-character string, you need a character array that's at least N+1 elements wide.

String management functions like strcmp rely on that terminator to tell them where the end of the string is. The likely reason you're getting weird output is that the string constant "Mar" is 0-terminated, while the string stored in your month array is not, so strcmp is reading past the end of the month array until it sees a 0-valued byte and is returning the difference between "Mar" and "Mar x " , where x represents some unknown sequence of bytes.

Accessing memory past the end of an array results in undefined behavior , and any result is possible.

To fix this particular issue, declare month as a 4-element array of char and limit the input to 3 characters on the scanf call:

char month[4];
...
scanf( "%3s", month );

The standard signature of main is either

int main( void )

or

int main( int argc, char *argv[] ); // NOT const

The argv array is supposed to be modifiable:

5.1.2.2.1 Program startup
...
— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

C 2011 Online Draft

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM