Functions in C-4 | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

exit() vs _Exit() in C and C++

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:

Showing difference through programs

// 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:

Power Function in C/C++

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:

  • x : floating point base value
  • y : floating point power value

Program

  • C
    // C program to illustrate
    // power function
    #include <math.h>
    #include <stdio.h>
    int main()
    {
        double x = 6.1, y = 4.8;
        // Storing the answer in result.
        double result = pow(x, y);
        printf("%.2lf", result);
        return θ;
    }
  • C++
    // CPP program to illustrate
    // power function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        double x = 6.1, y = 4.8;
        // Storing the answer in result.
        double result = pow(x, y);
        // printing the result upto 2
        // decimal place
        cout << fixed << setprecision(2) << result << endl;
        return θ;
    }

Output:
5882.79

Working of pow() function with integers

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.

  • This is because 52 (i.e. 25) might be stored as 24.9999999 or 25.θθθθθθθθθ1 because the return type is double. When assigned to int, 25.θθθθθθθθθ1 becomes 25 but 24.9999999 will give output 24.
  • To overcome this and output the accurate answer in integer format, we can add θ.5 to the result and typecast it to int e.g (int)(pow(5, 2)+θ.5) will give the correct answer(25, in above example), irrespective of the compiler.

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

Predefined Identifier __func__ in C

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__!

time() function in C

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θ

tolower() function in C

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

The document Functions in C-4 | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Functions in C-4 - Programming and Data Structures - Computer Science Engineering (CSE)

1. What are the basic characteristics of functions in C?
Ans.Functions in C are defined by a return type, a name, and a parameter list. They allow for code reusability and modular programming. Each function can return a value, and parameters can be passed by value or by reference.
2. How do you declare and define a function in C?
Ans.To declare a function in C, you specify the return type followed by the function name and parameters in parentheses. For example: `int add(int a, int b);`. The definition includes the function body, where the logic is implemented, e.g., `int add(int a, int b) { return a + b; }`.
3. What is the difference between a function declaration and a function definition in C?
Ans.A function declaration tells the compiler about the function's name, return type, and parameters, while a function definition provides the actual body of the function. Declarations are often placed in header files, and definitions are implemented in source files.
4. Can a function in C return multiple values?
Ans.No, a function in C cannot return multiple values directly. However, you can achieve this by using pointers, structures, or arrays to return multiple values indirectly.
5. What are the advantages of using functions in C programming?
Ans.Functions in C provide several advantages, including code reusability, better organization of code, easier debugging and testing, and improved readability. They help in breaking down complex problems into smaller, manageable parts.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

Extra Questions

,

MCQs

,

Previous Year Questions with Solutions

,

past year papers

,

Exam

,

Viva Questions

,

study material

,

pdf

,

mock tests for examination

,

Free

,

practice quizzes

,

Objective type Questions

,

ppt

,

Semester Notes

,

video lectures

,

Functions in C-4 | Programming and Data Structures - Computer Science Engineering (CSE)

,

Functions in C-4 | Programming and Data Structures - Computer Science Engineering (CSE)

,

Functions in C-4 | Programming and Data Structures - Computer Science Engineering (CSE)

,

Sample Paper

,

shortcuts and tricks

,

Important questions

;