You are on page 1of 13

1. Who has invented C?

Ans: C was invented by Dennis Ritchie at AT & T’s Bell Laboratories of USA in 1972.
2. What is datatype? Name the three basic datatypes in C.
Ans: A datatype is a type of data that we have to declare beforehand to tell the complier
which type of data we are going to use in our program. Then the complier captures enough
memory spaces to hold those variables. There are many data types in C language such as,
integer, float, character, short, long etc.
3. What are variables?
Ans: Variables in C are memory locations that are given names and can be assigned
values. We use variables to store data in memory for later use. There are 2 basic kinds of
variables in C which are numeric and character.
● Numeric variables-​Numeric variables can either be integer values or they can be
Real values. Integer values are whole numbers without a fraction part or decimal
point in them. Real numbers can have a decimal point in them.
● Character variables​-Character variables are letters of the alphabet as well as all
characters on the ASCII chart and even the numbers 0 - 9. Characters must always
be put between single quotes. A number put between single quotes is not the same
thing as a number without them.
● What is the difference between variables and constants?
Ans: The difference between variables and constants is that variables can change their value at
any time but constants can never change their value. Constants can be useful for items such as Pi.
Using constants can stop you from changing the value of an item by mistake.
For example, in the equation 3X+2Y=20 3 and 20 are constants, because they
cannot change. X and Y can change, hence they are called variables.
5. What do you mean by variable declaration and variable definition. Give example.
Ans: A variable is declared by the statement int x;
Which means that x is an integer type variable and it can hold 2 bytes memory space. But
we have not give the value of x. Here, x is just declared as the integer variable.
When we write the statement x=10;
It means that x is a integer type variable which has the value 10. A variable is defined in
this way.
6. Give the name of any 10 keywords .
Ans. Name of ten keywords are int, char, float, if, else, void, switch, for, while, continue.
7. What are the largest and smallest possible integer constant in C?
Ans: The largest and smallest possible integer constant in C are +32767 & -32768
respectively.
8. How many logical and arithmetical operators in C and write their names.
Ans: Logical operators in C are : ! (Logical NOT), &&(Logical AND), ||(Logical OR).
Arithmetical operators in C are: +(Addition), -(Subtraction), *(Multiplication),
/(Division), %(Modulo Opeartor).
9. What is a loop? How many loop structures are in C? Write their names.
Ans: Loop means the repetition of some portion in the program either a specified number
of times or until a particular condition is being satisfied.
There are 3 loop structures in C: For loop, While loop and Do-while loop.
10. Write the difference between these two expressions: b=a++ and b=++a.
Ans: b=a++ means a postfix expression and b=++a means a prefix expression. b=a++
means first the value of a is assigned into b and then a is incremented by 1 and b=++a means first
the value of a is incremented by 1 and then a is assigned to b.
For example, if the value of a is 5 then after b=a++, values of a and b will be 6 and 5
respectively.
And if the value of a is 5 then after b=++a, values of a and b will be 6 and 6 respectively.

11. Write the main difference between a while loop and do-while loop​.
Ans: The differences of the while and do-while loop is the place where the condition is tested.
The while loop tests the condition before executing any of the statements within the while loop.
Whereas, the do-while tests the condition after having executed the statements within the loop.
This means that do-while would executed its statements at least once, even if the condition fails
for the first time itself. The while, on the other hand will not execute its statements if the
condition fails for the first time.
12. What is the difference between “continue” and “break” statement?
Continue means, whatever code that follows the continue statement within the loop, code block
will not be executed and the program will go to the next iteration, Break statement will just stop
execution of the loop and exits from the inner most loop and then go to the next statement after
the loop if any.
int i;

for (i=0;i<10;i++)

if (i==5)

continue;

printf("%d",i);

if (i==8)

break;

This code will print 0 to 8 except 5.

For example, in the above when the control reaches i=5, it checks the condition in the if
statement and executes 'continue' and increments i by 1, So the statements after continue, which
are the printf statement, the next if statement, will not be executed.
In this case when i=8 the program will jump out of the inner most loop (for loop). So, it won’t
continue i=9 and 10.

