Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Header, Prototypes | Functions
C Basics

bookHeader, Prototypes

Throughout this course, we've consistently utilized the stdio.h file. This file serves as the standard input/output header. Contained within it is the prototype for the printf() function, a function we've employed in every lesson.

Main.c

Main.c

copy
12345678
//#include <stdio.h> int main() { printf("Using `printf()` without `stdio.h`"); return 0; }

Should you exclude the stdio.h file, the printf() function will cease to function. Invoking this function without the header will yield an error.

Understanding Header Files

Header files like stdio.h house declarations of variables, arrays, and function prototypes. They modularize your project code, allowing you to attach components as necessary. This approach streamlines your projects.

Function Prototypes

A function prototype is essentially a function declaration without its actual implementation. Think of a prototype as a "heads-up" to the compiler, signaling the existence of your function.

function_type function_name(arguments);

It resembles a standard function but without the details. Take note of the concluding semicolon (;). Now, how do we handle a function prototype in a dedicated header file?

Multi-File Projects

Up to this point, we've written our variables and functions in a single file, right alongside the main function. However, in professional development, this isn't the norm. Let's devise a function to estimate the charge/discharge duration of a capacitor based on its capacitance and resistance.

Project will be segmented into three files:

  1. main.c - this primary file will be where all functions are invoked;
  2. func.h - this will store the function prototype;
  3. func.c - the implementation of our capacitor charge/discharge time calculation function will reside here.
main.c

main.c

function.h

function.h

function.c

function.c

copy
12345678910
#include "func.h" int main() { R = 10; C = 150; printf("Charge/discharge will be %.2f ms\n", chrg_dchrg(R,C)); return 0; }
Note
Note

The %.2f specifies that the result should be displayed with two decimal places.

Executing this program will result in:

"Charge/discharge will be 7.50 ms"
question mark

What does a function prototype do in C?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What does the function to calculate capacitor charge/discharge time look like?

Can you explain how to include and use custom header files in a C project?

Why is it important to separate code into multiple files in C projects?

Awesome!

Completion rate improved to 2.63

bookHeader, Prototypes

Swipe to show menu

Throughout this course, we've consistently utilized the stdio.h file. This file serves as the standard input/output header. Contained within it is the prototype for the printf() function, a function we've employed in every lesson.

Main.c

Main.c

copy
12345678
//#include <stdio.h> int main() { printf("Using `printf()` without `stdio.h`"); return 0; }

Should you exclude the stdio.h file, the printf() function will cease to function. Invoking this function without the header will yield an error.

Understanding Header Files

Header files like stdio.h house declarations of variables, arrays, and function prototypes. They modularize your project code, allowing you to attach components as necessary. This approach streamlines your projects.

Function Prototypes

A function prototype is essentially a function declaration without its actual implementation. Think of a prototype as a "heads-up" to the compiler, signaling the existence of your function.

function_type function_name(arguments);

It resembles a standard function but without the details. Take note of the concluding semicolon (;). Now, how do we handle a function prototype in a dedicated header file?

Multi-File Projects

Up to this point, we've written our variables and functions in a single file, right alongside the main function. However, in professional development, this isn't the norm. Let's devise a function to estimate the charge/discharge duration of a capacitor based on its capacitance and resistance.

Project will be segmented into three files:

  1. main.c - this primary file will be where all functions are invoked;
  2. func.h - this will store the function prototype;
  3. func.c - the implementation of our capacitor charge/discharge time calculation function will reside here.
main.c

main.c

function.h

function.h

function.c

function.c

copy
12345678910
#include "func.h" int main() { R = 10; C = 150; printf("Charge/discharge will be %.2f ms\n", chrg_dchrg(R,C)); return 0; }
Note
Note

The %.2f specifies that the result should be displayed with two decimal places.

Executing this program will result in:

"Charge/discharge will be 7.50 ms"
question mark

What does a function prototype do in C?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 5
some-alt