C Interview Questions and Answers

21) What is an auto keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

22) What is a const keyword in C?

Constants are fixed value or variables, whose value cannot be changed throughout the execution of program. The fixed value is known as literal or literal constant.

You can define a constant for any type uisng "const" keyword. They behave like normal variables expect that they are readonly (once assigned cannot be modified). For example: const int emp = 50577

C supports two styles of constant definition.

  1. Using const keyword
  2. Using #define directive

23) What is static keyword in C?

Static keyword makes any variable, object, or function as constant In C. Basically, Static is used for a constant variable, or object function every local variable of a function is known as an automatic (auto) variable.            Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value. 

24) What is a register keyword in C?

In C, Register variable tell the complier to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility. 

25) What is a sizeof keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value. 

26) What is a typedef keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value. 

27) What is an extern keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value. 


User defined function in C:
By Ahmad Irshad | C Programming Tutorial > User defined function

We have already learned function and its properties. In this tutorial, you will learn how to create user-defined functions and how to use them in C Programming with the help of examples.

function is a group of statements (Block or Piece of code) wrapped with curly braces ({}) that together perform a specific task is called function.
C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you want to find sum of two integers, you need to define a function for adding two integers. The function name should be meaningful like sum(); or add(); 

Syntax for creating user defined function:

return_type function_name(Arguments list);
{
  // Code or group of statements to be executed 
}

Now let's see the below example, how to create user defined function?

/* Example of user defined function or function definition*/
int sum(int num1, int num2) { int s; s = num1 + num2; return s; }

Above example is a definition of a function, to use this function we need to call it by passing values as an arguments with the function name. Let's have a look at the below example how to call a function?

For Example:

#include <stdio.h>
int main()
{
     int a,b,c;
     printf("Enter two numbers:\n"); // Taking input of two numbers by user
     scanf("%d,%d", &a, &b);
     c = sum(a, b); // function call by passing value
     prinf("Sum is %d", c);
}

Let's understand the above example with a complete program:

  1. /* Program two add two integers input given by user */
  2. #include <stdio.h>
  3. #include <conio.h>

  4. /*Function declaration OR function prototype*/
  5. int sum(int a, int b); 
  6. void main()  
  7. {  
  8.   int a, b, sum;  
  9.   clrscr();  
  10.   printf("Enter two numbers: "); 
  11.   scanf("%d, %d\n", &a, &b);
  12.   /* Function call by passing value */
  13.   sum = sum(a, b);
  14.   printf("sum is %d", sum); 
  15.   getch();  

  16. /* Creating user defined function OR function definition*/ 
   int sum(int n1, int n2)
   {
        int s;
        s = n1 +n2;
        return s;
   }

Output:

  1. Enter two numbers: 10, 20
  2. sum is 30

Function Declaration:

A function declaration tell the compiler about the function's name, function parameters, and return type. It doesn't contain function body. Function declaration is also known as function prototype or function signature.

A function declaration gives information to the compiler that the function may later be used in the program.

Syntax of function declaration:

return_type function_name(Arguments list);

For Example:

This statement taken from the above example written just before the void main()

/* Function declaration or function prototype */
int sum(int a, int b);

Since this function is adding two integer therefore, It should have some meaningful name like sum(), need two integer variables like int a and int b,  and the result of two integers will be an integer. Hence, Function should return a value. Therefore, its return type should be an int as you can see in the above example. But do remember It doesn't contain function body.  

Note: While providing argument list during function declaration, you can avoid the argument name, For Example: int sum (int, int); instead of  int sum (int a, int b); 

When do you need a function declaration? 

When you define a function before the main() function in your program then you don’t need to do function declaration. Let's understand with a simple example.

But if you are defining a function after the main() then you need to declare the function first, else you will get compilation error. Let's see the  Example:

Function Definition:

Block of code written inside the curly braces is called function definition or body of the function

For Example:
  1. /*Function definition or function body*/
  2. int sum(int n1, int n2)
       {
            int result;
            result = n1 +n2;
            return result;
       }

  •  

Function Call:

To execute the codes of function body, the user defined function need to be invoked (call).

In C language, to call a function you simply need to pass the required parameters along with the function name, and if the function return a value you can store the return value.

The parentheses may or may not contain argument list, its depending on the function definition.

For Example:

/* Function call by passing value */
sum = sum(a, b);

See the above example where a function sum(a, b); is called inside the main() 

Remember: The function can be called from anywhere in the program. It can be called from the main function or from any other function if the program is using more than one function. The function that calls another function is called the “Calling function”.

Every C program has at least one function, which is main() and it is called by the operating system. main() function is the entry point of any C program. It is the point at which execution of program is started. When a C program is executed, the execution control goes directly to the main() function.


Points to Remember:

  • Any program must have at least one function. If a program contains only one function, it must be main().
  • Function is an operation, once defined can be used many times.
  • Every C program has a function called main(); that is called by the operating system when a user run a program.
  • Function consume memory only when it is invoked and released from RAM as soon as it finished its job.
  • Every function has a name that must be unique. 

jjjgj








Comments