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.