C Programming Important Questions and Answers | C Language Questions and Answers

Q1. Explain the concept and use of type void?

Ans: Void is an empty data type. The void type specifies that no value is available. It is used in three kinds of situations:

When void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters. 


Void as a Function Return Type

Void return type specifies that the function does not return a value. There are various functions in C which do not return any value or we can say they return void. A function with no return value has the return type as void.


Example:

void sum(int a, int b); // sum function won’t return any value to the calling function.


Void as a Function Parameter

When void data type is used as a function’s parameter list, it means that the function takes no parameters. There are various functions in C which do not accept any parameter. A function with no parameter can accept a void.

 

Example:     int myfunction(void);

 

Void as a Pointer Declaration

When void data type is used in the declaration of a pointer variable, it means that the pointer is "universal" and it can point to anything. It acts as generic pointer. It could point to an int, char, double, structure or any type. We are using void pointer when we are not sure on the data type that the pointer will point to.

 

Example: void *ptr;



Q2.What does data type of a variable signify?

Ans: The data type defines an attribute to the variable. It tells what kind of data that variable can have. Data type defines the set of legal values that the variable can store. A data type specifies the type of data that a variable can store such as integer, float, character etc.


For example,         

int roll;    float a;    char x;


Q3. What are constants? Explain any four basic types of constants.

Ans: A constant is one that has a  fixed value that do not change throughout the program.  The constant can be viewed as a read only memory variable. Constants can be declared using the keyword const

Syntax: const variable_name=value;

 

Four basic types of constants in C:  

  • Integer Constants
  • Real/Floating point Constants
  • Character constants
  • String Constants

 

Integer Constant:

An integer constant is a a sequence of digits. Blanks and commas are not allowed within an integer constant. An integer constant can be either +ve or -ve.

Integer constants are whole number which has no decimal point. An integer constant can be Decimal, Octal and Hexadecimal

Example: 15, -265, 0, 99818, +25, 045, 0X6

 

Real constant:

Real constant is also called floating point constant. These have fractional parts to represent quantities like average, fees, height, area etc. which cannot be represented by integer number precisely. It can be either +ve or -ve. Commas and blank space are not allowed within a real constant.

A real constant can be represented in 2 forms.

1. Decimal form      E.g. 15.5, -16.45  

2. Exponent form    E.g. 1275E-2, -14E-2


Character Constant:

A character constant is a character surrounded by a pair of single quotes. A character constant is of size 1 byte and can contain only 1 character. A character can be an alphabet like a, b, A, C etc or a special character like &,^, $, #,@ etc or a single digit from 0 through 9.


Example:        'X', '5', ';' 

The character constant '5' is not the same as the integer 5. Each character constant has an ASCII (American Standard Code for Information Interchange) value associated with it. For example, the following statement will print 65 and A respectively:

printf(“%d”, ‘A’);

printf(“%c”, ‘65’);

 

String Constant:

 

A string constant is a group of characters surrounded by double quotes. These characters may be letters, numbers or any special characters.

Example:   "2015", "welcome"


Q4. What is a data type? Describe the fundamental data types in ‘C’ language.

Ans: The data type defines an attribute to the variable. It tells what kind of data that variable can have. Data type defines the set of legal values that the variable can store. A data type specifies the type of data that a variable can store such as integer, float, character etc.

For example,        int roll;    float a;    char x;

Following are the fundamental data types used in C:

  • Integer data type(int)
  • Floating point type (float)
  • Character data type (char)


Integer data type (int): 

The data type ‘int’ represents whole numbers with a range of values supported by a particular machine. For instance, in a 16 bit word length machine, the integer values lie between -32768 to 32767.

C facilitates some control over the integer data type by providing sub data types namely short intintlong int.

The sub type short int represents a fairly small integer value and requires half the amount of storage as a normal int uses.

Similarly a long int represents a fairly higher integer value and requires generally twice the number of bits as a normal int uses.

Another option in integer data type is declaring it as unsigned. This unsigned integer uses all bits for the magnitude of the number and is always positive. For instance, in a 16 bit machine the range of unsigned integer is 0 to65, 535. Thus long and unsigned are intended for increasing the range of values.

Keyword “int” is used for declaring an integer variable. To define an integer variable to store the roll number of a student:

int roll;


Floating point type (float):

It is used to store floating point numberFloating point numbers are numbers that have a decimal point. This data type in ‘C’ is an attribute for real numbers. The required declaration is,

          float a;

which instructs the compiler that the variable ‘a’ belongs to the data type real. If we want to initialize the variable, then

       float a;

a=14.752;

This can also be achieved through a single statement.

float a=14.752;

The keyword float defines the floating point number.

When more accuracy is required, another sub-data type, double can be used. Double data type uses twice the storage as that of the float data type. Float is just a single-precision data type, double is the double-precision data type.


Character data type (char):

It is usually used to store a single character in 1 byte (8 bits) of internal storage. The char keyword defines a character data type. Thus the declaration for this is

          char x;

          x= ‘a’;

The variable x is of type character and is initialized to the character ‘a’. The same effect could be achieved as,

          char x= ‘a’;


Q5. What is an operator? Describe various types of operators in C language.

Ans: An operator is a symbol that tells the compiler to perform certain mathematical or logical computations. Operators help the programmer to perform computation on values.

Various types of operators in C language are explained below:


