Introduction To Programming in C
Introduction To Programming in C
PROGRAMMING IN C
Name
Dr Masoud H. Mahundi
Affiliation
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
• Hardware
• Various devices comprising a computer
• Software
• Programs that run on a computer
Software Categories
• Computer programming is mostly about writing software
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
2. Algorithms
3. Pseudocode
Flowcharts
• The process of breaking down a problem through figures of different types
▪ Lab Exercise #1
Write a flowchart for instructions to withdraw
money from an ATM machine
• 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
▪ 50% of R is written in C
~~ Denis Ritchie ~~
History of C Language
▪ Originally designed in 1972 by in the Bell Telephone Laboratories in U.S.A - Bell Labs.
▪ 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
~~ 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
• 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
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
Line 3: printf()
2. What is programming?
5. What is “compilation”?
7. What is “portability”?
9. What is a “comment”?
Data Types
Data Types
▪ Programs are always meant to be
▪ processing some input data and generate some output data
▪ 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)
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
▪ 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”
▪ Examples of declaration
1. float salary;
2. int age;
3. double weight;
4. char sex;
5. char username[15];
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
2. int age,
3. double weight
4. character sex;
5. char username[15];
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.}