Difference between return and printf in C
In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let’s see each of them and see what are their functions and differences.
1. Return Statement
In C, return is a statement used within a function to terminate the function’s execution and return a value to the caller. It is used to pass data back from the function to the calling code.
Syntax
return value;
Example
C
// C program to illustrate the return statement #include <stdio.h> // function that return value int foo() { return 10; } // driver code int main() { int z = foo(); printf ( "The value returned by function: %d" , z); return 0; } |
The value returned by function: 10
2. Printf Function
The printf is a standard C library function used for the formatted output. It is not a statement-like return but rather a function that takes a format string and a variable number of arguments. It displays formatted data to the console or other output streams and does not pass data between the functions.
Syntax
printf("format string", argument1, argument2, ...);
Example
C
// C program to illustrate printf #include <stdio.h> // function that prints value void foo() { printf ( "In the function" ); } // driver code int main() { foo(); return 0; } |
In the function
Difference between return and printf in C
Characteristics | return | printf |
---|---|---|
Purpose | Used to terminate the function and return a value | Used for formatted output to console |
Return Type | Depends on the function’s declared return type | int – returns the number of the characters printed |
Usage | Inside functions to return a value to the caller | Inside functions to display data on the console |
Value Passing | Passes a value from the function back to the caller | Does not pass data between functions only displays the output |
Terminates Function | Immediately exits the current function | Does not terminate the function execution continues |
Example |
return 42; returns the integer value 42 |
printf(“Hello, World!”); displays “Hello, World!” |
Contact Us