C Programming LU1
C Programming LU1
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.
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;