0% found this document useful (0 votes)
13 views72 pages

Name: Mahtab Alam Ansari: Supreme Institute of Management and Technology

Uploaded by

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

Name: Mahtab Alam Ansari: Supreme Institute of Management and Technology

Uploaded by

98arsaim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

Supreme Institute of Management and Technology

Name : Mahtab Alam Ansari

Stream :

BCA

Semester :

01

Year : 0 1

Subject name : Programming for problem


solving through C

Subject code :- BCAC102


CHAPTER-01 CHAPTER-02 CHAPTER-03
“Overview of “Constants,Varaibles & “Operators an
C” Data Types” Expressions”

CHAPTER-04 CHAPTER-05 CHAPTER-06


“Managing Input “Decision-Making and “Decision-Making and
and Output Branching” looping”
Operators”

CHAPTER-07
“Arrays”
CHAPTER-01

“Overview of C”
Overvie
Introduction
w
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.

D ennis Ritchie
Foundation for Other Languages: C influenced and
became
the basis for languages like C++, Java, and many
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
Hello World
Program:
Programs
#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
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”


Overvie
w
Introduction Variables
Character Set in Data
C C Tokens Types
Keywords and Declaration of Variables
Identifiers Constants Assigning Values to
Variables Defining
Symbolic Constants
INTRODUCTIO
N
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
inincludes
Definition: Character set C 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.
Exampl char letter =
e: '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).
Exampl int num = : Symbols that perform
Operators
e: 5;operations (+, *, =).
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 (_).

Exampl int
e: age;
Constan
ts
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


Exampl single
constquotes
float pi(e.g.,
= 'A', '5').
e: 3.14;
Variable
Definition :
s
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;


Exampl char grade =
e: 'A';
Data
Definition :
Types
Specify the type of data a variable
can hold.

Basic Types :
int: Whole numbers.

float: Decimal

numbers. char:

Exampl int num = 10; Single characters.


e: float avg =
5.67;
Declaration of
Variables
Definition : Introducing a variable to the
program.

Syntax: data_type variable_name;

Exampl int age;


e:
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;

Exampl
e: 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.

Exampl #define PI
e: 3.14159
CHAPTER-03

“Operators and Expressions”


Overvie
w Arithmetic expressions
Introduction
Arithmetic of Bitwise operators
Operators Relational Evaluation of
operators Logical expressions
operators Assignment Precedence of arithmetic
operators operators Operator precedence
Increment and decrement and associativity
operators Conditional operator
INTRODUCTION

An operator is a symbol that tells the compiler to perform


specific mathematical, logical, or relational
operations.
Arithmetic
Definition: These Operators
operators perform basic mathematical operations
like addition, subtraction, multiplication, etc.

List of Operators:

Addition [+] Subtraction [-] Multiplication [*] Division [/] Modulus [%]

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


<stdio.h> void void main() void main() <stdio.h> void <stdio.h> void
main() { { main() main()
{ int a=5,b=10,sub; int a=5,b=10,mul; { {
sub = a - b; mul = a * b;
int a=2,b=3,sum; int a=5,b=10,div; int a=5,b=10,mod;
printf("The printf("The
sum = a + b; div = a / b; mod = a % b;
substraction of %d multiplication of %d
printf("The sum of and %d is %d\ n", a, printf("The printf("The modulus
and %d is %d\ n",
%d and %d is division of %d and of %d and %d is
a, b, sub); b, mul);
%d\n", a, b, sum); %d is %d\n", a, b, mod);
%d\n", a, b, div);
Relational
Definition: TheseOperators
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
Definition: TheseOperators
operators assign values to
variables.
Increment and
Decrement Operato
Definition: These operatorsrs
increase or decrease the value of a
variable by 1.
Increment operator Decrement operator
in C 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 Operato
rs defines the order in which operators
Definition: Operator precedence
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
Definition: IfAssociativity
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”


Overvie
w
Introduction
Reading a
Character Writing
a Character
Formatted input
Formatted output
INTRODUCTIO
N
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
:
c
h
a
r
Explanation: The getchar() function reads one character at a time
from the input buffer. This is useful when you want to capture
c and handle characters individually.
h
;
Writing a
device.
Character
Definition: Writing or displaying a single character to the output

Example
:
#incl
ude
<stdi
o.h>
void
main(
)
{
char ch = 'A';
printf("ch = %c\
n",ch);
Formatted
Input
Definition: Input that is read in a specific data format, like
integers or floating- point numbers.

Examp
l
e
:
int num;
scanf("%d", &num); // Reads an
integer
Explanation: The scanf() function allows value
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.

Exampl
e
:
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
CHAPTER-05

“Decision-Making and Branching”


Overvie
w
Introduction The ELSE IF ladder
Decision making with The switch
IF statement statement The ? :
Simple IF statement Operator
The IF.....ELSE The GOTO
statement Nesting of statement

IF.....ELSE statements
INTRODUCTIO
N
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.

Synta Exampl
x: e:

if (condition) {
if (age > 18)
// code to be executed if
{ printf("You are an
condition
adult.");
is true
}
}
Simple IF
Statement
Definition: A simple if statement checks a single
condition.

Synta Exampl
x: e:

if if (x > 10) {
(condition) printf("x is greater
{ than 10.");
// code }
}
The IF ELSE
Statement
Definition: The IF ELSE statement executes one block of code if the
condition is true, and another block if false.

Synta Exampl
x: e:

if (condition) { if (age >=


// code if condition is 18)
true { printf("Adu
} else { lt");
// code if condition is } else
false { printf("Not an
} adult");
Nesting of IF...ELSE
Statements
Definition: Using one IF or ELSE IF statement
inside another.

Synta Exampl
x: e:

if if (x > 0 ) {
(condition1) if (x < 10 0 )
{ if { printf("x is
(condition2) between 1 and
{ 99");
// code if both }
condition1 and }
condition2 are true
The ELSE IF
Ladder
Definition: Multiple conditions are checked using ELSE IF,
where the first true condition's block is
executed.

Synta Exampl
x: e:
if (condition1) { if (score > 90 )
// code if condition1 is { printf("Grad
true e A");
} else if (condition2) { } else if (score >
// code if condition2 is 75)
true { printf("Grad
} else { e B ");
// code if all conditions } else
are false
The Switch
Statement
Definition: The switch statement allows selecting one of many
blocks of code to execute.
Syntax: Example:

switch
switch (day) {
(expression) {
case 1:
case
printf("Monda
constant1:
y");
// code for
brea
case 1
k;
break;
case
case constant2:
2:
// code for
printf("Tuesda
case 2
y");
break;
break;
default:
default:
The ?:
Operator
Definition: Also known as the conditional or ternary operator, it’s a
shorthand for the IF ELSE statement.

Synta Exampl
x: e:

condition ? expression_if_true : int result = (a >


expression_if_false; b) ? a : b;
The GOTO
Statement
Definition: Directs the program to jump to a labeled part
of the code.

Synta Exampl
x: e:
if (x < 0 )
goto
label; { goto
... error;
label: }
// code to error:
be printf("Err
executed or:
N egative
CHAPTER-06

“Decision-Making and Looping”


Overvie
w

Introduction
The WHILE
statement The DO
statement The FOR
statement Jumps in
LOOPS
INTRODUCTIO
N
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.

Synta Exampl
x: e:
int i =
do {
0;
// code to
do {
be
printf("%
executed
d",
} while
i);
(conditio
i+
n);
+;
The FOR
Statement
Definition: The FOR loop repeats a block of code a specific
number of times.

Synta Exampl
x: e:

for (initialization; condition; for (int i = 0; i < 5;


increment) { 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 Exampl


Continue: e:
for (int i =
0; i < 5;
i++) {
if if
if (i ==
(condition) (conditio
3)
{ n)
{ bre
break; { continu
ak;
} e;
}
}
printf("%d",
CHAPTER-07

“Arrays

Overvie
w

Introduction
One-dimensional arrays
Two-dimensional arrays
Initializing two-
dimensional arrays
Multidimensional arrays
INTRODUCTIO
N
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.
Arra
y
Synta Exampl
x: e:
int numbers[5]; //
dataType
Declares an array of
arrayName[arraySize];
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


dataType arrayName[arraySize];
array 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, int matrix[2][2] = {{1, 2}, {3,
value4}}; 4}};

Explanation: You can initialize values directly when declaring a 2D


array.
REFERRENCE
S
URL :- https://www.geeksforgeeks.org/c-programming-langua
ge/

B ook :- Programming in ANSI C [2nd


edition] By E. Balagurusamy
THANK
YOU
ANY
QUESTIONS ?

You might also like