2

I mean this usage:

sprintf_with_start_pos(out_variable, 3, "format_str %s %i etc...", var1, var2);

and such function should withheld returning output until it arrives at position 3. I know I can create this effect making my own buffer and then copy from that, but I wonder if is there in c a one liner for this ?

5
  • Position 3 in what sense? Do you want to print the first 3 characters? That's done using %.3s.
    – user529758
    Feb 8, 2014 at 12:39
  • What do you mean by withholding output? Feb 8, 2014 at 12:40
  • no I want to print from 3rd character onwards
    – rsk82
    Feb 8, 2014 at 12:40
  • 1
    @rsk82 Then you pass var1 + 3 to the function.
    – user529758
    Feb 8, 2014 at 12:42
  • ..after checking that there are at least three chars. Feb 8, 2014 at 12:43

1 Answer 1

2

test.c:

#include <stdio.h>

main() {
    char* var1 = "1234567";
    char buffer[50];
    sprintf(buffer, "%s", &var1[2]);

    printf("%s\n", buffer);
    printf("%.3s\n", buffer);
}

Output:

$ gcc test.c 
$ ./a.out 
34567
345

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.