SlideShare a Scribd company logo
1 of 41
COM1407
Computer Programming
Lecture 02
Introduction to C Programming
K.A.S.H. Kulathilake
B.Sc. (Hons) IT, MCS , M.Phil., SEDA(UK)
Rajarata University of Sri Lanka
Department of Physical Sciences
1
Objectives
• At the end of this lecture students should be able
to;
▫ Describe features of C programming language.
▫ Justify the terminology related to computer
programming.
▫ Define the editing, compiling, linking, debugging
stages of C programming
▫ Recognize the basic structure of a C program
▫ Apply comments for C programs to improve
readability.
2
History
• The C programming Language was
pioneered by Dennis Ritchie at AT&T
Bell Laboratories in the early 1970s.
• C became popular with the development
of UNIX operating system which was
developed in the same laboratory.
• C grew in popularity across different
operating systems as a result of
marketing different C compilers
produced by different vendors.
• In 1990, the first official ANSI standard
definition of C was published.
• In 1999, ISO standard for C was
published.
3
Programming
• Computers are really very dumb machines indeed
because they do only what they are told to do.
• The basic operations of a computer system form
what is known as the computer’s instruction set.
• To solve a problem using a computer, you must
express the solution to the problem in terms of the
instructions of the particular computer.
• A computer program is just a collection of the
instructions necessary to solve a specific problem.
• The approach or method that is used to solve the
problem is known as an algorithm.
4
Programming (Cont…)
5
Normally, to develop a program to
solve a particular problem, you
first express the solution to the
problem in terms of an algorithm
and then develop a program that
implements that algorithm.
High Level Languages
6
In Assembly
Language.
Programmer
used symbolic
names to
represents the
operations
Assembler
translates the
assembly codes
to machine
instructions
Programs written in assembly
language are not portable
Have 1-to1
correspondent
between
assembly
language and
machine
language.
High Level Languages (Cont…)
7
What is meant by “the
programs written in Low
Level Languages are not
Portable”?
That is, the program
will not run on a
different processor
type without being
rewritten. This is
because different
processor types have
different instruction
sets, and because
assembly language
programs are written in
terms of these
instruction sets, they
are machine dependent.
High Level Languages (Cont…)
• Programmers developing programs in high level
languages no longer had to concern themselves with
the architecture of the particular computer.
• Operations performed in High level languages were
of a much more sophisticated or higher level, far
removed from the instruction set of the particular
machine.
• One High Level Language instruction or statement
resulted in many different machine instructions
being executed, unlike the one-to-one
correspondence found between assembly language
statements and machine instructions.
8
High Level Languages (Cont…)
• Standardization of the syntax of a Higher-Level
Language meant that a program could be written
in the language to be machine independent.
• That is, a program could run on any machine
that supported the language with few or no
changes.
9
To support a higher-level language, a special computer program must be developed
that translates the statements of the program developed in the higher-level language
into a form that the computer can understand—in other words, into the particular
instructions of the computer. Such a program is known as a COMPILER.
Compiling Programs
• Editing
▫ First write your program
using text editor or
Integrated Development
Environment (IDE).
▫ Then you need to save
your file by giving a
suitable name with “.c”
file extension.
 e.g.
 MyFirstProgram.c
10
Compiling Programs (Cont…)
11
Compiling Programs (Cont…)
• Compiling
▫ The program that is entered into the file is known
as the source program because it represents
the original form of the program expressed in the
C language.
▫ Then you can compile your program using c
compiler;
▫ If you are using the popular GNU C compiler in
UNIX, the command you use is gcc.
▫ Type following command to compile your program
gcc MyFirstProgram.c
12
Compiling Programs (Cont…)
▫ In the first step of the compilation process, the
compiler examines each program statement contained
in the source program and checks it to ensure that it
conforms to the syntax and semantics of the language.
▫ During the compilation process, compiler scans the
source file and checks for the syntactic and sematic
errors in the source file.
 If passed, you can proceed ahead
 If failed, compiler indicates the errors and you have to fix
