Supreme Institute of Management and Technology
Name : Mahtab Alam Ansari
Stream : BCA
Semester : 01
Year : 01
Subject name : Programming for problem solving
through C
Subject code :- BCAC102
PPT topic :- C programming
CHAPTER-01 CHAPTER-02 CHAPTER-03
“Overview of C” “Constants,Varaibles & “Operators an
Data Types” Expressions”
CHAPTER-04 CHAPTER-05 CHAPTER-06
“Managing Input and “Decision-Making and “Decision-Making and
Output Operators” Branching” looping”
CHAPTER-07
“Arrays”
CHAPTER-01
“Overview of C”
Overview
Introduction
History of C
Importance of C
Sample C programs
Basic structures of C programs
Programming style
Executing a C program
INTRODUCTION
C programming language is known for its simplicity and efficiency. It is the
best choice to start with programming as it gives you a foundational
understanding of programming.
History of C
Created in 1972: Developed by Dennis Ritchie at Bell Labs
to improve upon the B language for system programming.
Structured Programming: C introduced concepts like
functions, loops, and conditionals that promoted modular
and efficient coding.
Dennis Ritchie
Foundation for Other Languages: C influenced and became
the basis for languages like C++, Java, and many others.
Importance of C
Foundation for Other Languages: C is the base for many modern languages
like C++, Java, and Python.
System-Level Programming: Used for developing operating systems,
drivers, and embedded systems.
Efficiency: Highly efficient, fast, and provides direct access to
memory.
Portability: C code is portable across different platforms with minimal
modification.
Large Community Support: Extensive libraries and frameworks for
faster development.
Sample C Programs
Hello World Program:
#include<stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Basic Addition Program:
#include<stdio.h>
int main() {
int a = 5, b = 3;
printf("Sum: %d", a + b);
return 0;
}
Basic Structures of C Programs
Preprocessor Directives : #include<stdio.h>
Main Function : int main() { }
Variable Declaration : int a, b;
Logic : Statements to perform operations.
Return Statement : return 0; to indicate program completion.
Programming Style in C
Indentation and Readability : Proper indentation improves readability.
Example : int main() {
int num = 10;
printf("%d",
num);
}
Meaningful Variable Names: Use descriptive names (e.g., totalMarks instead of t).
Consistent Bracing Style: Keep braces {} aligned for better clarity.
Executing a C Program
Steps to Execute :
1. Write the Code: Write .c file in a text editor.
2. Compile: Use a compiler like GCC (gcc program.c).
3. Link: The linker resolves references between files.
4. Execute: Run the compiled program (./a.out).
Error Handling: Fix compilation errors before execution.
CHAPTER-02
“Constants,Varaibles & Data Types”
Overview
Introduction Variables
Character Set in C Data Types
C Tokens Declaration of Variables
Keywords and Identifiers Assigning Values to Variables
Constants Defining Symbolic Constants
INTRODUCTION
Like any other language, C has its own vocabulary and grammar. In this
chapter , we will discuss the concepts of constants and variables and their
types.
Character Set in C
Definition: Character set includes letters, digits, special symbols, and spaces used in C.
Categories :
Alphabets : A-Z, a-z.
Digits : 0-9.
Special Symbols : $, %, *, @, etc.
White Spaces : Space, newline, tab.
Example : char letter = 'A';
C Tokens
Definition: Smallest individual units in a C program.
Types :
Keywords : Predefined words (e.g., int, return).
Identifiers : User-defined names (e.g., sum, num).
Constants : Fixed values (e.g., 5, 3.14).
Operators : Symbols that perform operations (+, *, =).
Example : int num = 5;
Keywords and Identifiers
Keywords : Reserved words with special meaning
Identifiers: User-defined names for variables, functions, etc.
Rules :
Cannot start with a digit.
No special symbols except underscore (_).
Example : int age;
Constants
Definition: Fixed values that do not change during execution.
Types :
Integer Constants : Whole numbers (e.g., 100, -45).
Floating-Point Constants : Decimal numbers (e.g., 3.14, -0.01).
Character Constants : ingle characters enclosed in single quotes (e.g., 'A', '5').
Example : const float pi = 3.14;
Variables
Definition : A named storage location that holds a value.
Identifiers: User-defined names for variables, functions, etc.
Naming Rules :
Must begin with a letter or underscore.
No spaces or special characters.
Syntax : int age = 25;
Example : char grade = 'A';
Data Types
Definition : Specify the type of data a variable can hold.
Basic Types :
int: Whole numbers.
float: Decimal numbers.
char: Single characters.
Example : int num = 10;
float avg = 5.67;
Declaration of Variables
Definition : Introducing a variable to the program.
Syntax : data_type variable_name;
Example : int age;
float salary;
char grade;
Multiple Declarations : int x, y, z;
Assigning Values to Variables
Definition : Assigning a specific value to a variable
Syntax : variable_name = value;
Example :
int num = 10;
char letter = 'B';
Defining Symbolic Constants
Definition : A name that represents a constant value
Syntax : #define CONSTANT_NAME value
Characteristics :
Cannot be modified.
Typically written in uppercase letters.
Example : #define PI 3.14159
CHAPTER-03
“Operators and Expressions”
Overview
Introduction Arithmetic expressions
Arithmetic of Operators Bitwise operators
Relational operators Evaluation of expressions
Logical operators Precedence of arithmetic operators
Assignment operators Operator precedence and
Increment and decrement operators associativity
Conditional operator
INTRODUCTION
An operator is a symbol that tells the compiler to perform specific
mathematical, logical, or relational operations.
Arithmetic Operators
Definition: These operators perform basic mathematical operations like
addition, subtraction, multiplication, etc.
List of Operators:
Addition [+] Subtraction [-] Multiplication [*] Division [/] Modulus [%]
#include <stdio.h> #include <stdio.h> #include <stdio.h> #include <stdio.h> #include <stdio.h>
void main() void main() void main() void main() void main()
{ { { { {
int a=2,b=3,sum; int a=5,b=10,sub; int a=5,b=10,mul; int a=5,b=10,div; int a=5,b=10,mod;
sub = a - b; mul = a * b;
sum = a + b; div = a / b; mod = a % b;
printf("The printf("The
printf("The sum of printf("The division printf("The modulus
substraction of %d multiplication of %d
%d and %d is of %d and %d is of %d and %d is
and %d is %d\n", a, and %d is %d\n", a, b,
%d\n", a, b, sum); %d\n", a, b, div); %d\n", a, b, mod);
b, sub); mul);
Relational Operators
Definition: These operators compare two values and return true or false.
List of Operators:
Logical Operators
Definition: These operators are used to combine multiple conditions.
List of Operators:
Assignment Operators
Definition: These operators assign values to variables.
Increment and Decrement
Operators
Definition: These operators increase or decrease the value of a variable by 1.
Increment operator in C Decrement operator in C
Conditional Operators
Definition: A shorthand for if-else statements, also known as the ternary operator.
Syntax : condition ? value_if_true : value_if_false;
Example : (5 > 3) ? "Yes" : "No" gives "Yes".
Arithmetic Expressions
Definition: An expression involving arithmetic operators.
Arithmetic Operators In C
Bitwise Operators
Definition: These operators perform bit-level operations on integers.
Evaluation of Expressions
Definition: The process by which an expression is simplified or calculated.
Example : 3 + 4 * 2 will be evaluated as 3 + (4 * 2) = 11.
Precedence of Arithmetic
Operators
Definition: Operator precedence defines the order in which operators are evaluated.
Example : Multiplication and division have higher precedence than addition
and subtraction.
3 + 5 * 2 is evaluated as 3 + (5 * 2).
Operator Precedence and Associativity
Definition: If two operators have the same precedence, associativity defines the direction
in which the expression is evaluated.
Example: 3 - 2 + 5 is evaluated from
left to right because - and + are
left-associative
CHAPTER-04
“Managing Input and Output Operators”
Overview
Introduction
Reading a Character
Writing a Character
Formatted input
Formatted output
INTRODUCTION
In C programming, input refers to taking data from the user or a file, and
output refers to displaying data to the screen or another device.
Input and output operations are essential for user interaction, debugging,
and file handling.
Reading a Character
Definition: Reading a single character from the user or from a file.
Example:
char ch;
ch = getchar(); // Reads a character from the user
Explanation: The getchar() function reads one character at a time from the input
buffer. This is useful when you want to capture and handle characters
individually.
Writing a Character
Definition: Writing or displaying a single character to the output device.
Example:
#include <stdio.h>
void main()
{
char ch = 'A';
printf("ch = %c\n",ch);
printf("ch = %d, hence an integer\n",ch);
}
Explanation: The ("%c",ch); function writes a single character to the console or
another output device. It is the simplest form of output.
Formatted Input
Definition: Input that is read in a specific data format, like integers or floating-
point numbers.
Example:
int num;
scanf("%d", &num); // Reads an integer value
Explanation: The scanf() function allows reading formatted data from the user.
In this example, %d is used to read an integer. The input is stored in the variable
num.
Formatted Output
Definition: Outputting data in a specific format to make it more readable and
structured.
Example:
int num = 5;
printf("The number is: %d", num); // Outputs: The
number is: 5
Explanation: The scanf() function allows reading formatted data from the user.
In this example, %d is used to read an integer. The input is stored in the variable
num.
CHAPTER-05
“Decision-Making and Branching”
Overview
Introduction The ELSE IF ladder
Decision making with IF The switch statement
statement The ? : Operator
Simple IF statement The GOTO statement
The IF.....ELSE statement
Nesting of IF.....ELSE
statements
INTRODUCTION
Decision-making structures control the flow of execution in a program
based on conditions.
Explanation: They help in executing certain parts of code depending on
whether the conditions are true or false.
Decision Making with IF Statement
Definition: The IF statement executes a block of code only if the condition is true.
Syntax: Example:
if (condition) {
if (age > 18) {
// code to be executed if condition
printf("You are an adult.");
is true
}
}
Simple IF Statement
Definition: A simple if statement checks a single condition.
Syntax: Example:
if (condition) { if (x > 10) {
// code printf("x is greater than 10.");
} }
The IF ELSE Statement
Definition: The IF ELSE statement executes one block of code if the condition is
true, and another block if false.
Syntax: Example:
if (condition) { if (age >= 18) {
// code if condition is true printf("Adult");
} else { } else {
// code if condition is false printf("Not an adult");
} }
Nesting of IF...ELSE Statements
Definition: Using one IF or ELSE IF statement inside another.
Syntax: Example:
if (condition1) { if (x > 0) {
if (condition2) { if (x < 100) {
// code if both condition1 and printf("x is between 1 and
condition2 are true 99");
} }
} }
The ELSE IF Ladder
Definition: Multiple conditions are checked using ELSE IF, where the first true
condition's block is executed.
Syntax: Example:
if (condition1) { if (score > 90) {
// code if condition1 is true printf("Grade A");
} else if (condition2) { } else if (score > 75) {
// code if condition2 is true printf("Grade B");
} else { } else {
// code if all conditions are false printf("Grade C");
} }
The Switch Statement
Definition: The switch statement allows selecting one of many blocks of code to
execute.
Syntax: Example:
switch (expression) {
switch (day) {
case constant1:
case 1:
// code for case 1
printf("Monday");
break;
break;
case constant2:
case 2:
// code for case 2
printf("Tuesday");
break;
break;
default:
default:
// code if none of the
printf("Invalid day");
cases match
}
}
The ?: Operator
Definition: Also known as the conditional or ternary operator, it’s a shorthand for
the IF ELSE statement.
Syntax: Example:
condition ? expression_if_true : expression_if_false; int result = (a > b) ? a : b;
The GOTO Statement
Definition: Directs the program to jump to a labeled part of the code.
Syntax: Example:
if (x < 0) {
goto label;
goto error;
...
}
label:
error:
// code to be
printf("Error:
executed
Negative number");
CHAPTER-06
“Decision-Making and Looping”
Overview
Introduction
The WHILE statement
The DO statement
The FOR statement
Jumps in LOOPS
INTRODUCTION
Looping structures allow repeating a set of instructions until a condition is
met.
Explanation: These structures help in performing repetitive tasks efficiently,
reducing code redundancy.
The DO Statement
Definition: The DO WHILE loop executes a block of code once, then repeats it as
long as the condition is true.
Syntax: Example:
int i = 0;
do {
do {
// code to be
printf("%d",
executed
i);
} while
i++;
(condition);
} while (i < 5);
The FOR Statement
Definition: The FOR loop repeats a block of code a specific number of times.
Syntax: Example:
for (initialization; condition; increment) { for (int i = 0; i < 5; i++) {
// code to be executed printf("%d", i);
} }
Jumps in Loops
Definition: Jump statements like break and continue alter the flow of loops
Break: Exits the loop immediately.
Continue: Skips the current iteration and moves to the next.
Syntax for Break: Syntax for Continue: Example:
for (int i = 0; i <
5; i++) {
if (i == 3) {
if (condition) { if (condition) {
break;
break; continue;
}
} }
printf("%d",
i);
}
CHAPTER-07
“Arrays”
Overview
Introduction
One-dimensional arrays
Two-dimensional arrays
Initializing two-dimensional
arrays
Multidimensional arrays
INTRODUCTION
An array is a collection of variables that are stored in contiguous memory
locations. All elements in an array must be of the same data type.
Array
Syntax: Example:
int numbers[5]; // Declares an
dataType arrayName[arraySize];
array of 5 integers
Explanation: Arrays allow you to store multiple values of the same type, which can
be accessed by their index.
One-Dimensional Arrays
Definition: A one-dimensional array is a list of elements of the same type.
Syntax: Example:
int scores[3] = {85, 90, 78}; // An array
dataType arrayName[arraySize];
holding 3 integer scores
Explanation: In a one-dimensional array, elements are accessed using an index
starting from 0.
Declaration of Arrays
Definition: Declaring an array specifies the type of elements and the number of
elements.
Syntax: Example:
data_type array_name[array_size]; int numbers[5];
Explanation: Memory is allocated for the array.
The array is indexed starting from 0.
All values are uninitialized (contain garbage values until
explicitly initialized).
Two-Dimensional Arrays
Definition: A two-dimensional array is an array of arrays, where data is stored in a
grid (rows and columns).
Syntax: Example:
dataType arrayName[rows][columns]; int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // 2
rows, 3 columns
Explanation: Two-dimensional arrays are used to store tables or matrices of data.
Initializing Two-Dimensional Arrays
Definition: Two-dimensional arrays can be initialized during declaration.
Syntax: Example:
dataType arrayName[rows][columns]
= {{value1, value2}, {value3, value4}}; int matrix[2][2] = {{1, 2}, {3, 4}};
Explanation: You can initialize values directly when declaring a 2D array.
REFERRENCES
URL :- https://www.geeksforgeeks.org/c-programming-language/
Book :- Programming in ANSI C [2nd edition]
By E. Balagurusamy
THANK YOU
ANY QUESTIONS ?