Learn C programming Questions answers through examples.
The Arithmetic and Logic Unit (ALU) is a part of the CPU that performs arithmetic operations (like addition, subtraction, multiplication, and division) and logical operations (like comparisons, AND, OR, NOT). It is responsible for mathematical calculations and decision-making in a computer.
The Control Unit (CU) is a part of the CPU that directs the flow of instructions and data within the computer. It interprets instructions from the memory and controls the execution of operations by coordinating with other components like ALU and memory.
A compiler translates the entire program from high-level language to machine code at once, creating an executable file. An interpreter translates and executes the program line by line, without generating an executable file.
A linker combines multiple object files into a single executable file. A dynamic linker links shared libraries at runtime. A static linker links all required libraries into the executable at compile time.
A variable is a named memory location used to store data values that can be changed during program execution.
Data types specify the type of data a variable can store, such as integers, floating-point numbers, characters, etc. Example: int, float, char.
Operators are symbols used to perform operations on variables, like +, -, *, /. Expressions are combinations of variables, constants, and operators that produce a value, e.g., a + b.
Functions are reusable blocks of code designed to perform a specific task. They help in breaking a program into smaller, manageable parts. Example: main(), printf().
A compound statement is a group of multiple statements enclosed within { } that are executed together.
Conditional Statement
Uses if-else to execute specific code based on conditions.
Supports a range of conditions.
More flexible but can be complex.
Switch Statement
Uses multiple case labels to handle different values of a variable.
Works with discrete values like integers or characters.
Easier to read for multi-value conditions.
A while loop repeatedly executes a block of code as long as the condition is true. The condition is checked before execution.
A do-while loop executes the block of code at least once, then checks the condition to decide further execution.
Arrays store multiple values of the same data type under one name. Functions perform specific tasks and can be called multiple times with different arguments.
A 1-Dimensional array is a linear list of elements stored in a single row. Example: int arr[5];
A 2-Dimensional array stores data in rows and columns like a table. Example: int arr[3][3];
A function argument is a value passed to a function when it is called. It allows the function to use external data for its operation.
A local variable is declared inside a function and is accessible only within that function.
A global variable is declared outside all functions and can be accessed by any function in the program.
History: C was developed by Dennis Ritchie in 1972 at Bell Labs for the UNIX operating system.
Features:
An operator is a symbol used to perform operations on variables and values. Types:
Operator Precedence: Determines the order in which operators are evaluated. Example: * has higher precedence than +.
Associativity: Defines the direction of execution for operators with the same precedence. Example: Left-to-right for + and *.
Conditional control statements like if, if-else, and switch allow decision-making in a program by executing code based on conditions.
Break: Exits a loop or switch statement immediately.
Continue: Skips the current iteration of a loop and moves to the next iteration.
Loop control structures like for, while, and do-while are used to execute a block of code repeatedly.
for(int i = 0; i < 5; i++) {
printf("%d ", i);
}
Output: 0 1 2 3 4
An array is a collection of elements of the same data type stored in contiguous memory locations.
A 2D array is a collection of elements arranged in rows and columns.
Memory Representation:
| Row\Col | Col 0 | Col 1 | Col 2 |
|---|---|---|---|
| Row 0 | arr[0][0] | arr[0][1] | arr[0][2] |
| Row 1 | arr[1][0] | arr[1][1] | arr[1][2] |
Memory is stored row by row in a linear fashion.
A pointer is a variable that stores the memory address of another variable. Example: int *ptr;
DMA Functions:
A structure groups related variables of different data types under one name.
A union is similar but shares memory for all its members, storing only one value at a time.
Difference Between Structure and Union:
| Structure | Union |
|---|---|
| Allocates separate memory for each member. | All members share the same memory location. |
| Stores multiple values simultaneously. | Stores only one value at a time. |
A macro is a preprocessor directive used to define constants or code snippets.
It substitutes the macro name with its definition during preprocessing.
Example:
#define PI 3.14
#define SQUARE(x) (x * x)
In the program, PI will be replaced with 3.14, and SQUARE(5) will be replaced with (5 * 5).
An operator is a symbol used to perform operations on variables and values.
Types of Operators:
Left Shift (<<) and Right Shift (>>):
5 << 1 results in 10.5 >> 1 results in 2.#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
C is called a mid-level programming language because:
Basic data types in C:
int a = 10;float b = 3.14;char c = 'A';Preprocessor directives are instructions processed before compilation, like #include, #define, and #ifdef.
Macros are defined using #define and replaced before compilation.
Example:
#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
Recursion is a process where a function calls itself to solve a smaller problem until a base condition is met.
Example:
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
// Base condition
return n * factorial(n - 1);
}
int main() {
printf("Factorial of 5: %d\n",
factorial(5));
return 0;
}
Local Variable
Declared inside a function. Accessible only within that function.
Global Variable
Declared outside all functions. Accessible by all functions.
Type Casting (Explicit): Done manually using (type) syntax. Example: (float)a / b;
Type Conversion (Implicit): Done automatically by the compiler. Example: int x = 3.5; → x becomes 3.
A function is a reusable block of code to perform specific tasks.
Types:
printf().Application: Functions help in modular programming, making code reusable and easier to debug.
Difference Between Macro and Function:
| Macro | Function |
|---|---|
| Preprocessed, no runtime overhead. | Executed at runtime. |
| No type-checking. | Supports type-checking. |
Example: #define SQUARE(x) x*x |
Example: int square(int x) { return x*x; } |
Storage Classes in C:
Call by Value: A copy of the actual parameter is passed to the function. Changes made in the function do not affect the original value.
Call by Reference: The memory address of the parameter is passed to the function. Changes made in the function affect the original value.
Example:
#include <stdio.h>
void callByValue(int x) {
x = 10; // Changes local copy
}
void callByReference(int *y) {
*y = 10; // Changes original value
}
int main() {
int a = 5, b = 5;
callByValue(a);
callByReference(&b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output: a = 5, b = 10
#include <stdio.h>
int main() {
int n, i, flag = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) flag = 0; // Not prime
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (flag) printf("%d is a prime
number.\n", n);
else printf("%d is not a prime
number.\n", n);
return 0;
}
Static Memory Allocation: Memory is allocated at compile time, and the size cannot be changed during runtime. Example: int arr[10];.
Dynamic Memory Allocation: Memory is allocated at runtime using functions like malloc(), calloc(), and can be resized using realloc().
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
scanf("%s", str);
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0)
printf("The string is a palindrome.\n");
else printf("The string is not a
palindrome.\n");
return 0;
}
printf(): Used to display output. Example: printf("Hello");
scanf(): Used to take input. Example: scanf("%d", &var);
Format Specifiers:
A C program is a set of instructions that tell the computer how to perform a task. C is a high-level programming language used for system and application development.
C programs are written in plain text and then compiled into machine code. The process involves writing the program, compiling it, and executing it.
In C, variables are used to store data. A variable is a named memory location used to store values that can be modified during program execution.