Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

Introduction To Programming in C

This document provides information about a programming in C course taught by Dr. Masoud H. Mahundi at the University of Dar es Salaam. It includes Dr. Mahundi's contact information, an overview of the course modality and assessments, slots for practical sessions, and introductory sections on problem-solving techniques like flowcharts and algorithms as well as the history and features of the C programming language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Introduction To Programming in C

This document provides information about a programming in C course taught by Dr. Masoud H. Mahundi at the University of Dar es Salaam. It includes Dr. Mahundi's contact information, an overview of the course modality and assessments, slots for practical sessions, and introductory sections on problem-solving techniques like flowcharts and algorithms as well as the history and features of the C programming language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

UNIVERSITY OF DAR ES SALAAM

PROGRAMMING IN C

Name

Dr Masoud H. Mahundi

Affiliation

Department of Computer Science and Engineering


University of Dar es Salaam

Emails
mmahundi97@gmail.com, mahundi.masoud@udsm.ac.tz

Phone Numbers
+255713832252, +255768832424
Modality

1. Delivery
• Lectures • Follow the University
• PowerPoint slides regulations
• Mid-Lecture Discussions
• On Tests and Exams
• Mid-lecture exercises – carry something to write on

2. Practical Sessions • We will focus on competence


• Assessment – more than knowledge
• 2 Test – a total of 25%
• Practical and quizzes – total of 15%
• University Exam – 60%
Slots for Practical Sessions
Monday 10:00 – 13:00 B106
Wednesday 13:00 – 15:00 C4/C5
Thursday 12:00 – 15:00 B106
12:00 – 14:00 C4/C5
07:00 – 09:00 C4/C5
INTRODUCTION
Introduction to Computer Programming
What is a Computer?
• Computer
• Device capable of performing computations and making logical decisions

• Hardware
• Various devices comprising a computer

• Keyboard, screen, mouse, disks, memory, CD-ROM, and processing units

• Software
• Programs that run on a computer
Software Categories
• Computer programming is mostly about writing software

• These software could be

1. System SW
▪ Programs written for computer systems
▪ Compilers, operating systems, drivers …

2. Application SW
▪ Programs written for computer users
▪ Word processors, spreadsheets, & other application packages
▪ Stock management
▪ Data capture and analysis
▪ Calculators
▪ Tax management, Banking e.t.c
INTRODUCTION
Problem-Solving
(flow charts, algorithms, pseudocode)
Making a cup of tea
• Explain to somebody from another planet
how to make a cup of tea

• There are different ways to do that


including
1. Flowcharts

2. Algorithms

3. Pseudocode
Flowcharts
• The process of breaking down a problem through figures of different types

• A symbol for what beginning and ending


• Like a box, but with softer edged

• An arrow showing direction of activities

• A diamond shape indicate decision (condition)


• Three tips of the shape are used
• One for entering into a decision, another for when the condition is TRUE and another when
the condition is FALSE
• Example could be If PIN is correct log in (one tip) else display error page (another tip)

• A rectangle represents a process

• A parallelogram represents inputs or outputs of a certain computation


A Lamp Problem

▪ The problem is the lamp is not working

▪ We need to solve this problem

▪ Using a flow chart to breakdown the


problem into a solution

Adapted from https://www.edrawmax.com/flowchart/examples/ on 4th October 2023


Profit and Loss

▪ Calculating profit and loss

Adapted from https://www.visual-paradigm.com/tutorials/flowchart-tutorial/on 4th October 2023


▪ Exercise on flowchart

▪ Lab Exercise #1
Write a flowchart for instructions to withdraw
money from an ATM machine

Adapted from https://www.visual-paradigm.com/tutorials/flowchart-tutorial/on 4th October 2023


Programming Language : Definition
• Programming exploits the potential of a computer
• Being able to handle thousands of instructions in a second
• Being able to store huge volumes of data
• Ability to access the data from memories in a split second

• Programming means writing instructions for a computer to execute

• Such instructions are like


• Compute a+b
• Capture the user PIN
• Compare with PIN in the database

• To give these instructions to a computer we use a Programming Language

• A programming language: A vocabulary and set of grammatical rules for instructing a computer
to perform specific tasks.

• Example programming languages include C, Java, Python, Ruby, C++, and others
We program for different machines

computers

Mobile phones

cars

robots
Programming Language

▪ We write in English like statements – 3rd and Our


program
4th Generation languages

▪ We compile it – converting the English-like


statements to a language understandable by
compilation
machines

▪ We “run” the machine-readable – the


executable ready for use
Machine
readable
Why C Programming
▪ Strengthen the knowledge on structured programming

▪ C programing is the “Latin of programming”

▪ The foundation of many other programming languages


▪ First Java compiler - by Sun Microsystems was written in C

