Header, Prototypes Header, Prototypes

We have used the stdio.h file in every program we have used in this course. The file stdio.h is a standard input/output header file.

In this file, the prototype of the printf() function is located, which we also used in all lessons.

Mistake made to show the example

c

Main.c

If you disable the stdio.h file, the printf() function will simply stop working, and when you call this function, we will get an error.

Header

Header files such as std.h contain declarations of variables, arrays, and function prototypes. This mechanism divides your project code into modules that can be attached as needed. It makes your project more convenient.

Prototypes

A function prototype is a declaration of a function without its implementation. You can think of a prototype as a "warning" to the compiler that your function exists.

It looks like a regular function but trimmed. Also, note that this expression ends with a ;. What do we do now with our function prototype in a separate header file?

Multi-file projects

We wrote all our variables and functions in one file, along with the main function, but no one does this in professional development. Let's write a function to calculate a capacitor's charge/discharge time depending on the capacitance and resistance.

Our project will have three files:

  1. func.h - there will be a function prototype;
  2. func.c - this file will contain the implementation of the function for calculating the charge/discharge time of the capacitor;
  3. main.c is our main file where all functions will be called.

After running this program, we get the following:

Note

%.2f - .2 indicates how many decimal places to display.

question-icon

What is the function prototype?

Select the correct answer

Everything was clear?

Section 5. Chapter 5