Saturday, 17 October 2015

C Programming Basics - Questions For Technical Interview

C Basics for Interview are covered in this post with each and every questions asked in technical interview of different companies like TCS, Wipro, Infosys, etc. At first we will provide complete theory on every aspects of C language and then you will find C basics interview questions and answers with pdf to download.

C Programming Language Basics:


C Basics for Interview are shared below and we have tried our level best to make it easy for you to understand. We will be covering up these topics in this post:

  • C Dynamic Memory
  • C Strings
  • C Structure & Union

Introduction to C Programming:


C is a mid-level programming language developed in 1972 by Dennis Ritchie for creating UNIX Operating System. It has properties of both low level & high level language since it is used for creating system applications like kernels and the property of machine in-dependency implies its high level nature.

Features of C Language:


  • Simple: C is a simple programming language which provides structural approach, rich sets of library functions, data types, etc.
  • Machine Independent: Means you can use the same code on different Computer system having same C compiler.
  • Mid-Level Language: It is used to develop system applications such as kernels, drivers, etc: which shows the feature of low level language. It is a machine independent language hence it also have feature of high level language.
  • Structured Programming Language; We can break the program into parts using functions.
  • Procedural Programming Language: In C programming, a list of instructions tells Computer what to do step by step.
  • Rich Library: C comprises of many inbuilt functions.
Header File: stdio.h Functions: printf, scanf
Header File: conio.h Functions: getch, clrscr
  • Memory Management.
  • Speed: Compilation & Execution speed is fast.
  • Pointers: Using pointers we can have interaction with memory.
  • Recursion: We can use function within function in order to make our code more shorter.
  • Extensible: It can easily adopt new features.

C Variables are the name of the memory locations in which some value is stored for the program. It can be used throughout the program in accordance with its scope.

Types of C Variables:


There are 5 types of variables present in C programming language namely:
  1. Local Variable.
  2. Global Variable.
  3. Static Variable.
  4. Automatic Variable.
  5. External Variable.

Local Variable:

Variables which are declared within function or a block.

Local & Global Variable in C
Global Variable:

These are the variables which are declared outside the function or a block and therefore they can be used throughout the program.


Static Variable:

These variables holds their value throughout the program with multiple function calls. That is its value is stored in a static memory location and it remains intact even if programs switches from one function to another.

Denoted By: static int x;

Automatic Variable:

All variables declared within a function or block are called as automatic variables by default.

Denoted By: auto int x; OR int x;

External Variable:


External Variable in C

These variables come into usage when we want to access variable from some other C Source file into current C Source file in which you are writing your code. That is using this you can share variables in multiple C source files.

Denoted By: extern int x = 10;

C Basics PDF Free Download
[Till Variables]

C Data Types:


Data types are the one which indicates the characteristics of a variable, whether it is a integer or a character or a decimal value, etc. These are necessary since while C program compilation, memory is allocated to variables according to their data type.

Frequently used data types are:

  1. Integer Type:  int
  2. Character Type: char
  3. Floating Type: float
  4. Double Floating Type: double

and many more...

Memory space used by different data types are:

char - 1 byte
int - 2 byte OR 4 byte
float - 4 byte
double - 8 byte
long double - 10 byte

Note 1: Previously when their were 16 bit processors, Integer data type uses 2 bytes of memory space but currently in 32 bit processors, Integer data type uses 4 bytes of memory space.

Note 2: float can have upto 6 decimal digits, double can have 15 decimal digits and long double can have 19 decimal digits.

%d : Integer value

%c or %s : Character or String

%u : Address

Keywords in C:


There are total 32 keywords in C programming language which cannot be used as a variable name or constant name. Ex.: int, float, for, char, while...

Operators in C:


An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc.

Ex.: x + y = 10;

In the above example the symbol + and = are operators.

C Control Statements:


C Control Statements includes - if else, switch, while - do while, for loop, break, continue ,etc. These are the controls of C programming using which we can execute a particular set of instruction till the condition satisfies correct.


C Control Statements


1. If Statement:

These type of statements works on the basis of TRUE or FALSE condition. If a particular condition comes out to be true then the code inside that function will be executed otherwise it will come out of that condition.

2. If Else Statement:

Here we provide different instructions for True and False conditions. If condition comes out to be true then it will execute code inside if statement, otherwise code inside else statement will be executed by the compiler.

3. While Loop Statement:

While statement works on the basis of loop system. Here we pass a parameter condition in the while statement and the code inside while statement will print till the condition satisfies correctly. If condition is wrong then will come out of the loop.

4. Do While Loop Statement:

Difference between While and Do While statement is that in while statement the code inside is loop is executed only if condition satisfies but in Do While Loop Statement the code inside the loop is executed atleast once before checking for condition.

5. For Loop Statement:

For loops are used when we need to execute some instructions upto a particular interval. Like if we want to print something for 10 times then we will start a loop from 1 and increment loop till 10 comes.

C Functions:


Functions in C programming are used to create a particular set of instructions separately so that it can be used (called) multiple times in a program. Its main motive is to reduce the coding part and make it re-usable. So suppose you need to make a program for calculator, and normally you have to write logic again and again but using function you just have to provide logic once and by passing parameters to the function we can get result at once.

Benefits of Function:

  1. Code Optimization: Avoid code repetition.
  2. Code Re-usability.
  3. Easy to debug.
