0% found this document useful (0 votes)
3 views15 pages

Coding

The document provides an overview of fundamental programming concepts, including coding, compilation, debugging, testing, documentation, algorithms, flowcharts, and execution. It also details the structure of a C program, data types, variable declaration and initialization, control statements, recursion, storage classes, strings, arrays, and pointers. Each concept is explained with definitions, examples, and syntax to aid understanding of programming in C.

Uploaded by

rizontheeng123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Coding

The document provides an overview of fundamental programming concepts, including coding, compilation, debugging, testing, documentation, algorithms, flowcharts, and execution. It also details the structure of a C program, data types, variable declaration and initialization, control statements, recursion, storage classes, strings, arrays, and pointers. Each concept is explained with definitions, examples, and syntax to aid understanding of programming in C.

Uploaded by

rizontheeng123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Coding: Coding is the process of writing instructions for a computer using a programming language

such as Python, Java, or C++. It involves translating a problem or idea into a set of commands that the
computer can understand and execute. Good coding requires logical thinking, problem-solving skills,
and knowledge of syntax and structure. 2. Compilation: Compilation is the
process of converting source code written in a high-level programming language into machine code
(binary) that the computer’s processor can understand. This is done by a program called a compiler.
During compilation, errors in syntax or structure are often detected and reported.
3. Debugging: Debugging is the process of identifying, analyzing, and fixing errors or bugs in a
program. These errors may cause the program to crash, produce incorrect results, or behave
unexpectedly. Debugging tools and techniques help programmers trace and correct problems
efficiently.
4. Testing: Testing is the process of evaluating a program to ensure it works correctly and meets the
required specifications. It involves running the program with different inputs to check for errors and
verify that all parts function properly. Testing improves reliability and quality.
5. Documentation: Documentation refers to written or recorded information that explains how a
program is designed, developed, and used. It may include user manuals, technical guides, and
comments within the code. Good documentation makes it easier for others (and future developers) to
understand and maintain the program. 6. Algorithm: An algorithm is a
clear, step-by-step set of instructions used to solve a problem or complete a task. It must be logical,
finite, and unambiguous. Algorithms form the foundation of programming because they outline the
solution before coding begins. The characteristics of an algorithm are : Input: An algorithm should
have zero or more inputs. These are the values or data provided to solve a problem. Output: It must
produce at least one output. The result should be clearly defined and related to the input.
Definiteness (Clarity): Each step of the algorithm must be clear, precise, and unambiguous. There
should be no confusion about what each instruction means. Finiteness: An algorithm must have a
definite end. It should complete after a finite number of steps, not run forever. Effectiveness: Each
step should be simple, basic, and executable within a reasonable time. The instructions must be
practical and possible to carry out. Generality: An algorithm should be able to solve a class of
problems, not just a single specific case. It should work for different inputs within its defined scope.
Correctness: The algorithm should produce the correct output for all valid inputs. Accuracy is
essential for a good algorithm.
7. Flowchart: A flowchart is a graphical representation of an algorithm or process. It uses standard
symbols (like rectangles for processes and diamonds for decisions) connected by arrows to show the
flow of steps. Flowcharts help in planning, understanding, and explaining programs visually.
8. Execution: Execution is the process of running a compiled or interpreted program so that the
computer performs the instructions written in the code. During execution, the program processes
input, performs operations, and produces output.
The basic structure of a C program shows how a C program is organized and written. It usually
consists of the following parts:

1. Documentation Section: This section includes comments that describe the program, such as its
purpose, author, and date. Example:

/* This program prints Hello World */

2. Link Section: This section includes header files needed for the program using #include. Example:

#include <stdio.h>

3. Definition Section: Here, symbolic constants are defined using #define. Example:

#define PI 3.14

4. Global Declaration Section: Variables and functions that are used throughout the program are
declared here. Example:

int a, b;

5. main() Function Section


This is the starting point of every C program. It contains two parts: Declaration Part (declaring
variables) Execution Part (actual statements)

Example:

int main() {
int x = 5;
printf("Value of x = %d", x);
return 0;
}

6. Subprogram Section: This section contains user-defined functions (if any), which are called in the
main() function.
Example:

void display() {
printf("Hello");
}

Complete Example:

/* Simple C Program */

#include <stdio.h>
#define VALUE 10

int main() {
int num = VALUE;
printf("Number = %d", num);
return 0;
}
1. Basic (Primary) Data Types: These are the fundamental data types used in C:
int (Integer) Used to store whole numbers (positive or negative).
Example: int a = 10;
float: Used to store decimal (floating-point) numbers with single precision.
Example: float x = 3.14;
double: Used to store larger decimal numbers with double precision (more accuracy than float).
Example: double y = 3.141592;
char (Character): Used to store a single character (letter, digit, or symbol).
Example: char ch = 'A';

2. Derived Data Types: These are formed from basic data types:
Array: A collection of elements of the same type stored in contiguous memory.
Example: int arr[5];
Pointer: A variable that stores the memory address of another variable.
Example: int *p;
Function: A block of code that performs a specific task and may return a value.
Example: int add(int a, int b);

3. User-Defined Data Types: These are created by the programmer:


struct (Structure): Groups variables of different types under one name.
Example:

struct student {
int id;
char name[20];
};

Union: Similar to structure, but all members share the same memory location.
enum (Enumeration): Used to assign names to a set of integer constants.
Example: enum day {Sun, Mon, Tue};
typedef: Used to give a new name (alias) to an existing data type.
Example: typedef int number;

4. Void Data Type


void: Represents the absence of a value.
It is used when a function does not return anything or when no parameters are passed.
Example: void display();

1. Variable Declaration

Declaration means telling the compiler: 1. the name of the variable 2. the type of data it will
store Example: int age;
float salary;
char grade;
Here: int, float, char are data types age, salary, grade are
variable names 2. Variable Initialization: Initialization
means assigning a value to the variable.
Example: age = 20;
salary = 15000.50;
grade = 'A';
3. Declaration and Initialization Together: You can declare and initialize a variable in one step.
Example: int age = 20;
float salary = 15000.50;
char grade = 'A';
1. Identifier: An identifier is the name given to a variable, function, array, or any other user-defined
item. It is used to identify these elements in a program.
Rules: Must start with a letter or underscore _ Cannot be a keyword Cannot contain spaces
Example:
int age;
float total_marks;

2. Keywords: Keywords are reserved words in C that have special meanings and cannot be used as
identifiers. Examples: int, float, if, else, while, return These words are
predefined by the language.

3. Constants are fixed values that do not change during program execution.
Types of constants: Integer constants → 10, -5 Floating constants → 3.14 Character
constants → 'A' Example: const int MAX = 100;

4. Literals are the actual values directly written in the program. All literals are constants, but written
directly in code. Examples: 10 (integer literal) 3.14 (float literal) 'A' (character literal)
"Hello" (string literal)

5. Escape sequences are special characters used inside strings to represent actions like new line,
tab, etc. They start with a backslash \. Common escape sequences: \n → New
line \t → Tab space \\ → Backslash \" → Double quote

A format specifier is a placeholder that begins with % and tells the compiler what type of value (int,
float, char, etc.) is being used. Common Format Specifiers

Specifi Meanin
Example
er g

%d Integer int a = 10;

float x =
%f Float
3.14;

double y =
%lf Double
3.14;

Charact
%c char ch = 'A';
er

%s String "Hello"

Example using printf()

#include <stdio.h>

int main() {
int age = 20;
float marks = 85.5;
char grade = 'A';

printf("Age = %d\n", age);


printf("Marks = %f\n", marks);
printf("Grade = %c\n", grade);

return 0;
}
1. if Statement

Syntax:

if (condition) {
// statements
}

Example:

int num = 10;

if (num > 0) {
printf("Number is positive");
}

2. if–else Statement

Syntax:

if (condition) {
// true block
} else {
// false block
}

Example:

int num = -5;

if (num > 0) {
printf("Positive");
} else {
printf("Negative");
}

3. if–else if–else Statement (Ladder) Syntax:

if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
} Example:

int marks = 75;

if (marks >= 80) {


printf("Distinction");
} else if (marks >= 60) {
printf("First Division");
} else {
printf("Pass");
}

4. for Loop Syntax:

for (initialization; condition; increment/decrement) {


// statements
} Example:

int i;

for (i = 1; i <= 5; i++) {


printf("%d\n", i);
}

5. while Loop Syntax:

while (condition) {
// statements
} Example:

int i = 1;

while (i <= 5) {
printf("%d\n", i);
i++;
}

6. do–while Loop Syntax:

do {
// statements
} while (condition); Example:

int i = 1;

do {
printf("%d\n", i);
i++;
} while (i <= 5);

