r/C_Programming • • Oct 18 '26

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.

0 Upvotes

54 comments sorted by

21

u/EpochVanquisher Oct 18 '26

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.

-11

u/Glum_Hamster_2104 Oct 18 '26

Couldn't they just have both? printf/scanf for legacy code and optionally just using print/scan for the future.

22

u/EpochVanquisher Oct 18 '26

If you name a function print() in the standard library, it will break the code of everyone who already has a function named print().

2

u/Glum_Hamster_2104 Oct 18 '26

Fair point

4

u/EpochVanquisher Oct 18 '26

You can see that the committee is very conservative about the names of new functions in the standard library. One of the new names they chose for a function is stdc_leading_zeros, because they think it’s okay to name new functions with a stdc_ prefix.

I’ll say again, if you want a language which is “nice” to program in, C is not that language. C is engineered to keep backwards compatibility with old C code, even at the cost of making the language kind of a pain in the ass.

1

u/Glum_Hamster_2104 Oct 18 '26

Yea I haven't really looked into much of this stuff since I've been really only learning C for a month but I do think it is nice to program in compared to something like C++ or Python.

1

u/aalmkainzi Oct 18 '26

Yes, stdc_ is a reserved namespace.

3

u/aioeu Oct 18 '26 edited Oct 18 '26

Note that "keeping existing code working" doesn't really mean "no breaking changes ever". The C standard has had breaking changes over time. It's just that these changes are deliberately kept minimal, and are carefully planned out.

I think bool, true and false are instructive examples. The following code was perfectly valid (and maybe even useful) in ANSI C:

typedef int bool;
const bool false = 0;
const bool true = 1;

But there was a desire to make a boolean type and boolean "true" and "false" constants part of the language itself, rather than having everybody reimplement them independently.

In C99 we got <stdbool.h>, which defined some bool, true and false macros. The important point here was that people who had their own definitions for these identifiers were given the opportunity to start using C99 without touching their existing code ... all they would have to do was not use <stdbool.h>. Essentially this approach decoupled "starting to use a newer C standard" from "having to update existing code" — those two things could be tackled by programmers separately.

By the time C23 came around, it was expected that most people using booleans were likely to be doing it through this header, and that anybody still defining them manually weren't going to be interested in using C23 anyway. That is, it was a case of "24 years is long enough for people to start using this". So the decision then was made to make bool, true and false bona fide language keywords, not macros. <stdbool.h> is still kept though — after all, there's existing code still using it! — but under C23 it is essentially an empty header.

So yes, this was a breaking change in C23, but it was one made after careful consideration about just how much it would break. In both cases, in C99 and in C23, the changes were made such that as little code as possible would be immediately broken by the new version of the standard.

Simply saying "in C23 you have to use printf; in C2Y you can use print instead" doesn't do that.

1

u/aalmkainzi Oct 18 '26

Actually the C23 true and false are not identical to the C99 header. C23 made their type bool, while in C99 they were int (for some reason)

3

u/Ratfus Oct 18 '26

At that point, why not just use Python, instead of C?

Same reason you don't see many people trying to build classes in C.

1

u/Glum_Hamster_2104 Oct 18 '26

I hate Python

2

u/Ratfus Oct 18 '26

Why?

Honestly, I used to hate python's weak typing, but then I started actually doing stuff in it. The weak typing actually makes editing things so much easier. In C, if i want to change a LONG to an INT, I likely have to change the return types of all those functions. Then, at values that depend on the return, also need to be changed. Then all my printf()'s also need to be changed because my format specifier ("%...") is now wrong. Structures are helpful for updating things, but even they have limitations.

Btw... printf(abc) without the specifier is a big security risk. I like C, but Python is so much easier to edit and so much more secure.

2

u/Glum_Hamster_2104 Oct 18 '26

I dislike the syntax a lot especially the indents which I'd rather just use {}.

2

u/aalmkainzi Oct 18 '26

Getting a compiler error when a changing a type causes a mismatch is exactly the thing you should want.

15

u/bothunter Oct 18 '26 edited Oct 18 '26

 #define print printf

Edit: coding on a phone is hard.  Thank you u/hugonikanor

6

u/HugoNikanor Oct 18 '26

Other way around would be more useful

3

u/bothunter Oct 18 '26

Derp.  

-5

u/Ratfus Oct 18 '26

Could you do #define #define #IFUCKINGHATEMACROS

?

5

u/HugoNikanor Oct 18 '26

No, the name part of a #define follows the same rules as all variable names in C, which doesn't allow # signs anywhere.

1

u/Vladislav20007 Oct 18 '26

actually defines allow for # and ##, but that's a different thing.

2

u/Ratfus Oct 18 '26

People don't seem to get the fact that I wasn't actually being serious on that statement.

It would be awful coding if it were actually allowed by the compiler.

2

u/WittyStick Oct 18 '26 edited Oct 18 '26

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, and printf itself 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 vprintf in 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 Oct 18 '26

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 warning

1

u/WittyStick Oct 18 '26

You can fix that with

__attribute__((__format__(printf, 1, 2)))
int print(const char *restrict format, ...) __asm__("printf");

The compiler warns for printf because glibc's implementation uses the format attribute.

3

u/Direct_Chemistry_179 Oct 18 '26

Could be a fun project to hack existing libc urself. Not sure how, but I saw someone do this with tcc to remove semicolons.

1

u/Vladislav20007 Oct 21 '26

