0% found this document useful (0 votes)
5 views35 pages

C Programming LU1

This document provides an introduction to programming in C, covering key concepts such as program definition, compilation, linking, and the structure of C code. It explains the importance of object code, the role of functions, constants, and the use of header files for code reusability. Additionally, it outlines the basic syntax, data types, and the significance of constants in programming.

Uploaded by

Kumbu Mwanganya
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)
5 views35 pages

C Programming LU1

This document provides an introduction to programming in C, covering key concepts such as program definition, compilation, linking, and the structure of C code. It explains the importance of object code, the role of functions, constants, and the use of header files for code reusability. Additionally, it outlines the basic syntax, data types, and the significance of constants in programming.

Uploaded by

Kumbu Mwanganya
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/ 35

STRUCTURED

PROGRAMMIN
G IN C
BICT1201
INTRODUCTION TO
PROGRAMMING
What is a program?
Program Definition
 Program: Set of orders (instructions or sentences) written in a
programming language that are provided to the computer to
develop a task.
 High-level programming language: – Source code must be
converted into machine code
 C has two compilation steps:
• Compilation
• Linking
Object Code
 Object code is in a binary format that the computer's CPU can
execute. It consists of machine language instructions that
correspond to the high-level C code.

 When a C source file (with a .c extension) is compiled, the


compiler produces object code, typically stored in files with a .o
or .obj extension.

 Object code is not a complete, executable program. It often


requires linking with other object code files and libraries to
create a final executable.
Compilation and Linking
 Compilation is the process of  Linking is the process of
translating the high-level C combining multiple object
source code into machine files and libraries into a
code (object code) that the single executable file.
computer's processor can  Takes one or more object
execute. files generated by the
 Detects syntax and semantic compiler and resolves
errors in the code. references between them
Compilation and Linking
Interpreters
 A tool or program that directly executes C code line by line
without requiring it to be compiled into machine code beforehand.
 Interpreters process and execute code sequentially, which allows
for real-time debugging and testing.
 Errors are detected immediately upon encountering problematic
lines, making debugging easier.
BASIC PROGRAMMING STRUCTURE
Elements of a program
Basic C code
#include <stdio.h>
int main() {
printf("Hello, I am an IT student\n");
return 0;
}
C Alphabet
 A programming language is characterized by:
Alphabet
Allowed characters
Lexicon
Words
Syntax
Rules for word combination to make meaningful
programs
C Alphabet
 Symbols that can appear in a C program
 Letters All but ‘ñ’ and accents (only in comments!)
 Numbers and special characters
 C is case sensitive: uppercase and lowercase letters are
different. Keywords are written in lowercase
C Lexicon
 The lexicon includes the primitive elements to build sentences
 Keywords
 Terms with a specific meaning and are lowercase (include, define,
main, if, etc.)
 Delimiters
 Blank spaces, tabs, line breaks
 Operators
 Represent operations: arithmetic, logic, assignment, etc. (+, -,*, etc.)
C Lexicon
 Identifiers
 Keywords cannot be used as identifiers
 Variable names (user_age) – cannot start with a number
 Function names (printf, scanf)

 Literals/ Constants
 Values that do not change: Numbers: 2, 3.14159, Strings: "Hello
world“, and Characters: 'a'
C syntax
 Data
 Values processed by the program
 Expressions
 Combination of operands and operators with a single value as a result
May include function calls, even though they do not return a value
user_age >= 18
3.14159*radius*radius
C Syntax
 Statements/Instructions/Statements - Complete action
 area=3.14159*radius*radius;
 printf("Hello world");
 int a;
 Blocks or compound statements
 Group of statements
 Braces { }
 The statements of the main function are enclosed in a block
File Inclusion
 C encourages the use of previous code. New functions can be
created and reused C provides functions in libraries that can be
used in our program.
 Input and output functions in stdio.h printf() and scanf().
 To include a file, use the directive #include with the name of the file.
