Why are arguments like `putchar ()`, `fputc ()` and `putc ()` not `char`?

Does anyone know why the type of argument putchar(), fputc()and putc()did not char, but the type of argument putwchar(), fputwc()and putwc()equal wchar_t? See also this and this .

+3
source share
2 answers

The answer is “legacy” (or “history”). Prior to the C90 standard, there were no prototypes of functions, and all arguments for all functions were subject to the default distribution rules, therefore a was charautomatically passed as int( shortwas upgraded to int), and floatto double, and similarly for unsigned types). The standard could not afford to break existing code, so it retained this type for these functions. This has almost no effect on practice. The value you pass will be considered as a character type, even if you pass a value that is out of range. The specification fputc(int c, FILE *stream)says:

The function fputcwrites the character specified c(converted to unsigned char) to the output stream pointed to stream...

§6.5.2.2

¶6 , , , , , float, . ....

¶7... . .

6.3.1.

¶2 :21 > ​​ unsigned int:

  • ( int unsigned int), int unsigned int.
  • _Bool, int, signed int unsigned int.

int ( , a -), int; unsigned int. . 58) .

¶3 , . , "plain" char , .

58) : , , +, - ~, , .

¶1 10 .

+4

, . . , , char ( int), EOF, char ,

void f(char c) { ...
...
char x = 't';
f((unsigned char)x);
...

warning: conversion to ‘char’ from ‘unsigned char’ may change the sign of the result

( unsigned char , , char undefined.) , - int.

+1

Source: https://habr.com/ru/post/1661443/


All Articles