Simple question about C program

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Simple question about C program

    I am a beginner in C programming. I have a question about C
    programming.
    In one of our c programs:

    char prep_name[18];
    char s_time[8];
    sprintf(prep_name, "S%s%d%d, s_time, pid, sql_stmt_ctr++);

    s_time is alawys 8 characters but pid is 4 ~ 5 and sql_stmt_ctr is
    usually
    1 ~ 6. We found out that prep_name[] could be over 18 characters when
    sql_stmt_ctr is more than 6 digits. We'd like to have just 18
    characters
    in prep_name even when sql_stmt_ctr is over 6 digits(truncate the rest
    digits).
    Is there any way to do it?

    Regards
    Hugh
    Hugh Kang Guest

  2. Similar Questions and Discussions

    1. Simple Drawing Program with Ruler Scaling?
      Hi All, Is there a relatively simple (and reasonably priced) drawing program available for OSX that has scaled rulers (ala MacDraw 2 or early...
    2. Need Simple Answer to Simple Contribute/Firefox question
      Hello all; I've tried the Adobe help in CS3, tech support, phone support, this forum, other forums, Mozilla, and nowhere can I get a straight...
    3. simple client-server program ??
      Hi, how do I develop the following 'simple' program using ASP.NET webforms : a client has a text-file (aaa.txt) that has to be manipulated by...
    4. need help developing a simple drawing program
      I am looking to build a simple drawing program that lets the user make ovals and lines with options to change line width and color. I downloaded an...
    5. Program Logic Question
      Hello All, I have written a little VB program to send packets of data to my web server every 5 seconds in the form of a small csv file. The idea...
  3. #2

    Default Re: Simple question about C program

    On Tue, 22 Jul 2003 04:50:55 +0200, Måns Rullgård wrote:
    > [email]skang@leaguedata.com[/email] (Hugh Kang) writes:
    >
    >> I am a beginner in C programming. I have a question about C
    >> programming.
    >> In one of our c programs:
    >>
    >> char prep_name[18];
    >> char s_time[8];
    >> sprintf(prep_name, "S%s%d%d, s_time, pid, sql_stmt_ctr++);
    >>
    >> s_time is alawys 8 characters but pid is 4 ~ 5 and sql_stmt_ctr is
    >> usually
    >> 1 ~ 6. We found out that prep_name[] could be over 18 characters when
    >> sql_stmt_ctr is more than 6 digits. We'd like to have just 18
    >> characters
    >> in prep_name even when sql_stmt_ctr is over 6 digits(truncate the rest
    >> digits).
    >> Is there any way to do it?
    >
    > snprintf(prep_name, 18, "S%s%d%d, s_time, pid, sql_stmt_ctr++);
    Note that this will only have 17 characters and a terminator. This may or
    may not be what the OP wanted.
    I'd worry that truncating sql_stmt_ctr could affect the result, so it
    might be better to make sure prep_name is big enough to hold all values.

    Also, if you want to use snprintf() it would be much more maintainable
    as...

    snprintf(prep_name, sizeof(prep_name), "%c%s%lu%u",
    s_time, (unsigned long) pid, sql_stmt_ctr++);

    --
    James Antill -- [email]james@and.org[/email]
    Need an efficent and powerful string library for C?
    [url]http://www.and.org/vstr/[/url]

    James Antill Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139