CSE Theoretical QS D.note by 2201121
CSE Theoretical QS D.note by 2201121
Created by:
Mahathir Mohammad Siam
2201121
DECISION MAKING IN C
1.Function Return Type: When used as the return type of a function, void
indicates that the function does not return any value. It is commonly used for
functions that perform a task without producing a result.
2. Pointer Declaration: It is often used in situations where the type of data being
pointed to is not known at compile time.
3. Function Parameters: In a function declaration, using void as a parameter
indicates that the function takes no parameters.
FORMAL AND ACTUAL PARAMETER/ARGUMENT
1. Actual Parameters/Arguments:
• Actual parameters are the values or expressions supplied in a
function call.
• These are the arguments passed to a function when it is called.
• They are placed within the parentheses of the function call and
correspond to the formal parameters defined in the function
declaration or definition.
2. Formal Parameters/Parameter:
• Formal parameters are the variables declared in the function
declaration or definition.
• They act as placeholders for the values that will be passed to the
function when it is called.
• Formal parameters are used within the function to process the values
received from the actual parameters.
MALLOC:
The malloc function allocates a block of memory of the specified size in bytes. It
does not initialize the allocated memory. The content of the memory block
allocated by malloc is indeterminate (contains garbage values) until explicitly set
by the program. It takes a single argument specifying the number of bytes to
allocate.
CALLOC:
The calloc function also allocates a block of memory, but it initializes the memory
to zero. It takes two arguments: the number of elements to allocate and the size
of each element in bytes. It initializes each element to zero. Because calloc
initializes the memory, it may be slightly slower than malloc for large memory
allocations, especially if the memory needs to be zero-initialized.
In summary, the key difference is that malloc allocates uninitialized memory, while
calloc allocates and initializes memory to zero. This difference may influence the
choice between the two functions depending on the specific requirements of your
program.
RECURSIVE VS NORMAL
FILE
• The operations using the C program are done on a prompt/terminal which
is not stored anywhere.
• The output is deleted when the program is closed.
• However, the output may require to be stored. That’s why File is needed in
C.
NECESSITY
Reusability: The data stored in the file can be accessed, updated, and deleted
anywhere and anytime providing high reusability.
Portability: Without losing any data, files can be transferred to another computer
system
Draw a block diagram of a computer and explain the function of each block
Explanation of each block:
1. Input Devices: These devices allow users to provide data and instructions to
the computer. Examples include
keyboards, mice, touchpads, scanners, webcams, microphones, etc. They
convert user input into digital signals that the computer can understand.
2. CPU (Central Processing Unit): This is considered the "brain" of the
computer responsible for processing data and instructions. It consists -of
three main components:
o Control Unit: Decodes instructions, manages the flow of data, and
controls other components.
o Arithmetic Logic Unit (ALU): Performs arithmetic and logical
operations on data.
o Registers: Temporary storage locations for data and instructions being
processed.
3. Memory: Stores data and instructions that the CPU needs to access
quickly. It has two main types:
o RAM (Random Access Memory): Volatile memory used for temporary
storage. Data is lost when the computer is turned off.
o ROM (Read-Only Memory): Non-volatile memory used for storing
permanent instructions and data (e.g., BIOS).
4. Output Devices: These devices display or output processed data and
information to the user. Examples include
monitors, printers, speakers, projectors, etc. They convert digital signals
from the computer into human-readable formats.
The storage unit of a computer refers to the components where data and
instructions are stored for processing by the computer's CPU (Central Processing
Unit). There are two main types of storage in a computer system: primary storage
and secondary storage.
1. Primary Storage:
• Definition: Primary storage, also known as main memory or RAM
(Random Access Memory)
2. Secondary Storage:
• Definition: Secondary storage refers to non-volatile memory devices used
for long-term storage of data, programs, and other files.
1. Volatility:
• Primary storage is volatile, meaning data is lost when the power is
turned off.
• Secondary storage is non-volatile, meaning data remains intact even
when the power is turned off.
2. Speed:
• Primary storage provides faster access times compared to secondary
storage, allowing for quick retrieval of data.
• Secondary storage typically has slower access times compared to
primary storage due to the nature of the storage medium.
3. Accessibility:
• Primary storage is directly accessible by the CPU and is used for
actively running programs and processing data.
• Secondary storage is used for long-term storage and is accessed as
needed to load data into primary storage for processing.
4. Capacity:
• Primary storage generally has limited capacity due to its high-speed,
expensive nature.
• Secondary storage devices typically offer larger storage capacities
compared to primary storage, making them suitable for storing large
amounts of data and files.
A++
• This is a post-increment operator. It increments the value of a by 1, but it
returns the original value of a before the increment.
• a++ is generally preferred when we want to increment the value of a and
also use its original value in an expression.
A=A+1
• This is an assignment statement. It explicitly assigns the value of a + 1 back
to a.
• a = a + 1 is more explicit and might be preferred when clarity or readability
is more important than brevity
In general, a++ is more concise and idiomatic, especially when you only need to
increment the value of a without using its original value in an expression.
However, if you need to use the original value of a in an expression or if you
prioritize clarity and readability, a = a + 1 might be preferred. Ultimately, the
choice between them depends on the specific context and coding style
preferences.
TOKENS
In C programming, a token is the smallest unit of a program that is meaningful to
the compiler. When the compiler processes a C program, it breaks it down into
individual tokens for further analysis and interpretation. Tokens are classified into
several categories based on their types and roles within the program. Here are the
main types of tokens in C programming:
1. Keywords: Keywords are reserved words that have special meanings in the
C language. They cannot be used as identifiers (variable names or function
names) and are used to define the syntax and structure of C programs.
Examples include int, char, if, else, for, while, return, etc.
2. Identifiers: Identifiers are names given to variables, functions, arrays, and
other user-defined entities in a C program. An identifier must begin with a
letter (either uppercase or lowercase) or an underscore (_) and can be
followed by letters, digits, or underscores. Identifiers are case-sensitive.
Examples include count, totalAmount, MAX_SIZE, etc.
3. Constants: Constants are fixed values that do not change during the
execution of a program. There are several types of constants in C
programming:
• Integer Constants: These are whole numbers without decimal points,
such as 10, -5, 0xFF (hexadecimal), etc.
• Floating-point Constants: These are real numbers with decimal
points, such as 3.14, -0.5, 1.0e6 (scientific notation), etc.
• Character Constants: These are single characters enclosed in single
quotes, such as 'a', '5', '\n' (newline), etc.
• String Constants: These are sequences of characters enclosed in
double quotes, such as "Hello", "C programming", "123", etc.
4. Operators: Operators are symbols used to perform operations on operands.
They include arithmetic operators (+, -, *, /, %), relational operators (==, !=,
<, >, <=, >=), logical operators (&&, ||, !), assignment operators (=, +=, -=,
*=, /=, %=), bitwise operators (&, |, ^, ~, <<, >>), etc.
5. Punctuators: Punctuators are special symbols used to structure and
punctuate C programs. They include braces {}, parentheses (), brackets [],
commas ,, semicolons ;, periods ., etc.
6. Preprocessor Directives: Preprocessor directives are special instructions
that begin with a hash (#) symbol and are processed by the preprocessor
before compilation. They are used to include header files (#include), define
macros (#define), perform conditional compilation (#ifdef, #ifndef, #if,
#else, #endif), etc.
FUNCTION PROTOTYPE
Function prototypes in C programming serve as declarations that describe the
interface of a function before its actual implementation. They provide the
compiler with essential information about the function, including its name, return
type, and parameters. Function prototypes typically appear at the beginning of a C
source file or in a header file.
• Type checking: The compiler uses function prototypes to ensure that
function calls are made correctly, matching the expected parameter types
and return type. This helps prevent errors and makes code more accurate.
• Modular programming: Function prototypes allow you to define functions
in separate files, promoting modularity and reusability of code.
• Code organization: Prototypes act as documentation, clarifying what each
function does and how to use it. This improves code readability and
maintainability.
SCOPE
In C programming, scope refers to the region of a program where a particular
identifier (such as a variable or function) is visible and accessible.
Local Variables: Variables that are declared inside a function are called local
variables to that function. Don’t have effect outside of that function.
• Scope: Limited to the block in which they are declared
• Lifetime: Exists only during the execution of the block in which it is
declared. It's created when the block is entered and destroyed when the
block is exited.
Global Variables: Global variables are defined outside of a function, usually on
top of the program. Global variables can be accessed from any function after its
declaration.
Static Variables: Preserve their values even after they are out of their scope.
A static variable remains in memory while the program is running.
FUNCTION CALL
Call by Value:
• In call by value, a copy of the argument's value is passed to the function.
• Any changes made to the parameter inside the function are local to that
function and do not affect the original value of the argument outside the
function.
• Call by value is the default behavior in C for passing arguments to functions.
Call by Reference :
• In call by reference (simulated in C), a pointer to the memory location of the
argument is passed to the function.
• This allows the function to modify the original value of the argument
directly.
• It is needed to declare the function parameters as pointer types.
Function Definition:
• A function definition includes the actual implementation of the function. It
provides the body of the function, containing the statements that define
what the function does.
• It includes the function's return type, name, parameters, and the code
block enclosed within curly braces {}.
• Function definitions can appear either before or after their use in the
program.
• A function definition does not end with a semicolon.
One of the major issues with gets() is that it doesn't check the size of the buffer
into which it is reading. This can lead to buffer overflow vulnerabilities if the input
exceeds the size of the buffer.
• gets():
o Reads input until it encounters a newline character (\n) or end-of-file
(EOF).
o Includes whitespace in the input string.
o Only reads strings, not other data types.
• scanf():
o Reads input based on format specifiers (e.g., %d for integers, %s for
strings).
o Stops reading at whitespace, newline, or specific format specifier
limitations.
o Can read various data types (numbers, characters, strings).
An infinite loop is a loop in a computer program that continues indefinitely
because its terminating condition is never met. In other words, the loop keeps
executing its body repeatedly without ever stopping, leading to the program
becoming unresponsive or consuming excessive system resources.
Infinite loops are not inherently harmful for programming, as they can be
intentionally used in certain scenarios where continuous execution is desired,
such as:
1. Server applications: Servers often need to run continuously, waiting for
incoming requests. A server may use an infinite loop to continuously listen
for and process incoming requests.
2. Embedded systems
However, infinite loops can become problematic if they are unintentional or if
they consume excessive system resources without accomplishing anything useful.
They can lead to:
• Program freeze: If an infinite loop occurs unintentionally, it can cause the
program to become unresponsive, requiring the user to forcefully terminate
it.
• Resource consumption: Infinite loops can consume CPU resources
continuously, leading to high CPU usage and potentially slowing down the
entire system.
Structure:
• Each member of a structure has a separate memory location. This means
wecan store and access different values for each member independently.
• The size of a structure is equal to the sum of the sizes of its individual
members.
• Structures are well-suited for representing collections of related
data where each piece of data needs its own space.
Union:
• All members of a union share the same memory location. This means only
one member can hold a value at any given time.
• The size of a union is equal to the size of its largest member.
• Unions are primarily used for memory efficiency when we know we will
only need one of the member values at a time.
getch():
• This function is commonly used to read a single character from the console
without echoing it back to the screen.
getche():
• Similar to getch(), getche() reads a single character from the console, but it
echoes the character back to the screen.
Unlike getch() and getche(), getchar() waits for the user to press Enter before
reading the character.
fabs():
• It can be used to get the magnitude of a floating-point value irrespective of
its sign.
Constructors:
• Purpose: Initialize objects when created (set values, allocate memory).
• Key points: Same name as class, no return type, can be overloaded.
Destructors:
• Purpose: Clean up resources when objects are destroyed (release memory,
close files).
• Key points: Same name as class with tilde (~), no return type or arguments