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 7443423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:19:40+00:00 2026-05-29T11:19:40+00:00

I have been trying to extract hours, seconds and minutes from an input text

  • 0

I have been trying to extract hours, seconds and minutes from an input text using sscanf. After sscanf function is performed, only s variable which holds the seconds has the right value. h and m which have hours and minutes in them hold only zeros. Please suggest changes to my code below.

     char text[20];
     if (fgets(text, sizeof text, stdin)!= NULL){
         char* newline = strchr(text, '\n');
         if (newline != NULL){
             *newline = '\0';
         }
     }
     uint8_t s = 0;
     uint8_t m = 0;
     uint8_t h = 0;
     sscanf(text, "%02i:%02i:%02i",&h,&m,&s);

Note in the debugger, text has the right values.

  • 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-05-29T11:19:41+00:00Added an answer on May 29, 2026 at 11:19 am

    This program:

    #include <stdio.h>
    
    int main(void)
    {
        const char hhmmss[] = "10:32:54";
        int hh, mm, ss;
        if (sscanf(hhmmss, "%i:%i:%i", &hh, &mm, &ss) != 3)
            printf("Failed to scan 3 values from '%s'\n", hhmmss);
        else
            printf("From <<%s>> hh = %d, mm = %d, ss = %d\n", hhmmss, hh, mm, ss);
        return 0;
    }
    

    gives this output:

    From <<10:32:54>> hh = 10, mm = 32, ss = 54
    

    The %02i conversions should also work, but the digits are somewhat superfluous.


    The amended question shows that the variables are of type uint8_t, in which case you must use the correct conversion specifiers from <inttypes.h>:

    #include <stdio.h>
    #include <inttypes.h>
    
    int main(void)
    {
        const char hhmmss[] = "10:32:54";
        uint8_t s;
        uint8_t m;
        uint8_t h;
    
        if (sscanf(hhmmss, "%02" SCNi8 ":%02" SCNi8 ":%02" SCNi8, &h, &m, &s) != 3)
            printf("Failed to scan 3 values from '%s'\n", hhmmss);
        else
            printf("From <<%s>> h = %d, m = %d, s = %d\n", hhmmss, h, m, s);
        return 0;
    }
    

    This produces the same output as before. With any of the scanf() family of functions, it is crucial that your format conversion specifiers match the types of the pointers you are passing into the function. You can get away with quite a lot of mismatches in printf() – certainly by comparison – because of default integer (in particular) promotions, but scanf() is a lot less forgiving.

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

Sidebar

Related Questions

I have been using XPath with scrapy to extract text from html tags online,
I have been trying to extract data from a database and fill in a
I have been able to extract certain lines from a large tab-separated text file
I have been attacking atoi from several different angles trying to extract ints from
I have been going round in circles trying to extract meta tag information from
I have been trying to get a response from a server sending a GET
I have been trying to figure out the following excerpt from Ruby on Rails
I have been trying to pull out my SQLite database from my android application
I have been trying to put together something that allows me to extract points
I'm trying to extract functions and function headers from some source code files. Here's

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.