0% found this document useful (0 votes)
5 views

Lecture 1 - Introduction

The document outlines a course on Programming in C at the University of Dar es Salaam, detailing assessment methods, course content, and the history and features of the C programming language. It covers fundamental programming concepts, data types, variables, and includes practical examples and exercises. The document emphasizes the importance of C in programming and its foundational role for many other languages.

Uploaded by

disneyking300
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 1 - Introduction

The document outlines a course on Programming in C at the University of Dar es Salaam, detailing assessment methods, course content, and the history and features of the C programming language. It covers fundamental programming concepts, data types, variables, and includes practical examples and exercises. The document emphasizes the importance of C in programming and its foundational role for many other languages.

Uploaded by

disneyking300
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

UNIVERSITY OF DAR ES SALAAM

PROGRAMMING IN C

Masoud H. Mahundi
[email protected]
+255 713832252
Assessment
 Assignments – during lectures and special assignments – 15

 2 Tests, one 15 marks, another 10 marks

 University Exam – 60%

Mind the rules


 Timing for tests and assignments

 Failure to complete course work – abscond

 For special cases, report as early as possible


Course Outline
1. Introduction to Programming
2. Introduction to C language
3. Data Types – Variables – Declarations
4. Input – Outputs
5. Operators
6. Strings
7. Arrays
8. Pointers
9. Flow Controls
10. Functions
11. Structure and Unions
12. Files Management
Introduction to 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
• System SW
– Programs written for computer systems
• Compilers, operating systems, …

• 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


Programming Language : Definition
• These software are basically instructions to the computer
– Compute a+b

– Assign the answer to c

– 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.
Programming Language :
Definition
Our
 We write in English like program

statements – 3rd and 4th


Generation languages
 We compile it – converting the compilation

English-like statements to a
language understandable by
machines Machine
readable
 We “run” the machine readable
– the executable
PL hierarchy
We program for different machines
computers

Mobile phones

cars

robots
Evolution of Programming languages
– First Generation : Machine languages
– Strings of numbers giving machine specific instructions
– Example:
+1300042774
+1400593419
+1200274027
– Second Generation : Assembly languages
– English-like abbreviations representing elementary computer
operations (translated via assemblers)
– Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
– Third Generation : High-level languages
• Codes similar to everyday English
• Use mathematical notations (translated via compilers)
• Example: grossPay = basePay + overTimePay
What does the computer understand?
• Computer only understands machine language
instructions.
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”
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=
CAWbcgR2xgQ
https://www.youtube.com/watch?
v=de2Hsvxaf8M
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
Image from http://e-coa.blogspot.com/2013_11_01_ar
machine can be used in other different machines with
few 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
Line 3: printf()
be compiled
 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
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


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


UNIVERSITY OF DAR ES 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;


1.
Variables naming rules
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. float 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. 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;

You might also like