TCS Interview Question: C programming
Main domain:
● Final year project
● C /Java/Python
● DSA
● DBMS
● Basic of operating system
● Basic of networking
● Basic of cloud computing
Interview Rounds:
· Technical Round:
o Question will be asked from resume
o Favourite Subject / Technical Skill / Internship / Certificate
o Details about project
o Your role / challenges & how you overcome
o Coding questions asked during online assessment
o Write a logic or code
o SQL Table
· Managerial Round:
o Why do you like python over C
o Question on core programming language
o Rate yourself
o Why TCS
o Situational based question
o Time taken to learn new technology
o Curiosity and life long learning
· HR Round:
o About higher study
o Do you have multiple offer?
o About family
o Relocation
o Shifting
o Work on weekend
Sample Questions:
· Reverse a string
· Reverse a number
· Prime number
· Greatest among 10 numbers
· Fibonacci series
· Swap 2 numbers without using 3rd variable
· Armstrong number
· Decimal to binary & vice versa
· Average of 2 numbers
· Odd-even number
· Check leap year
C programming Question
1. Why C is middle level language
2. What are the features of C programming language
Answer: C is a procedural programming language developed by Dennis
Ritchie at Bell Labs in the early 1970s. Key features include:
i. Portability: C programs can be run on different platforms with
minimal changes.
ii. Efficiency: C allows direct memory manipulation, leading to efficient
code execution.
iii. Mid-level Language: It bridges the gap between high-level and low-
level languages.
iv. Rich Set of Operators and Functions: Provides a variety of built-in
operators and library functions.
v. Structured Programming Approach: Emphasizes breaking down
problems into smaller, manageable functions.
3. Explain compilation and run time environments in c programming language
The compilation environment is the phase where your C source code (.c file) is converted
into executable machine code. This environment handles syntax checking, code
translation, and optimization before execution.
➤ Compilation Process Steps:
Ste Name Description
p
1 Preprocessin Handles directives like #include, #define, etc.
g
2 Compilation Converts preprocessed code into assembly code.
3 Assembly Converts assembly code to machine code (.obj or .o).
4 Linking Links multiple object files and libraries to create the final executable
(.exe).
Tools involved: C Compiler (e.g., GCC, Turbo C)
2. Run Time Environment in C
The run time environment is where the executable file is loaded and executed by the
system.
➤ What Happens at Run Time:
Component Purpose
Memory Allocation Allocates memory for code, data, stack, and heap.
Input/Output Handling Processes user input/output using scanf, printf, etc.
Error Handling Detects and handles errors like divide by zero, null pointer
access.
Function Execution Executes main() and other functions line by line.
Example: When you run the program, it starts from main() and executes based on
logic.
Key Differences
Feature Compilation Environment Run Time Environment
When It Happens Before the program runs While the program is running
Main Goal Translate code to machine Execute the translated code
language
Tools Used Compiler, Assembler, Linker Operating System, C Runtime
Library
Error Type Compile-time errors (syntax, etc.) Run-time errors (logic, memory, etc.)
4. Primary data type available in C
List of Primary Data Types in C
Data Size (in Format Example Description
Type bytes) Specifier Value
Int 2 or 4 bytes %d or %i 10 Stores integers (whole numbers)
Float 4 bytes %f 3.14 Stores decimal (floating-point)
numbers
double 8 bytes %lf 3.14159 Stores large or precise decimal
numbers
Char 1 byte %c 'A' Stores single characters
Void 0 bytes — — Indicates absence of data (used in
functions)
5. How many keywords are available in c
In the C programming language, there are 32 keywords. These are reserved words that
have special meaning and cannot be used as identifiers (like variable or function names).
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while
6. Main is a keyword?
Main is a function. Entry point of any C language i,e execution start from main
7. What is function
A function in C is a block of code that performs a specific task and can be called multiple
times in a program. It helps in modular programming, making code reusable, organized, and
easier to debug.
Types of Functions
1. Library Functions – Predefined in C (e.g., printf(), scanf(), strlen())
2. User-Defined Functions – Created by the programmer (e.g., int add(int a, int b))
8. How library function and header file interlink
What is a Library Function?
● A library function is a predefined function provided by C.
● Examples:
printf(), scanf(), strlen(), sqrt(), fopen(), etc.
● These functions perform common tasks like input/output, math operations, string
handling, etc.
What is a Header File?
● A header file is a file with .h extension that contains:
○ Function declarations (also called function prototypes)
○ Macro definitions
○ Constants and type definitions
● Examples:
#include <stdio.h> – for input/output
#include <string.h> – for string functions
#include <math.h> – for math functions
How They Work Together
Header File Declares Functions Like Library Where Code Exists
stdio.h printf(), scanf() Standard I/O library
string.h strlen(), strcpy(), String handling library
strcmp()
math.h sqrt(), pow(), sin() Math library (libm)
stdlib.h malloc(), exit(), atoi() Standard library
9. Difference between malloc and calloc
The primary difference between malloc and calloc in C/C++ lies in how they initialize
the allocated memory. malloc allocates a block of memory but doesn't initialize it,
leaving the memory with "garbage" values, while calloc allocates memory and
initializes all bytes to zero.
Here's a more detailed breakdown:
Initialization:
malloc allocates memory without initializing it, meaning the memory will contain whatever
random values were present before the allocation. calloc, on the other hand, initializes the
allocated memory to zero.
Arguments:
malloc takes one argument: the total size of the memory block to allocate. calloc takes two
arguments: the number of elements and the size of each element.
Performance:
malloc generally performs slightly faster than calloc because it doesn't have to initialize the
memory. However, the difference in performance is often negligible.
Use Cases:
malloc: Use when you need to allocate memory and initialize it yourself, or when you need a
block of memory that is guaranteed to be uninitialized (e.g., for storing raw data or as a
scratch buffer).
●
calloc: Use when you need to allocate memory and have it initialized to zero (e.g., for creating
arrays, structures, or other data structures where zero initialization is necessary).
10. Differentiate between While and Do-While loop or entry control(for loop, while) and exit
control loop (do-while)
Difference between while and do-while Loop
Feature while Loop do-while Loop
Definition Entry-controlled loop (condition Exit-controlled loop (condition
checked before execution) checked after execution)
Syntax while(condition) { // code } do { // code } while(condition);
Condition At the beginning of the loop At the end of the loop
Check
Minimum May not execute even once if Executes at least once
Execution condition is false
Use Case When the loop should run only if When the loop must run at least
condition is true from the start once
11. Disadvantages of switch case
1. Limited to int-Compatible Types
● switch can only evaluate expressions that result in:
○ int, char, or enum types.
● It doesn't support:
○ float, double, string, or complex conditions.
📝 Example: You cannot switch on a float value.
You cannot use relational operators (>, <) or logical conditions (&&, ||).
Forgetting the break statement leads to fall-through, which can cause unexpected behavior.
12. Differentiate between break and continue
Difference Between break and continue
Feature break continue
Purpose Immediately exits the loop or Skips the current iteration and moves to
switch the next iteration of the loop
Effect on Terminates the entire loop Does not terminate the loop; only skips
Loop remaining code in the current iteration
Used In for, while, do-while loops and for, while, do-while loops only
switch
After Control moves to the statement Control moves to the loop condition
Execution after the loop or switch check for next iteration
Example Exit loop when condition met Skip processing for some condition but
continue looping
13. What is pointer and types of pointer
A pointer is a special variable that stores the memory address of another variable.
Key Points:
● Instead of holding data directly, a pointer holds the address where data is stored.
● Pointers allow direct memory access and manipulation.
● They are very powerful and widely used in C for dynamic memory, arrays, functions, and
more.
Why Use Pointers?
● Efficient array handling
● Dynamic memory allocation
● Pass large data to functions without copying (pass by reference)
● Build complex data structures like linked lists, trees, etc.
Types of Pointers in C
Pointer Type Description Example
1. Null Pointer Points to nothing (usually initialized to int *p = NULL;
NULL)
2. Void Pointer Generic pointer that can point to any data void *ptr;
type
3. Wild Pointer Uninitialized pointer that points to random int *p; (without initialization)
memory
4. Dangling Pointer pointing to a memory location that After free(p); or returning local
Pointer has been freed or deleted variable address
5. Function Points to a function instead of data int (*funcPtr)(int, int);
Pointer
6. Pointer to A pointer that stores the address of int **pp;
Pointer another pointer
Explanation:
● Null Pointer: Used to indicate that the pointer doesn’t point anywhere.
● Void Pointer: Can point to any data type but must be cast before dereferencing.
● Wild Pointer: Dangerous, causes undefined behavior; always initialize pointers.
● Dangling Pointer: Points to freed/deleted memory; must be avoided to prevent errors.
● Function Pointer: Used to call functions indirectly, useful in callbacks.
● Pointer to Pointer: Useful for dynamic memory and multi-level referencing.
you should not store a float variable in an int pointer — doing so is unsafe and incorrect.
14. Call by value and call by reference
1. Call by Value
In Call by Value, a copy of the actual variable is passed to the function.
Changes made inside the function do not affect the original variable.
Example:
c
CopyEdit
#include <stdio.h>
void modify(int x) {
x = x + 10;
}
int main() {
int a = 5;
modify(a);
printf("Value of a: %d", a); // Output: 5 (no change)
return 0;
}
🔸 The value of a remains the same because only a copy of a was passed.
2. Call by Reference
In Call by Reference, the address of the variable is passed to the function using pointers.
Changes made inside the function directly affect the original variable.
Example:
c
CopyEdit
#include <stdio.h>
void modify(int *x) {
*x = *x + 10;
}
int main() {
int a = 5;
modify(&a);
printf("Value of a: %d", a); // Output: 15 (changed)
return 0;
}
🔸 The value of a changes because the function has access to its memory address.
🧠 Summary Table:
Feature Call by Value Call by Reference
What is passed? Copy of the variable Address of the variable
Original value ❌ No ✅ Yes
changed?
Uses Pointers? ❌ No ✅ Yes
Safety Safe (less error- Less safe (requires
prone) care)
14. What is recursion ? Explain tail and non tail recursion
Recursion is a programming technique where a function calls itself directly or indirectly to
solve a problem.A recursive function must have a base case to stop recursion, otherwise it
causes infinite calls.
Tail Recursion vs Non-Tail Recursion
Aspect Tail Recursion Non-Tail Recursion
Definition Recursive call is the last operation in Recursive call is not the last
the function operation
Example return factorial(n-1); is the last step return n * factorial(n-1);
(multiplication after recursion)
Optimization Can be optimized by compiler to avoid Cannot be optimized easily, uses
extra stack frames (tail call optimization) more stack memory
Stack Usage Less stack memory usage More stack memory usage
15. Difference between Recursion and iteration
Feature Recursion Iteration
Definition A function calls itself to solve a problem Repeating a block of code
using loops
Approach Divide problem into smaller sub-problems Repeatedly execute a loop
until condition met
Termination Uses base case to stop recursive calls Uses loop condition to exit
Memory Uses more memory due to function call Uses less memory (only loop
Usage stack variables)
Performance Generally slower due to overhead of calls Generally faster and efficient
Complexity Can be complex and harder to understand Usually simpler and more
intuitive
When to Use Suitable for problems naturally defined Suitable for simple repetitive
recursively (like trees, factorial) tasks
Example Factorial function calling itself Factorial calculated with
for/while loop
16. Structure vs union
Feature Structure Union
Memory Separate memory for each Shared memory for all members
allocation member
Size Sum of sizes of all members Size of largest member
Usage Store all members simultaneously Store only one member at a time
Access Access any/all members anytime Access only one member at a time
Initialization Members can be initialized Only one member can be initialized
individually at a time