際際滷

際際滷Share a Scribd company logo
1
Fundamentals of C
Language
(1 hour)
CHAPTER 2
2
Course Learning Outcomes
Apply knowledge of basic concepts and
fundamentals of structured programming in
solving a variety of engineering and scientific
problems using a high level C programming
language. (C3, PLO1)
Build programs written in C language by using
C standard revision.(P4, PLO5)
CLO 1
CLO 2
4
Fundamentals of C
Language
 C is a widely-used programming language known for
its simplicity, efficiency, and versatility. It serves as
the foundation for many modern programming
languages and is commonly used for system
programming, embedded systems, and application
development.
5
Basics of the C
Program
 A C program typically consists of
functions and statements that
are executed in a structured
manner.
 To understand the C language, it is
important to grasp the following
fundamental concepts:
6
ANSI C
ANSI C refers to the standardised version of the C
programming language developed by the American
National Standards Institute (ANSI). The standard
ensures consistency and portability across different
platforms. ANSI C includes:
 Standardised syntax and functions.
 Libraries such as <stdio.h> and <stdlib.h>.
7
Identifiers
 Identifiers are names given to variables, constant,
functions, arrays, classes, etc. in a C program. They
help in identifying and referring to program elements.
  Valid: studentName, _count, total_sum, firstName2
  Invalid: 2total, my-name, void, #price
8
Valid Identifiers
9
Variables in C Program
 A variable is a named area of storage that can
hold a single value (numeric or character).
 Declaring a variable involves specifying its type
and name before using it.
 Syntax:
data_type variable_name;
11
Variables in C Program
Variable Declaration Variable Initialization
examples:
int age;
float temperature;
char grade;
examples:
int age = 25;
float temperature =
98.6;
char grade = 'A';
12
Constants
 Constants are fixed values that cannot be
changed during program execution.
 They are defined using the const keyword or
#define preprocessor directive.
 Example:
 const float PI = 3.14;
 #define MAX_VALUE 100
13
Data
Types
C provides several data types to
handle different kinds of data:
char: Stores single characters.
int: Stores whole numbers.
float: Stores decimal numbers.
double: Stores double-precision
decimal numbers.
14
#include Directive
The #include directive is a
preprocessor command used to include
header files in a program.
Example:
#include <stdio.h> // Includes standard input/output
library
15
main() Function
 The main() function is the entry point of any C
program. Execution starts and ends within this
function.
 Example:
int main()
{
printf("Hello, World!n");
return 0;
}
16
char Data Type
 The char data type stores single characters and occupies 1 byte of
memory.
 Syntax:
char <variable name>;
 Example:
char initial = 'A;
17
int Data Type
 C provides several standard integer types, from small magnitude to large
magnitude numbers: short int, int, long int, long long int.
 Each type can be signed or unsigned.
 Signed int represent positive and negative numbers
 unsigned int can represent zero and positive numbers.
 Syntax:
 int <variable name>;
 Example:
 int num1;
 short int num2;
 long int num3;
18
int Data Type
Integer
types
signed
short int
int
long int
unsigned
short int
int
long int
signed short int
int
long short int
unsigned short int
unsigned int
19
float Data Type
The float data type is used to store fractional numbers
(real numbers) with 6 digits of precision.
Syntax:
float <variable name>;
Example:
 float temperature = 36.6;
20
double Data Type
 The double data type is used for double-precision decimal numbers,
providing more accuracy than float.
 Example:
double pi = 3.141592653589;
 To extend the precision further we can use long double which occupies
10 bytes of memory space.
21
Float vs double
Both for floating-point number
Feature
float (Single-
Precision)
double (Double-Precision)
Precision
~6 to 7 significant
decimal digits
~15 to 16 significant
decimal digits
Size (bytes) Typically 4 bytes Typically 8 bytes
Range to to
Storage 32 bits 64 bits
22
Keywords in C
 Keywords are reserved words that have special meanings in C.
 There are 32 keywords in C language. All of them are listed in the table