them and re-compile your source file after saving the
changes.
13
Compiling Programs (Cont…)
▫ When all the syntactic and semantic errors have been
removed from the program, the compiler then
proceeds to take each statement of the program and
translate it into a “lower” form.
▫ On most systems, this means that each statement is
translated by the compiler into the equivalent
statement or statements in assembly language needed
to perform the identical task.
▫ After the program has been translated into an
equivalent assembly language program, the next step
in the compilation process is to translate the assembly
language statements into actual machine instructions.
14
Compiling Programs (Cont…)
▫ This step might or might not involve the execution of a
separate program known as an assembler.
▫ On most systems, the assembler is executed
automatically as part of the compilation process.
▫ The assembler takes each assembly language
statement and converts it into a binary format
known as object code, which is then written into
another file on the system.
▫ This file typically has the same name as the source file
under Unix, with the last letter an “o” (for object)
instead of a “c”.
▫ Under Windows, the suffix letters "obj" typically
replace the “c” in the filename.
15
Compiling Programs (Cont…)
• Linking
▫ After the program has been translated into object
code, it is ready to be linked.
▫ This process is once again performed automatically
whenever the cc or gcc command is issued under Unix.
▫ The purpose of the linking phase is to get the program
into a final form for execution on the computer.
▫ If the program uses other programs that were
previously processed by the compiler, then during this
phase the programs are linked together.
▫ Programs that are used from the system’s program
library are also searched and linked together with the
object program during this phase.
16
Compiling Programs (Cont…)
▫ The process of compiling and linking a program is often
called building.
▫ The final linked file, which is in an executable object code format,
is stored in another file on the system, ready to be run or
executed.
▫ Under Unix, this file is called a.out by default.
▫ Under Windows, the executable file usually has the same name as
the source file, with the c extension replaced by an exe extension.
▫ So, the command;
 MyFirstProgram.out in UNIX
 MyFirstProgram.exe in Windows
