Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does some functions in stdio have the stream as last argument? [closed]

Tags:

c

libc

Some of the functions in stdio seem to have the stream as the last argument, for example:

char    *fgets(char *restrict, int, FILE *restrict);
int      fputs(const char *restrict, FILE *restrict);
size_t   fread(void *restrict, size_t, size_t, FILE *restrict);
size_t   fwrite(const void *restrict, size_t, size_t, FILE *restrict);

while some have it as the first argument:

int      fgetpos(FILE *restrict, fpos_t *restrict);
int      fseek(FILE *, long, int);

Why is this inconsistency? Were these functions added at different time in the evolvement of the standard library? In that case which were first, and why was the convention changed?

I realize that it's more or less needed for fprintf with friends to have the FILE* first (or at least early) due to the ellipsis (and for fclose and similar to have it first, and last).

like image 372
skyking Avatar asked Feb 24 '16 09:02

skyking


People also ask

What is stream function C?

A stream is a logical entity that represents a file or device, that can accept input or output. All input and output functions in standard C, operate on data streams. Streams can be divided into text, streams and binary streams.

What is the use of cstdio in C++?

The cstdio header file contains definitions for C++ for performing input and output. Include the standard header into a C++ program to effectively include the standard header <stdio. h> within the std namespace.

For which library files the functions pow () and length () are used?

h: This header file contains mathematical functions. It contains functions such as sqrt, pow, exp, log, sin, etc.


1 Answers

I am convinced that a clear and evident answer will not be found to this question, although, this question is not opinion-based, as a clear answer exists somewhere, even though it is unreachable.

However, I recognize the frustration about the problem: one cannot easily work, if, besides learning the function names and what they depend on, memorizing the parameter order for each function individually. Instead, it would be nice to have a consistent parameter order.

To achieve that, one can implement a consistent stdio library, which would use consistent order for the parameters and would wrap each stdio function into such a function. Example:

int      mystdio_fseek(long, int, FILE *);

This would return the result of

int      fseek(FILE *, long, int);

So, the mystdio_ might be a prefix to make sure that names are almost similar, but different and the parameter order would be consistent. This way, one would need to remember only the function names, what each function depends on and there will be no longer need to memorize the parameter order for each function individually. One could use these methods whenever there will be no need for micro optimization.

like image 146
Lajos Arpad Avatar answered Sep 24 '22 14:09

Lajos Arpad