below:
auto do if struct
break else long sizeof
char enum register typedef
case extern return unsigned
const goto short union
continue float signed void
default for switch volatile
double int static while
Notes:
A keyword name can not be used as a variable name.
Keywords must be written in lower case.
23
Structure of C Programs
24
Structure of C
Programs
 In a programming language, the rules are known as the
syntax. If these rules are not followed, a program will not
work.
 Each programming language has its own set of syntax and
structures to be followed. A typical C program will appear
as shown
25
example
line C program example structure explaination
1//example 1 comment/ documentation
Used to add notes or title to the
program. Comments are ignored by
the compiler.
2#include <stdio.h>
Preprocessor Directives &
header files
#include is preprocessor directives,
stdio.h is header file for standars
input output
3int main(void) Main function
The entry point of every C program.
Program execution begins here.
4 { left braces {
Marks the beginning of the main
function block.
5 int a = 10; variable declaration
Declares and initializes a local
variable within the function. If
declared outside function, it would
be a global variable.
6
printf("The number is %d",
a);
statement
Prints the value of variable 'a' using
printf function.
7 return 0; return
In main(), return 0 indicates
successful program execution. If
26
Input/Output Statement
Input and output operations in C are essential for user interaction.
printf() scanf()
 Used for output (displays data on
the screen).
 Syntax:
printf("Text only");
printf("Text or format specifier",
variables);
 Example:
printf("Hello, World!n");
printf("The value is: %dn", value);
 Used for input (reads data from the
keyboard).
 Syntax:
scanf("Format specifier", &variable);
 Example:
int age;
printf("Enter your age: ");
scanf("%d", &age);
27
Common Programming Errors
1. Syntax Errors
 Caused by incorrect code syntax (e.g., missing semicolons).
2. Logical Errors
 Code runs without errors but produces incorrect results.
3. Run-time Errors
 Occur while the program is executing (e.g., division by zero).
4. Semantic Errors
 Code meaning doesnt align with the intended logic.
28
1. Syntax
Errors
 Missing semicolons at the end of statements
 Missing brackets ({ })
 Misspelled keywords
 Incorrect case usage (C is case-sensitive)
29
2. Logical
Errors
 Incorrect operators (using + instead of *)
 Improper loop conditions
 Off-by-one errors in loops
 Incorrect precedence understanding
30
3. Runtime
Errors
 Division by zero
 Array index out of bounds
 Stack overflow
 Memory leaks
 Characteristics of Runtime Errors:
1. Only occur when the program is running
2. Can cause program crashes or
unexpected termination
3. May depend on specific input values or
conditions
4. Can be detected and handled using error
handling mechanisms
31
4. Semantic
Errors
 A semantic error in
programming occurs
when the code is
syntactically correct
(free of syntax errors)
but does not produce
the intended result
because it violates the
logical rules or
meaning of the
program.
33
Types of Operators
 Operators can be classified according to the type of their
operands and of their output. These are:
a) Assignment operators
b) Mathematical operators (a.k.a Arithmetic operators)
c) Unary Operators
d) Increment operators
e) Decrement Operators
f) Relational operators (chapter 3)
g) Logical operators (chapter 3)
h) Boolean(Bitwise) Operators
34
a) Assignment operators
 used to assign values to variables.
 They can combine arithmetic or
bitwise operations with
assignment.
 Example:
 int x = 10;
 x += 5; // equivalent to x = x + 5
35
b) Mathematical
operators
 Perform basic arithmetic operations:
 Addition (+), Subtraction (-),
Multiplication (*), Division (/), Modulo
(%)
Operator Operation Description Example
+ Addition
Adds two operands
together
z = x + y
- Subtraction
Subtracts the second
operand from the first
operand
z = x - y
*
Multiplicatio
n
Multiplies one operand
with another
z = x * y
/ Division
Divides the first
operand by the second
operand
z = x / y
% Modulo
Calculates the
remainder when one
operand is divided by
z = x % y
36
c) Unary Operators
 Operate on a single
