- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape Sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if Statement
- C - if...else Statement
- C - if...else if Ladder
- C - Nested if Statements
- C - Switch Statement
- C - Nested Switch Statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Pointers in C
- C - Pointers
- C - Initialization of Pointer Arrays
- C - Applications of Pointers
- C - Dereference Pointer
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Pointer Arithmetics
- C - Pointers and Arrays
- C - Pointer vs Array
- C - Pointer to an Array
- C - Array of Pointers
- C - Pointers vs. Multi-dimensional Arrays
- C - Pointer to Pointer
- C - Chain of Pointers
- C - Character Pointers and Functions
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Array of Function Pointers
- C - Pointers to Structures
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- User-Defined Data Types
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- C - Dynamic Array Resizing
- C - Memory Leaks
- File Handling in C
- C - File Handling
- C - Input & Output
- C - File Operations
- C - Formatted Output
- C - getc, getchar, getch, getche
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Macros
- C - Working of Preprocessor
- C - Preprocessor Operators
- C - Header Files
- C - Custom Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C - Online Compiler
Function Prototype in C
A function prototype in C programming is a declaration that specifies the function's name, its return type, and the number and data types of its parameters. A function in C is a block of code that performs a specific task.
What is a Function Prototype?
A function prototype allows the compiler to verify that the arguments and the data types of a function match with the ones that are specified in its declaration and its call.
The following image illustrates the syntax and structure of a function prototype −
Ways to Write Function Prototypes in C
There are several different ways to write a function prototype in C. These prototypes are commonly used in C programming to declare functions before their actual definition.
// Example 1: Function declaration without parameters
// (does not specify the number and type of parameters)
int function_name();
// Example 2: Function prototype with data types only
// (specifies return type, number, and types of parameters)
int function_name(int, int);
// Example 3: Function prototype with parameter names
// (valid and commonly used)
int function_name(int a, int b);
// Example 4: Function definition (which itself serves as a prototype)
int sum(int a, int b) {
return a + b;
}
Example: Sum of Two Numbers
In this example, we calculate the sum of two numbers using a function prototype −
#include <stdio.h>
// function prototype
int sum(int a, int b);
int main() {
int a = 10, b = 20;
printf("Sum of a and b is %d\n", sum(a, b));
return 0;
}
// function definition
int sum (int x , int y){
return x + y;
}
Here is its output −
Sum of a and b is 30
In the above code, the function sum is called before its definition. Since the program contains the prototype of the sum function, it executes without any errors.
What Happens When the Function Prototype is Missing?
When the function prototype is missing in C, the compiler gives the following two errors −
- Implicit Declaration Warning − This warning indicates that the compiler cannot find a function with the given name.
- Incorrect Return Type Warning − You will get this warning when a functionâs return type is not explicitly specified. By default, the compiler assumes the return type to be int.
Example
This program shows the effect when the function prototype is missing in a C program −
#include <stdio.h>
// function definition
int sum (int x , int y){
return x+y;
}
int main() {
int a = 10, b = 20;
printf("Sum of a and b is %d\n", sum(a, b));
return 0;
}
When you run this code, it will flash the following error −
ERROR!
/tmp/uG7dM1iUzv/main.c: In function 'main':
/tmp/uG7dM1iUzv/main.c:6:36: error: implicit declaration of function 'sum' [-Wimplicit-function-declaration]
6 | printf("Sum of a and b is %d\n",sum(a, b));
In the above code, the function sum is called before it is defined and we don't have a function prototype of sum at the beginning. Hence, this code results in an implicit declaration error.
Benefits of Using Function Prototypes
Following are the benefits of using function prototypes −
- Function definition before definition − It allows a function to be called before its definition, but make sure the function prototypes are declared at the beginning. In complex programs, it is common to declare the function prototype at the beginning or in the header of the code, which enables function calls to occur before the functionâs actual implementation.
- Type checking − A function prototype allows the compiler to check whether the correct number and types of arguments are passed to the function.
- Code clarity − Declaring the function prototypes makes the programmers aware of the functionâs purpose and expected parameters, which helps in understanding the code structure.
In this chapter, we covered in detail the importance of using function prototypes in C programming.