Core C Language Fundamentals and Practical Programs
C Program to Find the Largest of Three Numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("%d is the largest.\n", a);
} else if (b >= a && b >= c) {
printf("%d is the largest.\n", b);
} else {
printf("%d is the largest.\n", c);
}
return 0;
}C Program to Check Positive, Negative, or Zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is Positive.\n", num);
} else if (num < 0) {
printf("%d is Negative.\n", num);
} else {
printf("The number is Zero.\n");
}
return 0;
}Difference Between Structure and Union
A structure and a union are both user-defined data types in C used to group different data types.
- Memory Allocation: Structure allocates memory for every member. Union allocates memory equal only to its largest member, sharing the space among all variables.
- Accessing Members: In structures, all members can be accessed simultaneously. In unions, only one member holds a valid value at any given time.
- Keyword & Syntax: Defined using
structandunionkeywords. - Size Calculation: Size of struct >= sum of sizes of all members. Size of union = size of its largest member.
- Value Alteration: In struct, modifying one member does not affect others. In union, modifying one member overwrites the shared memory.
Relational vs. Logical Operators in C
Operators are symbols that perform operations on variables. Relational operators compare two values, while Logical operators combine multiple conditions.
- Relational Operators:
<,>,<=,>=,==,!=. - Logical Operators:
&&(AND),||(OR),!(NOT).
Conditional Statements and Even/Odd Program
Conditional statements allow a program to execute specific blocks of code based on boolean conditions.
ifStatementif-elseStatementnested if-elseStatementelse-ifLadderswitch-caseStatement
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is an EVEN number.\n", num);
} else {
printf("%d is an ODD number.\n", num);
}
return 0;
}Control Flow of a Switch Statement
The switch statement is a multi-way decision-making structure that branches execution based on constant integer or character values.
- The expression is evaluated once.
- It is compared against
caselabels. - The
breakstatement prevents fall-through. - The
defaultblock executes if no match is found.
Arrays in C: 1D and 2D
An Array is a collection of elements of the same data type stored in contiguous memory.
- 1D Array: Represents a single row of linear data (e.g.,
int arr[5];). - 2D Array: Represents data in a tabular layout (e.g.,
int matrix[2][2];).
Functions and Call by Reference
A Function is a reusable block of code. In Call by Reference, pointers are passed to modify original memory values.
Origin and Features of C Language
- Origin: Created by Dennis Ritchie in 1972 at Bell Labs.
- Applications: Operating systems, embedded systems, compilers, and databases.
- Key Features: Procedural, fast, portable, low-level memory access, and extensive standard libraries.
