sharing on basic knowledge on fundamental programming which is using programming c as a platform
Size: 1.65 MB
Language: en
Added: Mar 11, 2025
Slides: 47 pages
Slide Content
Fundamentals of C Language (1 hour) CHAPTER 2 1
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 2
2.1 Remember the basic of the C Program 2.1.1 Describe the ANSI C 2.1.2 Describe the Variables in C program 2.1.3 Describe the Constants 2.1.4 Identify the Data Types 2.1.5 Identify the #include Directive 2.1.6 Identify the main() Function 2.1.7 Identify the char Data Type 2.1.8 Identify the int Data Type 2.1.9 Identify the float Data Type 2.1.10 Identify the double Data Type 2.1.11 Recognize keywords in C 2.1.12 Identify the Variable name in C 2.1.13 Describe the declaring variables 2.2 Understand the structure of C Programs 2.2.1 Explain the input/output statement 2.2.2 Explain the structure of C programs 2.2.3 Explain Common Programming Errors 2.2.4 Explain valid identifiers 2.2.5 Explain the following types of Operators: a. Assignment operators b. Mathematical operators c. Unary Operators d. Increment Operators e. Decrement Operators 2.3 Apply the fundamentals of C Programming 2.3.1 Construct a simple C program 2.3.2 Compile and execute programs 2.3.3 Use input statements in C Program 2.3.4 Demonstrate output statements in simple C program 2.3.5 Show the output in the specified format 2.3.6 Implement calculations by using operators and expressions 2.3.7 Implement mathematical calculations in simple C program 2.3.8 Implement mathematical calculations using the function in the main function 2.3.9 Implement AI application converting flowchart to C program code 2.3.10 Demonstrate Top-Down Program development: a. Determine the Desired Output b. Determine the Input Items c. Determine an Algorithm d. Do a Hand Calculation e. Select Variable Names f. Write the Program g. Test the Output 3
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
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
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
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 7
Valid Identifiers 8
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
Variable Name in C Variable names must follow these rules: Must begin with a letter or underscore (_) and cannot begin with number. Cannot be a keyword. Must not contain spaces or special characters (except _). 10
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'; 11
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 12
Data Types 13
#include Directive 14
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; } 15
char Data Type The char data type stores single characters and occupies 1 byte of memory. Syntax: char <variable name>; Example: char initial = 'A’; 16
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
int Data Type signed short int int long short int unsigned short int unsigned int 18
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; 19
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. 20
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 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 Storage 32 bits 64 bits 21
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
Structure of C Programs 23
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 24
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 3 int 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 void main(), return statement is optional. 8 } right braces Marks the end of the main function block. 25
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: %d\n", value); Used for input (reads data from the keyboard). Syntax: scanf ("Format specifier", &variable); Example: int age; printf ("Enter your age: "); scanf ("%d", &age); 26
Common Programming Errors Syntax Errors Caused by incorrect code syntax (e.g., missing semicolons). Logical Errors Code runs without errors but produces incorrect results. Run-time Errors Occur while the program is executing (e.g., division by zero). Semantic Errors Code meaning doesn’t align with the intended logic. 27
1. Syntax Errors Missing semicolons at the end of statements Missing brackets ({ }) Misspelled keywords Incorrect case usage (C is case-sensitive) 28
3. Runtime Errors Division by zero Array index out of bounds Stack overflow Memory leaks Characteristics of Runtime Errors: Only occur when the program is running Can cause program crashes or unexpected termination May depend on specific input values or conditions Can be detected and handled using error handling mechanisms 30
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. 31
Error Type Detection Time Example Problem Outcome Syntax Error Compile-time Missing semicolon Compilation fails. Logical Error Run-time Incorrect operation Incorrect output. Runtime Error Run-time Division by zero Program crashes or terminates. Semantic Error Run-time Misuse of formula Output does not match the intention. 32
Types of Operator s Operators can be classified according to the type of their operands and of their output. These are: Assignment operators Mathematical operators ( a.k.a Arithmetic operators) Unary Operators Increment operators Decrement Operators Relational operators (chapter 3) Logical operators (chapter 3) Boolean(Bitwise) Operators 33
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 34
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 * Multiplication 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 another z = x % y 35
c) Unary Operators Operate on a single operand. Example: int x = -10; // unary minus Operator Description Example + Unary plus +a - Unary minus -a ++ Increment ++a , a++ -- Decrement --a , a-- ! Logical NOT !a ~ Bitwise NOT ~a 36
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 37
Apply the Fundamentals of C Programming 38
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. 39
Compile and execute programs Step 3: Compile the program. Step 3: Run the program and observe the output. 40
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. 41
Apply output statements in simple C program 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. } 42
Display the output in the specified format 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. } 43
Calculations by Using Operators and Expressions 44
Mathematical Calculations in a Simple C Program 45
Mathematical Calculations Using Functions in the Main Function 46
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: %d\n", sum); return 0; } 47