▪ later libraries are written in Java

▪ Zend Engine (PHP) is written in C

▪ 50% of R is written in C

▪ Although old – it is still used


▪ “C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be there”

~~ Denis Ritchie ~~
History of C Language
▪ Originally designed in 1972 by in the Bell Telephone Laboratories in U.S.A - Bell Labs.

▪ Dennis Mac Alistair Ritchie (1941 – 2011) and Brian Kernighan

▪ Its development came in as a necessity in the course of developing UNIX operating


system

▪ UNIX - originally developed using assembly languages, a PDP series.

▪ There came a need for abstraction and portability into the resulting UNIX.

▪ Denis and Brian set to develop a language that would develop the UNIX.
▪ C was developed and used

▪ The fourth version of UNIX - entirely written in C language in 1973


History of C Language
▪ Many enhancements were observed from the resulted UNIX
▪ Important of all was portability – the ability to run in different platforms. In fact, it
helped a lot in enhancing portability of the Operating System.

Dennis MacAlistair Ritchie


(September 9, 1941 – c. October 12, 2011)

Watch the history here


https://www.youtube.com/watch?v=CAWb
cgR2xgQ
https://www.youtube.com/watch?v=de2Hs
vxaf8M
History of C Language
“UNIX is basically a simple operating system, but you have to be a genius to understand the
simplicity”

~~ Denis Ritchie ~~

▪ Up until 1978, C was being used only within the Bell Labs for UNIX development

▪ It was in 1978 when Ritchie and Kernighan published the formal description of the language

▪ The language came to be known as K & R C – The language was made public to the world

▪ In 1983 the ANSI started the process of standardising the language which went to 1989

▪ In 1990 the ISO started the same process

▪ The language was therefore open to the public


History of C Language
TIME STAGE OF DEVELOPMENT

1950s ALGOL language by International Group


1967 BCPL -- Basic Combined Programming Language
1970 B developed by Ken Tompson
1972 Traditional C developed by Denis Mark Alistair Ritchie at
the Bell Labs, US
1978 K&R C
1989 ANSI C
1990 ANSI/ISO C
Features of the Language
• Extensive use of functions: C language uses functions in
most of its implementations.

• C Metaphors
1. A “high-level assembly language” as it doesn't do everything
for you, there's a lot you have to do yourself. It actually
imposes relatively few built-in ways of doing things on the
programmer.
2. A “a sharp knife”: the freedom to do everything by one’s self
is, on the other hand, dangerous. Users are exposed to
delicate resources of the system like memory management, to
work with.
3. Portable: this means a program developed for one machine
can be used in other different machines with few Image from http://e-coa.blogspot.com/2013_11_01_archive.html

modifications. 40 compilers are available now


GETTING STARTED
Getting Started
1. #include<stdio.h>
2. main()
3. {
4. // this is a comment
5. printf(“Hello World\n”); /* this is also a comment
– mult-lines */
6. }

The above program is meant to print to the screen the words “Hello World”
Line 1: #include <stdio.h>

▪ A pre-processor directive
▪ It tells the compiler to include the content of a file named stdio.h
▪ A programmer does not write everything for themselves
▪ They often use files coming with the compilers’ library
▪ Richness of a language is also measured in how big is the library

Other Library Files