operand.
 Example:
 int x = -10; // unary
minus
Operato
r
Descriptio
n
Example
+ Unary plus +a
-
Unary
minus
-a
++ Increment ++a, a++
-- Decrement --a, a--
! Logical NOT !a
~ ~a
37
d) Increment operators
 Increase a value by 1.
 Syntax: ++ (prefix or postfix).
 Example:
 int x = 5;
 x++; // postfix increment
 ++x; // prefix increment
e) Decrement Operators
 Decrease a value by 1.
 Syntax: -- (prefix or postfix).
 Example:
 int y = 10;
 y--; // postfix decrement
 --y; // prefix decrement
38
Apply the Fundamentals
of C Programming
39
Construct a Simple C Program
Step 1: Open C editor and types :
1. #include <stdio.h>
2. main()
3. {
4. printf(" WELCOME ")
5. }
Step 2: Save the program.
40
Compile and execute programs
 Step 3: Compile the program.
 Step 3: Run the program and observe the output.
41
Use input statements in C Program
 Step 1: Open C editor and types:
1. #include <stdio.h>
2. main()
3. {
4. int age;
5. scanf("%d",&age);
6. return 0;
7. }
 Step 2: Save the program.
 Step 3: Compile the program.
 Step 3: Run the program and observe the output.
42
Apply output statements in simple C
program
Step 3 Run the program and observe the output.
Step 3 Compile the program.
Step 2 Save the program.
Step 1 Open C editor and types:
1. #include <stdio.h>
2. main()
3. {
4. int age;
5. char grade;
6. float mark;
7. printf("n Enter the age : ");
8. scanf("%d",&age);
9. printf("n Enter the grade : ");
10. scanf("%c",&age);
11. printf("n Enter the mark : ");
12. scanf("%f",&mark);
13. return 0;
14. }
43
Display the output in the specified
format
Step 3 Run the program and observe the output.
Step 3 Compile the program.
Step 2 Save the program.
Step 1 Open C editor and types:
1. #include <stdio.h>
2. main()
3. {
4. int age;
5. char grade;
6. float mark;
7. printf("n Enter the age : ");
8. scanf("%d",&age);
9. printf("n Enter the grade : ");
10. scanf("%c",&age);
11. printf("n Enter the mark : ");
12. scanf("%f",&mark);
13. printf("n The age :%d ",age);
14. printf("n The grade :%c ",grade);
15. printf("n The mark :%f ",mark);
16. return 0;
17. }
44
Calculations
by Using
Operators and
Expressions
Operators such as +, -, *, /,
and % are used for
calculations.
Example:
#include <stdio.h>
{
int a = 10, b = 5;
int sum = a + b;
}
45
Mathematical
Calculations
in a Simple C
Program
Complex calculations can involve
multiple operators and
parentheses for precedence.
Example:
#include <stdio.h>
{
int a = 10, b = 5;
int result = (a + b) * (a - b);
printf("Result: %dn", result);
}
46
Mathematical
Calculations
Using Functions
in the Main
Function
Functions help modularise
calculations.
#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int main() {
int result = add(10, 20);
printf("Sum: %dn", result);
return 0;
}
Example:
47
Implement AI Application Converting
Flowchart to C Program Code
 Translate flowchart symbols
into code:
 Start/End: Corresponds to
the main() function.
 Processes: Translate to
arithmetic or logical
statements.
 Decisions: Use if-else or
switch statements.
Example:
 Flowchart Task: Find the sum of two numbers.
 Code:
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %dn", sum);
return 0;
}
48
Implement AI Application Converting
Flowchart to C Program Code
 Webpage and apps for flowchart to code and vice
