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;
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.
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)
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. }
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;
}