Syntax: Data_Type Function_Name ( Parameters ) { }

C Function Example

** Passing Value or Address to Function:

There are two ways of passing data to a function: Call By Value & Call By Reference.

Important Note: This topic is generally asked in technical interview if you have mentioned C Basics in your resume, so prepare this topic very carefully.

Call By Value:

Short Definition: Passed variable is not modified, only the variable inside function is modified.

In Brief: Value / Variable being passed to the function is locally stored by the function parameter in stack memory location. So if value changes it will be only for the function's variable and not the passed variable.

Call By Reference:

Short Definition: Original value is modified because we pass address.

In Brief: Address of value is passed in the function, so actual and formal argument shares the same address space. Hence value changed inside the function is reflected inside as well as outside the function.

Lets look at one example for both topics, below we have listed an example where we are adding a value to the number within function and checking whether passed value changes or not.


Recursion in C:


Recursion is method of calling a function inside the same function.

Example:
As you can see in above example, we have declared and defined a function and called itself in that function, which is a property of recursion.

Advantage of Recursion in C:

  • Using Recursion, we can avoid unnecessary calling of functions.
  • You reduce size of the code when you use recursive call.

Disadvantage of Recursion in C:

  • It is difficult to trace the logic of the function.
  • Recursive calling increase the space complexity.

For examples on Recursion, check out our post on C Programs asked in Interviews.


Array in C:


Array is a collection of variables of similar data type. In array variables acquire continuous memory location.

Ex.: int marks[10], char name[20]

Suppose we have to input name of a person, then we will use this syntax:

char name[10];

If we enter a name as: AMIT, it have total 4 letters but array stores it in following format :

name[0] = A
name[1] = M
name[2] = I
name[3] = T

Array index starts with 0 and ends with SIZE - 1

Advantages of Array:

  • Array is used when we need to enter multiple data of same type. 
  • We can access any element of array randomly using its indexing.

Disadvantage of Array:

  • We cannot exceed the limit of size of array in between in the program that is it cannot increase its size dynamically.


Types of Array:


  • One Dimensional Array
  • Multidimensional Array

1-D Array are of the form:  

char name[5];                                                           //Declaration
char name[5] = {3, 6, 7, 9, 1};          // Declaration & Definition

2-D Array are of the form

int a[10][10];

When we talk about 2-D Array, we talk about tabular structure of elements listed in rows and columns.

Data_Type Array_Name[Size1][Size2];

Size1 = Row Size
Size2 = Column Size

2-D Array can also be represented by: int array[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

How to Pass Array to Function?


Passing array to function in C programming is useful when we need to reuse the array operation. It is denoted by following syntax:

Function_Name(Array_Name);

Ways of Passing Array to Function:

1. return_type Function_Name(type Array_Name[])

2. return_type Function_Name(type Array_Name[SIZE])

3. return_type Function_Name(type *Array_Name)

Pointers in C Language:


C pointer is a variable that points to address of a value.

Pointer Example

Advantages of Pointer:

  • It allows you to access any memory location in the Computer's memory.
  • Pointers allows us to perform dynamic memory allocation and deallocation.

Disadvantages of Pointer:

  • If pointer is updated with invalid value then it will lead to memory corruption.
  • Uninitialized pointers might cause segmentation fault.

Syntax:

int *a;
char *name;

Usage / Applications of Pointer:


  • It is used for Dynamic Memory Allocation.
  • Pointers are used in Array, Function, Structure, etc.

Types of Pointer:


1. NULL Pointer:

It is a pointer which points to nothing. That means the pointer do not hold any address within it.
Ex.: int *ptr = NULL;
2. Dangling Pointer:

Pointer pointing to a dead or some arbitrary location. That is, suppose there is a pointer which is pointing to a memory location of a variable and later that variable is deleted from that memory location while pointer is still pointing to that location.

3. Wild Pointer:

Pointer which is not initialized.

4. Generic Pointer:

Void pointers are the generic type pointers.

void *ptr;

The other pointers are Near Pointer, Far Pointer, Huge Pointer, etc.

Dynamic Memory Allocation:


Dynamic Memory Allocation allows us to allocate memory at run-time. Hence it provides an efficient use of memory.

Methods for DMA:


1. malloc() : Allocates single block of memory.
2. calloc() : Allocates multiple blocks of memory.
3. realloc() : Reallocate memory occupied by malloc() and calloc()
4. free() : frees the dynamically allocated memory.

All above functions comes under "stdlib.h" header file.

Malloc Function:

  • It allocates only one block of memory.
  • Initially it contains garbage value, since it does not initialize memory at execution time.
  • If memory is not enough, it returns NULL.
Syntax:

ptr = ( cast_type* ) malloc ( size_in_bytes );

Example:

Calloc Function:

  • It allocates multiple blocks of memory.
  • If memory is not enough, it returns NULL.
  • It initialize all bytes with 0 (zero).

Syntax:

ptr = ( cast_type* ) calloc ( number, size_in_bytes );
Example:
Realloc Function:

  •  It reallocates memory to malloc() and calloc() function, if initially memory is not sufficient.

Syntax:

ptr = realloc ( ptr, new_size);

Free Function:

  • Memory occupied by malloc() and calloc() funtion is released by free() function so that it will not consume memory until the program exits.

Syntax:

free ( ptr );

0 comments

Post a Comment