Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reasons for that are purely historical. Note, that in the old days of C language (K&amp;R C) there was no such thing as <em>function prototype</em>. A <code>strchr</code> function in those times would be declared as</p> <pre><code>char *strchr(); </code></pre> <p>and defined in K&amp;R style as</p> <pre><code>char *strchr(s, c) char *s; char c; { /* whatever */ } </code></pre> <p>However, in C language (in K&amp;R C and in the modern one as well) if the function is declared without a prototype (as shown above), the parameters passed in each function call are subjected to so called <em>default argument promotions</em>. Under default argument promotions any integral type smaller than <code>int</code> (or <code>unsigned int</code>) is always converted to <code>int</code> (or <code>unsigned int</code>). I.e. when the parameters are undeclared, whenever you pass a <code>char</code> value as an argument, this value is <em>implicitly</em> converted to <code>int</code>, and actually physically passed as an <code>int</code>. The same is true for <code>short</code>. (BTW, <code>float</code> is converted to <code>double</code> by default argument promotions). If inside the function the parameter is actually declared as a <code>char</code> (as in the K&amp;R style definition above), it is <em>implicitly</em> converted back to <code>char</code> type and used as a <code>char</code> inside the function. This is how it worked in K&amp;R times, and this actually is how it works to this day in modern C when function has no prototype or when variadic parameters are used.</p> <p>Now, cue in the modern C, which has <em>function prototypes</em> and uses modern-style function definition syntax. In order to preserve and reproduce the "traditional" functionality of <code>strchr</code>, as described above, we have no other choice but to declare the parameter of <code>strchr</code> as an <code>int</code> and explicitly convert it to <code>char</code> inside the function. This is exactly what you observe in the code you quoted. This is exactly as the functionality of <code>strchr</code> is described in the standard.</p> <p>Moreover, if you have an already-compiled legacy library, where <code>strchr</code> is defined in K&amp;R style as shown above, and you decided to provide modern prototypes for that library, the proper declaration for <code>strchr</code> would be</p> <pre><code>char *strchr(const char *s, int c); </code></pre> <p>because <code>int</code> is what the above legacy implementation expects to physically receive as <code>c</code>. Declaring it with a <code>char</code> parameter would be incorrect.</p> <p>For this reason, you will never see "traditional" standard library functions expecting parameters of type <code>char</code>, <code>short</code> or <code>float</code>. All these functions will be declared with parameters of type <code>int</code> or <code>double</code> instead.</p> <p>A very same rationale is behind the standard guarantee that char pointers and <code>void *</code> pointers share the same representation and alignment requirements. Relying on this guarantee you can declare <code>malloc</code> as a <code>void *</code>-returning function and then use this declaration with a pre-compiled legacy version of standard library where <code>malloc</code> actually returned <code>char *</code>.</p> <hr> <p>Reference: the C99 rationale, version 5.10</p> <blockquote> <p>7.1.4 Use of library functions<br> /--/<br> All library prototypes are specified in terms of the “widened” types: an argument formerly declared as char is now written as int. This ensures that most library functions can be called with or without a prototype in scope, thus maintaining backwards compatibility with pre-C89 code</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload