Key C Programming Concepts Explained
Key C Programming Concepts Explained
1. Define the process – Write down all steps, decisions, Useful for problem-solving and troubleshooting.
inputs, and outputs.
3. Swimlane Flowchart
2. Work with the team – Use templates and confirm all
Divides the chart into lanes (for departments or people).
details with team members.
Shows how different roles interact in a process.
3. Arrange steps – Connect actions and decisions in correct
order. 4. Organisational Flowchart
Displays a company’s structure and hierarchy. If it is, print "The number is positive."
Useful for resource planning and communication. If it is not, check if the number is less than 0.
Flowchart to Add two numbers If it is, print "The number is negative."
This flowchart outlines the essential steps required to add If it is not (meaning it is 0), print "The number is zero."
two numbers.
Stop.
Pseudo code
BEGIN
READ number
IF number > 0 THEN
PRINT "The number is positive"
ELSE IF number < 0 THEN
PRINT "The number is negative"
ELSE
PRINT "The number is zero"
12. Write an Algorithm, Pseudo code, flowchart for
finding given number is Positive or Negative. END IF
Algorithm END
Start.
Read a number from the user.
Check if the number is greater than 0.
Flowchart Example: int age = 30;
char:
Used to store a single character. It occupies 1 byte of
memory and stores the ASCII value of the character.
Example: char initial = 'G';
float:
Used to store single-precision floating-point numbers
(decimal numbers). It typically occupies 4 bytes.
Example: float price = 19.99;
double:
Used to store double-precision floating-point numbers. It
offers more precision than float and typically occupies 8
bytes.
Example: double pi = 3.1415926535;
These data types can be modified using type modifiers like
13. What are Primitive Data types and explain in detail.
signed, unsigned, short, and long to change their range and
Primitive data types are the basic building blocks for all size.
other data types in C. They are pre-defined by the language
14. Explain about Variables and Keywords.
and are used to store simple values.
Variables
int:
A variable is a named storage location in memory used to
Used to store whole numbers (integers). It typically
store data. It acts as a container for data that can be
occupies 2 or 4 bytes and can store both positive and
changed during program execution. Each variable has a
negative values.
name, a data type, and a scope.
● Declaration: + (Addition), - (Subtraction), * (Multiplication), / (Division), %
● int number; (This reserves memory for an integer (Modulo).
named number).
● Initialization:
● int number = 100; (This declares and assigns an
initial value). Operato
● Assignment: r Description Example Output
number = 50; (This changes the value of the variable). int a=10,b=3;
=+ Addition printf("%d", a+b); 13
Keywords
- Subtraction printf("%d", a-b); 7
Keywords are reserved words in the C language that have a
predefined meaning and cannot be used as variable names, * Multiplication printf("%d", a*b); 30
function names, or any other identifiers. They are
fundamental to the syntax of the language. There are 32 / Division printf("%d", a/b); 3
keywords in C.
Modulo printf("%d", a
Examples: % (Remainder) %b); 1
printf("%d", a!
!= Not equal to =b); 1 Assignment Operators:
> Greater than printf("%d", a>b); 0 Assign a value to a variable.
< Less than printf("%d", a<b); 1 = (Simple assignment), += (Add and assign), -= (Subtract
and assign), etc.
Greater than or printf("%d",
>= equal to a>=b); 0
int x=10;
Logical Operators: = Assigns value printf("%d", x); 10
Combine multiple conditions. Add and x+=5;
+= assign printf("%d", x); 15
&& (Logical AND), || (Logical OR), ! (Logical NOT).
Subtract and x-=3; printf("%d",
Operato -= assign x); 7
r Description Example Output
Multiply and x*=2; printf("%d",
int a=5,b=8; *= assign x); 20
printf("%d",
&& Logical AND (a<10 && b>5)); 1 Divide and x/=2; printf("%d",
/= assign x); 5
int a=5,b=8;
printf("%d", Modulus and x%=3;
|| Logical OR (a<10 || b>5)); 1 %= assign printf("%d", x); 1
& (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise
NOT), << (Left shift), >> (Right shift).
Increment/Decrement Operators:
Increase or decrease the value of a variable by 1.
++ (Increment), -- (Decrement). Can be used in prefix or Operato
postfix form. r Description Example Output
int a=5,b=3;
Operato printf("%d", a &
r Description Example Output & Bitwise AND b); 1
Pre-increment int a=5,b=3;
(Increase int a=5; printf("%d", a | `printf("%
=++a before use) printf("%d", ++a); 6 | `Bitwise OR b); d", a)
Post-increment printf("%d", a ^
(Increase after int a=5; 5 (then ^ Bitwise XOR b); 6
a++ use) printf("%d", a++); a=6)
~ Bitwise NOT printf("%d", ~a); -6
Pre-decrement
(Decrease int a=5; printf("%d", a <<
--a before use) printf("%d", --a); 4 << Left Shift 1); 10
Post- printf("%d", a >>
decrement >> Right Shift 1); 2
(Decrease int a=5; 5 (then
a-- after use) printf("%d", a--); a=4)
16. Explain in detail about various Built in Functions in
C.
Bitwise Operators:
Built-in functions (or standard library functions) are pre-
Perform operations on individual bits of an integer. defined functions provided by the C compiler, stored in
header files. They perform common tasks, simplifying
programming.
17. What are the different unconditional/Jump
stdio.h (Standard Input/Output): Statements and explain them with examples?
printf(): Prints formatted output to the console. Unconditional jump statements transfer the program control
from one part of the code to another without checking any
scanf(): Reads formatted input from the console. condition.
getchar(), putchar(): For single character I/O. break:
stdlib.h (Standard Library): Terminates the innermost loop ( for, while, do-while) or
switch statement immediately.
malloc(), calloc(), realloc(): Used for dynamic memory
allocation. Control passes to the statement following the loop/switch.
free(): Deallocates memory. Example:
exit(): Terminates the program. for (int i = 0; i < 10; i++) {
string.h (String Handling): if (i == 5) break; // Exits the loop when i is 5
strcpy(): Copies a string. printf("%d ", i);
strcat(): Concatenates two strings. }
strlen(): Returns the length of a string. continue:
strcmp(): Compares two strings. Skips the rest of the current iteration of the loop and
proceeds to the next iteration.
math.h (Mathematical Operations):
Example:
sqrt(): Calculates the square root.
for (int i = 0; i < 5; i++) {
pow(): Calculates a number raised to a power.
sin(), cos(), tan(): Trigonometric functions.
if (i == 2) continue; // Skips the printf() statement when i is 18. Explain in detail about if, if-else, nested if, if-else-if
2 ladder, switch case with suitable examples.
printf("%d ", i); if statement
} The if statement executes a block of code only if a specified
condition is true.
// Output: 0 1 3 4
if (condition) {
goto:
// code to be executed if condition is true
Transfers control to a labelled statement unconditionally. Its
use is generally discouraged as it can lead to complex and }
hard-to-read "spaghetti code."
Example:
Example:
int num = 10;
goto label;
if (num > 0) {
printf("This will not be printed.\n");
printf("Number is positive.\n");
label:
}
printf("This is the jump target.\n");
if-else statement
return:
The if-else statement executes one block of code if the
Terminates the execution of a function and returns control condition is true and another block if it is false.
to the calling function. It can also return a value.
if (condition) {
Example:
// code if condition is true
int add(int a, int b) {
} else {
return a + b; // Returns the sum of a and b
// code if condition is false
}
} if (a > b) {
Example: if (a > c) {
int num = -5; printf("a is the greatest.\n");
if (num > 0) { }
printf("Positive.\n"); } else {
} else { if (b > c) {
printf("Not positive.\n"); printf("b is the greatest.\n");
} } else {
nested if statement printf("c is the greatest.\n");
A nested if is an if statement inside another if or else block. }
It is used to check multiple conditions sequentially.
}
if (condition1) {
if-else-if ladder
// ...
The if-else-if ladder is used to check a series of conditions.
if (condition2) { The code block of the first true condition is executed, and
the rest are skipped.
// code if both conditions are true
if (condition1) {
}
// code
}
} else if (condition2) {
Example:
// code
int a = 10, b = 20, c = 5;
} else if (condition3) {
// code break;
} else { case constant2:
// code // code
} break;
Example: default:
int score = 85; // code
if (score >= 90) { }
printf("Grade A\n");
} else if (score >= 80) { Example:
printf("Grade B\n"); char grade = 'B';
} else { switch (grade) {
printf("Grade C\n"); case 'A':
} printf("Excellent!\n");
switch statement break;
The switch statement allows a variable to be tested for case 'B':
equality against a list of constant values (cases).
printf("Good!\n");
switch (expression) {
break;
case constant1:
default:
// code
printf("Try again.\n");
}
int main() {
1. Move n-1 disks from the source to the auxiliary
tower. int n = 3; // Number of disks
2. Move the nth disk from the source to the
destination tower. towersOfHanoi(n, 'A', 'C', 'B'); // A: Source, C:
3. Move n-1 disks from the auxiliary to the Destination, B: Auxiliary
destination tower.
return 0;
}
C Program:
Output:
#include <stdio.h>
The output for the provided C program will be a series
void towersOfHanoi(int n, char from_rod, char to_rod, of instructions detailing the steps required to move 3
char aux_rod) { disks from Rod 'A' to Rod 'C', using Rod 'B' as an
auxiliary rod:
if (n == 1) {
Move disk 1 from rod A to rod C
printf("Move disk 1 from rod %c to rod %c\n",
from_rod, to_rod); Move disk 2 from rod A to rod B
printf("Move disk %d from rod %c to rod %c\n", n, Move disk 2 from rod B to rod C
from_rod, to_rod);
Move disk 1 from rod A to rod C
towersOfHanoi(n - 1, aux_rod, to_rod, from_rod);
20. Solve Swapping of two numbers by swapByValue(x, y);
Parameters passing methods in C (Call by Value &
Call by Reference). printf("After swap (Call by Value): x = %d, y = %d\
n", x, y); // x and y are unchanged
Call by Value
return 0;
In call by value, a copy of the actual parameters is
passed to the formal parameters of the function. Any }
changes made to the formal parameters inside the
function do not affect the actual parameters. Output
int temp = a;
Call by Reference
a = b;
In call by reference, the address of the actual
b = temp; parameters is passed to the function. This means the
formal parameters are pointers that hold the memory
printf("Inside function (Call by Value): a = %d, b = addresses of the actual parameters. Changes made to
%d\n", a, b); the values at these addresses will affect the original
variables.
}
c program
int main() {
#include <stdio.h>
int x = 10, y = 20;
void swapByReference(int *a, int *b) {
printf("Before swap (Call by Value): x = %d, y = %d\
n", x, y); int temp = *a;
*a = *b;
printf("Inside function (Call by Reference): *a = %d, In C programming, functions are divided into four
*b = %d\n", *a, *b); types based on:
printf("After swap (Call by Reference): x = %d, y = Used for simple tasks like printing a message.
%d\n", x, y); // x and y are swapped
💻 Example:
return 0;
#include <stdio.h>
}
void printMessage() {
Output
printf("Hello! Welcome to C Programming.\n");
Before swap (Call by Reference): x = 10, y = 20
}
Inside function (Call by Reference): *a = 20, *b = 10
int main() {
After swap (Call by Reference): x = 20, y = 10
printMessage(); // Function call
return 0; return 0;
} }
Output: Output:
2. Function with Arguments but No Return Value 3. Function with No Arguments but With Return Value
🔹 Explanation: 🔹 Explanation:
The function takes input values (arguments). This type of function does not take input.
It does not return any result. It returns a value to the main function.
Used when we only need to display or process something. Used when the result is generated inside the function.
💻 Example: 💻 Example:
} }
return 0; return 0;
} }
Output: Output: