0% found this document useful (0 votes)
29 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)
29 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 :
01
Subject name : Programming for problem
solving through C

Subject code :-
BCAC102
PPT topic :- C
programming
CHAPTER- CHAPTER- CHAPTER-
01 of
“Overview 02
“Constants,Varaibl
es & Data Types”
03
“Operators
an
C”
Expressions

CHAPTER- CHAPTER- CHAPTER-


“Managing Input “Decision-Making “Decision-Making
04
and Output
05
and Branching”
06
and 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
INTRODUCTIO
N
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
• Foundation for Other Languages: C influenced and Ritchie
became the basis for languages like C++, Java,
and many others.
Importance
of CC is the base for many modern
• Foundation for Other Languages:
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
completion.
Programming Style
Indentation and in
ProperC
indentation improves
Readability : 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
Steps to Program
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 C • Data Types
• C Tokens • Declaration of Variables
• Keywords and Identifiers • Assigning Values to Variables
• Constants • 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
Definition: Character set includesin C
letters, digits, special symbols, and
spaces used in C.
Categorie
s:
• Alphabets : A-Z,
• Digits :a-z.
0-
9. Symbols : $, %, *, @,
• Special
etc. : Space,
• White Spaces
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;
Constant
Definition: Fixed values that do s
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;
Variabl
eslocation that holds
• Definition : A named storage
a value.
• Identifiers: User-defined names for variables,
• Naming Rules functions, etc.
:
• Must begin with a letter or
underscore.
• No spaces or special
characters.
• Syntax int age =
: 25;

Example char grade =


: 'A';
Data
• Definition : Specify theTypes
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
• Definition : AConstants
name that represents a
constant value
• Syntax #define CONSTANT_NAME
: value

• Characteristic
s: • Cannot be
modified.
• Typically written in uppercase
letters.

Example #define PI
: 3.14159
CHAPTER-
03
“Operators and
Expressions”
Overvie
• Introduction
w • Arithmetic expressions
• Arithmetic of Operators • Bitwise operators
• Relational operators • Evaluation of expressions
• Logical operators • Precedence of arithmetic
• Assignment operators operators
• Increment and decrement • Operator precedence and
operators associativity
• Conditional operator
INTRODUCTIO
N
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 #include #include #include #include
<stdio.h> <stdio.h> <stdio.h> <stdio.h> <stdio.h>
void main() void main() void main() void main() void main()
{ { { { {
int int
int int int
a=5,b=10,sub; a=5,b=10,mul;
a=2,b=3,sum; a=5,b=10,div; 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 printf("The printf("The
substraction of multiplication of
of %d and %d is division of %d modulus of %d
%d and %d is %d and %d is %d\
%d\n", a, b, and %d is %d\n", and %d is %d\n",
%d\n", a, b, sub); n", a, b, mul);
a, b, div); a, b, mod);
sum);
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
variables.
assign values to
Increment and
Decrement Operators
Definition: These operators 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;

Exampl (5 > 3) ? "Yes" : "No" gives "Yes".


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

Exampl 3 + 4 * 2 will be evaluated as 3 + (4


e: * 2) = 11.
Precedence of
Arithmetic Operators
Definition: Operator precedence defines the order in which operators
are evaluated.
Exampl Multiplication and division have higher precedence than
e: 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”
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
: 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
Definition: Input that is read inInput
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
Definition: Outputting data Output
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
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 adult.");
condition is true
}
}
Simple IF
Statement
Definition: A simple if statement checks a single
condition.

Synta Exampl
x: e:

if (condition) if (x > 10) {


{ printf("x is greater than
// code 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.

Synta Exampl
x: e:

if (condition) { if (age >= 18) {


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

Synta Exampl
x: e:
if (condition1) { if (x > 0) {
if (condition2) { if (x < 100) {
// code if both printf("x is between 1
condition1 and condition2 and 99");
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 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 printf("Grade C");
false }
}
The Switch
Statement
Definition: The switch statement allows selecting one of many blocks
of code to execute.
Synta Exampl
x: e:
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.

Synta Exampl
x: e:

condition ? expression_if_true : int result = (a > b) ?


expression_if_false; 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:
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
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 = 0;
do {
do {
// code to
be executed
printf("%d",
} while
i);
(condition);
i++;
} while (i <
5);
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 Syntax for Exampl
Break: Continue: e:
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
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]; // Declares
dataType
an array of 5 integers
arrayName[arraySize];

Explanation: Arrays allow you to store multiple values of the same


type, which can be accessed by their index.
One-Dimensional
Definition: A one-dimensional Arrays
array is a list of elements of the
same type.
Synta Exampl
x: e:

int scores[3] = {85, 90, 78}; //


dataType
An array holding 3 integer
arrayName[arraySize];
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.

Synta Exampl
x: e:

data_type int
array_name[array_size]; numbers[5];

Explanatio • Memory is allocated for the


n: array.
• The array is indexed starting
• All values from 0.
are uninitialized (contain garbage
values until explicitly initialized).
Two-Dimensional
Definition: A two-dimensional Arrays
array is an array of arrays, where data
is stored in a grid (rows and columns).

Synta Exampl
x: e:

dataType arrayName[rows] int matrix[2][3] = {{1, 2, 3}, {4,


[columns]; 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.

Synta Exampl
x: e:

dataType arrayName[rows]
[columns] = {{value1, value2}, int matrix[2][2] = {{1, 2},
{value3, value4}}; {3, 4}};

Explanation: You can initialize values directly when declaring


a 2D array.
REFERRENC
ES
URL :-
https://www.geeksforgeeks.org/c-programming-language/
Book :- Programming in ANSI C [2nd
edition]
By E. Balagurusamy
THANK
YOU
ANY
QUESTIONS ?

You might also like