0

I am new to C, and by looking to the declaration of fopen() function:

FILE *fopen(const char *filename, const char *mode)

I noticed the use of "const" before the two parameters, as far as I know about constants in C, they are used only in variables, arrays, etc, that you do not want to have their value changed during the execution of the program, since I always pass a value to fopen() function, wouldn't these two "const" trow an error (since "const" should not let the user give a new value to a variable, so it should not let you give a value to "const char *filename" and "const char *mode")?

User__1
  • 21
  • 3
  • 3
    Declaring `const` arguments means basically "I promise not to modify these". – tadman May 18 '20 at 19:24
  • Do you mean modify as giving a new value (storing a new value inside of them)? – User__1 May 18 '20 at 19:27
  • 1
    There is no "throwing" an error, since that would be at runtime. Adding `const` does not prevent a system from cheating. It is a hint for the compiler. If a `const char *` is passed to a function that accepts `char *` that may indicate undesired behaviour, and the compiler can then warn about it. – Cheatah May 18 '20 at 20:17
  • 1
    I mean modifying as in changing anything at the memory location that pointer indicates. You *promise* not to. The compiler can optimize accordingly. – tadman May 18 '20 at 21:38

0 Answers0