Subject: Computer Science
Paper Code: CSCDSM-151T
Semester: 2nd Semester (FYUG)
Exam Name: Even Semester Exam, 2024
A variable is a named storage location in memory used to hold a data value that can be modified during program execution
. To declare a variable, you specify the data type followed by the variable name:int count;.
C provides several categories of data types
:int, char, float, double, voidstruct, union, enumA token is the smallest individual unit in a C program that is meaningful to the compiler
. Examples include keywords (if), identifiers (total), constants (10), and operators (+).
A function prototype is a declaration that tells the compiler about a function's name, return type, and parameters before its actual implementation
.Example: int add(int a, int b);
Actual arguments are values passed to the function when it is called
. Formal arguments are variables declared in the function definition that receive those values.
void sum(int x, int y); // x, y are formal arguments
int main() {
sum(10, 20); // 10, 20 are actual arguments
}
A pointer is a variable that stores the memory address of another variable.
It is declared using the asterisk (*) operator: int *p;.
An array is a collection of homogeneous data elements stored in contiguous memory locations
. One-dimensional arrays are initialized as:int marks[3] = {80, 85, 90};.
| Feature | Array | Structure |
|---|---|---|
| Data Type | Homogeneous (same type) | Heterogeneous (different types) |
| Access | Using index numbers | Using dot (.) operator |
A string is an array of characters terminated by a null character (\0)
\0) indicates the end of the character sequence, allowing functions like printf or strlen to know where the string stops in memory.
while(i < 5) { ... i++; }do { ... i++; } while(i < 5);for(i=0; i < 5; i++) { ... }Key Difference: A while loop may never execute if the condition is initially false, whereas a do-while loop is guaranteed to execute at least once
Structure of C Program: Documentation section, Link section (headers), Definition section, Global declaration, Main function, and Subprograms
.
// Area of Circle
#include <stdio.h>
int main() {
float r, area;
printf("Enter radius: ");
scanf("%f", &r);
area = 3.14 * r * r;
printf("Area = %f", area);
return 0;
}
Storage classes define the scope, lifetime, and initial value of a variable
.Recursion is a function calling itself, while iteration uses loops
. Recursion is often more elegant but uses more stack memory.
// Recursive Factorial
int fact(int n) {
if (n == 0) return 1;
return n * fact(n-1);
}
Macro vs Function: Macros are pre-processed (text replacement) and faster as there is no function call overhead, but they do not check for data types like functions do
.
// File Copy Snippet
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);