title: How to concatenate negative integers into characters c language
body: <p>I have created a script to collect timestamps in sec and microsec. Then I am trying to calculate the difference of these values. The result is as expected as long as the integers are positive.</p>
  
  <p>As a next step I convert the integers into strings and concatenate them with a dot in between. It is much easier in human readable format to be read by the user.</p>
  
  <p>The problem appears when the integers are negative. First of all I can not figure it out how to print the negative sign when I convert the negative int into a string. (e.g. 3sec . -3microsec)</p>
  
  <p>Second of all it could become even worse in case that both integers are negative and I have two negative signs. (e.g. -3sec . -3microsec)</p>
  
  <p>So my question is, since I trying to subtract two integers that in reality are floats is there any other alternative that I could concatenate them into a float and then apply subtraction? (e.g. int 3sec int 3microsec concatenation to float 3.3sec)</p>
  
  <p>I am a beginner in C language so I am not sure if this is possible, this is the reason that I have been through in all of this process (converting int to strings and then concatenation).</p>
  
  <p>Sample of executable code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  #include &lt;math.h&gt;
  #include &lt;sys/timeb.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;assert.h&gt;
  
  #define TIME_CHAR 22
  #define PROCESS_CHARACTERS 32
  
  typedef struct rec {
    char time_1[TIME_CHAR];
    char time_2[TIME_CHAR];
    char time_3[TIME_CHAR];
    char process[PROCESS_CHARACTERS];
  }RECORD;
  
  char *u2s(long unsigned int number) {
  
    RECORD *ptr_record = malloc (sizeof(RECORD));
  
    if (ptr_record == NULL) {
      printf("Out of memory!\nExit!\n");
      exit(0);
    }
  
    const int n = snprintf(NULL, 0, "%lu", number);
    assert(n &gt; 0);
    char buf[n+1];
    snprintf(buf, n+1, "%lu", number);
    assert(buf[n] == '\0');
    memcpy(ptr_record-&gt;process, buf , sizeof buf);
  
    return ptr_record-&gt;process;
  
  }
  
  uint32_t ClockGetTime() {
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &amp;ts);
    return (int32_t)ts.tv_sec * 1000000LL + (int32_t)ts.tv_nsec / 1000LL;
  }
  
  void cleanup(RECORD** ptr_record) {
    free(*ptr_record);
    *ptr_record = NULL;
  }
  
  int main(int argc, char *argv[]) {
  
    char *dot = ".";
    RECORD *ptr_record;
  
    ptr_record = malloc (sizeof(RECORD));
  
    if (ptr_record == NULL) {
      printf("Out of memory!\nExit!\n");
      exit(0);
    }
  
    /* Originate Time Stamps for time_1 */
    time_t time_1_sec = time(NULL);
    uint32_t client_1_sec = time_1_sec;
    uint32_t client_1_microsec = ClockGetTime();
  
    char *client_1_sec_string = u2s(client_1_sec);
    char *client_1_microsec_string = u2s(client_1_microsec);
  
    memset( ptr_record-&gt;time_1 , '\0' , sizeof ptr_record-&gt;time_1 );
    strncat( ptr_record-&gt;time_1 , client_1_sec_string , strlen(client_1_sec_string) );
    strncat( ptr_record-&gt;time_1 , dot , strlen(dot) );
    strncat( ptr_record-&gt;time_1 , client_1_microsec_string ,     strlen(client_1_microsec_string) );
  
    /* Originate Time Stamps for time_2 */
    time_t time_2_sec = time(NULL);
    uint32_t client_2_sec = time_2_sec;
    uint32_t client_2_microsec = ClockGetTime();
  
    char *client_2_sec_string = u2s(client_2_sec);
    char *client_2_microsec_string = u2s(client_2_microsec);
  
    memset( ptr_record-&gt;time_2 , '\0' , sizeof ptr_record-&gt;time_2 );
    strncat( ptr_record-&gt;time_2 , client_2_sec_string , strlen(client_2_sec_string) );
    strncat( ptr_record-&gt;time_2 , dot , strlen(dot) );
    strncat( ptr_record-&gt;time_2 , client_2_microsec_string , strlen(client_2_microsec_string) );
  
    printf("This is buffer time_2: %s\n",ptr_record-&gt;time_2);
  
    int32_t d_positive_sec = client_2_sec - client_1_sec;
    int32_t d_positive_microsec = client_2_microsec - client_1_microsec;
  
    printf("This is the positive difference in sec: %"PRId32"\n",d_positive_sec);
    printf("This is the positive difference in microsec: %"PRId32"\n",d_possitive_microsec);
  
    int32_t d_negative_sec = client_1_sec - client_2_sec;
    int32_t d_negative_microsec = client_1_microsec - client_2_microsec;
  
    printf("This is the negative difference in sec: %"PRId32"\n",d_negative_sec);
    printf("This is the negative difference in microsec: %"PRId32"\n",d_negative_microsec);
  
    char *n_sec_string = u2s(d_negative_sec);
    char *n_microsec_string = u2s(d_negative_microsec);
  
    memset( ptr_record-&gt;time_3 , '\0' , sizeof ptr_record-&gt;time_3 );
    strncat( ptr_record-&gt;time_3 , n_sec_string , strlen(n_sec_string) );
    strncat( ptr_record-&gt;time_3 , dot , strlen(dot) );
    strncat( ptr_record-&gt;time_3 , n_microsec_string , strlen(n_microsec_string) );
  
    printf("This is negative concatenated %s\n",ptr_record-&gt;time_3);
  
    cleanup(&amp;ptr_record);
  
    return 0;
  
  } /* End of main(){} */
  </code></pre>
  
  <p>Sample of output:</p>
  
  <pre><code>This is the positive difference in sec: 0
  This is the positive difference in microsec: 37
  This is the negative difference in sec: 0
  This is the negative difference in microsec: -37
  This is negative concatenated 0.18446744073709551579
  </code></pre>
  
  <p>It is easy to tell that after the concatenation we should expect to see (0.-37), the best possible result would be (-0.37).</p>