it was tsoding and he changed the compiler, here you just have to copy libc compile it with printf renamed to print(and pray that it doesn't break mid-compilation) and link with new libc.

3

u/Daveinatx Oct 18 '26

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 Oct 18 '26

Yes, let's break almost 50 years of code for no good reason. Sounds like a plan.

5

u/onesole Oct 18 '26
#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

u/Serious_Wrangler_248 Oct 18 '26

Why use lot word

1

u/Vladislav20007 Oct 18 '26

you forgot to add #define d int(for decimal) and #define S char **(for strings, defines are case-sensitive right?)

2

u/MegaDork2000 Oct 18 '26

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?

2

u/SmokeMuch7356 Oct 18 '26

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.

3

u/tubameister Oct 18 '26

claude, submit a proposal to rename printf/scanf to just print

1

u/Gold-Part4688 Oct 18 '26

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 Oct 18 '26

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 Oct 18 '26 edited Oct 18 '26

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 by

oh 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 Oct 18 '26

btw c can have compile time type detection for this exact situation, it's just not standard ;)

1

u/Gold-Part4688 Oct 19 '26

awesome. yeah it does use llvm or a c backend, so that's probably how. damn. love you c

1

u/grimvian Oct 18 '26

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 Oct 18 '26

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.

2

u/flyingron Oct 20 '26

Alas, C for histerical reasons binds "char" to triple duty (causing it to fail badly at some things):

  1. A character
  2. A small integer (of inconsistent signeness)
  3. The basic addressable storage.

Note, this means that if you want to retain the ability to deal with things in terms of 8 bit chunks, you can't have a basic character that isn't also 8 bits. There are wchars and C mostly gets this right. C++ however is a freaking nightmare. Unlike C, they didn't define wchar_t based interfaces for everything, so it presumes there is a well known and uniquely invertible multibyte encoding for every possible character.

1

u/flatfinger Oct 20 '26

Generation of efficient code with simple compilers essentially required a char type with indeterminate signedness. If a platform had a load-signed-byte instruction but no load-unsigned-byte instruction, then the simplest way of processing *p2++=*p1++; when the pointers were of type unsigned char*, would be to process each "load unsigned byte" operation as a combination of "load signed byte" and "mask off upper bits". If char is signed on platforms that lack a "load unsigned byte" instruction, then code that loads a char would never have to do any masking, and a compiler would thus not have to worry about whether the masking was necessary.

2

u/flyingron Oct 20 '26

It has diddly squat to do with efficiency.

It's because nobody knew whether a basic character's integer value should be signed or not (varied with implementation). So as to not break code, they make the signeness of just "char" to be implementation defined. Unsigned char is unsigned, and then they added the (would have otherwise be redudant) signed keyword for people who wanted to force a char to be signed.

1

u/flatfinger Oct 20 '26

The 1974-1975 C Language Reference describes the first two C implementations. The PDP-11 targeted by the first C implementation has a "load signed byte" instruction but no equivalent to load an unsigned byte. I'm not familiar with the GCOS system targeted by the second but the C Language Reference states that it used a 9-bit unsigned character type, and it would seem unlikely that the second language implementation would have deviated from the first in such a significant way without a good reason, but a lack of a "load signed byte" instruction would qualify in that regard.

1

u/flyingron Oct 21 '26

But implementatios DID so vary. There was compelling argument (and implentations to boot) that the basic char would be treated 0...255 not -128 to 127.

1

u/flatfinger Oct 21 '26

How many implementations for targets that had signed character-load instructions but no unsigned character-load instructions could not be configured to use a signed character type?

If a processor doesn't have a signed character load instruction, then even code that would only ever use values 0-127 would run more slowly with signed characters than with unsigned characters.

1

u/flyingron Oct 22 '26

It's not whether they could support it, it was that absent real clear guidance back in the day, did an implementer decide that his chars should have range of 0..255 rather than -128 to 127. Back in 1975 or whenever there was a difference of opinion, and a lot of concepts weren't really fully thought out.

Implmeentations with char as unsigned EXISTED and the later standardization efforts decided NOT to invalidate these when things got cleaned up. Just like many other early stupidities didn't and we are stuck with them today.

1

u/flatfinger Oct 22 '26

In the 1970s there would have been four main kinds of machines that were relevant for C:

  1. Those that had only signed byte load instructions.
  2. Those that had only unsigned byte load instructions.
  3. Those that had both signed and unsigned byte load instructions.
  4. Those that didn't have any instructions to access anything smaller than int.

I would expect almost all C compilers for the first and fourth kinds of machine to be able to treat char as signed, and almost all compilers for the second to be able to treat them as unsigned. The signed character type used by the fourth kind would behave just like int.

I am dubious about the existence of implementations targeting the first or fourth kind of platform that wouldn't use signed char by default, or those for the second that wouldn't use unsigned char by default.

→ More replies

1

u/grimvian Oct 19 '26

As long I can a have thing consisting of 8 bits I consider it as a byte.

1

u/Vladislav20007 Oct 20 '26

pretty sure char is a pretty bad byte, if you ask me. no guarantee that it's signed or unsigned and is at least the size of a single ASCII char(which is 7bits btw).

1

u/mwiseca Oct 23 '26

I saw some people rename printf and scanf with #define

I tried it and it worked.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define p printf
#define f fgets
#define n NULL
#define crr clearerr(stdin);
#define sn strlen
#define scp strcmp


void flush(){
    int clear;
    while ((clear = getc(stdin)) != '\n' && clear != EOF) {
    }
}

int main() {
    char text [25];
    p("Enter text x to exit.\n");
    while(1) {
        if(f(text,sizeof(text),stdin) == n) {
            crr
            p("\nInvalid input\n\n");
            continue;
        }
        text[strcspn(text,"\n")]=0;
        if(sn(text) >= 24) {
            flush();
        }
        if(scp(text,"x")==0) {
            break;
        }
        p("You entered: %s%c",text,0x0a);
    }
return 0;
}