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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:22:43+00:00 2026-05-11T05:22:43+00:00

Some background: If I wanted to use for, for instance, scanf() to convert a

  • 0

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from <inttypes.h>, like this:

#include <stdio.h> #include <inttypes.h> uint16_t x; char *xs = '17'; sscanf(xs, '%' SCNu16, &x); 

But a more uncommon integer type like pid_t does not have any such thing; only the normal integer types are supported by <inttypes.h>. To convert the other way, to portably printf() a pid_t, I can cast it to intmax_t and use PRIdMAX, like this:

#include <stdio.h> #include <inttypes.h> #include <sys/types.h> pid_t x = 17; printf('%' PRIdMAX, (intmax_t)x); 

However, there does not seem to be a way to portably scanf() into a pid_t. So this is my question: How to do this portably?

#include <stdio.h> #include <sys/types.h> pid_t x; char *xs = 17; sscanf(xs, '%u', &x);  /* Not portable! pid_t might not be int! /* 

I thought of scanf()ing to an intmax_t and then checking that the value is within pid_t’s limits before casting to pid_t, but there does not seem to be a way to get the maximum or minimum values for pid_t.

  • 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. 2026-05-11T05:22:43+00:00Added an answer on May 11, 2026 at 5:22 am

    There is one robust and portable solution, which is to use strtoimax() and check for overflows.

    That is, I parse an intmax_t, check for an error from strtoimax(), and then also see if it ‘fits’ in a pid_t by casting it and comparing it to the original intmax_t value.

    #include <inttypes.h> #include <stdio.h> #include <iso646.h> #include <sys/types.h> char *xs = '17';            /* The string to convert */ intmax_t xmax; char *tmp; pid_t x;                    /* Target variable */  errno = 0; xmax = strtoimax(xs, &tmp, 10); if(errno != 0 or tmp == xs or *tmp != '\0'    or xmax != (pid_t)xmax){   fprintf(stderr, 'Bad PID!\n'); } else {   x = (pid_t)xmax;   ... } 

    It is not possible to use scanf(), because, (as I said in a comment) scanf() will not detect overflows. But I was wrong in saying that none of the strtoll()-related functions takes an intmax_t; strtoimax() does!

    It also will not work to use anything else than strtoimax() unless you know the size of your integer type (pid_t, in this case).

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Full disclosure: I originally preferred the globbing approach for its… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer Be sure to put all referenced schemas on the cmd… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer debugger != procrastination - when you're doing graphical stuff, you… May 11, 2026 at 11:50 pm

Related Questions

I wanted some of those spiffy rounded corners for a web project that I'm
I used to use this when I wanted to trigger errors in PHP, coming
I've noticed that a number of top universities are offering courses where students are
<edit> Thanks to everyone who has answered so far. The zip and os.path.join are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.