versa, example:
 https://app.code2flow.com/
 draw.io ( https://app.diagrams.net )
 ChatGPT
 Claude.ai
 others
49
Top-Down Program Development
1. Determine the Desired Output
 Clearly define what the program should produce.
2. Determine the Input Items
 Identify all required inputs.
3. Determine an Algorithm
 Create step-by-step logic to solve the problem.
4. Do a Hand Calculation
 Verify the algorithm with example data.
5. Select Variable Names
 Use meaningful names for readability.
6. Write the Program
 Convert the algorithm into C code.
7. Test the Output
 Run and validate the program with different test cases.
50
Example of Top-Down Approach:
Task: Calculate the area of a rectangle.
 Desired Output: Area of the rectangle.
 Input Items: Length (l) and width (w).
 Algorithm:
 Input l and w.
 Calculate area = l * w.
 Display the area.
 Code:
#include <stdio.h>
int main() {
float length, width, area;
printf("Enter length and width: ");
scanf("%f %f", &length, &width);
area = length * width;
printf("Area: %.2fn", area);
return 0;
}

More Related Content

Similar to Fundamental programming Nota Topic 2.pptx (20)

PPT
C programming
Harshit Varshney
PPTX
C for Engineers
Julie Iskander
PPTX
C programming language
Abin Rimal
PPT
5 introduction-to-c
Rohit Shrivastava
PPTX
C programming
AsifRahaman16
PDF
Basic Information About C language PDF
Suraj Das
DOC
1. introduction to computer
Shankar Gangaju
PPTX
Programming Fundamentals
umar78600
PPTX
C introduction by thooyavan
Thooyavan Venkatachalam
PDF
EC2311-Data Structures and C Programming
Padma Priya
PDF
Fundamentals of c language
AkshhayPatel
PDF
Introduction to C programming
Kathmandu University
PPTX
Cpu
Mohit Jain
PPT
C material
tarique472
PPT
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
PPTX
C language
Priya698357
PPTX
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
PDF
88 c-programs
Leandro Schenone
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1
C programming
Harshit Varshney
C for Engineers
Julie Iskander
C programming language
Abin Rimal
5 introduction-to-c
Rohit Shrivastava
C programming
AsifRahaman16
Basic Information About C language PDF
Suraj Das
1. introduction to computer
Shankar Gangaju
Programming Fundamentals
umar78600
C introduction by thooyavan
Thooyavan Venkatachalam
EC2311-Data Structures and C Programming
Padma Priya
Fundamentals of c language
AkshhayPatel
Introduction to C programming
Kathmandu University
C material
tarique472
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
C language
Priya698357
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
88 c-programs
Leandro Schenone
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1

Recently uploaded (20)

PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
PPTX
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
PDF
I3PM Case study smart parking 2025 with uptoIP速 and ABP
MIPLM
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
I3PM Case study smart parking 2025 with uptoIP速 and ABP
MIPLM
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
Ad

Fundamental programming Nota Topic 2.pptx

  • 2. 2 Course Learning Outcomes Apply knowledge of basic concepts and fundamentals of structured programming in solving a variety of engineering and scientific problems using a high level C programming language. (C3, PLO1) Build programs written in C language by using C standard revision.(P4, PLO5) CLO 1 CLO 2
  • 3. 4 Fundamentals of C Language C is a widely-used programming language known for its simplicity, efficiency, and versatility. It serves as the foundation for many modern programming languages and is commonly used for system programming, embedded systems, and application development.
  • 4. 5 Basics of the C Program A C program typically consists of functions and statements that are executed in a structured manner. To understand the C language, it is important to grasp the following fundamental concepts:
  • 5. 6 ANSI C ANSI C refers to the standardised version of the C programming language developed by the American National Standards Institute (ANSI). The standard ensures consistency and portability across different platforms. ANSI C includes: Standardised syntax and functions. Libraries such as <stdio.h> and <stdlib.h>.
  • 6. 7 Identifiers Identifiers are names given to variables, constant, functions, arrays, classes, etc. in a C program. They help in identifying and referring to program elements. Valid: studentName, _count, total_sum, firstName2 Invalid: 2total, my-name, void, #price
  • 8. 9 Variables in C Program A variable is a named area of storage that can hold a single value (numeric or character). Declaring a variable involves specifying its type and name before using it. Syntax: data_type variable_name;
  • 9. 11 Variables in C Program Variable Declaration Variable Initialization examples: int age; float temperature; char grade; examples: int age = 25; float temperature = 98.6; char grade = 'A';
  • 10. 12 Constants Constants are fixed values that cannot be changed during program execution. They are defined using the const keyword or #define preprocessor directive. Example: const float PI = 3.14; #define MAX_VALUE 100
  • 11. 13 Data Types C provides several data types to handle different kinds of data: char: Stores single characters. int: Stores whole numbers. float: Stores decimal numbers. double: Stores double-precision decimal numbers.
  • 12. 14 #include Directive The #include directive is a preprocessor command used to include header files in a program. Example: #include <stdio.h> // Includes standard input/output library
  • 13. 15 main() Function The main() function is the entry point of any C program. Execution starts and ends within this function. Example: int main() { printf("Hello, World!n"); return 0; }
  • 14. 16 char Data Type The char data type stores single characters and occupies 1 byte of memory. Syntax: char <variable name>; Example: char initial = 'A;
  • 15. 17 int Data Type C provides several standard integer types, from small magnitude to large magnitude numbers: short int, int, long int, long long int. Each type can be signed or unsigned. Signed int represent positive and negative numbers unsigned int can represent zero and positive numbers. Syntax: int <variable name>; Example: int num1; short int num2; long int num3;
  • 16. 18 int Data Type Integer types signed short int int long int unsigned short int int long int signed short int int long short int unsigned short int unsigned int
  • 17. 19 float Data Type The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Syntax: float <variable name>; Example: float temperature = 36.6;
  • 18. 20 double Data Type The double data type is used for double-precision decimal numbers, providing more accuracy than float. Example: double pi = 3.141592653589; To extend the precision further we can use long double which occupies 10 bytes of memory space.
  • 19. 21 Float vs double Both for floating-point number Feature float (Single- Precision) double (Double-Precision) Precision ~6 to 7 significant decimal digits ~15 to 16 significant decimal digits Size (bytes) Typically 4 bytes Typically 8 bytes Range to to Storage 32 bits 64 bits
  • 20. 22 Keywords in C Keywords are reserved words that have special meanings in C. There are 32 keywords in C language. All of them are listed in the table below: auto do if struct break else long sizeof char enum register typedef case extern return unsigned const goto short union continue float signed void default for switch volatile double int static while Notes: A keyword name can not be used as a variable name. Keywords must be written in lower case.
  • 21. 23 Structure of C Programs
  • 22. 24 Structure of C Programs In a programming language, the rules are known as the syntax. If these rules are not followed, a program will not work. Each programming language has its own set of syntax and structures to be followed. A typical C program will appear as shown
  • 23. 25 example line C program example structure explaination 1//example 1 comment/ documentation Used to add notes or title to the program. Comments are ignored by the compiler. 2#include <stdio.h> Preprocessor Directives & header files #include is preprocessor directives, stdio.h is header file for standars input output 3int main(void) Main function The entry point of every C program. Program execution begins here. 4 { left braces { Marks the beginning of the main function block. 5 int a = 10; variable declaration Declares and initializes a local variable within the function. If declared outside function, it would be a global variable. 6 printf("The number is %d", a); statement Prints the value of variable 'a' using printf function. 7 return 0; return In main(), return 0 indicates successful program execution. If
  • 24. 26 Input/Output Statement Input and output operations in C are essential for user interaction. printf() scanf() Used for output (displays data on the screen). Syntax: printf("Text only"); printf("Text or format specifier", variables); Example: printf("Hello, World!n"); printf("The value is: %dn", value); Used for input (reads data from the keyboard). Syntax: scanf("Format specifier", &variable); Example: int age; printf("Enter your age: "); scanf("%d", &age);
  • 25. 27 Common Programming Errors 1. Syntax Errors Caused by incorrect code syntax (e.g., missing semicolons). 2. Logical Errors Code runs without errors but produces incorrect results. 3. Run-time Errors Occur while the program is executing (e.g., division by zero). 4. Semantic Errors Code meaning doesnt align with the intended logic.
  • 26. 28 1. Syntax Errors Missing semicolons at the end of statements Missing brackets ({ }) Misspelled keywords Incorrect case usage (C is case-sensitive)
  • 27. 29 2. Logical Errors Incorrect operators (using + instead of *) Improper loop conditions Off-by-one errors in loops Incorrect precedence understanding
  • 28. 30 3. Runtime Errors Division by zero Array index out of bounds Stack overflow Memory leaks Characteristics of Runtime Errors: 1. Only occur when the program is running 2. Can cause program crashes or unexpected termination 3. May depend on specific input values or conditions 4. Can be detected and handled using error handling mechanisms
  • 29. 31 4. Semantic Errors A semantic error in programming occurs when the code is syntactically correct (free of syntax errors) but does not produce the intended result because it violates the logical rules or meaning of the program.
  • 30. 33 Types of Operators Operators can be classified according to the type of their operands and of their output. These are: a) Assignment operators b) Mathematical operators (a.k.a Arithmetic operators) c) Unary Operators d) Increment operators e) Decrement Operators f) Relational operators (chapter 3) g) Logical operators (chapter 3) h) Boolean(Bitwise) Operators
  • 31. 34 a) Assignment operators used to assign values to variables. They can combine arithmetic or bitwise operations with assignment. Example: int x = 10; x += 5; // equivalent to x = x + 5
  • 32. 35 b) Mathematical operators Perform basic arithmetic operations: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%) Operator Operation Description Example + Addition Adds two operands together z = x + y - Subtraction Subtracts the second operand from the first operand z = x - y * Multiplicatio n Multiplies one operand with another z = x * y / Division Divides the first operand by the second operand z = x / y % Modulo Calculates the remainder when one operand is divided by z = x % y
  • 33. 36 c) Unary Operators Operate on a single operand. Example: int x = -10; // unary minus Operato r Descriptio n Example + Unary plus +a - Unary minus -a ++ Increment ++a, a++ -- Decrement --a, a-- ! Logical NOT !a ~ ~a
  • 34. 37 d) Increment operators Increase a value by 1. Syntax: ++ (prefix or postfix). Example: int x = 5; x++; // postfix increment ++x; // prefix increment e) Decrement Operators Decrease a value by 1. Syntax: -- (prefix or postfix). Example: int y = 10; y--; // postfix decrement --y; // prefix decrement
  • 36. 39 Construct a Simple C Program Step 1: Open C editor and types : 1. #include <stdio.h> 2. main() 3. { 4. printf(" WELCOME ") 5. } Step 2: Save the program.
  • 37. 40 Compile and execute programs Step 3: Compile the program. Step 3: Run the program and observe the output.
  • 38. 41 Use input statements in C Program Step 1: Open C editor and types: 1. #include <stdio.h> 2. main() 3. { 4. int age; 5. scanf("%d",&age); 6. return 0; 7. } Step 2: Save the program. Step 3: Compile the program. Step 3: Run the program and observe the output.
  • 39. 42 Apply output statements in simple C program Step 3 Run the program and observe the output. Step 3 Compile the program. Step 2 Save the program. Step 1 Open C editor and types: 1. #include <stdio.h> 2. main() 3. { 4. int age; 5. char grade; 6. float mark; 7. printf("n Enter the age : "); 8. scanf("%d",&age); 9. printf("n Enter the grade : "); 10. scanf("%c",&age); 11. printf("n Enter the mark : "); 12. scanf("%f",&mark); 13. return 0; 14. }
  • 40. 43 Display the output in the specified format Step 3 Run the program and observe the output. Step 3 Compile the program. Step 2 Save the program. Step 1 Open C editor and types: 1. #include <stdio.h> 2. main() 3. { 4. int age; 5. char grade; 6. float mark; 7. printf("n Enter the age : "); 8. scanf("%d",&age); 9. printf("n Enter the grade : "); 10. scanf("%c",&age); 11. printf("n Enter the mark : "); 12. scanf("%f",&mark); 13. printf("n The age :%d ",age); 14. printf("n The grade :%c ",grade); 15. printf("n The mark :%f ",mark); 16. return 0; 17. }
  • 41. 44 Calculations by Using Operators and Expressions Operators such as +, -, *, /, and % are used for calculations. Example: #include <stdio.h> { int a = 10, b = 5; int sum = a + b; }
  • 42. 45 Mathematical Calculations in a Simple C Program Complex calculations can involve multiple operators and parentheses for precedence. Example: #include <stdio.h> { int a = 10, b = 5; int result = (a + b) * (a - b); printf("Result: %dn", result); }
  • 43. 46 Mathematical Calculations Using Functions in the Main Function Functions help modularise calculations. #include <stdio.h> int add(int x, int y) { return x + y; } int main() { int result = add(10, 20); printf("Sum: %dn", result); return 0; } Example:
  • 44. 47 Implement AI Application Converting Flowchart to C Program Code Translate flowchart symbols into code: Start/End: Corresponds to the main() function. Processes: Translate to arithmetic or logical statements. Decisions: Use if-else or switch statements. Example: Flowchart Task: Find the sum of two numbers. Code: #include <stdio.h> int main() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = a + b; printf("Sum: %dn", sum); return 0; }
  • 45. 48 Implement AI Application Converting Flowchart to C Program Code Webpage and apps for flowchart to code and vice versa, example: https://app.code2flow.com/ draw.io ( https://app.diagrams.net ) ChatGPT Claude.ai others
  • 46. 49 Top-Down Program Development 1. Determine the Desired Output Clearly define what the program should produce. 2. Determine the Input Items Identify all required inputs. 3. Determine an Algorithm Create step-by-step logic to solve the problem. 4. Do a Hand Calculation Verify the algorithm with example data. 5. Select Variable Names Use meaningful names for readability. 6. Write the Program Convert the algorithm into C code. 7. Test the Output Run and validate the program with different test cases.
  • 47. 50 Example of Top-Down Approach: Task: Calculate the area of a rectangle. Desired Output: Area of the rectangle. Input Items: Length (l) and width (w). Algorithm: Input l and w. Calculate area = l * w. Display the area. Code: #include <stdio.h> int main() { float length, width, area; printf("Enter length and width: "); scanf("%f %f", &length, &width); area = length * width; printf("Area: %.2fn", area); return 0; }

Editor's Notes

  • #28: //syntax errors #include <stdio.h> int main() { printf("Hello, World!\n") // Missing semicolon at the end of the line. return 0; }
  • #29: //logical errors #include <stdio.h> int main() { int a = 5, b = 10; int sum = a - b; // Logical error: Used subtraction instead of addition. printf("The sum is: %d\n", sum); return 0; }
  • #30: //Runtime error #include <stdio.h> int main() { int a = 10, b = 0; int result = a / b; // Runtime error: Division by zero. printf("Result: %d\n", result); return 0; }
  • #31: //Semantic Errors #include <stdio.h> int main() { int radius = 5; float area = 2 * 3.14 * radius; // Semantic error: Formula for area is incorrect. printf("Area of the circle: %.2f\n", area); return 0; }