Short Notes On Programming For Problem Solving
Short Notes On Programming For Problem Solving
Explanation: Programming languages have evolved to make coding easier and more powerful,
moving from machine language to high-level languages like C, which is easier to understand and
write.
Start
Print sum
End
Explanation: Pseudo code helps plan the logic of the code in simple steps before actual coding.
4. Comments in C
Single-line Comment
Syntax:
Explanation: Used for short notes about the code; C ignores the comment during compilation.
Multi-line Comment
Syntax:
Explanation: Used for longer explanations or to temporarily remove code during debugging.
5. Structure of a C Program
Explanation: A basic C program consists of headers, the main() function, and statements within { }.
Example:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
Example:
int age;
scanf("%d", &age);
Explanation: scanf reads user input, and printf displays text to the screen.
Definition: A variable is a named location in memory to store data. An identifier is the name given to a
variable.
Syntax:
int age;
Explanation: int declares an integer variable age that can store a whole number.
8. Constants, Keywords
Keywords: Reserved words in C, like int, float, return, which cannot be used as variable names.
Example:
Scope
Example:
int main() {
Explanation: Variables declared inside a function have a local scope, accessible only within that
function.
Binding - Explanation: Binding is the association of variables with values, such as int x = 5;, where x
is bound to 5.
Definition: Storage classes determine the scope, lifetime, and visibility of variables.
Example:
Explanation: static preserves a variable’s value even after the function in which it’s declared ends.
Integer
Example:
Floating Point
Example:
Char
Example:
String
Example:
Example:
Syntax:
int x = 5;
x++; // Post-increment
++x; // Pre-increment
Comma Operator
Example:
int a = 1, b = 2;
Example:
p->x = 10;
Assignment Operator
Example:
int x = 5;
Bitwise Operator
Example:
int x = 5 & 3;
Size-of Operator
Example:
printf("%lu", sizeof(int));
Arithmetic Operators
Example:
int sum = 5 + 3;
Relational Operators
Example:
int x = 5, y = 3;
Logical Operators
Example:
Syntax:
int x = (y > 0) ? 1 : 0;
Explanation: Returns 1 if y is positive, otherwise 0.
Explanation: Determines the order in which operators are evaluated. Multiplication * and division /
have higher precedence than addition + and subtraction -.
Example:
int x = 5;
int y = x++;
UNIT-II
Simple if Statement
Syntax:
if (condition) {
Example:
Explanation: This program checks if the age is 18 or older. If true, it prints a message indicating
eligibility to vote.
if...else Statement
Syntax:
if (condition) {
// code if true
} else {
// code if false
Example:
printf("Eligible to vote.\n");
} else {
Explanation: If age is less than 18, it will print "Not eligible to vote"; otherwise, it prints "Eligible to
vote."
Definition: else if provides multiple conditions, and a nested if contains an if within another if.
Syntax:
if (condition1) {
} else if (condition2) {
} else {
Example:
printf("Grade: A\n");
printf("Grade: B\n");
} else {
printf("Grade: C\n");
Explanation: This code assigns grades based on marks, demonstrating both else if and
conditional branches.
2. Switch Case
Definition: The switch statement allows a variable to be tested for equality against multiple values.
Syntax:
switch (expression) {
case constant1:
// code
break;
case constant2:
// code
break;
default:
// code
Example:
int day = 3;
switch (day) {
Explanation: The code displays the day based on the value of day. Each case provides a different
message.
break Statement
if (i == 3) break;
continue Statement
Definition: Skips the current loop iteration and continues with the next.
Example:
if (i == 3) continue;
goto Statement
Example:
int x = 1;
if (x == 1) goto label;
label:
printf("Jumped to label!\n");
for Loop
Syntax:
// code
Example:
for (int i = 1; i <= 5; i++) {
while Loop
Syntax:
while (condition) {
// code
Example:
int i = 1;
while (i <= 5) {
i++;
do...while Loop
Syntax:
do {
// code
} while (condition);
Example:
int i = 1;
do {
i++;
Explanation: Executes the code at least once, then checks the condition.
Nested Loops
Example:
printf("\n");
Explanation: Uses a for loop inside another for loop to print a matrix.
5. Introduction to Arrays
Syntax:
Example:
Syntax:
Example:
6. Pointers
Syntax:
int x = 10;
Void Pointers
Definition: A pointer with no specific data type; can point to any data type.
Example:
void *ptr;
int x = 10;
ptr = &x;
Example:
UNIT-III
1. String Basics
Syntax:
Explanation: Declares a character array str and initializes it with the string "Hello, world!".
2. String Functions
Example:
char str[50];
gets(str);
puts("You entered:");
puts(str);
Explanation: gets() takes a string input from the user, and puts() outputs it.
Example:
char ch;
ch = getchar();
putchar(ch);
printf()
Example:
atoi
Example:
strlen
Example:
strcat
Example:
strcat(str1, str2);
printf("%s", str1);
strcmp
Example:
if (strcmp(str1, str2) == 0) {
} else {
sprintf
Example:
char buffer[50];
puts(buffer);
sscanf
Example:
int a, b;
strrev(str);
printf("%s", str);
strcpy
Example:
char dest[20];
strcpy(dest, src);
printf("%s", dest);
strstr
Example:
strtok
Example:
printf("%s\n", token);
5. Operations on Strings
Example:
6. Function Basics
Example:
printf("Hello, student!");
Example:
int main() {
int n = 5;
Example:
Example:
Call by Value
Example:
Explanation: Call by value passes a copy; changes in the function do not affect the original variable.
Call by Reference
Example:
Explanation: Call by reference passes an address, so changes affect the original variable.
Example:
Explanation: The entire array arr is passed to the function to print each element.
Example:
9. Function Pointers
Example:
int main() {
void (*ptr)() = greet;
ptr();
Explanation: ptr is a function pointer that stores the address of greet, which is then called.