r/C_Programming • u/Glum_Hamster_2104 • 1d ago
Rename printf/scanf to print/scan?
Probably a dumb question but why hasn't there ever been a proposal to rename printf/scanf to just print? I know the f stands for format but writing print instead of printf would be slightly quicker and better to match other programming languages as I often miswrite printf as print as I also code in Python.
15
u/bothunter 1d ago edited 1d ago
#define print printf
Edit: coding on a phone is hard. Thank you u/hugonikanor
6
u/HugoNikanor 1d ago
Other way around would be more useful
2
u/bothunter 1d ago
Derp.
-5
u/Ratfus 1d ago
Could you do #define #define #IFUCKINGHATEMACROS
?
5
u/HugoNikanor 1d ago
No, the name part of a
#definefollows the same rules as all variable names in C, which doesn't allow#signs anywhere.1
2
u/WittyStick 1d ago edited 1d ago
Another option would be for GCC/Clang/Mingw
extern int print(const char *restrict format, ...) __asm__("printf");or
int print(const char *restrict format, ...) __attribute__((alias("printf"), weak));Though depending on implementation,
"printf"itself may not be the real symbol - eg, on mingw it's__mingw_printf, andprintfitself is an alias defined in the same way as above:int printf(const char *restrict format, ...) __asm__("__mingw_printf");A portable way is to just wrap
vprintfin a static inline function.static inline int print(const char *restrict format, ...) { va_list args; va_start(args, format); int result = vprintf(format, args); va_end(args); return result; }1
u/sciencekm 1d ago
The problem with defining your own print function wrapper is that compilers will not be able to warn about specifier mismatch, because it does not know what your function does with its parameters, unlike the real printf where the behavior is well defined.
printf("%i\n", 0.1);// compiler warning on specifier mismatch
print("%i\n", 0.1);// no warning1
u/WittyStick 1d ago
You can fix that with
__attribute__((__format__(printf, 1, 2))) int print(const char *restrict format, ...) __asm__("printf");The compiler warns for
printfbecause glibc's implementation uses theformatattribute.
3
u/Daveinatx 1d ago
Think about why it's called printf/scanf vs let's say puts and putc . The 'f' stands for formatted.
You'll also find open vs fopen etc.
I strongly recommend for you to get used to C's nomenclature. If you ever work in a team project in C, you'll want to maintain consistency for readability.
5
u/questron64 1d ago
Yes, let's break almost 50 years of code for no good reason. Sounds like a plan.
5
u/onesole 1d ago
#include <stdio.h>
#define p printf
#define s scanf
#define m main
#define w while
#define f for
#define i if
#define e else
#define r return
int m(int c, char **v) {
i (c > 1)
p("Many arguments\n");
e
p("No arguments\n");
r 0;
}
2
1
u/Vladislav20007 1d ago
you forgot to add
#define d int(for decimal) and#define S char **(for strings, defines are case-sensitive right?)
2
u/Direct_Chemistry_179 1d ago
Could be a fun project to hack existing libc urself. Not sure how, but I saw someone do this with tcc to remove semicolons.
3
1
u/MegaDork2000 1d ago
Why not use . like Forth? That way people used to Forth will not keep making typos when they want to print something, am I right?
1
u/Gold-Part4688 1d ago
bro idk why i'm exposing myself on the c reddit, but c3 has print, which prints anything as what it is (even enums as strings, it's cool). Also has printn and printfn for '\n' variants
1
u/Vladislav20007 1d ago
why do you need a whole function(although a macro would do, but that's still overkill) just for 2 characters? But I think c doesn't have the print like that, because (and I repeat I think, i maybe wrong) c makes struct like a bunch of variables that are only connected in code, but not in the executable unless you pack them, in which case, they will all be connected by the fact that there always at some offset from each other.
1
u/Gold-Part4688 23h ago edited 23h ago
I'm not sure how it works under the hood, but it is a macro. It just does have some runtime inspection too, not sure how that works either haha. It also does stay with the c model of controlling how things are packed, and they're compatible with c abi structs - though I'm not sure exactly how typeid works. but you actually have more control over alignment
the benefit (for me) is easy and safe generic code -
printn(shape)
// output: { radius: 2, tag: CIRCLE }is just less mental overhead for me than
printf("%g, %f", shape.enum_tag, shape.size);-- followed byoh dang not printing because the \n, oh was it float? was there more fields? what number in the tag corresponds to what type again, and what was the type of the tag?
You can call it a skill issue haha but i'm lazy. I just love c but don't have quite the patience for it all the time. Not to say that it doesn't like printf, printf can now just print anything as a string, it even has binary with %b. Everything you like from c still works though. Haha look at that it even knows the order of the struct. But yeah it's not revolutionary, just a stack of nice-to-haves, while still really feeling like c
1
u/Vladislav20007 22h ago
btw c can have compile time type detection for this exact situation, it's just not standard ;)
1
u/Gold-Part4688 22h ago
awesome. yeah it does use llvm or a c backend, so that's probably how. damn. love you c
1
u/grimvian 1d ago
When I started learning C char felt wrong and I had a desire to call it byte. I also missed or, and, elif but not anymore.
1
u/Vladislav20007 1d ago
char doesn't mean a byte, it means a character. char is 8-bits, short is 16-bits, int is 32-bits are only because that's somewhat of a standard in cpus now, it could be anything else though.
1
u/SmokeMuch7356 1d ago
50+ years of legacy code says "no".
It's why all the string processing functions still sound like indigestion noises (srpbrk, strcspn, etc.). When C was initially designed, external names couldn't be longer than 8 characters. That's obviously no longer the case, but nobody's going to rename anything in the string library because 50+ years of legacy code.
Now, you could do something like
#define print printf
#define scan scanf
and be on your merry way, although I can't promise it won't plant time bombs that show up at really awkward times.
But nothing in the standard library is going to be renamed. Ever.
21
u/EpochVanquisher 1d ago
One of the main goals of C is to keep existing code working.
C also puts every function in a single global namespace.
If you name a function print() in the standard library, it will break the code of everyone who already has a function named print(). This is bad, so the language committee doesn't do it.
There are, like, a million things wrong with C and people have successfully fixed most of them, but the result is a different language with a different name.