13. Write the full form of ASCII. Write the name of the built-in function which is used to
convert a character to its ASCII equivalent?
Ans: The full form of ASCII is American Standard Code for Information Interchange.
The built-in function is toascii( ).
14. Why the “break” statement is necessary in switch statement?
Ans: “break” statement is used in switch-case statements to delimit the different cases and it
takes the control outside the switch .
switch(i)
{
​case 1:​ ​printf("Case 1");
​case 2:​ ​printf("Case 2");
​case 3:​ ​printf("Case 3");
​default:​ ​printf("Default Case");
}
For example, in the above statement, there is no break statement at any case. So, when the
control reaches at any one of the cases, the successive cases are executed, therefore, the
successive printf( ) statements are also executed, which is not our expected output. So, break
statement are used to give a limit at each case so that no other cases can be executed while the
control is at any one case.
15. Is a float datatype allowed in switch expression?
Ans: No, a float datatype is not allowed in a switch expression.

16. What are the differences between a switch and an if statement?


Ans: A switch statement cannot use a condition like i<=20 unlike if statement. And in switch
statement, only a int and a char constants are allowed, but a float is not allowed whereas, in if
statements, all types of constants are allowed.

17. What will happen if we use “continue” statement in switch instead of “break”
statement?
Ans: A “​continue​” statement will not take the control to the beginning of ​switch ​which
must be done and a very important issue while executing a switch statement . ​So, the compiler
generate an error message.

18. How the register variable is different from an​ ​ordinary variable. Explain?
Ans: A variable declared as register variable is stored in CPU register instead of
memory. A value stored in CPU register can always be accessed faster than the one that is stored
in memory. So, if a variable is used at many places in a program it is better to declare its storage
class as ​register.
19. Can we declare a floating or double variable as register? Justify your answer.
Ans: No, we cannot declare a floating or double variable as register, because the size of a CPU
register is 16-bit. And the sizes of a floating and double variable are 4 and 8 bytes respectively.
So, the CPU register do not have the enough memory space to hold a floating and a double
variables.

20. What is a function?


Ans: A function is a self-contained block of statements that perform a coherent task of some
kind. Functions are of two types-library function and user-defined function. Library functions are
the functions those exist in header files and user-defined functions are defined by user manually.
A user-defined function or simply a function is called by either a main function or a user-defined
function.

21. What is the basic difference between the main function and other user-defined
functions?
Ans: Main function is called by operating system, and the user-defined function is called by
either the main function or by any other user-defined function.

22. What are the advantages of functions?


Ans:
● Divide a large program into modules.
● Large code gets shorter by using a function.
● It is easier to understand the logic involved in the program
● Testing is easier
● Recursive call is possible
● Debugging is easier.

23. What are called function and calling function?


Ans: The function which calls another function is called the ‘called function’ and the function
which is called by another function is called the ‘calling function’.
For example,

void main()
{
​func();
}
In this example, main( ) is the called function and func( ) is the calling function.
24. What is an argument? differentiate between formal arguments and actual
arguments?
Ans: The parameters in the ‘called function’ and ‘calling function’ are called arguments. The
parameters in the ‘called function’ are called ‘actual arguments’ and the parameters in the
‘calling function’ are called ‘formal arguments’.
calsum(a,b,c) ----- a,b and c are actual arguments.
calsum(int x, int y, int z) -----x, y and z are formal arguments.

25. What is the difference between an iterative function and a recursive function?
Ans: An iterative function is a function is a function which is called by another function and
which can call another function, but a recursive function is a process by which a function calls
itself repeatedly, until some specified condition has been satisfied.

void func()
{
​-----
​-----
​-----
​another_func();
}
void func()
{
​-----
​-----
​-----
​func();
}

26. What is the difference between call by value and call by reference?
Ans: Whenever we call a function and pass the ‘values’ of variables to the called function. Such
functions calls are called ‘calls by value’.
And when we call a function and pass the ‘addresses’ of variables to the called function.
Such functions calls are called ‘call by reference’.

Example of call by reference:


show(int *a)

void main()
{
​int a=10;
​show(&a);
​printf(“Value is= %d”, a);
}

show(int *a)
{
​*a=25;
​printf(“Value is= %d\n”, *a);

}
Example of call by value:
show(int a)

void main()
{
​int a=10;
​show(a);
​printf(“Value is= %d”, a);
}

