Thread: Java-style println() in C

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Java-style println() in C

    Hi,

    I've been trying to create a java-style System.out.println() function in C, so that I don't have to keep writing \n at the end of every printf(), which I also keep forgetting...

    I've read this thread about it:
    Writing Println code
    Where you concluded it not to be possible in C.
    It is.

    You just have to use a macro to do it:
    Code:
    #define println(fmt, args...) ({\
        int __a = printf(fmt,args);\
        printf("\n");\
        ++__a;\
    })
    It's consistent with printf() in the way that the return value is the number of total characters printed.

    Hope it's helpful to others who want to do this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Except it doesn't work.
    Code:
    #include <stdio.h>
    
    #define println(fmt, args...) ({\
        int __a = printf(fmt,args);\
        printf("\n");\
        ++__a;\
    })
    
    int main ( ) {
      printf("hello\n");
      println("hello");
      return 0;
    }
    
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:4:30: error: expected expression before ‘)’ token
         int __a = printf(fmt,args);\
                                  ^
    foo.c:11:3: note: in expansion of macro ‘println’
       println("hello");
       ^
    $ gcc -std=c99 foo.c
    foo.c: In function ‘main’:
    foo.c:4:30: error: expected expression before ‘)’ token
         int __a = printf(fmt,args);\
                                  ^
    foo.c:11:3: note: in expansion of macro ‘println’
       println("hello");
       ^
    $ gcc -std=c11 foo.c
    foo.c: In function ‘main’:
    foo.c:4:30: error: expected expression before ‘)’ token
         int __a = printf(fmt,args);\
                                  ^
    foo.c:11:3: note: in expansion of macro ‘println’
       println("hello");
       ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I suppose you could create some nonsense like this - but the ratio of effort spent to effect is not that great.

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    int jck_println ( const char * format, ... )
    {
        int result = 0;
        char buffer[BUFSIZ];
        va_list args;
        
        va_start(args, format);
        result = vsprintf(buffer, format, args);
        va_end(args);
        
        if (result > 0) {
            result = puts ( buffer );
        }
    
        return result;
    }
    
    int main(void)
    {
        jck_println("%d", 123);
        jck_println("hello");
        jck_println("%s %d", "hello", 2);
    
        return 0;
    }
    It's still not all that java-like at all. Not to mention that it is assumed in this code that no one will ever try to print more than about BUFSIZ characters at once. It is probably safer and easier just to use "\n" in your format strings.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Here is an alternative. All you need to do is call vprintf followed by some code which outputs a newline. You don't need buffers or anything like that. If you are using GCC you can even get it to check your format string for you.

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    #ifdef __GNUC__
    int printfln(const char *format, ...)
             __attribute__ ((format (printf, 1, 2)));
    #endif
    
    int printfln(const char *format, ...)
    {
        va_list ap;
        va_start(ap, format);
        int ret = vprintf(format, ap);
        va_end(ap);
        puts("");
        return ret;
    }
    
    //
    // Example usage
    //
    
    int main()
    {
        printfln("x: %d"); // WARNING: 'int' argument missing.
        printfln("y: %d", 234);
        printfln("z: %d", 345);
        printfln("Bye.");
        return 0;
    }
    Last edited by c99tutorial; 05-24-2017 at 02:39 PM. Reason: Add return value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-23-2015, 02:55 PM
  2. Writing Println code
    By eddieL91 in forum C Programming
    Replies: 4
    Last Post: 04-03-2012, 01:15 PM
  3. How to write Java style classes in C++
    By Aardappel in forum C++ Programming
    Replies: 0
    Last Post: 12-28-2005, 10:17 AM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. how do I extract a style from a DWORD style
    By zMan in forum Windows Programming
    Replies: 1
    Last Post: 01-17-2002, 10:09 AM

Tags for this Thread