C Basics

What is C language?

The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system.  C is the most popular programming language used for writing system software as well as for business packages.

What are the features of C?

1.C is a procedural-Oriented programming language .
2.C is a compiler-based language.
3.C is a middle-level language.
4.C uses the top-down approach
5.Programs written in C are efficient and fast.
6.C is highly portable language.


  • Character Set: A character denotes any alphabet, digit, white space or any special symbol that is used to represent information. A character set is collection of characters.
  • Token: A token is the smallest individual unit of a program.
  • Instruction: An instruction is a statement that is given to computer to perform a specific operation.
  • Function: A function is a collection of instructions that performs a particular task.
  • Program: A program is a well-organized collection of instructions that is used to communicate with the computer system to accomplish desired objective.

How many files are created when a C program is written using Borland c compiler?
When a C program is written, four files will be created:
  1. Source file (e.g., first.c)
  2. Back up file (e.g, first.bak)
  3. Object file (e.g., first.obj)
  4. Executable file (e.g., first.exe)
What are the steps in executing a C program?

Language processors are the system softwares that play key role in translating a source file into an executable file. These language processors include: preprocessor, compiler, linker and loader. The translation process from source file into an executable file is depicted as follows:
  1. Entering a Program:- A text editor is used for this task. The statements written by the programmer are called source code and the file in which the statements are saved is called as source file.
  2. Preprocessing:- During the first phase of this process, a program called the preprocessor reads the source code. The preprocessor searches for special lines that begin with the # symbol. These lines (usually, called as preprocessor directives) contain commands that cause the preprocessor to modify the source code in someway. The preprocessor modifies the existing source code and stores modified source code into another file.
  3. Compiling:- During the next phase, the compiler steps through the preprocessed source code, translating each modified source code instruction into the appropriate machine language instruction. This process will uncover any syntax error that may be in program. If the program is free of syntax errors, the compiler stores the translated machine language instructions, which are called object code, in an ‘object’ file.
  4. Linking:- During the last phase of the translation process, another program called the linker combines the object file with library routines. Once the linker is finished with this step, an executable file is created. The executable file contains machine language instructions, or executable code, and is ready to run on the computer.
  5. Loading:- Once, the executable file is stored on disk, the loader that is a part of operating system brings it to main memory and starts it running.
This translation process is shown in the following figure:

What are the differences between break and exit() statements?

1) It is used to come out of loop or switch or block in which it is placed.
1) It is used to come out of entire program.
2) It is a keyword. Its definition has already defined by language developers.
2) It is a pre-defined function that is available in process.h or stdlib.h header files. It takes an argument: 0 or 1. exit(0) means a clean exit without an error message. exit(1) means an abrupt exit with an error message.

When does the goto statement is used compulsary? Why should we avoid the use of goto statement?
goto statement is the best option when one wants the control jumps from inner most loop to outer most loop at once.  
goto statement causes the loss of sequentiality in order of execution of instructions. Often, this leads us to difficulty in understanding the program. Hence, it is suggested that one should avoid the use of goto statement.

Type Qualifiers:-

Type qualifiers are the keywords that add new meanings to existing data types. There are two type qualifiers: const, volatile. 

  • Making a variable as read-only variable: In order to make the value of variable as unchanged during the execution of a program, initialize the variable with the type qualifier const as follows:

Ex: const double PI=3.1412;

This initialization tells the compiler that the value of PI must not be modified by the program. However, it can be used on the right hand side of assignment statement like other variable.
Ex: double x;

  • Making a variable as modifiable externally: In order to make variable’s value modifiable at any time by some external sources (from outside program), we use type qualifier volatile. For example,
          volatile int  x;

The value of x may be altered by some external factors even if it does not appear on the left-hand side of an assignment statement. When we declare a variable as volatile, the compiler will examine the value of the variable each time it is encountered to see whether any external alteration has changed the value. 

Some Interview Questions:

1. What is Scope?
Scope refers to the visibility of a function or variable.
If the function or variable is visible outside of the current source file, it is said to have global, or external, scope.
If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

2. What is modular programming?
If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.

3. Is using exit() the same as using return?
No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return
statement and exit() function are similar.

4. What is dangling else problem?
Dangling else problem is an ambiguous situation in which one can not judge the else statement to which if statement it belongs to. E.g., in the above syntax, we can not judge the else statement whether it belongs to outer if statement or inner if statement.
In order to solve this problem, it is better to include the outer-if-body in curly braces. Hence, the above syntax can be redefined as:

5. What is a modulus operator? What are the restrictions of a modulus operator?
A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double.

6. Differentiate between a linker and linkage?
A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

7. What is an argument? Differentiate between formal arguments and actual arguments?
An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types. 
Actual arguments are available in the function call.

8. Can a variable be both const and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. Const is the qualifier that provides more data security by throwing error when an accidental attempt is made to modify the same.However, the value can be changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

9. What are the different storage classes in C? 
There are 4 storage classes in C.
2. static
4. register

Static-duration variables are allocated in main memory, usually along with the executable code of the program, and persist for the lifetime of the program. Global variables (i.e, file scope) with or without the static specifier also have static scope.
Automatic-duration variables are allocated on the stack and come and go as functions are called and return. Variable having block scope and without static specifier have automatic storage duration.

Dynamic memory allocation in which memory is more explicitly (but more flexibly) managed, typically, by allocating it from the heap, an area of memory structured for this purpose.
Register variables uses processor registers to store their contents if available. If not  they will be converted to auto variables automatically.

10. What does static variable mean?
There are 3 main uses for the static.
1. If you declare within a function,it retains the value between function calls
2.If it is declared for a function name, it is invisible for the outer files.
(By default function is extern..so it will be visible from other files )
3. Static for global variables: that variable is limited to with in the file.
(By default we can use the global variables from outside files)

11. What are the differences between malloc() and calloc()?
There are 2 differences.
First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable).
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

12. What is the difference between declaring a variable and defining a variable?
Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.

13. What is an lvalue?
An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement,whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant.

14. Differentiate between an internal static and external static variable?
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.

15. When is a switch statement better than multiple if statements?
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.

16. What is a static function?
A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

17. What is a modulus operator? What are the restrictions of a modulus operator?
A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double.

18. Why should I prototype a function?
A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place.