Vous êtes sur la page 1sur 12

Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Q1: What is preferred for embedded applications: microcontrollers or microprocessors?

Ans: Microcontrollers are designed for embedded applications, whereas microprocessors are used in

personal computers or other general-purpose applications.

Q2: What is the difference between Complier and Interpreter?

Ans: Compiler translates the entire source program into object program at once and then the object

files are linked to produce a single executable file.

On the other hand, Interpreter translates one line of source code at a time and then executes it

before translating the next line of code.

Q3: Can we use “int” data type to store the value 32768? Why?

Ans: No. “int” data type can be used to store values from -32768 to 32767. To store 32768, we can

either use “long int” or “unsigned int”.

Q4: What is the difference between the statements “b = ++a;” and “b = a++;”?

Ans: The first statement employs prefix notation. It can be broken down into two statements a = a +

1 followed by b = a. Hence a will be incremented first and will then be assigned to b. Thus, values of

b and a will be same at the end.

The second statement employs postfix notation. It can be broken down into two statements b = a

followed by a = a + 1. Hence a will be assigned to b. Only after this assignment a will be incremented.

Thus, values of b and a will be different at the end.

Q5: Is the following statement valid?

int=10;

Ans: No. “int” is a reserved word in C and cannot be used as a user-defined variable.

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Q6: Is the following statement valid?

FLOAT = 10;

Ans: Yes. We might think that FLOAT is a reserved word which cannot be used for declaring

variables. But reserved words in C are in lower case and thus “FLOAT” will not be interpreted as a

reserved word.

Q7: Is it possible to have a C program without main() function?

Ans: Yes. If the program is written for a freestanding environment, then main() function is not

required. Freestanding environments are those where the program does not depend on any host

and can have any other function designated as the startup function. Examples of freestanding

implementations are embedded systems and the operating system kernel.

Q8: When should we not use type cast?

Ans: We should not use type casting to override a “const” or “volatile” declaration. Overriding these

type modifiers can cause the program to fail to run correctly.

Q9: What is the difference between functions getch() and getche()?

Ans: getch() captures a single character from the keyboard and assigns it to a variable, without

displaying that character on the screen.

getche() captures a single character from the keyboard and assigns it to a variable, and also displays

that character on the screen.

Q10: How can we print “10%” using the printf() function?

Ans: We can print “10%” using the following statement:

printf(“10%%”);

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Q11: What is the difference between “=” and “==” symbols?

Ans: “=” is an assignment operator which is used to assign a value to a variable.

“==” is a relational operator which is used to compare two values.

Q12: What will happen if a break statement is omitted in any case of a switch statement?

Ans: If a break statement is omitted in any case of a switch statement, the compiler will not issue an

error message. The flow of control will continue to the next case label, probably causing an incorrect

output.

Q13: Consider the statements

int a[20];

printf(“%d%d”, a[0], a[25]);

Will the compiler show any error?

Ans: No, the compiler will not show any error as C does not check the validity of the array index

neither at compile time nor at run time, but the result of running such a code is totally

unpredictable.

Q14: How can we determine whether a character is numeric, alphabetic, and so on?

Ans: The header file ctype.h defines various functions for determining what class a character belongs

to. For example, isdigit() returns a non-zero value if argument is a digit (0 – 9) and isalpha() returns a

non-zero value if argument is alphabetic.

Q15: What is the difference between Call by Value and Call by Reference?

Ans: By using Call by Value, we can send the value of the variable as parameter to a function,

whereas by using Call by Reference, we can send the address of the variable to a function.

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Also, under Call by Value, the value of the parameter is not affected by whatever operation that

takes place within the function, while in the case of Call by Reference, values can be affected by the

process within the function.

Q16: Are the expressions ++*ptr and *ptr++ same?

Ans: No. ++*ptr can be interpreted as ++(*ptr). Thus, it increments the value being pointed to by ptr.

*ptr++ can be interpreted as *(ptr++).Thus it increments the value of pointer ptr and not the value

pointed by it.

Q17: What is the difference between calloc () and malloc ()?

Ans: calloc(...) takes in two arguments: the first for the number of elements to be allocated and the

second for the size of each element. By default the bits in the allocated space are initialized to 0.

malloc(...) takes in only a single argument which is the memory required in bytes. It does not

initialize the bits to 0.

Q18: What is the difference between structures and union?

Ans: A structure variable contains each of the named members, and its size is greater or equal to the

sum of the size of all its members.

A union variable contains one of the named members at a given time and its size is same as that of

its largest member.

Q19: What are the two forms of #include directive?

Ans: The #include directive has two general forms

#include <file_name>

and

#include “file_name”

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

The first form is used for referring to the standard system header files. It searches for a file named

file_name in a standard header file library and inserts it at the current location.

The second form searches for a file in the current directory.

Q20: What is a dangling pointer in C?

Ans: In C, a pointer may be used to hold the address of dynamically allocated memory. After this

memory is freed with the free() function, the pointer itself will still contain the address of the

released block. This is referred to as a dangling pointer. Using the pointer in this state is a serious

programming error. Pointers should be assigned 0 or NULL after freeing memory to avoid this bug.

Q21: What is a wild pointer in C?

Ans: A pointer which has not been initialized is known as a wild pointer.

For example, consider the following code:

void main()

int *ptr;

printf("%u\n",ptr);

printf("%d",*ptr);

Output: Any address

Garbage value

Here ptr is a wild pointer because it has not been initialized.

Q22: What is the size of a void pointer?

Ans: The size of any type of pointer in C is independent of data type that the pointer is pointing to.