has the effect of loading the program called
MyFirstProgram.out/ MyFirstProgram.exe into the
computer’s memory and initiating its execution.
17
Compiling Programs (Cont…)
▫ When the program is executed, each of the statements
of the program is sequentially executed in turn.
▫ If the program requests any data from the user, known
as input, the program temporarily suspends its
execution so that the input can be entered.
▫ Or, the program might simply wait for an event, such
as a mouse being clicked, to occur.
▫ Results that are displayed by the program, known as
output, appear in a window, sometimes called the
console.
▫ Or, the output might be directly written to a file on the
system.
18
Compiling Programs (Cont…)
• Debugging
▫ If all goes well (and it probably won’t the first time the
program is executed), the program performs its intended
functions.
▫ If the program does not produce the desired results, it is
necessary to go back and reanalyze the program’s logic.
▫ This is known as the debugging phase, during which an
attempt is made to remove all the known problems or bugs
from the program.
▫ To do this, it will most likely be necessary to make changes
to the original source program.
▫ In that case, the entire process of compiling, linking, and
executing the program must be repeated until the desired
results are obtained.
19
IDE
• The process of editing, compiling, running, and
debugging programs is often managed by a
single integrated application known as an
Integrated Development Environment, or IDE
for short.
• An IDE is a windows-based program that allows
you to easily manage large software programs,
edit files in windows, and compile, link, run, and
debug your programs.
20
Introduction to Quincy C
• http://www.codecutter.net/tools/quincy/
• Refer Note
21
C Program Structure
22
/* Header Filed Placed Here*/
/* Global Data Placed Here */
function 1 () {
/* Local Data and C code placed here */
}
Function n () {
/* Local Data and C code placed here */
}
main () { /* Entry Point */
/* Local Data and C code placed here */
}
Every C Program
consists of 1 or more
functions.
main() is the
mandatory function in
every C program.
Depicts a typical single
source file C program.
First C Program
#include<stdio.h>
int main ()
{
printf("Programming is Funn");
return 0;
}
23
In the C programming language, lowercase and uppercase letters are distinct.
CASE SENSITIVE
First C Program (Cont…)
• Compile and Run
24
First C Program (Cont…)
• Contents in the Saved Location
25
First C Program (Cont…)
#include<stdio.h>
• Should be included at the beginning of just
about every program you write.
• It tells the compiler to include the standard
input output header file stdio.h as part of the
program.
• It contains the information about the printf
output routine that is used later in the program.
26
First C Program (Cont…)
int main (void)
• Informs the system that the name of the program is
main, and that it returns an integer value, which is
abbreviated “int.”
• Main is a special name that indicates precisely
where the program is to begin execution.
• The open and close parentheses immediately
following main specify that main is the name of a
function.
• The keyword void that is enclosed in the parentheses
specifies that the function main takes no arguments
(that is, it is void of arguments).
27
First C Program (Cont…)
{ }
• Describe the boundary of the main function.
• This is done by enclosing all program statements
of the routine within a pair of curly braces.
• All program statements included between the
braces are taken as part of the main routine by
the system.
• You have only two statements enclosed within
the braces of this program.
28
First C Program (Cont…)
printf("Programming is Funn");
• The first statement specifies that a routine named
printf is to be invoked or called.
• The parameter or argument to be passed to the
printf routine is the string of characters
“Programming is fun.n”
• The printf routine is a function in the C library that
simply prints or displays its argument (or
arguments, as you will see shortly) on your screen.
• n is the new line character
29
First C Program (Cont…)
n
• Any characters to be printed after the newline
character then appear on the next line of the
display.
• In fact, the newline character is similar in
concept to the carriage return key on a
typewriter.
• ** All program statements in C must be
terminated by a semicolon (;).
30
First C Program (Cont…)
return 0
• says to finish execution of main, and return to
the system a status value of 0.
• You can use any integer here.
• Zero is used by convention to indicate that the
program completed successfully—that is,
without running into any errors.
31
First C Program (Cont…)
32
C provides a collection of useful
functions which are called as
library functions.
A library is split into a group of
functions and each group has a .h
(header) file associated with it.
Often the .h file will contain other
components such as type
declarations.
Whenever the functions in a given
group are used, the group’s .h file
should be included with
#include<>.
What are Header
files ?
Adding another Phrase
#include <stdio.h>
int main (void)
{
printf ("Programming is fun.n");
printf ("And programming in C is
even more fun.n");
return 0;
}
33
Using n
#include <stdio.h>
int main (void)
{
printf
("Testing...n..1n...2n....3n");
return 0;
}
34
Comments
• A comment statement is used in a program to
document a program and to enhance its
readability.
• comments serve to tell the reader of the
program;
▫ The programmer or someone else whose
responsibility it is to maintain the program
▫ Just what the programmer had in mind when he
or she wrote a particular program or a particular
sequence of statements.
35
Comments (Cont…)
/* This program demonstrate the application */
/* of new line character */
#include <stdio.h>
int main (void)
{
/* Display text with new line character*/
printf ("Testing...n..1n...2n....3n");
return 0;
}
36
Comments (Cont…)
• Comments are ignored by the compiler.
• There are two ways to insert comments;
▫ /* */ - This form of comment is often used when
comments span several lines in the program.
▫ // - Any characters that follow these slashes up to
the end of the line are ignored by the compiler.
37
Comments (Cont…)
• It is a good idea to get into the habit of inserting
comment statements into the program as the program is
being written or typed in.
• There are good reasons for this;
▫ It is far easier to document the program while the particular
program logic is still fresh in your mind than it is to go back
and rethink the logic after the program has been completed.
▫ By inserting comments into the program at such an early
stage of the game, you get to reap the benefits of the
comments during the debug phase, when program logic
errors are being isolated and debugged.
• A comment can not only help you read through the
program, but it can also help point the way to the source
of the logic mistake.
38
Objective Re-cap
• Now you should be able to:
▫ Describe features of C programming language.
▫ Justify the terminology related to computer
programming.
▫ Define the editing, compiling, linking, debugging
stages of C programming
▫ Recognize the basic structure of a C program
▫ Apply comments for C programs to improve
readability.
39
References
• Chapter 01,02 and 03 - Programming in C, 3rd
Edition, Stephen G. Kochan
40
Next: Variables and Data Types
41

More Related Content

What's hot

iOS design: a case study
iOS design: a case studyiOS design: a case study
iOS design: a case studyJohan Ronsse
 
Android operating System
Android operating SystemAndroid operating System
Android operating Systemyash lakhmani
 
Flutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by StepFlutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by StepChandramouli Biyyala
 
Virtual Reality-Seminar presentation
Virtual Reality-Seminar  presentationVirtual Reality-Seminar  presentation
Virtual Reality-Seminar presentationShreyansh Vijay Singh
 
Rajul computer presentation
Rajul computer presentationRajul computer presentation
Rajul computer presentationNeetu Jain
 
ppt based on android technology with great animations
ppt based on android technology with great animationsppt based on android technology with great animations
ppt based on android technology with great animationsHriday Garg
 
Microsoft hololens
Microsoft hololensMicrosoft hololens
Microsoft hololensAtul Singh
 
Computer communication and internet
Computer communication and internetComputer communication and internet
Computer communication and interneteVidhya
 
Android Security
Android SecurityAndroid Security
Android SecurityLars Jacobs
 
A presentation on android OS
A presentation on android OSA presentation on android OS
A presentation on android OSNahian Ahmed
 
Android Web app
Android Web app Android Web app
Android Web app Sumit Kumar
 
Virtual Reality Technology.pptx
Virtual Reality Technology.pptxVirtual Reality Technology.pptx
Virtual Reality Technology.pptxLSURYAPRAKASHREDDY
 
Computer Viruses
Computer VirusesComputer Viruses
Computer VirusesAnnies Minu
 

What's hot (20)

iOS design: a case study
iOS design: a case studyiOS design: a case study
iOS design: a case study
 
Android Security
Android SecurityAndroid Security
Android Security
 
Android operating System
Android operating SystemAndroid operating System
Android operating System
 
Flutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by StepFlutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by Step
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Virtual Reality-Seminar presentation
Virtual Reality-Seminar  presentationVirtual Reality-Seminar  presentation
Virtual Reality-Seminar presentation
 
Rajul computer presentation
Rajul computer presentationRajul computer presentation
Rajul computer presentation
 
ppt based on android technology with great animations
ppt based on android technology with great animationsppt based on android technology with great animations
ppt based on android technology with great animations
 
Microsoft hololens
Microsoft hololensMicrosoft hololens
Microsoft hololens
 
Computer communication and internet
Computer communication and internetComputer communication and internet
Computer communication and internet
 
iOS - History of iOS
iOS - History of iOSiOS - History of iOS
iOS - History of iOS
 
Android Security
Android SecurityAndroid Security
Android Security
 
A presentation on android OS
A presentation on android OSA presentation on android OS
A presentation on android OS
 
Android Web app
Android Web app Android Web app
Android Web app
 
Virtual Reality Technology.pptx
Virtual Reality Technology.pptxVirtual Reality Technology.pptx
Virtual Reality Technology.pptx
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
 
Windows Phone PPT
Windows Phone PPTWindows Phone PPT
Windows Phone PPT
 
History of mobile apps
History of mobile appsHistory of mobile apps
History of mobile apps
 
Computer Viruses
Computer VirusesComputer Viruses
Computer Viruses
 

Similar to COM1407: Introduction to C Programming

INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfINTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfSubramanyambharathis
 
Computer and multimedia Week 1 Windows Architecture.pptx
Computer and multimedia Week 1 Windows Architecture.pptxComputer and multimedia Week 1 Windows Architecture.pptx
Computer and multimedia Week 1 Windows Architecture.pptxfatahozil
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkAbdullahNadeem78
 
Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Shipra Swati
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdfvino108206
 
Learn C Programming Full Course Free
Learn C Programming Full Course FreeLearn C Programming Full Course Free
Learn C Programming Full Course FreeDheeraj Patidar
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdfAdiseshaK
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computerzaheeriqbal41
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 

Similar to COM1407: Introduction to C Programming (20)

INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfINTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
 
C lecture notes new
C lecture notes newC lecture notes new
C lecture notes new
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
C programming part1
C programming part1C programming part1
C programming part1
 
Computer and multimedia Week 1 Windows Architecture.pptx
Computer and multimedia Week 1 Windows Architecture.pptxComputer and multimedia Week 1 Windows Architecture.pptx
Computer and multimedia Week 1 Windows Architecture.pptx
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net framework
 
Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7
 
Introduction to c language
Introduction to c language Introduction to c language
Introduction to c language
 
CS3251-_PIC
CS3251-_PICCS3251-_PIC
CS3251-_PIC
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C.pdf
C.pdfC.pdf
C.pdf
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Learn C Programming Full Course Free
Learn C Programming Full Course FreeLearn C Programming Full Course Free
Learn C Programming Full Course Free
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computer
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 

More from Hemantha Kulathilake

NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar Hemantha Kulathilake
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishHemantha Kulathilake
 
NLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelNLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelHemantha Kulathilake
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingHemantha Kulathilake
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation Hemantha Kulathilake
 

More from Hemantha Kulathilake (20)

NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for English
 
NLP_KASHK:POS Tagging
NLP_KASHK:POS TaggingNLP_KASHK:POS Tagging
NLP_KASHK:POS Tagging
 
NLP_KASHK:Markov Models
NLP_KASHK:Markov ModelsNLP_KASHK:Markov Models
NLP_KASHK:Markov Models
 
NLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram ModelsNLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram Models
 
NLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelNLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language Model
 
NLP_KASHK:N-Grams
NLP_KASHK:N-GramsNLP_KASHK:N-Grams
NLP_KASHK:N-Grams
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological Parsing
 
NLP_KASHK:Morphology
NLP_KASHK:MorphologyNLP_KASHK:Morphology
NLP_KASHK:Morphology
 
NLP_KASHK:Text Normalization
NLP_KASHK:Text NormalizationNLP_KASHK:Text Normalization
NLP_KASHK:Text Normalization
 
NLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State AutomataNLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State Automata
 
NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions
 
NLP_KASHK: Introduction
NLP_KASHK: Introduction NLP_KASHK: Introduction
NLP_KASHK: Introduction
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

COM1407: Introduction to C Programming

  • 1. COM1407 Computer Programming Lecture 02 Introduction to C Programming K.A.S.H. Kulathilake B.Sc. (Hons) IT, MCS , M.Phil., SEDA(UK) Rajarata University of Sri Lanka Department of Physical Sciences 1
  • 2. Objectives • At the end of this lecture students should be able to; ▫ Describe features of C programming language. ▫ Justify the terminology related to computer programming. ▫ Define the editing, compiling, linking, debugging stages of C programming ▫ Recognize the basic structure of a C program ▫ Apply comments for C programs to improve readability. 2
  • 3. History • The C programming Language was pioneered by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s. • C became popular with the development of UNIX operating system which was developed in the same laboratory. • C grew in popularity across different operating systems as a result of marketing different C compilers produced by different vendors. • In 1990, the first official ANSI standard definition of C was published. • In 1999, ISO standard for C was published. 3
  • 4. Programming • Computers are really very dumb machines indeed because they do only what they are told to do. • The basic operations of a computer system form what is known as the computer’s instruction set. • To solve a problem using a computer, you must express the solution to the problem in terms of the instructions of the particular computer. • A computer program is just a collection of the instructions necessary to solve a specific problem. • The approach or method that is used to solve the problem is known as an algorithm. 4
  • 5. Programming (Cont…) 5 Normally, to develop a program to solve a particular problem, you first express the solution to the problem in terms of an algorithm and then develop a program that implements that algorithm.
  • 6. High Level Languages 6 In Assembly Language. Programmer used symbolic names to represents the operations Assembler translates the assembly codes to machine instructions Programs written in assembly language are not portable Have 1-to1 correspondent between assembly language and machine language.
  • 7. High Level Languages (Cont…) 7 What is meant by “the programs written in Low Level Languages are not Portable”? That is, the program will not run on a different processor type without being rewritten. This is because different processor types have different instruction sets, and because assembly language programs are written in terms of these instruction sets, they are machine dependent.
  • 8. High Level Languages (Cont…) • Programmers developing programs in high level languages no longer had to concern themselves with the architecture of the particular computer. • Operations performed in High level languages were of a much more sophisticated or higher level, far removed from the instruction set of the particular machine. • One High Level Language instruction or statement resulted in many different machine instructions being executed, unlike the one-to-one correspondence found between assembly language statements and machine instructions. 8
  • 9. High Level Languages (Cont…) • Standardization of the syntax of a Higher-Level Language meant that a program could be written in the language to be machine independent. • That is, a program could run on any machine that supported the language with few or no changes. 9 To support a higher-level language, a special computer program must be developed that translates the statements of the program developed in the higher-level language into a form that the computer can understand—in other words, into the particular instructions of the computer. Such a program is known as a COMPILER.
  • 10. Compiling Programs • Editing ▫ First write your program using text editor or Integrated Development Environment (IDE). ▫ Then you need to save your file by giving a suitable name with “.c” file extension.  e.g.  MyFirstProgram.c 10
  • 12. Compiling Programs (Cont…) • Compiling ▫ The program that is entered into the file is known as the source program because it represents the original form of the program expressed in the C language. ▫ Then you can compile your program using c compiler; ▫ If you are using the popular GNU C compiler in UNIX, the command you use is gcc. ▫ Type following command to compile your program gcc MyFirstProgram.c 12
  • 13. Compiling Programs (Cont…) ▫ In the first step of the compilation process, the compiler examines each program statement contained in the source program and checks it to ensure that it conforms to the syntax and semantics of the language. ▫ During the compilation process, compiler scans the source file and checks for the syntactic and sematic errors in the source file.  If passed, you can proceed ahead  If failed, compiler indicates the errors and you have to fix them and re-compile your source file after saving the changes. 13
  • 14. Compiling Programs (Cont…) ▫ When all the syntactic and semantic errors have been removed from the program, the compiler then proceeds to take each statement of the program and translate it into a “lower” form. ▫ On most systems, this means that each statement is translated by the compiler into the equivalent statement or statements in assembly language needed to perform the identical task. ▫ After the program has been translated into an equivalent assembly language program, the next step in the compilation process is to translate the assembly language statements into actual machine instructions. 14
  • 15. Compiling Programs (Cont…) ▫ This step might or might not involve the execution of a separate program known as an assembler. ▫ On most systems, the assembler is executed automatically as part of the compilation process. ▫ The assembler takes each assembly language statement and converts it into a binary format known as object code, which is then written into another file on the system. ▫ This file typically has the same name as the source file under Unix, with the last letter an “o” (for object) instead of a “c”. ▫ Under Windows, the suffix letters "obj" typically replace the “c” in the filename. 15
  • 16. Compiling Programs (Cont…) • Linking ▫ After the program has been translated into object code, it is ready to be linked. ▫ This process is once again performed automatically whenever the cc or gcc command is issued under Unix. ▫ The purpose of the linking phase is to get the program into a final form for execution on the computer. ▫ If the program uses other programs that were previously processed by the compiler, then during this phase the programs are linked together. ▫ Programs that are used from the system’s program library are also searched and linked together with the object program during this phase. 16
  • 17. Compiling Programs (Cont…) ▫ The process of compiling and linking a program is often called building. ▫ The final linked file, which is in an executable object code format, is stored in another file on the system, ready to be run or executed. ▫ Under Unix, this file is called a.out by default. ▫ Under Windows, the executable file usually has the same name as the source file, with the c extension replaced by an exe extension. ▫ So, the command;  MyFirstProgram.out in UNIX  MyFirstProgram.exe in Windows has the effect of loading the program called MyFirstProgram.out/ MyFirstProgram.exe into the computer’s memory and initiating its execution. 17
  • 18. Compiling Programs (Cont…) ▫ When the program is executed, each of the statements of the program is sequentially executed in turn. ▫ If the program requests any data from the user, known as input, the program temporarily suspends its execution so that the input can be entered. ▫ Or, the program might simply wait for an event, such as a mouse being clicked, to occur. ▫ Results that are displayed by the program, known as output, appear in a window, sometimes called the console. ▫ Or, the output might be directly written to a file on the system. 18
  • 19. Compiling Programs (Cont…) • Debugging ▫ If all goes well (and it probably won’t the first time the program is executed), the program performs its intended functions. ▫ If the program does not produce the desired results, it is necessary to go back and reanalyze the program’s logic. ▫ This is known as the debugging phase, during which an attempt is made to remove all the known problems or bugs from the program. ▫ To do this, it will most likely be necessary to make changes to the original source program. ▫ In that case, the entire process of compiling, linking, and executing the program must be repeated until the desired results are obtained. 19
  • 20. IDE • The process of editing, compiling, running, and debugging programs is often managed by a single integrated application known as an Integrated Development Environment, or IDE for short. • An IDE is a windows-based program that allows you to easily manage large software programs, edit files in windows, and compile, link, run, and debug your programs. 20
  • 21. Introduction to Quincy C • http://www.codecutter.net/tools/quincy/ • Refer Note 21
  • 22. C Program Structure 22 /* Header Filed Placed Here*/ /* Global Data Placed Here */ function 1 () { /* Local Data and C code placed here */ } Function n () { /* Local Data and C code placed here */ } main () { /* Entry Point */ /* Local Data and C code placed here */ } Every C Program consists of 1 or more functions. main() is the mandatory function in every C program. Depicts a typical single source file C program.
  • 23. First C Program #include<stdio.h> int main () { printf("Programming is Funn"); return 0; } 23 In the C programming language, lowercase and uppercase letters are distinct. CASE SENSITIVE
  • 24. First C Program (Cont…) • Compile and Run 24
  • 25. First C Program (Cont…) • Contents in the Saved Location 25
  • 26. First C Program (Cont…) #include<stdio.h> • Should be included at the beginning of just about every program you write. • It tells the compiler to include the standard input output header file stdio.h as part of the program. • It contains the information about the printf output routine that is used later in the program. 26
  • 27. First C Program (Cont…) int main (void) • Informs the system that the name of the program is main, and that it returns an integer value, which is abbreviated “int.” • Main is a special name that indicates precisely where the program is to begin execution. • The open and close parentheses immediately following main specify that main is the name of a function. • The keyword void that is enclosed in the parentheses specifies that the function main takes no arguments (that is, it is void of arguments). 27
  • 28. First C Program (Cont…) { } • Describe the boundary of the main function. • This is done by enclosing all program statements of the routine within a pair of curly braces. • All program statements included between the braces are taken as part of the main routine by the system. • You have only two statements enclosed within the braces of this program. 28
  • 29. First C Program (Cont…) printf("Programming is Funn"); • The first statement specifies that a routine named printf is to be invoked or called. • The parameter or argument to be passed to the printf routine is the string of characters “Programming is fun.n” • The printf routine is a function in the C library that simply prints or displays its argument (or arguments, as you will see shortly) on your screen. • n is the new line character 29
  • 30. First C Program (Cont…) n • Any characters to be printed after the newline character then appear on the next line of the display. • In fact, the newline character is similar in concept to the carriage return key on a typewriter. • ** All program statements in C must be terminated by a semicolon (;). 30
  • 31. First C Program (Cont…) return 0 • says to finish execution of main, and return to the system a status value of 0. • You can use any integer here. • Zero is used by convention to indicate that the program completed successfully—that is, without running into any errors. 31
  • 32. First C Program (Cont…) 32 C provides a collection of useful functions which are called as library functions. A library is split into a group of functions and each group has a .h (header) file associated with it. Often the .h file will contain other components such as type declarations. Whenever the functions in a given group are used, the group’s .h file should be included with #include<>. What are Header files ?
  • 33. Adding another Phrase #include <stdio.h> int main (void) { printf ("Programming is fun.n"); printf ("And programming in C is even more fun.n"); return 0; } 33
  • 34. Using n #include <stdio.h> int main (void) { printf ("Testing...n..1n...2n....3n"); return 0; } 34
  • 35. Comments • A comment statement is used in a program to document a program and to enhance its readability. • comments serve to tell the reader of the program; ▫ The programmer or someone else whose responsibility it is to maintain the program ▫ Just what the programmer had in mind when he or she wrote a particular program or a particular sequence of statements. 35
  • 36. Comments (Cont…) /* This program demonstrate the application */ /* of new line character */ #include <stdio.h> int main (void) { /* Display text with new line character*/ printf ("Testing...n..1n...2n....3n"); return 0; } 36
  • 37. Comments (Cont…) • Comments are ignored by the compiler. • There are two ways to insert comments; ▫ /* */ - This form of comment is often used when comments span several lines in the program. ▫ // - Any characters that follow these slashes up to the end of the line are ignored by the compiler. 37
  • 38. Comments (Cont…) • It is a good idea to get into the habit of inserting comment statements into the program as the program is being written or typed in. • There are good reasons for this; ▫ It is far easier to document the program while the particular program logic is still fresh in your mind than it is to go back and rethink the logic after the program has been completed. ▫ By inserting comments into the program at such an early stage of the game, you get to reap the benefits of the comments during the debug phase, when program logic errors are being isolated and debugged. • A comment can not only help you read through the program, but it can also help point the way to the source of the logic mistake. 38
  • 39. Objective Re-cap • Now you should be able to: ▫ Describe features of C programming language. ▫ Justify the terminology related to computer programming. ▫ Define the editing, compiling, linking, debugging stages of C programming ▫ Recognize the basic structure of a C program ▫ Apply comments for C programs to improve readability. 39
  • 40. References • Chapter 01,02 and 03 - Programming in C, 3rd Edition, Stephen G. Kochan 40
  • 41. Next: Variables and Data Types 41