C Functions

Last Updated : 28 Sep, 2023

Question 1
Output of following program?
#include <stdio.h>
int main()
{
    int i = 5;
    printf("%d %d %d", i++, i++, i++);
    return 0;
}
Cross
7 6 5
Cross
5 6 7
Cross
7 7 7
Tick
Compiler Dependent


Question 1-Explanation: 
When parameters are passed to a function, the value of every parameter is evaluated before being passed to the function. What is the order of evaluation of parameters - left-to-right or right-to-left? If evaluation order is left-to-right, then output should be 5 6 7 and if the evaluation order is right-to-left, then output should be 7 6 5. Unfortunately, there is no fixed order defined by C standard. A compiler may choose to evaluate either from left-to-right. So the output is compiler dependent.
Question 2

In C, parameters are always 
 

Tick

Passed by value
 

Cross

Passed by reference
 

Cross

Non-pointer variables are passed by value and pointers are passed by reference
 

Cross

Passed by value result
 



Question 2-Explanation: 

In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.
 

Question 3

Which of the following is true about return type of functions in C?
 

Cross

Functions can return any type
 

Tick

Functions can return any type except array and functions
 

Cross

Functions can return any type except array, functions and union
 

Cross

Functions can return any type except array, functions, function pointer and union
 



Question 3-Explanation: 

In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to array or pointer to function.
 

Question 4

What is the output of this program?

C

#include <stdio.h>
int main()
{
  printf("%d", main);  
  return 0;
}
Tick

Address of main function

Cross

Compiler Error

Cross

Runtime Error

Cross

Some random value



Question 4-Explanation: 

Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function. Symbol table is implemented like this.

struct
{
   char *name;
   int (*funcptr)();
}
symtab[] = {
   \"func\", func,
   \"anotherfunc\", anotherfunc,
};
Question 5
Output?
#include <stdio.h>

int main()
{
    int (*ptr)(int ) = fun;
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf("GeeksQuiz ");
  return 0;
}
Cross
GeeksQuiz GeeksQuiz GeeksQuiz
Cross
GeeksQuiz GeeksQuiz
Tick
Compiler Error
Cross
Runtime Error


Question 5-Explanation: 
The only problem with program is fun is not declared/defined before it is assigned to ptr. The following program works fine and prints \"GeeksQuiz GeeksQuiz GeeksQuiz \"
int fun(int n);

int main()
{
    // ptr is a pointer to function fun()
    int (*ptr)(int ) = fun;

    // fun() called using pointer 
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf(\"GeeksQuiz \");
}
Question 6

Output of following program? 

C

#include<stdio.h>

void dynamic(int s, ...)
{
    printf("%d", s);
}

int main()
{
    dynamic(2, 4, 6, 8);
    dynamic(3, 6, 9);
    return 0;
}
Tick

2 3

Cross

Compiler Error

Cross

4 3

Cross

3 2



Question 6-Explanation: 

In c three continuous dots is known as ellipsis which is variable number of arguments of function. The values to parameters are assigned one by one. Now the question is how to access other arguments. See this for details.

Question 7
Predict the output?
#include <stdio.h>
int main()
{
    void demo();
    void (*fun)();
    fun = demo;
    (*fun)();
    fun();
    return 0;
}

void demo()
{
    printf("GeeksQuiz ");
}
Cross
GeeksQuiz
Tick
GeeksQuiz GeeksQuiz
Cross
Compiler Error
Cross
Blank Screen


Question 7-Explanation: 
This is a simple program with function pointers. fun is assigned to point to demo. So the two statements \"(*fun)();\" and \"fun();\" mean the same thing.
Question 8
What is the meaning of using extern before function declaration? For example following function sum is made extern
extern int sum(int x, int y, int z)
{
    return (x + y + z);
}
Cross
Function is made globally available
Tick
extern means nothing, sum() is same without extern keyword.
Cross
Function need not to be declared before its use
Cross
Function is made local to the file.


Question 8-Explanation: 
extern keyword is used for global variables. Functions are global anyways, so adding extern doesn\'t add anything.
Question 9
What is the meaning of using static before function declaration? For example following function sum is made static
static int sum(int x, int y, int z)
{
    return (x + y + z);
}
Cross
Static means nothing, sum() is same without static keyword.
Cross
Function need not to be declared before its use
Tick
Access to static functions is restricted to the file where they are declared
Cross
Static functions are made inline


Question 9-Explanation: 
In C, functions are global by default. Unlike global functions, access to static functions is restricted to the file where they are declared. We can have file level encapsulation using static variables/functions in C because when we make a global variable static, access to the variable becomes limited to the file in which it is declared.
Question 10
In C, what is the meaning of following function prototype with empty parameter list
void fun()
{
   /* .... */
}
Cross
Function can only be called without any parameter
Tick
Function can be called with any number of parameters of any types
Cross
Function can be called with any number of integer parameters.
Cross
Function can be called with one integer parameter.


Question 10-Explanation: 
Empty list in C mean that the parameter list is not specified and function can be called with any parameters. In C, to declare a function that can only be called without any parameter, we should use \"void fun(void)\" As a side note, in C++, empty list means function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.
There are 41 questions to complete.


Share your thoughts in the comments

Similar Reads