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

  • SEARCH
  • Home
  • 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 8520747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:37:03+00:00 2026-06-11T06:37:03+00:00

I have created a printf -like function which takes the following arguments: One mandatory

  • 0

I have created a printf-like function which takes the following arguments:

  • One mandatory argument (custom error code)
  • Optional format string
  • Variable count of format arguments

The function prototype looks like:

int my_printf(int err_code, ...);

err_code also includes information whether the format string (and possible format tags) are given.

If they are given, I can extract the format string using the va_arg function and pass it to vfprintf together with the rest of the arguments.

The calls look like:

my_printf(-ERR_SIMPLE);
my_printf(-ERR_COMPLICATED, "Error: problem with %d", 123);

Unfortunately, I am unable to use the GCC attribute for format type-checking since it needs a string-index:

format (archetype, string-index, first-to-check)

Is it still possible to do the type-checking anyhow?
Solutions using helper macros, helper functions, modifying the optional format string part etc are acceptable.

Update:
I am unsure whether passing a modified va_list to vfprintf-like functions is OK. See comments.
If possible, it would be better to avoid this and use macros or something else for the optional format string part.

  • 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-06-11T06:37:05+00:00Added an answer on June 11, 2026 at 6:37 am

    (Sorry, I hadn’t seen that you had tried with __attribute__. My bad).

    You might try experimenting with “argument counting” as described here:

    http://locklessinc.com/articles/overloading/

    I don’t know if this can result in GCC selectively applying argument checking though; but I think it should.

    UPDATE it appears to be working, with an added #define hack:

    #include <stdio.h>
    
    int printf_1(int err)
    {
            printf("Got error %d\n", err);
            return 0;
    }
    
    int printf_2(int error, char *string)
    {
            printf("Error %d and message %s\n", error, string);
            return 0;
    }
    
    int printf_3(int error, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
    
    int printf_3(int error, char *fmt, ...)
    {
            printf("Received full string err=%d, fmt=%s\n", error, fmt);
            return 0;
    }
    
    #define printf_4        printf_3
    #define printf_5        printf_3
    #define printf_6        printf_3
    #define printf_7        printf_3
    #define printf_8        printf_3
    #define printf_9        printf_3
    #define printf_10       printf_3
    
    
    #define COUNT_PARMS2(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _, ...) _
    #define COUNT_PARMS(...)\
            COUNT_PARMS2(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
    
    #define CAT(A, B) CAT2(A, B)
    #define CAT2(A, B) A ## B
    
    #define myprintf(...)\
            CAT(printf_, COUNT_PARMS(__VA_ARGS__))(__VA_ARGS__)
    
    int main()
    {
            myprintf(19);
            myprintf(19, "Hello");
            myprintf(19, "Hello '%s'", "world");
            // Warning!
            myprintf(19, "Hello '%s'", "world", 42);
            myprintf(19, 42);
            return 0;
    }
    

    I correctly receive (gcc 4.6.2):

    $ gcc -W -Wall -o test test.c
    
    test.c: In function ‘main’:
    test.c:48:2: warning: too many arguments for format [-Wformat-extra-args]
    test.c:49:2: warning: passing argument 2 of ‘printf_2’ makes pointer from integer without a cast [enabled by default]
    test.c:9:5: note: expected ‘char *’ but argument is of type ‘int’
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which takes a normal HTTP GET Request and returns
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created a custom post type named People. I have created a page
I have created custom registration form in drupal 6. i have used changed the
PHP mysql database I have created a follow on question to this one here
I have some code which iteratively appends data to a file and looks similar
I have a array which contains values like this See below for script sample
Closely related to this question: Bash printf prefix I have the following Bash script
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to
I would like to create a small function, which should show the current status

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.