- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- 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
- 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
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- 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
Return Statement in C
The return statement terminates the execution of a function and returns control to the calling function. Every function should have a return statement as its last statement. While using the returns statement, the return type and returned value (expression) must be the same.
Syntax of return Statement
Here is the syntax of the return statement:
return value_or_expression;
The following main() function shows return as its last statement −
int main(){ // function body; return 0; }
The main() function returning 0 indicates the successful completion of the function. To indicate failure of the function, a non−zero expression is returned.
The void return statement
A function's return type can be void. In such a case, return statement is optional. It may be omitted, or return without any expression is used.
Example
#include <stdio.h> /* function declaration */ void test(){ return; } int main() { test(); printf("end"); return 0; }
Return type mismatch in return statement
Each function in the program must have a forward declaration of its prototype. By default, each function returns an integer. However, function of other return types without prototype is not accepted.
Example
int main(){ test(5); printf("end"); return 0; } float test(int a) { return 1.1 ; }
Output
Error: C:\Users\mlath\test.c|12|error: conflicting types for 'test'
This is because, function without prototype is assumed as of int type, which conflicts with the definition.
The same error occurs if the return type of a function in the prototype doesn't match with the type of return expression, an error is reported as below −
float test(int); int main(){ test(5); printf("end"); return 0; } float test(float a){ return 1.1 ; }
Multiple return values with return statement
A function can be defined with more than one arguments, but can return only one value. You can however use multiple conditional return statements as shown below −
Example
int test(int); int main() { test(5); printf("end"); return 0; } int test(int a){ if (a<3) return 1; else return 0; }
Function returning an array
It is not possible to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.
Example
The following program shows how to pass an array to a function that returns an array after performing a certain process.
#include <stdio.h> int* test(int *); int main(){ int a[] = {1,2,3,4}; int i; int *b = test(a); for (i=0; i<4; i++){ printf("%d\n", b[i]); } return 0; } int * test(int*a){ int i; for (i=0; i<4; i++){ a[i] = 2*a[i]; } return a; }
Output
2 4 6 8
function can only return a single value using return statement. To return multiple values, we use pointers or structures
exit() instead of return statement
Unlike the return statement, the exit() function is also used to terminate the execution of the program without transferring the control back to the calling function. It is used inside a function when the program has finished its execution or when an unrecoverable error occurs. It is a standard way of handling exception errors in C. When exit() is called, the program control does not return to the point where exit() was invoked. Instead, the control is handed back to the operating system.
The exit() function is library function defined in stdlib.h header file.
Syntax
void exit(int status);
exit() is typically called from the main() function or any other function to terminate the entire program.
It is included in the <stdlib.h> header file.
Since it results in termination of the program, exit() does not return a value directly to the caller function. Instead, it terminates the program and returns a status code. It is an integer that represents the exit status of the program, indicating success or failure.