Assignment Operator:

An assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The = symbol is used as an assignment operator.

Or simply we can say, a data or value can be stored in a variable with the help of assignment operator. The assignment operator is used in assignment statement and assignment expression.

The syntax of an assignment is,

Variable_name=expression;

For example,         a=5;  Sum=a+b;


Arithmetic Operators:

The arithmetic operators are used to perform mathematical calculations such as addition, subtraction etc. C supports all the common arithmetic operators.


Operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus


These operators can operate on any built–in data types such as int, char and float. However, the modulus operator requires an integral operand (must not be float).


Relational Operators:

These are used to compare the values two variable or constants. The relational operators are symbols that are used to test the relationship between variables or between a variable and a constant. 

For example, (salary==5000)

Here == is the operator that is used to test equality.        

There are six relational operators provided by C. They are:


Operator

Meaning

==

equal to

!=

not equal to

greater than

less than

>=

greater than or equal to

<=

less than or equal to


Logical Operators:

Logical operators are symbols that are used to combine two or more expressions containing relational operators. This situations will arise when we want to perform a statement block on a combined condition, like x>5 and x<10. Then we code this using an AND operator. The AND operator is represented as &&.


Example: ((x>5)&&(x<10))

(Salary>=10000&&salary<=20000)

The logical operators are used to combine conditions. These operators are used with one or more operand and return either value zero (for false) or one (for true). The operand may be constant, variables or expressions. And the expression that combines two or more expressions is termed as logical expression.


C has three logical operators:


Operator

Meaning

&&   

AND

||       

OR

!

NOT

 

Increment and decrement Operators:

The increment and decrement operators are two very useful operator used in C. Both increment and decrement operator are used on a single operand and variable, so it is called as a unary operator.


Increment operator:

Increment operators are used to increase the value of a variable by 1. This operator is represented by ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable. Thus it can be classified into two types:

    

    Pre-increment operator (Prefix increment operator):

A pre-increment operator is used to increment the value of a variable before using it in an expression. In the pre-increment, first increment the value of variable and then used inside the expression.


Syntax:      ++variable;

For example:    x=++i;

In this case, the value of i will be incremented first by 1. Then the new value will be assigned to x for the expression in which it is used.


Post–increment operator (Postfix increment operator):

A post increment operator is used to increment the value of the variable after executing the expression completely in which post increment is used. In the post-increment, first value of variable is used in the expression and then increment the value of variable.


Syntax:      variable++

For example:    x=i++;

In this case, the value of i is first assigned to x and after that, i is incremented.

 

Decrement Operators:

We use decrement operators in C to decrement the given value of a variable by 1. This operator is represented by -- symbol.

Decrement operator can be classified into two types:


Pre-decrement operator (Prefix-decrement operators):

The pre-decrement operator first decrement the value of variable and then used inside the expression.

Syntax:      --variable;

Example:    x=--i;

 Here, value of i will decrement first by 1 and then assign it to the variable x.


Post-decrement operator (Postfix-decrement operators):

In post-decrement first value of variable is used in the expression and then decrement the variable.

Syntax:      variable—

Example:   x=i--;

Here, the value of i is first assigned to x and after that, value of i is decremented.


Conditional Operators:

Sometimes we need to choose between two expressions based on the truth value of a third logical expression. The conditional operator can be used in such cases. It sometimes called as ternary operator. Since it required three expressions as operand and it is represented as (?  :)


Syntax:                exp1 ? exp2 :exp3;

Here exp1 is first evaluated. It is true then value return will be exp2 . If false then exp3.


Example:   c=(a>b)?a:b;

Where: a and b    are operands

         ?:    is the conditional operator

         >    is the relational operator

If the condition is true then value a is assigned to c otherwise b is assigned to c.


Bitwise Operators:

For manipulating data at the bit level C provides us special operators known as bitwise operators. The bitwise operators are the operators used to perform the operations on the data at the bit level. These operators are used to perform bitwise operations such as for testing the bits, shifting the bits to left or right, one’s complement of bits etc.

These operators can be applied only on int and char data types but not on float and double data types. Using bitwise operators, we can manipulate each and every bit of memory. Since we are doing it in bit level the operation will be much faster and efficient.

Various bitwise operators in C:


Operator

Meaning

~

Bitwise(1's) complement

<< 

Shift left

>> 

Shift right

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

(Exclusive OR)



Q6. What is the use of malloc() and calloc() function?

Ans:  malloc() and calloc() functions are used for dynamic memory allocation in the C programming language. 

The malloc or memory allocation function is used to allocate a block of memory of the specified size (in bytes). It returns a pointer to the first byte of allocated space. The type of the pointer it returns is void, which can be cast into a pointer of any type. It returns NULL, if there is not enough memory available. The malloc() function does not initialize memory at execution time, so it has garbage value initially.


Syntax: 

ptr=(cast_type*)malloc(size_in_bytes)

The calloc or contiguous allocation function works similar to malloc() function except for the fact that it requires two parameters or arguments as against one parameter needed by malloc(). It allocates multiple block of requested memory.


Syntax:      

ptr=(cast_type*)calloc(n, size_in_bytes);

Here, n is the number of blocks and size_in_bytes specifies size of each block

calloc() initializes each block with a default value 0. It returns NULL, if there is not enough memory available.




Post a Comment

0 Comments