stdio.h Standard Input Output
string.h String handling functions
math.h Mathematics functions
stdlib.h Miscellaneous functions
malloc.h Dynamic memory allocation functions;
calloc and malloc
Line 2: main (){

▪ C programming is entirely done through functions

▪ A big task is often divided into small modules of manageable task


▪ The main program that coordinate these tasks – the main function
▪ { is a sign for start any code fragment in C and therefore the main
program also
▪ // This means a comment – text for the programmer – wont be
compiled

Line 3: printf()

▪ a function to print the contents to the screen

▪ It prints on the screen whatever is within the brackets


▪ Character within the quote marks are printed as they are
▪ The definition of this function is in the file stdio.h
Wake Up

Write a small program that prints the words

“mimi sijambo” in your native language


Recap
1. What is a computer?

2. What is programming?

3. What is a programming language?

4. How many types of software are there?

5. What is “compilation”?

6. Why do we need a compiler?

7. What is “portability”?

8. Why do we have to “include” files from the library?

9. What is a “comment”?
Data Types
Data Types
▪ Programs are always meant to be
▪ processing some input data and generate some output data

▪ There is a need to manage data being processed - temporarily

▪ Consider the following types of data


▪ Students names to be verified

▪ Disease statistics for some computation

▪ Weights and height measures of patients

▪ Salaries to be processed by the Paymaster

▪ Each requires a different amount of memory to be used for its storage and
processes
Data Types
▪ There are three data types (some say two)

▪ primary, secondary and user defined

1. Primary data types: original data types defined in the compiler


integers, characters, float, and double

2. Secondary data type: a derivative data type


Arrays and pointers

3. User-defined data types


Structures, unions and enum
Data Types
▪ Three reasons we have to be aware of data types
1. The amount of storage allocated for the data,

2. The permissible range of values the data can accept and

3. The different operations on the data

1. Integers – int, short, long


For storing whole numbers only without the fractional parts

Share the mathematical definition of integers, containing negatives and positives

specifying whether the integer will also take negatives or not


▪ signed int : an int can be positive, negative or zero
▪ Unsigned: an int can only contain zero and positive
Data Types
2. Character – char

For storing characters

Technically this is an integer type

Computers use numerical code to represent


characters using some scheme – for example
ASCII
Data Types
3. Floating Point numbers
Stores numbers which include the fractional or decimal part in it
Internally, the floating point numbers have to be stored in two folds
The base number and the exponent

There are basically three types of floating point numbers, float, double
and long double
Normally float means 4 bytes of memory, double means 8 bytes while
long double means 10 bytes.
▪ Wake Up

▪ What data type will be suitable for the following situations


1. Managing ages in a primary school

2. Managing salaries in Zimbabwe

3. Managing salaries in the US

4. Managing names of the courses taught in a university

5. Managing weights and heights in a hospital


SALAAM
PROGRAMMING IN C
Variables and
Declaration
Masoud H. Mahundi
Variables
▪ Some external data entered by
▪ Users, other systems, external files or a result of some computations

▪ They do change depending on the user needs – variables

▪ A variable, therefore, is a named container into which a value can be placed,


manipulated and retrieved

▪ Declaration is the definition of these variables in terms of the name, and data type
– for memory size and types of computation
Variables
float salary;

▪ This declaration asks for a memory of 4 Bytes to be reserved – under the logical
name “salary”

▪ This memory is reserved in what is referred to as a “stack”

▪ Its freed when the variable goes out of scope

▪ Drawback: Most OS provide limited amount of memory for “stack”

▪ Windows is said to provide only 1 MB as “stack”


Declaration
▪ Declaration: <<data type>> <<variable>>;

▪ Examples of declaration

1. float salary;

2. int age;

3. double weight;

4. char sex;

5. char username[15];

6. float salary, pension, paye;

7. int number_of_children, age;


Variables naming rules
1. First character must be an alphabet (or underscore); it cannot be a number or some
other characters.
2. Variable names must consist of only letters, digits and underscore; it should NOT
contain special characters like * \ / + - ^ % & #
3. There cannot be two successive underscores
4. A variable name should have less than 31 characters; which is still reasonable,
something you can remember
5. Any standard C language keyword cannot be used as a variable name; one cannot use
names like return, switch, break or similar things as they are keywords to C, meaning
they have special meanings in the C compiler. They are otherwise called reserved
words.
6. A identifier should not contain a space within like user name, it has to be username
7. It is not compulsory, but good practice to use meaningful variables names.
Declaration
▪ Type casting - otherwise referred to as forced conversion

▪ The process of converting a data type to another to suit some purpose.

▪ The syntax is (type) variable


1. #include<stdio.h>
2. main(){
3. int intv1;
float salary = 4500.52;
4. float flov1 = 13.523;
int agSalary;
5. intv1 = (int)flov1;
agSalary = (int) salary;
6. printf("%d",intv1);
7. printf("\n");
8. }
Constants
▪ Unlike variables, constants remain unchanged throughout the program
▪ Often not values entered by the users

AS A PREPROCESSOR DIRECTIVE
1. #include<stdio.h>
2. #define PI 3.142
3. main(){
4. float radius, area;
5. printf(“Enter Radius: “);
6. scanf(“%f”, &radius);
7. printf(“The Area is %0.2f”, PI*radius*radius);
8. }
Constants
▪ Unlike variables, constants remain unchanged throughout the program
▪ Often not values entered by the users

INSIDE THE PROGRAM


1. #include<stdio.h>
2. main(){
3. float radius, area;
4. Const float PI = 3.142
5. printf(“Enter Radius: “);
6. scanf(“%f”, &radius);
7. printf(“The Area is %0.2f”, PI*radius*radius);
8. }
Wake Up
Identify errors in the following declarations

1. float salary amount;

2. int age,

3. double weight

4. character sex;

5. char username[15];

6. float salary; pension; paye;

7. int number of children, age;

8. float #pesa;

9. int xxx;
Wake Up

1. #include<stdio.h>
2. main(){
3. char jina[13]; int age;
4. variables;
5. printf("Name :");
6. scanf("%s", jina);
7. prin("Hello %s, how old are you? ", name);
8. scanf("%d",age);
9. printf("O, you are %d, age)";
10.}

You might also like