Thread: Writing Println code

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22

    Writing Println code

    I like println because instead of

    Code:
    printf("quotequotequote\n");
    I can write

    Code:
    println("quotequotequote");
    I'm kind of lazy. I also like to be able to write

    Code:
    println(132);
    
    //instead of
    
    printf("%d\n",132);
    In due time I want to add
    Code:
    println("the cat" + String + "jumped over the" +42);
    but right now the code I have has some problems. Can you help? I know in Java you can declare multiple classes with the same name and different inputs. can you do that in c?

    Code:
         1    #include <string.h>
         2    
         3    void println(char *s);
         4    void println(int d);
         5    void println(char c);
         6    void println(float f, char decimal);
         7    //-------------------------------------------------------------
         8    //--------print character
         9    void println(char c){
        10        printf("%c\n",c);
        11    }
        12    //--------print integer
        13    void println(int d){
        14        printf("%d\n",d);
        15    }
        16    //--------print string
        17    void println(char *s){
        18        printf("%s\n", s);
        19    }
        20    //--------print float
        21    void println(float f, char decimal){
        22        char format[16] = "%0.";
        23        strcat(format,decimal);
        24        strcat(format,"f\\n");
        25        printf(format,f);
        26    }
    
    
         3    #include<stdio.h>
         4    #include"println.h"
         5    main()
         6    {
         7     
         8    println("testing my println header");
         9    println("c");
        10    println(123);
        11    println(4.4328,2);
        12    
        13    }
        14
    but get these errors:

    Code:
    In file included from printlntest.c:4:
    println.h:4: error: conflicting types for ‘println’
    println.h:3: error: previous declaration of ‘println’ was here
    println.h:5: error: conflicting types for ‘println’
    println.h:3: error: previous declaration of ‘println’ was here
    println.h:6: error: conflicting types for ‘println’
    println.h:3: error: previous declaration of ‘println’ was here
    println.h:9: error: conflicting types for ‘println’
    println.h:3: error: previous declaration of ‘println’ was here
    println.h:13: error: conflicting types for ‘println’
    println.h:3: error: previous declaration of ‘println’ was here
    println.h:21: error: conflicting types for ‘println’
    println.h:17: error: previous definition of ‘println’ was here
    println.h: In function ‘println’:
    println.h:23: warning: passing argument 2 of ‘__builtin___strcat_chk’ makes pointer from integer without a cast
    println.h:23: warning: passing argument 2 of ‘__inline_strcat_chk’ makes pointer from integer without a cast
    printlntest.c: In function ‘main’:
    printlntest.c:8: error: incompatible type for argument 1 of ‘println’
    printlntest.c:8: error: too few arguments to function ‘println’
    printlntest.c:9: error: incompatible type for argument 1 of ‘println’
    printlntest.c:9: error: too few arguments to function ‘println’
    printlntest.c:10: error: too few arguments to function ‘println’

    I also understand in due time I should seperate the header from the classes to save space. The header should only declare the println methods and then be directed to println.c where they will be carried out. For now, baby steps though. This is my first header and my first code written out for my own purposes, not because of a class.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In C you can not overload functions, there can only be one function named print(). What you are trying to do is not possible in C but is possible in C++.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    so would it be possible to name each method println1, println2, etc... with a switch-case statement in the original method -- println -- that directs you to each one? Also, if that is possible, is there an easier way to do (string + int + string) other than with structs, string tokens and strcat?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by eddieL91 View Post
    so would it be possible to name each method println1, println2, etc... with a switch-case statement in the original method -- println -- that directs you to each one?
    Nope, not possible in C. The enclosing println() funciton would still have to be able to take any kind of parameter, which is not possible*.
    Also, if that is possible, is there an easier way to do (string + int + string) other than with structs, string tokens and strcat?
    Also not possible, you can't overload anything in C, not operators, not functions. Not exactly sure what you mean by "other than structs, string tokens and strcat", but yes, there are other ways. sprintf is useful. There are libraries that have string functionality that makes appending stuff and growing your string easier, but no operator overloading.
    Quote Originally Posted by eddieL91 View Post
    I'm kind of lazy.
    We're all kind of lazy, but that is the absolute worst excuse for a bunch of ugly syntactic sugar and hacked up pseudo-polymorphism. You wouldn't give your variables/functions cryptic 1-letter names, or using global variables simply to save keystrokes. Neither should you replace a clear, well-understood standard function. Never try to save keystrokes at the expense of code that is easy to read/understand. If it's hard to read/understand, it's hard to write, hard to maintain, easy to screw up and hard to fix.

    * There is the ... (elipses) operator that allows variable argument functions (any number and type), but it requires a parameter before it, so it knows where to start pulling off the "whatever" parameters, and it needs some information about what type the following parameters are. That is actually exactly what printf already does for you.
    Last edited by anduril462; 04-03-2012 at 01:15 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You have two choices:
    1. Put up with the fact that you cant get close to what you want in C, or
    2. Use C++

    The fact that you can't do what you want in C, is afterall the reason why it is done the way it is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  3. Need some help on writing code
    By A slow C++ user in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2003, 09:32 PM
  4. self-writing code?
    By genghis in forum C++ Programming
    Replies: 19
    Last Post: 01-15-2003, 04:13 PM