Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:47:37+00:00 2026-06-16T17:47:37+00:00

In the follow code, I want to just use one printf at the end

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:47:38+00:00Added an answer on June 16, 2026 at 5:47 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’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with the following code. I want to use one UIImageView
I want to create 1MB String for benchmark,so I writed code as follow: public
I just completed with one of my applications which is now ready to be
I want to run wget as follows shell_exec('wget 'http://somedomain.com/somefile.mp4''); sleep(20); continue my code... What
I have this follow code in my javascript. I call this function when the
i have the follow code: searchResults.SearchResultCollection.Add( new SearchResult() { Header = HttpUtility.HtmlDecode( htmlDocument.DocumentNode .SelectSingleNode(initialXPath
Consider the follow code: TMyList = class(TList<IMyItem>, IMyList) Delphi shows me the error: [DCC
Hi i am having the follow code <div id=container> <div class=element NOTME></div> <div class=element></div>
This has me stumped. The follow code returns ,,,,,,: <script type=text/javascript> $(function() { $('#listB').sortable({
I had created a zip file (together with directory) under Windows as follow (Code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.