show(int a)
{
​a=25;
​printf(“Value is= %d\n”, a);

27. What is an array?


Ans: An array is a collection of similar types of data that are stored in consecutive memory
locations. These similar elements can be all integers, or all floats, or all characters etc​. ​These can
be the percentage of marks of 100 students, or salaries of 10 employees etc. Each member in an
array is referred to by its position in the array. For example marks is an array having marks of 5
students. The array marks declared as
int marks[5]={12, 24, 56, 34, 78};
Here, we referred the 2​nd element as marks[1], 4​th element as marks[3]. 1,3 are called the index of
array. First element of an array starts with index 0, not with 1.
28. How an array is initialized and how elements are inputted to an array. Describe with
examples.
Ans: An array is initialized by the statement
int elements[5];
it means, elements is an array which can hold 5 integers.
And elements are inputted to an array by the following statement:
elements[5]={ 10, 20, 30, 40, 50};

29. What is the difference between an integer and an array?


Ans: An integer can hold only one value whereas an array can hold a no. of elements which are
stored in consecutive memory locations.
30. What are the disadvantages of an array?
Ans: An array can not contain dissimilar types of elements. i.e. we can not create an array which
contains some integers and some floating-point variables.

31. What is a string? What is the difference between a string and an array?
Ans: A string is an array of characters. A string variable is any valid C variable name and is
​ he general form of declaration of a string variable is
always declared as an ​array. T

char string_name[ size ];

​ ome examples are:


The size determines the number of characters in the ​string-name. S

char city[10];
char name[30];

When the compiler assigns a character string to a character array, it automatically supplies a ​null
character ('\0 ') at the end of the string. Therefore, the ​size s​ hould be equal to the maximum
number of characters in the string ​plus​ one
Differences between an array and a string are:
● An array is collection of integers and floats whereas; a string is a collection of characters.
● The termination of a string is indicated by ‘\0’ (null) but there are no such indications to
indicate the termination of an array rather than the index of the array.

32. What are the functions of the library functions ​strlen​ and ​strcat?

Ans: The function ​strlen​ inputs a string and returns the length of the string.
strlen(“Kolkata”);
Here, the strlen function returns 7 as the length of the string “Kolkata”.
The function ​strcat​ inputs two strings and appends a string to the end of another and returns
the total string.
For example, strcat(“Kolkata”, “is a city”);
Here, the strcat function appends the string “is a city” to the string “Kolkata” and returns
the string “Kolkata is a city” as the output.

33. How do you print the address of a variable? Give an example.


Ans: We print the address of a variable by the ‘&’ operator which is known as ‘address of’
operator.

Location number
(Address)
Value of location
Location name
6000
3
i

void main()
{
int i=10;
printf(“Address of i is %u”, &i);
printf(“\nValue of i is %d”, i);

34. What is a pointer?

5
6000
a
j
6000
6500
A ​pointer​ is a ​data type​ whose value refers directly to (or "points to") another value stored
elsewhere in the ​computer memory​ using its ​address​. Obtaining or requesting the value to which
a pointer refers is called ​dereferencing​ the pointer..
The basic syntax to define a pointer is
int *x;
This declares x as a pointer to an integer.
Once a pointer has been declared then, perhaps, the next logical step is to point it at something
int a = 5;
int *x;

x = &a;
This assigns the value of x to be the address of ​a​. For example, if ​a is stored at memory location
of 6000 then the value of ​money​ will be 6000 after the assignment. Address of x is 6500.

35. Why pointers are used?


Ans:
● They are primarily used for constructing ​references​, as well as in passing data between
different parts of a program.
● Pointers are used to traverse through an array or a string to get the desired elements of
array or string.
● Pointers are used to pass parameters by reference. This is useful if we want a function's
modifications to a parameter to be visible to the function's caller.
● This is also useful for returning multiple values from a function.
● Pointers can also be used to allocate and deallocate dynamic variables and arrays in
memory.

36. What is a null pointer?


Ans: NULL pointer is a type of pointer of any data type and generally takes a value as zero. This
is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory
address.
For example:
int*​ ​ oint;
p

point=NULL;
The above statement denotes point as an integer pointer type that does not point to a valid
memory address. This shows that point has a NULL pointer value.
37. Write the function of the sizeof( ) operator with an example.
Ans: The ​unary operator '​sizeof' is used to calculate the size of any datatype. ​sizeof can be
applied to all datatypes, such as the ​integer and ​floating-point types defined in the language,
pointers to memory addresses​, or the compound datatypes (​unions​, ​structs​, or C++ ​classes​).
sizeof is a ​compile-time operator that returns the size in bytes of the variable or parenthesized
type-specifier that it precedes.

sz=sizeof(int);
In the preceding code, sizeof( ​) operator returns 2 as the size of an integer variable.
38. What is a structure?
Ans: A ​structure​ is a collection of variables under a single name. These variables can be of
different types, and each has a name which is used to select it from the structure. A structure is a
convenient way of grouping several pieces of related information together
A structure can use other structures, arrays or pointers as some of its members.

struct point {
int x;
int y;
};
Here, the name of the struct is "point". We define our variables inside the braces. The variables
inside the braces are called members. Each member has it's own type. We can use float, double,
char, string, even an array as a member.

39. What is the difference between a structure and an array?


Ans: i) Array is collection of similar datatypes whereas a structure is a collection of dissimilar
datatypes.
ii) The size of an array is the no. of elements*size of the variable by which the array has
been created i.e int, float or char.
But size of the structure is the sum of the sizes of all its attributes.
iii) array is static memory allocation where as structure is a dynamic memory allocation.

