C Important Questions
C Important Questions
1)What is the difference between low level and high level language.
1)
2)
STRUCTURE UNION
a)To define structure struct keyword is used a)To define union Union Keyword is used.
b)At any me,any member can be accessed by b)At any me only one member can be accessed
the structure. by the union.
c)Member of structure do not share memory and c)Member of union can share memory and there
need separate memory for all its member. is no need to allocate separate memory to all its
members.
d)All member of the structure can be ini alized. d)Only the first member of the union can be
ini alized.
e)struct Structurename{ e)union Unionname{
statement 1; statement 1;
}structvariablename; }unionvariablename;
3)What is flowchart explain all type of symbols used in flowchart.
3) Flowchart is a graphical representation of an algorithm. Programmers often use it as a program-planning tool to solve a
problem. It makes use of symbols which are connected among them to indicate the flow of information and processing.
4) a) Compiler:
1)A compiler is a program that translates the source code wri en in C (or any other programming language)
into machine code or an intermediate representa on.
2) It performs lexical analysis, syntax analysis, and code op miza on to generate an executable program or
object code.
3)The compiler checks for syntax errors and type checking during this process and produces object files that
contain the compiled code but are not yet executable.
b)Linker:
1)The linker is a separate tool that takes one or more object files (produced by the compiler) and combines
them to create a single executable program or library.
2) It resolves references to func ons and variables between different object files, ensuring that the code can
be executed as a complete program.
-3)The linker also performs addi onal tasks like crea ng symbol tables, managing memory addresses, and
op mizing the final binary.
c) Loader:
1)The loader is responsible for loading the executable program into memory when it is run by the user. It
prepares the program for execu on.
2)It allocates memory for the program, resolves references to dynamic libraries, and sets up the execu on
environment.
3)The loader loads the program from its stored format (usually an executable file on disk) into memory so
that it can be run by the opera ng system.
5)Explain format specifiers in c language.
5) In C programming, a format specifier is a special sequence used within forma ng func ons, such as `prin `
and `scanf`, to specify how data should be input or output. It tells these func ons how to interpret and display
the data. Format specifiers are preceded by the '%' character and are followed by a le er or le ers that
indicate the data type of the argument. Here are some common format specifiers:
5. `%x`: Used for forma ng and prin ng integers in hexadecimal (base 16) format.
6. `%o`: Used for forma ng and prin ng integers in octal (base 8) format.
6)What is the difference between Entry and Exit control loops.Explain all the there basic loops with syntax.
6)
1. Entry-Controlled Loop:
- An "entry-controlled" loop is another term for a loop that checks the loop condi on before execu ng the
loop body. The most common example of an entry-controlled loop is the `while` loop.
- It evaluates the loop condi on first, and if the condi on is true, it enters the loop body. If the condi on is
false from the beginning, the loop body is never executed.
// Loop body
2. Exit-Controlled Loop:
- An "exit-controlled" loop is another way of describing a loop that checks the loop condi on a er execu ng
the loop body. The most common example of an exit-controlled loop is the `do-while` loop.
- In this case, the loop body is executed at least once, and then the condi on is checked to determine
whether to con nue or exit the loop.
do {
// Loop body
1. While Loop:
- A `while` loop repeatedly executes a block of code as long as a given condi on is true.
- It checks the condi on before entering the loop, so it's possible that the loop may not execute at all if the
condi on is false from the beginning.
2. For Loop:
- A `for` loop provides a way to specify ini aliza on, condi on, and itera on expressions in a single line.
- It's typically used when you know in advance how many mes you want the loop to run.
3. Do-While Loop:
- A `do-while` loop is similar to a `while` loop, but it guarantees that the code block is executed at least once
because it checks the condi on a er the code block.
do {
These control structures allow you to control the flow of your program by itera ng over a block of code
mul ple mes, as long as a certain condi on is met.
7)
1. if Statement:
- The `if` statement is used for condi onal execu on. It allows you to execute a block of code if a specified
condi on is true.
if (condi on) {
2. Else Statement:
- The `else` statement is used in combina on with the `if` statement to specify an alterna ve block of code to
execute if the condi on in the `if` statement is false.
if (condi on) {
} else {
3. Nested if-else:
- You can nest `if` and `else` statements inside one another to create more complex condi onal logic.
if (condi on1) {
// Code to execute when condi on1 is true
} else {
4. Switch Statement:
- The `switch` statement allows you to select one of many code blocks to be executed based on the value of
an expression.
- It provides a way to simplify mul ple `if-else` statements when you have a limited number of discrete cases.
switch (expression) {
case value1:
break;
case value2:
break;
default:
5. Break Statement:
- The `break` statement is used inside loops and the `switch` statement to exit the loop or switch early.
- It is o en used to terminate the loop when a certain condi on is met or to exit a `switch` statement once a
case is matched.
- The `con nue` statement is used within loops to skip the current itera on and move to the next itera on.
- It allows you to avoid execu ng the remaining code in the current itera on when a specific condi on is met.
8)Call by Value:
In call by value, when you pass an argument to a func on, a copy of the argument's value is made, and this
copy is used within the func on. Any changes made to the parameter within the func on do not affect the
original variable that was passed as an argument. Here's a step-by-step explana on:
1. The value of the argument is copied to a new memory loca on, which is the func on's parameter.
Example:
void modifyValue(int x) {
x = x * 2;
int main() {
int num = 5;
In this case, the `num` variable remains 5 because the func on `modifyValue` operates on a copy of `num`, not
the original `num`.
In call by reference, you pass a reference to the memory loca on of the argument to the func on. Instead of
copying the value, the func on directly accesses and modifies the original data. Here's how it works:
1. You pass a pointer to the argument, which contains the memory address where the data is stored.
2. The func on uses this pointer to access and modify the data at the original memory loca on.
3. Any changes made within the func on directly affect the original variable.
Example:
void modifyValueByReference(int* x) {
int main() {
int num = 5;
In this case, the `num` variable is modified because the func on `modifyValueByReference` works directly on
the original `num` using a pointer.
b)const
9)1. Sta c:
- When used within a func on, a `sta c` variable retains its value between func on calls. It's local to the
func on but persists across mul ple calls.
- When used with a global variable, `sta c` limits the variable's scope to the file where it's defined, making it
accessible only within that file.
- When used with func ons, a `sta c` func on has internal linkage, meaning it's only visible within the same
transla on unit (file).
2. Const:
- The `const` keyword is used to declare constants in C. It indicates that a variable's value cannot be modified
once it is assigned.
- Constants declared with `const` are o en used for readability, op miza on, and to prevent uninten onal
modifica ons of values.
- Preprocessor direc ves are commands that are processed by the C preprocessor before actual compila on.
They start with a `#` symbol.
- Common preprocessor direc ves include `#include`, `#define`, and condi onal direc ves like `#ifdef`,
`#ifndef`, `#else`, and `#endif`.
- Preprocessor direc ves are used for tasks like including header files, defining macros, condi onal
compila on, and more.
#include <stdio.h>
#define PI 3.14159265359.
#ifdef DEBUG
10)
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
} else {
return 0;
11) Arrays are a fundamental data structure in C and many other programming languages. They are used to
store collec ons of data elements of the same data type. In C, there are one-dimensional (1-D) arrays, two-
dimensional (2-D) arrays, and mul dimensional arrays, each with its own characteris cs:
- A 1-D array is a collec on of elements of the same data type arranged in a linear sequence.
- The index of the first element is typically 0, and it increases sequen ally.
- Example:
- A 2-D array is an array of arrays, essen ally crea ng a matrix or grid of elements.
- You access elements in a 2-D array using two indices, one for the row and one for the column.
Example:
- A mul dimensional array refers to arrays with more than two dimensions.
- These can be thought of as an extension of 2-D arrays, with mul ple layers.
- In prac ce, 2-D arrays are the most common, but you can have 3-D, 4-D, or higher-dimensional arrays.
- Accessing elements in mul dimensional arrays requires specifying the indices for each dimension.
Example:
a)Iden fier
b)keywords
3)Tokens
12)
a) Iden fier:
- An iden fier is a name given to a variable, func on, constant, array, or any other user-defined item in a C
program.
- Iden fiers are used to uniquely iden fy these program elements, allowing you to refer to them in your code.
- Case-sensi ve: "myVariable" and "myvariable" are treated as different iden fiers.
- Should not use C keywords (explained in the next point) as iden fiers.
b) Keywords:
- Keywords, also known as reserved words, are a set of predefined words in the C programming language.
- These words have specific meanings and are reserved for specific purposes within the language.
- You cannot use keywords as iden fiers for variables, func ons, or other user-defined en es.
- Examples of C keywords include `int`, `for`, `while`, `if`, `else`, `return`, `break`, and many others.
c) Tokens:
- Tokens are the smallest units in a C program, which include iden fiers, keywords, operators, constants, and
symbols.
- Tokens are separated by white spaces, such as spaces and newlines, in the source code.
- C compilers use tokens to analyze and understand the structure of your code during the compila on
process.
- Examples of C tokens:
13)
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
// Call the swap func on with the addresses of num1 and num2
swap(&num1, &num2);
return 0;
14) Operators in C are symbols that perform opera ons on operands, which can be values, variables, or
expressions. They enable you to manipulate data and perform a wide range of tasks. There are various types of
operators in C, and here are the major categories:
1. Arithme c Operators:
- Common arithme c operators include `+` (addi on), `-` (subtrac on), `*` (mul plica on), `/` (division), and
`%` (modulo, or remainder).
- Rela onal operators are used to compare two values and determine the rela onship between them. They
return a Boolean result (either true or false).
- Common rela onal operators include `==` (equal to), `!=` (not equal to), `<` (less than), `>` (greater than),
`<=` (less than or equal to), and `>=` (greater than or equal to).
3. Logical Operators:
- Logical operators are used to perform logical opera ons on Boolean values (true or false).
- Common logical operators include `&&` (logical AND), `||` (logical OR), and `!` (logical NOT).
4. Assignment Operators:
- The most basic assignment operator is `=` (assignment), but there are also compound assignment operators
like `+=`, `-=`, `*=`, and `/=`, which perform an opera on and assignment in one step.
5. Increment and Decrement Operators:
- Increment and decrement operators are used to increase or decrease the value of a variable by 1.
- The `++` operator increments, and the `--` operator decrements a variable.
6. Bitwise Operators:
- Bitwise operators perform opera ons at the bit-level. They are o en used in low-level programming.
- Common bitwise operators include `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `~` (bitwise NOT),
`<<` (le shi ), and `>>` (right shi ).
- The condi onal operator `? :` is used to return one of two values based on a condi on. It's a shorthand for
an `if-else` statement.
15) The `=` operator and the `==` operator serve completely different purposes in C and many programming
languages:
- When you write `variable = value;`, you are assigning the value on the right side to the variable on the le
side.
- When you write `expression1 == expression2`, you are tes ng if `expression1` is equal to `expression2`.
- It returns a Boolean value, typically `true` if the expressions are equal and `false` otherwise.
- Example: `if (x == 5)` checks if the value of the variable `x` is equal to 5.
int main() {
int number;
unsigned long long factorial = 1;
scanf("%d", &number);
if (number < 0) {
else {
factorial *= i;
return 0;
17)Write a program using array to count the number of even and odd element in the array.
int main() {
scanf("%d", &size);
int numbers[size];
scanf("%d", &numbers[i]);
if (numbers[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
return 0;