[go: up one dir, main page]

0% found this document useful (0 votes)
17 views21 pages

Unit3 (Introduction To C Language) Lesson1

data structure

Uploaded by

tuyisengeeric034
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)
17 views21 pages

Unit3 (Introduction To C Language) Lesson1

data structure

Uploaded by

tuyisengeeric034
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/ 21

Course: Introduction To Programming With

C Language

Lecturer : Shimirwa Aline Valerie


Email: a.shimirwa@ines.ac.rw
Tel: 0784931189
Unit 3: The Art Of Computer Programming What is the difference between a program and a
software?
What is a program and why do we need programs? Program and software are often used
interchangeably, but they do have slightly different
 A program: Program is a set of instructions that tells
meanings.
a computer what operations to perform. Programs are  A program is a set of instructions that a computer
written in a specific programming language and are follows to perform a specific task or function. It is a
then compiled or interpreted into machine-readable single executable file that can be run on a computer
code that can be executed by a computer. system to perform a specific operation. For example, a
program might be a calculator application or a web
 Programs are needed because computers cannot browser.
perform tasks on their own without specific  Software, on the other hand, refers to a collection of
instructions. They are designed to follow a set of programs, data, and other resources that are designed to
instructions to perform a specific task, and programs work together to provide a specific set of functions or
provide these instructions. services. For example, an operating system such as
Windows or macOS is software that includes multiple
programs and resources designed to work together to
manage computer hardware, run applications, and
provide various system services.
Basic Principles of
Programming
2. Maintainability: By this, we mean that you should be able
 As we have already seen previously, a computer is
to modify the program when the need arises.
designed to solve problems by following the
instructions given. However, you should know that a 3. Portability: The concept of portability in programming is
Computers do not solve problems the way we do. that a program should be capable of being transferable to
Human beings make use of reasoning; intelligence different computer platforms with a minimum modification, if
and intuition to solve problems while computers any at all.
solve problems according to instructions supplied by 4. Readability: A program should be easy for other
programmers. programmers to read and understand. The code should be
 Therefore, as a programmer There are several basic documented.
principles of programming that are essential to 5. Performance: A program that doesn't carry out the expected
understand in order to write high-quality and tasks quickly and efficiently has lost the performance aim.
efficient code. These principles include: Therefore, a major aim in program design is that the program
should execute quickly and efficiently too.
1. Reliability: By reliability, we mean that you
should be able to depend on the program to 6. Memory Saving: What is meant here is simply that a
always do what it has been designed to do. program should not be unnecessarily too long and requiring
excessive large memory to execute.
Basic Principles of
Programming cont…
involve breaking down complex problems into smaller,
7. DRY (Don't Repeat Yourself): The DRY manageable In summary, the basic principles of
principle involves avoiding duplication of code or programming parts, encapsulating and hiding the details
logic in order to reduce errors, simplify of a function, avoiding duplication of code, keeping
maintenance, and improve efficiency. code simple and straightforward, and avoiding
8. KISS (Keep It Simple, Stupid): The KISS unnecessary complexity or features. These principles
principle involves keeping code as simple and help to improve the quality, readability, efficiency, and
straightforward as possible in order to improve maintainability of code.
readability, reduce errors, and improve
maintainability.
9. YAGNI (You Aren't Gonna Need It): The
YAGNI principle involves avoiding unnecessary
complexity or features in the code. This helps to
keep the code simple, reduce errors, and improve
efficiency.
The Different Stages Involved
In Program Development
4. Testing the program: This involves running the program
 Having gone through the underlying principles for
and testing it for bugs or errors, ensuring that it produces
creating good programs, let’s see now the different
stages involved in program development: the expected results and meets the requirements
identified in the analysis phase.
1. Problem Definition: This involves identifying
the problem that the program is intended to 5. Debugging and refining the program: This involves fixing
solve, understanding the requirements, and any errors or bugs found during testing, and making any
determining the inputs and outputs of the
program.
necessary changes to the program to improve its
performance, efficiency, or usability.
2. Designing the program: This involves creating
a plan or blueprint for the program, including 6. Deploying and maintaining the program: This involves
the overall structure, the algorithms and data releasing the program to users or clients, providing
structures needed, and the user interface ongoing support and maintenance, and making updates
design.
or improvements as needed based on feedback and
3. Coding the program: This involves writing the changing requirements.
code for the program based on the design,
using a specific programming language and
follow the best practices for coding style and
documentation.
Unit 4: Introduction to C
language Assembly Language: Assembly language is the next
level up from machine language. Assembly language
Low-Level Vs High-Level languages uses mnemonics, or symbolic codes, to represent
 Human beings communicate using natural languages machine language instructions. Each mnemonic
such as Kinyarwanda, English, etc. When we want to corresponds to a specific machine language instruction.
communicate with computers there are languages we
should use. These language are classified into different
levels ( low-level languages and High level language).
1. Low-level languages:
 Machine Language: Machine language is the
lowest level of programming language that can be
directly executed by a computer's hardware.
Machine language consists of binary code, which is 2. High-Level Languages:
a sequence of 0s and 1s that represent specific  High-level languages are programming languages
instructions. that are more abstract and closer to human language
than low-level languages. These languages include
languages like C, C++, Python, Java, Ruby,
JavaScript, etc.
Programming Language
Translators
3. Compiler: A compiler is a program that converts
 Computer only understand binary numbers or
source code written in a high-level programming
machine code. Therefore, the programs written in language into machine code that can be executed by a
other programming languages need to be computer.
translated into machine code that computers can
execute. There are three main types of
programming language translators:
1. Assembler: An assembler is a program that Difference between compiler and interpreter
converts assembly language code into machine Compiler Interpreter
code.
The compiler scans the Translates the program one
2. Interpreter: An interpreter is a program that
translates each line of code into machine code on whole program in one go. statement at a time.
the fly as it is executed. Interpreters are often As it scans the code in one Considering it scans code
used for scripting languages, such as Python, go, the errors (if any) are one line at a time, errors are
JavaScript, and Ruby. shown at the end together shown line by line.
It does not require source It requires source code for
code for later execution. later execution.
C, C++, C#, etc are Python, Ruby, JavaScript, etc
programming languages that are programming languages
are compiler-based. that are interpreter-based.
Brief History Of C
Language In 1999, the ISO (International Organization for
Standardization) released a new version of the C
 C is a general-purpose programming language that was
standard, known as "C99," which added new features
developed in the early 1970s by Dennis Ritchie at Bell
such as support for variable-length arrays, inline
Labs. It was created as a system programming language
to write the UNIX operating system, which was also functions, and more.
being developed at Bell Labs at that time. Today, C remains a widely used programming
 The language quickly gained popularity and became language, particularly in systems programming and
widely used in the computing industry for a variety of embedded systems. Its simplicity and low-level features
applications, including embedded systems, scientific make it a popular choice for high-performance
computing, game development, and more. computing applications and real-time systems. It has
 In 1983, the American National Standards Institute also influenced the development of many other
(ANSI) established a committee to develop a standard programming languages, including C++, Objective-C,
specification for the C language, which resulted in the and more.
publication of the first ANSI C standard in 1989. This
standard, known as "C89" or "ANSI C," provided a
common language specification that made it easier for
programmers to write portable code that could run on a
variety of platforms.
Features of C Language 5. Portability: C is a highly portable language that can be
compiled and executed on a wide range of platforms,
Some of the key features of C include: including embedded systems, desktop computers, and
servers.
1. Low-level memory manipulation: C provides
6. Powerful standard library: C includes a powerful
direct access to computer memory through standard library that provides a wide range of functions for
pointers, which allows for efficient memory string manipulation, input/output operations, and other
manipulation and direct control over hardware common tasks.
resources.
2. Efficient performance: C is a compiled
language that produces highly optimized
machine code, making it well-suited for
applications that require fast performance.
3. Modularity: C supports modular programming,
which allows developers to divide complex
programs into smaller, more manageable
modules that can be developed and tested
independently.
Structure of C Language 4. User defined Functions: These are additional
functions that are called from the main function
 A C program is structured into a number of different or from other functions. Function definitions
components that work together to define the program's consist of a function header and a function body.
behavior. The basic structure of a C program typically
includes the following components:

1. Preprocessor directives: These Preprocessor directives are


used to include header files and define macros( a macro is a
preprocessor directive that defines a shorthand notation
for a piece of code or a value.). Preprocessor directives
begin with the # symbol.
2. Global declarations: These are variable and function
declarations that are accessible to the entire program. Global
declarations are typically placed at the beginning of the
program before the main function.
3. Main function: The main function is the entry point for the
program and contains the code that is executed when the
program is run. The main function can be defined with the int
data type and return an integer value.
Structure of C Language
cont..
The figure in this slide shows a simple C program example
that illustrates the above components:
 In this example, the program begins with a preprocessor
directive that includes the standard input/output header
file (stdio.h).
 The program then defines a global variable,
global_variable, and a function prototype for
my_function().
 The main function declares a local variable,
local_variable, and then prints a message to the console
using the printf() function.
 Finally, the main function calls the my_function()
function, which is defined below the main function. The
program ends with a return statement that returns 0 to
indicate that the program executed successfully.
Structure of C Language
cont.. 5. math.h: This header file provides declarations for
mathematical functions such as pow(), sin(), cos(),
 Header files in C are files that contain built-in
and sqrt().
functions. These files typically have a .h extension
6. time.h: This header file provides declarations for
and are included in C programs using preprocessor
directives (#include). There are several standard date and time functions such as time(), localtime(),
header files that are commonly used in C and strftime().
programs. below are some examples: 7. ctype.h: This header file provides declarations for
character classification functions such as isalpha(),
1. stdio.h: This header file provides declarations isdigit(), and islower().
for input/output functions such as printf(), 8. stdbool.h: This header file provides a definition
scanf(), and getchar(). for the bool data type and the values true and false.
2. stdlib.h: This header file provides
declarations for general-purpose functions There are many other header files available in C, we
such as malloc(), free(), and exit(). will see more as we go through this course
3. string.h: This header file provides
declarations for string manipulation functions
such as strcpy(), strcat(), and strlen().
The Execution Process Of a C
Program
The execution process of a C program involves the following steps:
1. Preprocessing: Preprocessor converts all the directives into
their respective values. It generates an expanded source
code by attaching all the header files with the source code.
2. Compilation: The compiler takes the preprocessed file and
translates it into assembly code.
3. Assembly: The assembler converts the assembly code into
machine language (object code).
4. Linking: The linker converts object code to executable code.
Multiple units of object codes are linked together in this
step.
5. Loading: The loader loads the executable file into memory(RAM).
6. Execution: Finally, the processor executes the program, and the
output is displayed on the screen or saved in a file, depending on
the program's logic.
Variable in C Language
constant variable
A variable is a container that holds a value in computer program. In C, a constant variable is a variable whose
It allows programmers to store and manipulate data in their
programs. value cannot be changed once it has been
initialized.
How to declare a variable in C language? This is useful when you want to ensure that a
 To declare a variable, you need to specify its type and name. value remains constant throughout the program,
 The type determines what kind of data the variable can and cannot be accidentally modified.
hold, such as numbers, text, or Boolean values. It is important to note that constant variables
 The name is a unique identifier that you can use to refer to must be initialized at the time of declaration. If
the variable in your code. you try to assign a value to a constant variable
E,g: int age ; after it has been declared, the compiler will
generate an error.
How to initialize a variable in C?
 To initialize a variable means to give it an initial How to declare a constant variable?
value when it is first declared. For example: age = 40;
 This can be done at the same time as declaring the  To declare a constant variable in C, you use the
variable, like this: int age = 40 ; const keyword before the variable type, like this:
const int max = 15;
 Other way to declare the constants is to use macros
like this:
Types Of Variables In C
Languages
4. Booleans: These are variables that can hold only
In the C programming language, there are several two values: true or false. In C, the boolean type is not
types of variables that you can declare: built-in, but you can use the stdbool.h library to
1. Integers: These are variables that can hold whole declare a boolean variable as bool.
numbers. They can be signed (positive or 5. Pointers: These are variables that hold memory
negative) or unsigned (only positive). Examples of addresses of other variables. They are represented in C
integer types in C include int, short, long, and by the type void *, which can be cast to any other
char. pointer type.
2. Floating-point numbers: These are variables that 6. Arrays: These are variables that can hold a
can hold decimal numbers. They are represented collection of values of the same type. They are
in C by the types float and double. declared using square brackets, like this: int
3. Characters: These are variables that can hold myArray[5];.
individual characters, such as letters, digits, or 7. Structures: These are variables that can hold
punctuation marks. They are represented in C by multiple values of different types. They are declared
the type char. using the struct keyword.
keywords and identifiers A list of 32 reserved keywords in c language is given
in C language below:

What Is The Difference Between Keywords And Identifiers In C

 Keywords are reserved words that have a specific meaning in


the C language. They are used to define syntax, structure, and
control flow in a program. Examples of C keywords include if,
else, while, for, int, float, void, and return. Since these words
have a specific meaning in the language, they cannot be used
as identifiers (variable names, function names, etc.).

 Identifiers, on the other hand, are names given by the


programmer to variables, functions, and other entities in the
program. They are used to give a meaningful name to these
entities, and to refer to them in the code. Identifiers can be
composed of letters, digits, and underscores, but they cannot
start with a digit. Identifiers are case-sensitive, meaning that
age and Age are two different identifiers.
Data types In C
language 4. char: a character data type that represents a
single character. The range of values for a char is
In computer science, data types can be broadly categorized into typically -128 to 127.
two categories: primitive data types and derived data types. 5. float: a floating-point data type that represents
Primitive Data Types: decimal numbers with single precision. The range
of values for a float is approximately 1.2E-38 to
These are the basic data types that are built into a programming 3.4E+38.
language and are usually used as building blocks for more
complex data types. Some examples of primitive data types 6. double: a floating-point data type that represents
include: decimal numbers with double precision. The range
1. int: an integer data type that represents whole numbers. The of values for a double is approximately 2.2E-308 to
range of values for an int is typically -32,768 to 32,767 on 1.8E+308.
most systems.
2. Long int: a long integer data type that represents larger whole
numbers than an int. The range of values for a long is
typically -2,147,483,648 to 2,147,483,647.
Data types In C
language cont… Naming Rules For Variables:
 An identifier consists of a sequence of letters,
Derived Data Types
numbers and _ (underscore).
These are data types that are created by combining one or more
primitive data types. Some examples of derived data types  First letter of the name must be in small letter
include: not in capital.
1. Array: An array is a collection of elements of the same data  First letter must be an underscore ( _ ) or an
type that are stored in contiguous memory locations. alphabet not a number.
2. Structure: A structure is a collection of elements of different  Spaces must not be given between the variable
data types that are grouped together under a single name. names.
3. Pointer: A pointer is a variable that stores the memory  For space you can used underscore ( _ ) like e.g.
address of another variable.
var_sum.
4. Function: A function is a set of instructions that perform a  One name cannot be assigned to two or more
specific task and can take input parameters and return a value.
variables.
5. Union: A union is a data type that allows storing of different  You can not use special characters between
data types in the same memory location
variables name.
How To View The Contents Format Specifiers In C
Of A Variable In C?
In C programming language, format specifiers are
 In C, you can view the contents of a variable by used to indicate the type of data to be printed or read
using the printf() function to print the value of the from the standard input/output. Here are some
variable to the console. Here is an example: commonly used format specifiers in C:
 %d: Used to format an integer value.
 %f: Used to format a floating-point value.
 %c: Used to format a single character.
 %s: Used to format a string of characters.
 %x: Used to format an integer value as a
hexadecimal number.
 In the above example, we declare an integer These are just a few examples of format specifiers in
variable x and assign it the value 5. We then use C. There are many more format specifiers that can be
printf() to print the value of x to the console using used for different data types and formatting options.
the %d format specifier.
 It is possible to display the value of several variables in a single
printf. Like this:
printf ("The number1 is %d and The number1 %d", number1,
number2);
How To Get Input From
The User In C In this example, the scanf() function is used to read an
integer input from the user and store it in the age
variable. The & operator is used to pass the address of
 To get input from the user in C, you can use
the age variable to scanf(), which allows scanf() to
the scanf() function. below is an example: modify the variable.

 Below is the output of the above code:


 You are 25 years old.
 you weigh 65.000000 Kg
Quiz 2
1. What is the difference between a program and software.

2. Why do we need program languages?

3. List and define Basic Principles of Programming?

4. List and define different stages which are involved in program development

You might also like