The size of all type of pointers in C is either two bytes or four bytes (depending on the compiler),

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

irrespective of whether it is a char pointer, double pointer, function pointer, or null pointer. Void

pointer is no exception to this rule and size of void pointer is also of either two or four bytes.

Q23: Can a C program be invoked from another C program? If yes, how it can be done?

Ans: Yes, a C program can be invoked from another program using system calls.

Q24: What kind of programming technique is used to generate Fibonacci series?

Ans: Fibonacci series can be generated by recursion programming.

Q25: Can a file other than a “.h” file be included with #include directive?

Ans: The preprocessor will include whatever files we specify in #include statement. Therefore, if we

have the line #include <macro.inc> in our program, the file “macros.inc” will be included in our

precompiled program.

We should always put a “.h” extension on any of the C files that we are going to include. This

method makes it easier for others to identify which files are being used for preprocessing purposes.

For instance, someone modifying or debugging our program might not know to look at the

macros.inc file for macro definitions. That person might try in vain by searching all files with .h

extensions and come up empty. If our file had been named macros.h, the search would have

included the macros.h file, and the searcher would have been able to see what macros we defined in

it.

Q26: Is NULL always defined as 0?

Ans: NULL is defined as either 0 or (void*)0. These values are almost identical: either a literal zero or

a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is

needed (although the compiler can’t always tell when a pointer is needed).

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Q27: Is it possible to create your own header files?

Ans: Yes, it is possible to create a customized header file. We can include the function prototypes

that we want to use in our program in it, and then use the #include directive followed by the name

of our header file.

Q28: How can we generate random numbers in C?

Ans: Random numbers can be generated in C using the rand() function.

For example: anyNum = rand() will generate any integer number beginning from 0, assuming that

anyNum is a variable of type integer.

Q29: Some coders debug their programs by placing comment symbols on some codes instead of

deleting it. How does this aid in debugging?

Ans: Placing comment symbols /* */ around a code, also referred to as “commenting out”, is a way

of isolating some codes that we think are causing errors in the program, without deleting the code.

The idea is that if the code is in fact correct, we can simply remove the comment symbols and

continue on. It also saves time and effort on having to retype the codes if we have deleted it in the

first place.

Q30: What is the difference between an internal static and an external static variable?

Ans: An internal static variable is declared inside a block with static storage class whereas an

external static variable is declared outside all the blocks in a file.

An internal static variable has persistent storage, block scope, and no linkage. An external static

variable has permanent storage, file scope, and internal linkage.

Q31: Are pointers integers?

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Ans: No, pointers are not integers. A pointer is an address. It is merely a positive number and not an

integer.

Q32: Which directive can be used to avoid multiple inclusions of a header file?

Ans: #ifndef preprocessor directive can be used to avoid multiple inclusions of a header file. For

example, the following code includes "headerfile.h" only if it is not already included.

#ifndef headerfile.h

#define headerfile.h

#endif

Q33: What is the cyclic property of data type in C? Explain with an example.

Ans: Consider the following code:

void main()

signed char c1=130;

signed char c2=-130;

printf("%d %d",c1,c2);

Output: -126 126

This situation is known as overflow of signed char. Range of unsigned char is -128 to 127. If we assign

a value greater than 127, then value of variable will be changed to a value if we move along

clockwise direction as shown in the figure. If we assign a number which is less than -128 then we

have to move in anti-clockwise direction.

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Q34: Write a C program to print “Hello world” without using any semicolon.

Ans:

void main()

if(printf("Hello world"))

Q35: What would be the output of the following code?

void main()

const int a=100;

int *p;

p=&a;

(*p)++;

printf("a=%d and (*p)=%d",a,*p);

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Ans: Compiler will generate an error: Cannot modify a constant value. “a” is a constant integer and

we tried to modify its value by “(*p)++”.

Q36: Is the following statement valid?

myName = “Robin”;

Ans: No. We cannot use the “=” operator to assign values to a string variable. Instead, we can use

the strcpy function.

The correct statement would be:

strcpy (myName, “Robin”);

Q37: Consider the following code:

int recur (int x)

if (x<=1)

recur=1;

else

recur=recur(x–3)+recur(x-1);

What would the value of recur if we call the function recur (6)?

Ouput: 9

Q38: Consider the following code:

void main()

int a;

printf("The number printed on screen =%d", scanf("%d", &a)); // value 20 is given as input here

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Output: The number printed on screen = 1.

scanf() returns the number of input fields successfully scanned, converted, and stored. In this case

scanf () reads only one value.

Q39: Consider the following code:

void main()

struct XX

int a=3;

char name [] = “Robin”;

};

struct XX *s=malloc(sizeof(struct XX));

printf("%d",s->a);

printf("%s",s->name);

Ans: Compiler error. We cannot initialize structure members inside the structure declaration.

Q40: Consider the following code:

void main()

char *str1=“ABCD”;

char str2[]=“ABC”;

printf("The size of str1 is %d\n The size of str2 is %d",sizeof(str1), sizeof(str2));

© Oxford University Press 2013. All rights reserved.


Pradip Dey & Manas Ghosh Computer Fundamentals and Programming in C, 2/e

Output: The size of str1 is 4

The size of str2 is 4

str1 is a character pointer; thus sizeof(str1) gives the size of the pointer variable. It could be 4 or 2

depending on the compiler.

Str2 is a character array with size 4 (including the terminating character '\0').

© Oxford University Press 2013. All rights reserved.

Vous aimerez peut-être aussi