#include "file.h" Searches in the current folder
#include <file.h> Searches in the default compiler folder
#include Files
 #include files are directives that instruct the preprocessor to
include the contents of a specified file into the program before
compilation.
 This is commonly used to incorporate libraries or header files that
contain function declarations, macros, and other definitions.
Types of #include Directives
 There are two types of headers in C, these are;
 Standard Library headers
 These are predefined headers provided by the C standard library. They
typically contain function prototypes and macros for various functionalities.
 User-defined headers
 The user creates their own header files to encapsulate function declarations,
constants, and macros specific to their programs.
Examples of #include Directives
 Standard Library Headers
 #include <stdio.h> // For input/output functions
 #include <math.h> // For mathematical functions

 User-defined headers
 // myheader.h
void myFunction();
 It is included in C as #include "myheader.h"
Advantages of using #include
 Code Reusability
 You can reuse code in multiple programs by including header files.
 Modularity
 Helps in organizing code into separate files, making it easier to
manage and maintain.
 Access to Library Functions
 Provides access to a wide range of standard library functions and
features.
Functions in C
 The basic building block in C is the function
 A C program is a collection of functions.
 A function is a piece of code that performs a task when it is
called/invoked
 Functions include;
 Variable declaration (for storing data)
 Statements (for performing operations)
The Main Function
 All C programs have a main function which is the starting point of
the program.
 Automatically started when the program is run.
 The simplest C program:
int main(void) {}
return is optional, but recommended
Function to add two numbers
 #include <stdio.h> scanf("%d", &num2);
// Function declaration // Calling the add function
int add(int a, int b); // Function that sum = add(num1, num2);
returns the sum of two integers
// Display the result
int main() {
printf("Sum of %d and %d is: %d\n",
int num1, num2, sum; num1, num2, sum);
// User input return 0; // Successful termination
printf("Enter first number: "); }
scanf("%d", &num1); // Function definition
printf("Enter second number: "); int add(int a, int b) {
return a + b; // Returning the sum of
a and b
}
DISCUSSION
What are comments in
programming?
Constants
 Constants are fixed values that do not change during the
execution of a program.
 They can be used to represent values that should remain the same
throughout your code, enhancing readability and maintainability
 There are several types of Constants as outlined below.
 Integer Constants which are whole numbers without any decimal
points.
 Examples: 10, -5, 0
Constants
 Floating-point Constants, which are numbers that include a
decimal point.
Examples: 3.14, -0.001, 2.0
 Character Constants - are enclosed in single quotes.
Examples: 'a', 'Z', '#'
 String Constants are sequences of characters enclosed in double-
quotes.
 Examples: "Hello", "C programming"
Constants
 Enumeration Constants are defined using the enum keyword,
which assigns names to integral values.
 Example: enum Color { RED, GREEN, BLUE };
Advantages of Constants
 Readability
 Named constants (like PI) make the code more understandable
compared to using raw values.
 Maintainability
 If a constant value needs to change, you only need to update it in
one place.
 Preventing Errors
 Using constants can help prevent accidental changes to values that
should remain fixed.
How to Use Constants
 Using const Keyword
 You can also declare constants using the const keyword, which
indicates that the value cannot be modified after initialization.
 Example : const int MAX_USERS = 100;
Constants in a C code
#include <stdio.h>
#define PI 3.14 // Defined constant
int main() {
const int radius = 5; // Constant variable
float area = PI * radius * radius;

printf("Area of the circle: %.2f\n", area);


return 0;
}
Constants in a C code
#include <stdio.h>
#define MAX_USERS 40
int main() {
int currentUsers = 0;
// Check if adding a new user exceeds the maximum limit
if (currentUsers < MAX_USERS) {
// Add user
}
return 0;
}
Reading Task

Familiarize yourselves with the following

- Basic data types


- Modifying data types
THE END
QUESTIONS??

You might also like