7. Nested Statements: Nested means using one control structure inside another. Example
(Nested if):

int a = 10, b = 20;

if (a > 0) {
if (b > 0) {
printf("Both are positive");
}
}

Example (Nested loop):


int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
printf("%d %d\n", i, j);
}
}

Definition: Recursion is a method of solving a problem by breaking it into smaller subproblems, where
the function repeatedly calls itself until a stopping condition is reached.

Key Parts of Recursion Base Case → The condition where the


function stops calling itself Recursive Case → The part
where the function calls itself
How it Works
factorial(5)
= 5 × factorial(4)
= 5 × 4 × factorial(3)
= 5 × 4 × 3 × factorial(2)
= 5 × 4 × 3 × 2 × factorial(1)
= 5 × 4 × 3 × 2 × 1 = 120

1. Storage Class: A storage class defines the scope (where a variable can be accessed), lifetime (how
long it exists in memory), and storage location of a variable. It tells the compiler how to treat the
variable. Common storage classes in C include auto, register, static, and extern.

2. Local Variable: A local variable is a variable declared inside a function or a block. It can only be
accessed within that specific function or block where it is defined. Its lifetime is limited to the execution
of that block, meaning it is created when the block starts and destroyed when the block ends.

3. Global Variable: A global variable is declared outside all functions, usually at the top of the
program. It can be accessed and modified by any function in the program. Its lifetime lasts throughout
the entire execution of the program.

4. Static Variable: A static variable is a variable that retains its value between function calls. It is
initialized only once and exists for the entire lifetime of the program. If declared inside a function, it
remains local in scope but does not lose its value after the function ends.

1. String (Definition) A string in C is a collection (array) of characters ending with a special


character called the null character (\0). It is used to store and manipulate text.

Example:

char str[] = "Hello";

2. String Functions: String functions are predefined functions available in the header file #include
<string.h> used to perform operations on strings.

a) strlen() – Find Length of String

Syntax:

strlen(string);

Example:

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello";
printf("Length = %lu", strlen(str));
return 0;
}

b) strcpy() – Copy String Syntax:

strcpy(destination, source); Example:

#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello";
char dest[20];

strcpy(dest, src);
printf("Copied string = %s", dest);

return 0;
}

c) strcat() – Concatenate Strings Syntax:

strcat(string1, string2); Example:

#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello ";
char str2[] = "World";

strcat(str1, str2);
printf("%s", str1);

return 0;
}

d) strcmp() – Compare Strings Syntax:

strcmp(string1, string2); Example:

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Apple";
char str2[] = "Banana";

int result = strcmp(str1, str2);

if (result == 0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}

e) strrev() – Reverse String (may not be standard in all compilers) Syntax:

strrev(string); Example:

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello";

strrev(str);
printf("%s", str);

return 0;
}

f) strlwr() and strupr() – Change Case (compiler-dependent) Syntax:

strlwr(string); // to lowercase
strupr(string); // to uppercase Example:

char str[] = "HELLO";

strlwr(str); // hello
strupr(str); // HELLO

1. Array: An array is a collection of elements of the same data type stored in contiguous memory
locations. It is used to store multiple values using a single variable name. Example:

int arr[5];

2. Initialization of 1D Array: A one-dimensional (1D) array stores elements in a single row.


Syntax:

data_type array_name[size] = {values}; Examples:

int arr[5] = {1, 2, 3, 4, 5};

OR (size can be omitted):

int arr[] = {10, 20, 30};

You can also initialize partially:

int arr[5] = {1, 2}; // remaining elements become 0

3. Initialization of 2D Array: A two-dimensional (2D) array stores elements in rows and columns
(like a table or matrix). Syntax:

data_type array_name[rows][columns] = {{values}, {values}}; Examples:

int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
1. Pointer (Definition)

A pointer is a variable that stores the memory address of another variable instead of storing a direct
value. It allows direct access and manipulation of memory.

2. Declaration of Pointer

To declare a pointer, we use the * symbol.

Syntax:

data_type *pointer_name;

Example:

int *p;
float *f;
char *c;

Here, p, f, and c are pointers that can store addresses of int, float, and char variables respectively.

3. Initialization of Pointer

Initialization means assigning the address of a variable to the pointer using the & (address-of)
operator.

Syntax:

pointer_name = &variable_name;

OR combined:

data_type *pointer_name = &variable_name;

Example:

#include <stdio.h>