40. Suppose you have to store 10 integers in memory. What would you prefer, an array
or a structure. Give the reason on your choice.
Ans: I prefer an array to hold 10 integers in memory because an array is a collection of similar
data types. And an array of 10 integers is also a collection of similar data types. So, it would be
better to prefer an array instead of structure to store 10 integers.

41. What does a malloc( ) function do?


Ans: malloc( ) is the dynamic memory allocation function in C. malloc( ) will dynamically
allocate a section of memory of a given size for the data structure.
For example,:
malloc(sizeof(struct char*))
means, malloc( ) function dynamically allocates a string.

42. What are the difference between structure and union.


Ans: .
● Union allocates the memory equal to the maximum memory required by the member of the
union but structure allocates the memory equal to the total memory required by the
members.
● In union one block is used by all the member of the union but in case of structure each
member have their own memory space.
● Union is best in the environment where memory is less as it shares the memory allocated.
But structure can not implemented in shared memory.
● As memory is shared ambiguity are more in union but less in structure.
● Self referencial union can not be implemented in any data structure but self-referencial
structure can be implemented.

43. How we create an object of a structure? Give an example.


Ans: We can create an object of a structure by the following declaration.
struct <struct_name> <object_name>;
for example, if we have a structure
struct point {
int x;
int y;
};
Then we can create an object of the structure point by
struct point p1;

we can also create an object of any structure during the declaration of a structure as
struct point {
int x;
int y;
}p2;

Here, p2 is an object of the “point” structure. This type of object is called ​global object.​

44. How we can access the elements of a structure using an object?


Ans: We can access the elements of a structure by writing the object name followed by a ‘.’
operator. This operator signifies which member belongs to which structure.
For example, to access the member x and y of the structure point, we need the following
declarations:
p1.x;
p1.y;

if p1 is an object of structure “point”.

45. What are the different types of modes in a FILE? Answer briefly.
Ans: Different types of modes are:
​r: ​Reading from a file,
​w: ​Writing to the file,
​a: ​Adding new contents at the end of the file,
​r+: ​Reading existing contents, writing new contents, modifying existing
contents of the file. ​w+:​ Writing new contents, reading them back and modifying existing
contents of the file
​a+: ​ Reading existing contents, adding new contents at the end of the file but cannot
modify existing contents.

46. If we want to open a file named “myfile.c” in ​write ​mode, then what happens, if

● “myfile.c” does not exist on the disk.


● “myfile.c” exists on the disk.

Ans: If the file “myfile” does not exist, then a new file is created by that name.
And if the file exists, then it contents are overwritten by new contents.

47. If we want to open a file named “myfile.c” in ​read ​mode, then what happens, if
● “myfile.c” does not exist on the disk.
● “myfile.c” exists on the disk.
Ans: If the file “myfile” does not exist, then loads it into memory and sets up a pointer which
points to the first character in it. If the file doesn’t exist it returns NULL.

48. Why the fopen( ) and fclose( ) functions are needed in File?
Ans: ​fopen( )​ is used to open a file. To open a file by using this function the following 3 steps
are performed.
1. Firstly, it searches the file on the disk.
2. If the file is present, it loads the file from the disk into memory. If the file is absent, ​fopen(
)​ returns a ​NULL​.
3. It sets up a pointer which points to the character according to the mode of the file.
​fclose( )​ is used to close a file.
49. What is the difference between scanf( ) and gets( )?

Ans: ​scanf( )​ cannot handle multi-word strings whereas ​gets( ) ​can handle this.
For example,
char str[80];
scanf(“%s”, str);
If we input “Kolkata is a city” in the string str, then the str can take only the substring “kolkata”,
not the entire string.
But if we use ​gets( ) ​instead of ​scanf( )
gets(str)
Then, str can take the entire string as “Kolkata is a city”.

50. Why getc( ) functions are used in File?

Ans: To read the file’s contents from memory ​fgetc( )​ function is used.
ch=fgetc(fp);

fgetc( ) reads the character from the current pointer position, advances the pointer so that it now
points to the next character, and returns the character that is read, which we collected in the
variable ​ch.

You might also like