简体   繁体   中英

C: Substitute for printing a string from variable where it's assigned?

In the follow code, I want to just use one printf at the end of the program to print the 12-hour time, but I want it to print am or pm depending on which is stored in the variable am_pm .

I thought I read that I could store characters in int (or floats?), though I'm not sure if I remember correctly. Of course, it seems illegal since I'm getting type errors.

I also read that I can use arrays to do this, but I haven't learned about arrays yet and I was wondering if there was a simpler substitute for such a problem.

I know the alternative is to simply use two printf 's, one where I simply type "am" and one where I simply type "pm" at the end of the string, but that seems redundant to me.

#include <stdio.h>

int main(void) {

int hour, minutes, am_pm;

printf("Enter a 24-hour time:"); scanf("%d:%d", &hour, &minutes);
if (hour > 12) 
    {
    hour = (hour - 12); 
    am_pm = "pm"; // ERROR 
    }
else
    am_pm = "am";  // ERROR

printf("Equivalent 12-hour time: %.2d:%.2d%d", hour, minutes, am_pm);

} // end main 

How can I do something similar to what I'm trying to do above? I know in python I would simply do something such as print("equivalent time is:" + hour + minutes + am_pm)

You can store a single character in an int. The value of the int is the ASCII code of the character. And it is only one character per int. That's probably what you were (mis)-remembering.

As others have written, declare am_pm as a char * , or better still, a const char * . The const tells the compiler that the string pointed to is read-only.

Another option would be to store 'a' or 'p' in am_pm as an int,

am_pm = 'p'; // Note single quotes for character (double quotes for strings)

and then write

printf("Equivalent 12-hour time: %.2d:%.2d%cm", hour, minutes, am_pm);

%c means interpret am_pm as a character. This takes advantage of the fact that only the first letter of "am"/"pm" changes.

You can't store string literals in int!

Declare am_pm as:

char *am_pm;

and print using %s .

printf("Equivalent 12-hour time: %.2d:%.2d%s", hour, minutes, am_pm);

First of all am_pm is declared as int and a few lines below you try to assign string to it. This is illegal. The other answers show how to correct this. I'd add another solution:

#include <stdio.h>

int main(void) {

int hour, minutes;
int is_pm = 0;

printf("Enter a 24-hour time:"); scanf("%d:%d", &hour, &minutes);
if (hour > 12) 
{
    hour = (hour - 12); 
    is_pm = 1;
}


printf("Equivalent 12-hour time: %.2d:%.2d%s", hour, minutes, (is_pm)?"PM":"AM");

} // end main 

If this seems strange to you - read about 'question mark operator'.

Use a const char * for am_pm , eg change:

int hour, minutes, am_pm;

... 

printf("Equivalent 12-hour time: %.2d:%.2d%d", hour, minutes, am_pm);

to:

int hour, minutes;
const char * am_pm;

...

printf("Equivalent 12-hour time: %.2d:%.2d %s", hour, minutes, am_pm);

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