int main() {
int a = 10;
int *p = &a; // declaration + initialization

printf("Value of a = %d\n", a);


printf("Address of a = %p\n", &a);
printf("Pointer p stores = %p\n", p);
printf("Value using pointer = %d\n", *p);

return 0;
}

4. Important Symbols

 * → used to declare pointer and access value (dereference)

 & → used to get the address of a variable


1. DMA (Dynamic Memory Allocation) – Dynamic Memory Allocation (DMA) is a technique in C
used to allocate memory during program execution (run time) instead of compile time. It allows
programmers to request memory from the heap as needed and release it when no longer required.

2. Importance of DMA: Dynamic memory allocation is important because:


✔ It allows flexible memory usage (size decided at runtime) ✔ It helps in handling large data
structures like linked lists, trees, etc. ✔ It prevents memory wastage by allocating only required
memory ✔ It supports programs where memory requirements are not known in advance

3. DMA Functions in C In C, DMA is handled using functions from the header file:

#include <stdlib.h>

a) malloc() – Memory Allocation Syntax:

ptr = (cast_type*) malloc(size); Example:

int *p;
p = (int*) malloc(5 * sizeof(int));

✔ Allocates memory for 5 integers


✔ Memory is uninitialized (garbage values)

b) calloc() – Contiguous Allocation

Syntax:

ptr = (cast_type*) calloc(n, size);

Example:

int *p;
p = (int*) calloc(5, sizeof(int));

✔ Allocates memory for 5 elements ✔ Initializes all values to 0

c) free() – Deallocation of Memory

Syntax:

free(ptr);

Example:

free(p);

✔ Releases dynamically allocated memory ✔ Prevents memory leaks

d) realloc() – Reallocation of Memory

Syntax:

ptr = (cast_type*) realloc(ptr, new_size);

Example:

p = (int*) realloc(p, 10 * sizeof(int));

✔ Changes the size of previously allocated memory ✔ Can increase or decrease


memory size
1. Program to check Even and Odd Number

#include <stdio.h>

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num % 2 == 0) {
printf("%d is Even number", num);
} else {
printf("%d is Odd number", num);
}

return 0;
}

2. Program to print Even Numbers (1 to N)

#include <stdio.h>

int main() {
int n, i;

printf("Enter range: ");


scanf("%d", &n);

printf("Even numbers are:\n");

for (i = 1; i <= n; i++) {


if (i % 2 == 0) {
printf("%d ", i);
}
}

return 0;
}

3. Program to check Prime Number

#include <stdio.h>

int main() {
int num, i, flag = 0;

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
printf("Not a Prime Number");
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}

if (flag == 0)
printf("%d is Prime Number", num);
else
printf("%d is Not Prime Number", num);

return 0;
}

1. Matrix Addition

#include <stdio.h>

int main() {
int a[10][10], b[10][10], sum[10][10];
int i, j, r, c;

printf("Enter rows and columns: ");


scanf("%d %d", &r, &c);

printf("Enter elements of first matrix:\n");


for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of second matrix:\n");


for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
scanf("%d", &b[i][j]);
}
}

for(i = 0; i < r; i++) {


for(j = 0; j < c; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}

printf("Sum matrix:\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}

2. Matrix Subtraction

#include <stdio.h>

int main() {
int a[10][10], b[10][10], diff[10][10];
int i, j, r, c;

printf("Enter rows and columns: ");


scanf("%d %d", &r, &c);

printf("Enter first matrix:\n");


for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter second matrix:\n");


for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
scanf("%d", &b[i][j]);
}
}

for(i = 0; i < r; i++) {


for(j = 0; j < c; j++) {
diff[i][j] = a[i][j] - b[i][j];
}
}

printf("Difference matrix:\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", diff[i][j]);
}
printf("\n");
}

return 0;
}

3. Matrix Multiplication

#include <stdio.h>

int main() {
int a[10][10], b[10][10], mul[10][10];
int i, j, k, r1, c1, r2, c2;

printf("Enter rows and columns of first matrix: ");


scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);

if(c1 != r2) {
printf("Multiplication not possible");
return 0;
}

printf("Enter first matrix:\n");


for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter second matrix:\n");


for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}

for(i = 0; i < r1; i++) {


for(j = 0; j < c2; j++) {
mul[i][j] = 0;
for(k = 0; k < c1; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Product matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", mul[i][j]);
}
printf("\n");
}

return 0;
}

You might also like