ComputerShikshak.com

C Programming Questions and Answers

What is a Keyword in C?

In C programming language a keyword is a predefined word reserved for a specific purpose. Keywords are also known as reserved words. Each keyword has a specific meaning and can be used only for a specific purpose as defined previously. 

For example, if, else, for, switch, break etc. are some keywords available in C.

What is a Constant in C?

In C programming language a constant is a fixed value that do not change during the execution of a program. Constant is also known as ‘literal’.

For example, 8, 2.5, ‘P’, ‘g’, “Computer” etc. are constants.

What are the different types of Constants available in C?

The different types of constants available in C are as follows:

  1. Integer Constants: An integer constant is made up of a sequence of digits. The three types of integer constants available in C are as follows:
    • Decimal Integer Constant: A decimal integer constant is made up of any combination of digits from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, preceded by an optional or + sign.
      For example, 120, -25, +70 etc. are valid decimal integer constants.
    • Octal Integer Constant: An octal integer constant is made up of any combination of digits from the set {0, 1, 2, 3, 4, 5, 6, 7} preceded by a 0.
      For example, 045, 056, 0324 etc. are valid octal integer constants.
    • Hexa-decimal Integer Constant: A Hexa-decimal integer constant is made up of any combination of digits from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} or {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f} preceded by 0x or 0X.  The letters A, B, C, D, E and F whether in uppercase or lowercase represent the numbers 10, 11, 12, 13, 14 and 15 respectively.
      For example, OX36, OxA, 0X3D1 etc. are valid hexa-decimal integer constants.
  2. Real Constants: The numbers which contains fractional parts are known as real or floating point constants.
    For example, 3.45, 7.31, 25e4, 1.5E-4 etc. are valid real constants.
  3. Single Character Constants: A single character enclosed within a pair of single quotation mark is known as a single character constant or character constant.
    For example, ‘S’, ‘r’, ‘;’ etc. are valid character constants.
  4. String Constants: A sequence of characters enclosed within a pair of double quotation mark is known as a string constant.
    For example, “Program”, “562356”, “12, R. D. Street” etc. are valid string constants.
  5. Backslash Character Constants/Escape Sequences: A character(an alphabet or a special character or a digit) preceded by a backslash(\) is known as backslash character constant. Backslash character constants available in C are predefined. 
    For example, ‘\n’, ‘\t’, ‘\0’ etc. are some backslash character constants.

What is an Identifier in C?

An identifier is a name given to a variable, function, structure etc. Identifiers are user-defined names.

For example, the statement “int x,y,z;” consists of three variables namely x, y and z which are also known as identifiers.

What is a variable?

A variable is an identifier that may be used to store different values at different times during the execution of a program. It is basically a name that is given to a memory location of a computer system where a value can be stored. Using the name of a variable attached to a memory location we can also access the value stored in that memory location.

For example, the statement “int x=5;” when executed creates an integer type variable ‘x’ and ‘5’ is stored in ‘x’. It actually means a memory location is reserved to store an integer value and the name ‘x’ is given to that memory location and ‘5’ is stored in that memory location. This is shown below:

What is a pointer variable?

A pointer variable is a type of variable that can be used to store the address of another variable.

Note that the data type of both the variables must be same i.e. if we want to store the address of an integer variable in a pointer variable then the pointer variable must also be of integer type.

For example, consider the code snippet given below:

				
					int a,*ptr;
a=5;
ptr=&a;
				
			

In the code snippet given above a normal variable namely a and a pointer variable namely ptr are declared at line number 1. Both of these variables are of integer type.  Here, the operator asterisk(*) before ptr tells that ptr is a pointer variable.

At line number 2 the value 5 is stored in a.

The operator ampersand(&) is known as address of operator, when placed before a variable, gives the address of that variable.

At line number 3 address of a is stored in the variable ptr using the & operator.

The diagrammatic representation of the explanation given above is as follows:

What do you mean by entry-controlled loop?

The loop in which condition is checked before the execution of the body of the loop is known as entry-controlled loop. Entry-controlled loops available in C are for loop and while loop.

What do you mean by exit-controlled loop?

The loop in which condition is checked after the execution of the body of the loop is known as exit-controlled loop. Exit-controlled loop available in C is do-while loop.

Differentiate between while and do-while loop.

whiledo-while
It is an entry-controlled loop since condition is checked before the execution of the body of the loop.It is an exit-controlled loop since condition is checked after the execution of the body of the loop.
It will not execute at all if the condition is false at the very first attempt.It will always be executed at least once even if the condition is false at the very first attempt.

What do you mean by call by value?

While calling a function if simply one or more values or values stored in one or more variables are passed to a function as arguments or parameters then it is known as pass by value.

For example, consider the program given below:

				
					#include<stdio.h>
void add(int x,int y);
void main()
{
	add(20,30);
}
void add(int x,int y)
{
	printf("Sum is: %d",x+y);
}
				
			

In the program given above line number 5 is a function call statement. From line number 5 the values 20 and 30 are passed as arguments to the function add and 20 is stored in x and 30 is stored in y at line number 7. Thus the function add is called by value.

Now consider the program given below:
				
					#include<stdio.h>
void add(int x,int y);
void main()
{
	int a=10,b=15;
	add(a,b);
}
void add(int x,int y)
{
	printf("Sum is: %d",x+y);
}

				
			

In the program given above 10 and 15 are stored in the variables a and b respectively at line number 5. Line number 6 is a function call statement. From line number 6 the values stored in a and b i.e. 10 and 15 are passed as arguments to the function add and 10 is stored in x and 15 is stored in y at line number 8. Thus the function add is called by value.

What do you mean by call by reference?

While calling a function if addresses of one or more variables are passed to a function as arguments or parameters then it is known as pass by reference.

For example consider the program given below:

				
					#include<stdio.h>
void add(int *x,int *y);
void main()
{
	int a=50,b=100;
	add(&a,&b);
}
void add(int *x,int *y)
{
	printf("Sum is: %d",*x+*y);
}

				
			

In the program given above 50 and 100 are stored in the variables a and b respectively. Line number 6 is a function call statement. From line number 6 the addresses of a and b are passed as arguments to the function add and stored in the pointer variables x and y respectively at line number 8. Thus the function add is called by reference i.e. addresses.

Share this page on
Scroll to Top