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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:49:31+00:00 2026-05-25T21:49:31+00:00

I am experienced in Java but I am very new to C. I am

  • 0

I am experienced in Java but I am very new to C. I am writing this on Ubuntu.
Say I have:

char *msg1[1028];
pid_t cpid;
cpid = fork();

msg1[1] = " is the child's process id.";

How can I concatenate msg1[1] in such a way that when I call:

printf("Message: %s", msg1[1]);

The process id will be displayed in front of ” is the child’s process id.”?

I want to store the whole string in msg1[1]. My ultimate goal isn’t just to print it.

  • 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-25T21:49:32+00:00Added an answer on May 25, 2026 at 9:49 pm

    Easy solution:

    printf("Message: %jd is the child's process id.", (intmax_t)cpid);
    

    Not so easy, but also not too complicated solution: use the (non-portable) asprintf function:

    asprintf(&msg[1], "%jd is the child's process id.", (intmax_t)cpid);
    // check if msg[1] is not NULL, handle error if it is
    

    If your platform doesn’t have asprintf, you can use snprintf:

    const size_t MSGLEN = sizeof(" is the child's process id.") + 10; // arbitrary
    msg[1] = malloc(MSGLEN);
    // handle error if msg[1] == NULL
    if (snprintf(msg[1], MSGLEN, "%jd is the child's process id.", (intmax_t)cpid)
      > MSGLEN)
        // not enough space to hold the PID; unlikely, but possible,
        // so handle the error
    

    or define asprintf in terms of snprintf. It’s not very hard, but you have to understand varargs. asprintf is very useful to have and it should have been in the C standard library ages ago.

    EDIT: I originally advised casting to long, but this isn’t correct since POSIX doesn’t guarantee that a pid_t value fits in a long. Use an intmax_t instead (include <stdint.h> to get access to that type).

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

Sidebar

Related Questions

I'm new too Java, but I'm very experienced with PHP. When I was writing
Im very new to C programing, but have limited experience with Python, Java, and
I am an experienced C++/Java programmer but am new to iOS. I am writing
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I have a very simple page that is displaying canned data. I experienced this
As the topic says I'm very new to c++, but I have some experience
To preface, I'm very new to Obj-C but I do have some OOP experience
I'm pretty new to Perl but have been programming in java for several months
I'm fairly new to Android programming and to Java in general, but I have

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.