Thread: How does the signature of printf justify its use?

  1. #1
    Registered User
    Join Date
    Dec 2014

    How does the signature of printf justify its use?

    For example the prototype of printf is the following.

    int printf ( const char * format, ... );
    However, I don't understand what happens when we have the following code here.

    char myString[50] = "Print this string";
    int myInt = 50;
    printf("This is a string: %s", myString);
    printf("This is a string: %s and an int: %d", myString, myInt);

    if myInt is an integer, doesn't that disobey the const char* format for printfs arguments? Is there some sort of casting that's happening somewhere?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You are looking at the use of a variable argument list. There is a tutorial here that briefly covers its use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    There's a kind of casting going on, but it's through the va_arg macro.
    Consider a simplified printf with only %d and %s (I've also cheated and used printf to actually print the ints!)
    #include <stdio.h>
    #include <stdarg.h>
    int myprintf(const char *fmt, ...) {
      va_list va;
      va_start(va, fmt);
      for ( ; *fmt; fmt++) {
        switch (*fmt) {
        case '%':
          switch (fmt[1]) {
          case 'd':
            printf("%d", va_arg(va, int));  // interpret next arg as an int
          case 's':
            printf("%s", va_arg(va, char*)); // interpret next arg as char*
            ; // unknown format spec
        case '\\':
          if (fmt[1]) putchar(fmt[1]);
    int main(void) {
      myprintf("Hello world!\n");
      myprintf("%d dollars and %d cents\n", 121, 12);
      myprintf("%d %s enter a %s\n", 3, "nuns", "bar");
      return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I right justify this?
    By hermancarson in forum C Programming
    Replies: 4
    Last Post: 11-19-2007, 11:27 AM
  2. setw() - left justify
    By DJPG5 in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2003, 06:46 PM
  3. how to right justify toolbar ??
    By Feras in forum Windows Programming
    Replies: 1
    Last Post: 12-06-2002, 04:36 PM
  4. left justify
    By Max in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 06:32 PM
  5. Right justify?!?!
    By Claire in forum C++ Programming
    Replies: 6
    Last Post: 01-19-2002, 12:02 PM