I have my codes working where I got separate date and time columns. I would like to add one more column which have both date and time in. For the existing codes this how I did

  sprintf(all,'%02d:%02d:%02d.%06u%03u',CURDATE(),s / 3600, (s % 3600) / 60, s % 60, usec, nsec)

I tried this does not work and got error too many argument for format and also format expect type inta but argument has 16 has type achar *a

sprintf(all,'%02d:%02d:%02d.%06u%03u',CURDATE(),'CURDATE() %02d:%02d:%02d.%06u%03u',s / 3600, (s % 3600) / 60, s % 60, usec, nsec,s / 3600, (s % 3600) / 60, s % 60, usec, nsec)

I need the space between the curdate and time ?

The format string to sprintf details how many arguments are acceptable in the varargs portion of the call (the remaining values to print).
Generally speaking - unless you are printing an % or doing something fancy - you can expect one argument for each % found in your format string. So, for your first example,

"%02d:%02d:%02d.%06u%03u"
 ^    ^    ^    ^   ^
 1    2    3    4   5
  1. Expects an integer (CURDATE())
  2. Expects an integer (s / 3600)
  3. Expects an integer ((s % 3600) / 60)
  4. Expects an unsigned integer (s % 60)
  5. Expects an unsigned integer (usec)

As you can see, there is nothing to receive/convert the nsec argument you are providing. What you are doing in your second example is completely invalid.

If you want more things printed you have to add more format specifiers and more arguments; not just more arguments.

Also, I assume this is a copy-paste error, but strings in C are enclosed in " characters. The ' is used to indicate a single character. You should get a compile error (or at least a warning) if you enclose multiple characters with '.

Dear L7Sqr,
          I know C need to tally both arguement and specifier. I had this codes below. Why this code working with no issue ? What correction should you suggest here?





char all[500];
        sprintf(all,"TE1','%d','%d','%02d:%02d:%02d.%06u%03u',CURDATE(),'%s'",h->extended_hdr.parsed_header_len,
 h->extended_hdr.parsed_pkt.ipv4_tos,s / 3600, (s % 3600) / 60, s % 60, usec, nsec,intoa(h->extended_hdr.parsed_pkt.src));

Suprisingly for both the TE1 and CURDATE there is no specifier and it works. The breakdown is as follows.

1. '%d' h->extended_hdr.parsed_header_len

2. '%d' h->extended_hdr.parsed_pkt.tos

3. %02d s / 3600

4. %02d (s % 3600) / 60

5. %02d s % 60

6. %06u usec

7. %03u nsec

8. %s  intoa(h->extended_hdr.parsed_pkt.src)

Hi,
Please can you check the usage of sprintf using man sprintf if you are using a nix OS. I don't think, you are using it rightly.

Dear 2Teez,
          Yes I am on linux and this what I see in the man sprintf(char * restrict str, const char * restrict format, ...); ? So isnt this part is correct "sprintf(all,"?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.