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

  • SEARCH
  • Home
  • 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 8573287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:12:54+00:00 2026-06-11T19:12:54+00:00

This is the code that i came up with while solving a book problem

  • 0

This is the code that i came up with while solving a book problem where i was asked to write a program that gets input in 12 hour format and gives output in 24 hour format.

Valid inputs are e.g 1:11PM, 1:11P, 1:11pm, 1:11p, 1:11 pm, 1:11 p, 1:11 PM, 1:11 P

Here is the code :

#include<stdio.h>

int main (void)
{
int hr=0,min=0; char time;


scanf("%d : %d %c", &hr, &min, &time);

if(time =='p' || time == 'P' || time == 'PM' || time == 'pm' && hr >= 12)
{
    hr +=12;
}

printf("The 24 hour format time is = %d : %d", hr, min);


printf("% Test result = c", test);

return 0;
}

In the if condition i wrote :

if(time =='p' || time == 'P' || time == 'PM' || time == 'pm' && hr >= 12)

As far as i know char type is only allowed to store 1 character. So why does this work when i give a input like this 9:11 PM. How did it work for this condition?

time == 'pm' || time == 'PM'

The output was :

The 24 hour format time is =21:11

Edit 1 :

GNU GCC Compiler is used. The code is in a single file not in a project.

  • 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-11T19:12:55+00:00Added an answer on June 11, 2026 at 7:12 pm

    Even without the warning you get (or at least should get) you have the condition time == 'P' which is evaluated as true, and you’re done.

    Also note that you should wrap all the or’s in (), as otherwise the >= 12 is not even checked. E.g.:

    ((time =='p' || time == 'P') && hr >= 12)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this code showing format string exploitation while reading this article. #include
I just came a cross this nice code that makes this scatter matrix plot:
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across this code on reddit . I would have thought that type
I have this code that reads from XML file. It gets five strings (groupId,
I am using this IP Validation Function that I came across while browsing, it
While writing some code i came across this issue: #include <iostream> class random {
This is code that I came across implementing the quick sort algorithm. Can you
I came across this kind of code once in a while - I suspect
A while ago, I came across some code that marked a data member of

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.