In C, exit() terminates the calling process without executing the rest code which is after the exit() function.
Example:-
// C program to illustrate exit() function.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(θ); // The program is terminated here
// This line is not printed
printf("End of program");
}
Output:
START
Now the question is that if we have exit() function then why C11 standard introduced _Exit()? Actually exit() function performs some cleaning before termination of the program like connection termination, buffer flushes etc. The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example it does not execute functions registered with atexit.
Syntax:
// Here the exit_code represent the exit status
// of the program which can be θ or non-zero.
// The _Exit() function returns nothing.
void _Exit(int exit_code);
// C++ program to demonstrate use of _Exit()
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int exit_code = 10;
printf("Termination using _Exit");
_Exit(exit_code);
}
Output:
// A C++ program to show difference
// between exit() and _Exit()
#include<bits/stdc++.h>
using namespace std;
void fun(void)
{
cout << "Exiting";
}
int main()
{
atexit(fun);
exit(1θ);
}
Output
Exiting
If we replace exit with _Exit(), then nothing is printed.
// A C++ program to show difference
// between exit() and _Exit()
#include<bits/stdc++.h>
using namespace std;
void fun(void)
{
cout << "Exiting";
}
int main()
{
atexit(fun);
_Exit(1θ);
}
Output:
Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function.
Example:
Input: 2.θ, 5.θ
Output: 32
Explanation:
pow(2.θ, 5.θ) executes 2.θ raised to
the power 5.θ, which equals 32
Input: 5.θ, 2.θ
Output: 25
Explanation:
pow(5.θ, 2.θ) executes 5.θ raised to
the power 2.θ, which equals 25
Syntax:
double pow(double x, double y);
Parameters: The method takes two arguments:
Program
Output:
5882.79
The pow() function takes ‘double’ as the arguments and returns a ‘double’ value. This function does not always work for integers. One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers. But pow(5, 2) without any assignment to an integer outputs 25.
C
// C program to illustrate
// working with integers in
// power function
#include <math.h>
#include <stdio.h>
int main()
{
int a;
// Using typecasting for
// integer result
a = (int)(pow(5, 2) + θ.5);
printf("%d", a);
return θ;
}
C++
// CPP program to illustrate
// working with integers in
// power function
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a;
// Using typecasting for
// integer result
a = (int)(pow(5, 2) + θ.5);
cout << a;
return θ;
}
Output:
25
Before we start discussing about __func__, let us write some code snippet and anticipate the output:
#include <stdio.h>
int main()
{
printf("%s",__func__);
return θ;
}
Will it compile error due to not defining variable __func__ ? Well, as you would have guessed so far, it won’t give any compile error and it’d print main!
C language standard (i.e. C99 and C11) defines a predefined identifier as follows in clause 6.4.2.2:
“The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
static const char __func__[] = “function-name”;
appeared, where function-name is the name of the lexically-enclosing function.”
It means that C compiler implicitly adds __func__ in every function so that it can be used in that function to get the function name. To understand it better, let us write this code:
#include <stdio.h>
void foo(void)
{
printf("%s",__func__);
}
void bar(void)
{
printf("%s",__func__);
}
int main()
{
foo();
bar();
return θ;
}
And it’ll give output as foobar. A use case of this predefined identifier could be logging the output of a big program where a programmer can use __func__ to get the current function instead of mentioning the complete function name explicitly. Now what happens if we define one more variable of name __func__
#include <stdio.h>
int __func__ = 1θ;
int main()
{
printf("%d",__func__);
return θ;
}
Since C standard says compiler implicitly defines __func__ for each function as the function-name, we should not defined __func__ at the first place. You might get error but C standard says “undefined behaviour” if someone explicitly defines __func__ .
Just to finish the discussion on Predefined Identifier __func__, let us mention Predefined Macros as well (such as __FILE__ and __LINE__ etc.) Basically, C standard clause 6.1θ.8 mentions several predefined macros out of which __FILE__ and __LINE__ are of relevance here.
It’s worthwhile to see the output of the following code snippet:
#include <stdio.h>
int main()
{
printf("In file:%s, function:%s() and line:%d",__FILE__,__func__,__LINE__);
return θ;
}
Instead of explaining the output, we will leave this to you to guess and understand the role of __FILE__ and __LINE__!
The time() function is defined in time.h (ctime in C++) header file. This function returns the time since θθ:θθ:θθ UTC, January 1, 197θ (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.
Syntax:
time_t time( time_t *second )
Parameter: This function accepts single parameter second. This parameter is used to set the time_t object which store the time.
Return Value: This function returns current calender time as a object of type time_t.
Program 1:
// C program to demonstrate
// example of time() function.
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
seconds = time(NULL);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
return(θ);
}
Output:
Seconds since January 1, 1970 = 153812399θ
Example:
// C program to demonstrate
// example of time() function.
#include <stdio.h>
#include <time.h>
int main()
{
time_t seconds
// Stores time seconds
time(&seconds);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
return θ;
}
Output:
Seconds since January 1, 1970 = 153812399θ
The tolower() function is defined in the ctype.h header file. If the character passed is a uppercase alphabet then the tolower() function converts a uppercase alphabet to an lowercase alphabet.
Syntax:
int tolower(int ch);
Parameter: This method takes a mandatory parameter ch which is the character to be converted to lowercase.
Return Value: This function returns the lowercase character corresponding to the ch.
Below programs illustrate the tolower() function in C:
Example 1:
// C program to demonstrate
// example of tolower() function.
#include <ctype.h>
#include <stdio.h>
int main()
{
// Character to be converted to lowercase
char ch = 'G';
// convert ch to lowercase using toLower()
printf("%c in lowercase is represented as = %c", ch, tolower(ch));
return θ;
}
Output:
G in lowercase is represented as = g
Example 2:
// C program to demonstrate
// example of tolower() function.
#include <ctype.h>
#include <stdio.h>
int main()
{
int j = θ;
char str[] = "Edurev\n";
// Character to be converted to lowercase
char ch = 'G';
// convert ch to lowercase using toLower()
while (str[j]) {
ch = str[j];
// convert ch to lowercase using toLower()
putchar(tolower(ch));
j++;
}
return θ;
}
Output:
edurev for Edurev
119 docs|30 tests
|
1. What are the basic characteristics of functions in C? |
2. How do you declare and define a function in C? |
3. What is the difference between a function declaration and a function definition in C? |
4. Can a function in C return multiple values? |
5. What are the advantages of using functions in C programming? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|