SlideShare a Scribd company logo
1 of 51
SLO:8.0
Programming in C
Language
By: Alefya Murtaza
MSB Institute Haidery
MSB Educational Institute - Haidery
8.1 Introduction to programming languages
8.1.1Computer Program
• Set of instructions (statements).
• Written in a programming language to solve
a particular problem to achieve a specific
result.
• All tasks performed by a computer are
controlled by set of instructions.
• There are many languages written as a
problem-solving-tool.
8.1.2 Syntax and Semantic
•Syntax
• Syntax refers to the rules of a programming
language according to which statements are
written.
• It is a way to write a correct statement in a
program.
• It is like the grammar of a programming
language.
8.1.2 Syntax and Semantic
•Semantic
• Semantic gives meaning to the statements.
• It describe the sequence of operations to be
performed by a computer.
• E.g. sum=a+b
• The semantic of statement is to perform the
expression, i.e. add the value stored in a and b
and then store the value in variable sum
8.1.3 Programming Languages
• A programming language is a language that is
understood by computers.
• It is designed to give instructions to a computer
to perform a specific task.
• It is used to write computer programs.
• Programming language can be classified into
two categories:
1. low level languages
2. high level languages
8.1.3 Programming Languages
• Low-level languages
• It is machine-oriented language
• To understand it, detailed
knowledge of internal working of
computers required
• It includes machine language and
assembly language
8.1.3 Programming Languages
Low-level languages:
1. Machine language
• It is directly understood by computer
hardware.
• It is associated by computer architecture.
• Program written for one computer will not
run on another because of design
difference.
• It consist of 0s and 1s.
8.1.3 Programming Languages
Low-level languages:
• Assembly Language
• It consists of symbolic codes or abbreviations
known as mnemonics.
• It was developed to make programming easier
than machine language.
• Abbreviations are easy to learn and
understand.
• It must be converted into machine language by
a translator called an assembler.
8.1.3 Programming Languages
• Characteristics of Assembly language
• It allows programmers to have access to
all the special features of the computer
they are using. Such operations which is
not possible in high level language.
• It requires less storage and less running
time.
• Still best choice in some application but
its use is gradually declining.
8.1.3 Programming Languages
High-Level Language
• It is an English-oriented language.
• Commonly used to write a computer program.
• It uses English language words.
• E.g. Print, Input, Go, If, End, etc.
• Compiler or interpreter is required to translate
high-level language into machine language.
• It can be classified into:
• Procedural language
• Structured language
• object-oriented programming language.
High Level Language
• Procedural Language
• It is based on the concept of modular
programming.
• In modular programming, programs are divided
into smaller parts known as modules.
• It consists of one or more modules.
• Module has a group of statements that can be
performed one or more times in a program.
• It is easy to design, modify and debug a program
written in a procedural language.
• E.g. Fortran, Pascal, C and Basic.
8.1.3 Programming Languages
Structured Languages
• It consists of three fundamental elements which are
sequence, selection, and repetition.
• Sequence
• it means writing statements in a logical sequence.
Each statement logically progresses to the next
without producing any undesirable effects.
• Selection
• It allows the selection of any number of statements
based on the result of the evaluation of a condition
that may be true or false. E.g if-then-else
• Repetition (loop)
• It means executing one or more statements a number
o times until a condition is satisfied. E.g for-next,
while-wend.
E.g. ALGOL, PL/1,Ada, Pascal
8.1.3ProgrammingLanguages
High Level Language
Object-OrientedProgramming(OOPS)
• It refers to a programming method that is based on objects
such as student, vehicle, building, etc.
• OOP language provide a set of rules for defining and
managing objects.
• An object can be considered a thing which can perform set of
activities.
• E.G a vehicle have number of wheels, number of doors and
seats etc.
• The activities which can be performed on this object is Steer,
Accelerate, Brake etc.
• Large and complicated programs are difficult to design
develop, maintain and debug so OOP is used.
• E.g. C++,C# and Java
High Level Language
8.1.3ProgrammingLanguages
8.1.4 Characteristics of High-Level
Language
• Make programming easier
• Not machine dependable
• Must be translated into machine language by
a compiler or an interpreter
• Processing of finding and removing error is
called debugging and it is easier in high level
languages
• It is highly structured. Can be programmed in
modules and tested and run independently
8.1.5 Assembler
• An assembler is a program that takes basic
computer instructions and converts them into a
pattern of bits that the computer's processor can
use to perform its basic operations.
• It produces binary code in form of 0s and 1s.
8.1.5 Compiler
• A compiler is computer software that translates
source program into object program.
• Source code/program is written in any high level
programming language, example C, Java.
• It is created once it is translated by a compiler.
• It is understandable by computer but difficult to
understand and read by a human.
Source Object
Compiler
8.1.5 Interpreter
• It is also a software which translate high level
language in low level language.
• It translate source code line by line or one
instruction at a time.
• It executes instruction immediately then
translate next instruction.
• E.g Basic, Java Script etc.
Compiler Interpreter Assembler
8.1.5
Differentiate among assembler, compiler and an interpreter;
8.2 Programming Environment
• It is the set of processes and programming tools
used to develop computer programs.
• In the past separate tools were used for single
programs e.g. editor, compiler, linker, debugger,
etc.
• It was difficult and time-consuming.
• Now programmers use IDE – Integrated
Development Environment.
• It’s an application package which is consist of all
these options in one place.
8.2.1
IDE - IntegratedDevelopmentEnvironment
• Most of the new programming languages used IDE.
• It’s a computer program/software which brings all
the tools in one place required to create and run a
computer program.
• It makes a programmer’s life easier by grouping
together all the tasks needed for building
applications.
• Modern IDE are very and have a graphical user
interface (GUI) user-friendly
8.2 Programming environment
of C language:
• A C language IDE consists of the following
modules:
• Text editor
• Compiler
• Linker
• Loader
• Debugger
Programming environment of
C language:
• Text editor
• A text editor is a simple word- processor
that is used to create and edit source
code of a program.
• These are plain text files.
• It highlights compile errors to simplify
removing them.
Programming environment of
C language:
• Text editor
Programmingenvironmentof C language:
• Compiler:
• A compiler is computer software that
translates a C language program (source
program) into machine code (object program)
that can be understood and executed by the
computer.
• It also detects syntax errors in the source
program.
• A program written in a C language has the
extension .c (for example first.c)
• And after compilation the object program
extension will be .obj (For example first.obj)
Programmingenvironmentof C language:
•Compiler:
Source
program
Compiler
software
Object
program
Linker
software
C library
functions
Executable
Program
(.exe file)
The process of creating an executable program
Programmingenvironmentof C language:
Linker:
• Linker is a software that translates object program
into a single executable program.
Loader:
• It is a software that loads programs into memory
and then executes them.
Debugger:
• It is a software that executes a program line by line,
examines the values stored in variables, and helps
in finding and removing errors in the program.
8.2.2 Use the following menus in C
programming IDE:
• File
• Edit
• Search
• Compile
• Debug
• Project
• Options
• Window
• Help
SLO:
CA – Classroom
Activity
8.3 Programming Basics
• C is a popular and widely used programming language for
creating computer programs.
Language character set
➢Letters
• A to Z in capital letters
• a to z in small letters
• In C programming small and caps letter are distinct.
➢Digits
• 0 to 9 digits
8.3 Programming Basics
• C is a popular and widely used programming language
for creating computer programs.
Language
character set
➢Special
characters
8.3 Programming Basics
8.3.1 Define header files:
• In C language, header files contain a set of predefined standard
library functions.
• All the header files have a ‘.h’ extension. By including a header
file, we can use its contents in our program.
• Library file contains header files and each header file contains a
set of functions.
• Some commonly used header files are stdio.h, conio.h and
math.h
• Common functions included in stdio.h are printf(), scanf(), etc
• Common functions included in conio.h are clrscr(),getch() and
getche().
• Math.h header files contains sqrt(), pow(), log(), sin(), cos() and
tan().
8.3.2 Structure of a C program:
Preprocessor directives
8.3.3 Comments in C language
• It makes it easy for others to understand.
• These are ignored by the compiler.
• Generally written at the beginning of the program.
• Single Line comment
• start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the
compiler (will not be executed).
8.3.3 Comments in C language
Multiple line comment
• Start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler
8.4 Constants and variables
• 8.4.1 Differentiate between a constant and a variable in C
programming language;
Find constant and variables in the given
expression.
(i) 3x = 9
(ii) x – y = -10
Solution:
(i) In 3x = 9,
x is the variable and 9 is the constant.
If we further simplify the equation,
3x = 9
x = 9/3 = 3
then x is the variable and 3 is the constant.
(ii) x-y = -10
Here, x and y are the variables and -10 is the
constant value.
8.4.2 Variables in C language:
• In programming, a variable is a container (storage area) to
hold data.
• To indicate the storage area, each variable should be given a
unique name (identifier). Variable names are just the symbolic
representation of a memory location.
• In C, there are different types of variables (defined with
different keywords), for example:
• int - stores integers (whole numbers), without decimals, such
as 123 or -123
• float - stores floating point numbers, with decimals, such as
19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values
are surrounded by single quotes
Declaring Variables
To create a variable, specify the type and assign it
a value:
Syntax:
type variableName = value;
Example
Create a variable called myNum of type int and
assign the value 15 to it:
int myNum = 15;
Declaring Variables
• You can also declare a variable without assigning
the value, and assign the value later:
Example
int myNum;
myNum = 15;
Format Specifiers
• Format specifiers are used together with
the printf() function to tell the compiler what type of
data the variable is storing.
• It is basically a placeholder for the variable value.
• A format specifier starts with a percentage sign %,
followed by a character.
• For example, to output the value of an int variable, you
must use the format specifier %d or %i surrounded by
double quotes, inside the printf() function:
Example
int myNum = 15;
printf("%d", myNum); // Outputs 15
8.4.2 General rules for specifying
variables in C
The general rules for naming variables are:
• Names can contain letters, digits, and underscores
• Names must begin with a letter or an underscore (_)
• Names are case sensitive (myVar and myvar are
different variables)
• Names cannot contain whitespaces or special
characters like !, #, %, etc.
• Reserved words (such as int) cannot be used as names
8.4.3. Data typesin C
1. Integer data type
8.4.3. Data typesin C
2. Floating-point data type
8.4.3. Data typesin C
3. Character data type
8.4.5 What are implicit and explicit type
conversions in C language?
• Implicit type casting means conversion
of data types without losing their original
meaning. This type of typecasting is
essential when you want to change data
types without changing the significance
of the values stored inside the variable.
8.4.5 What are implicit and explicit type
conversions in C language?
In implicit type conversion, the data type is converted
automatically. There are some scenarios in which we may
have to force-type conversion. Suppose we have a variable
div that stores the division of two operands which are
declared as an int data type.
int result, var1=10, var2=3; result=var1/var2;
In this case, after the division performed on variables var1
and var2 the result stored in the variable “result” will be in
an integer format. Whenever this happens, the value stored
in the variable “result” loses its meaning because it does not
consider the fraction part which is normally obtained in the
division of two numbers.
To force the type conversion in such situations, we use
explicit type casting.
1. We have initialized a variable ‘a’ of type float.
2. Next, we have another variable ‘b’ of integer data type.
Since the variables ‘a’ and ‘b’ are of different data types,
‘C’ won’t allow the use of such an expression and it will
raise an error. In some versions of ‘C,’ the expression will
be evaluated but the result will not be desired.
3. To avoid such situations, we have to typecast the
variable ‘a’ of type float. By using explicit type casting
methods, we have successfully converted float into data
type integer.
4. We have printed value of ‘a’ which is still a float
5. After typecasting, the result will always be an integer
‘b.’
In this way, we can implement explicit type casting in C
programming.
• Task 1:
• Write a simple program using printf command
to print your name, school, class without
initializing the variables.
• Use n to print on each output on a new line.
• Make the flowchart of the same.
• Copy down the program in your notebook
along with the flowchart.
• Task 2:
• Now make the same program by declaring
variables.
• Copy down the program in your notebook
along with the flowchart.
X-CS-8.0 Programming in C Language 2022-2023.pdf

More Related Content

Similar to X-CS-8.0 Programming in C Language 2022-2023.pdf

Similar to X-CS-8.0 Programming in C Language 2022-2023.pdf (20)

Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Computer Programming In C.pptx
Computer Programming In C.pptxComputer Programming In C.pptx
Computer Programming In C.pptx
 
Presentation-1.pptx
Presentation-1.pptxPresentation-1.pptx
Presentation-1.pptx
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdf
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Chapter1.pdf
Chapter1.pdfChapter1.pdf
Chapter1.pdf
 
Python-L1.pptx
Python-L1.pptxPython-L1.pptx
Python-L1.pptx
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures
 
C.pdf
C.pdfC.pdf
C.pdf
 
Life cycle of a computer program
Life cycle of a computer programLife cycle of a computer program
Life cycle of a computer program
 
Language processors
Language processorsLanguage processors
Language processors
 
introduction computer programming languages
introduction computer programming languages introduction computer programming languages
introduction computer programming languages
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
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
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
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
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

X-CS-8.0 Programming in C Language 2022-2023.pdf

  • 1. SLO:8.0 Programming in C Language By: Alefya Murtaza MSB Institute Haidery MSB Educational Institute - Haidery
  • 2. 8.1 Introduction to programming languages 8.1.1Computer Program • Set of instructions (statements). • Written in a programming language to solve a particular problem to achieve a specific result. • All tasks performed by a computer are controlled by set of instructions. • There are many languages written as a problem-solving-tool.
  • 3. 8.1.2 Syntax and Semantic •Syntax • Syntax refers to the rules of a programming language according to which statements are written. • It is a way to write a correct statement in a program. • It is like the grammar of a programming language.
  • 4. 8.1.2 Syntax and Semantic •Semantic • Semantic gives meaning to the statements. • It describe the sequence of operations to be performed by a computer. • E.g. sum=a+b • The semantic of statement is to perform the expression, i.e. add the value stored in a and b and then store the value in variable sum
  • 5. 8.1.3 Programming Languages • A programming language is a language that is understood by computers. • It is designed to give instructions to a computer to perform a specific task. • It is used to write computer programs. • Programming language can be classified into two categories: 1. low level languages 2. high level languages
  • 6. 8.1.3 Programming Languages • Low-level languages • It is machine-oriented language • To understand it, detailed knowledge of internal working of computers required • It includes machine language and assembly language
  • 7. 8.1.3 Programming Languages Low-level languages: 1. Machine language • It is directly understood by computer hardware. • It is associated by computer architecture. • Program written for one computer will not run on another because of design difference. • It consist of 0s and 1s.
  • 8. 8.1.3 Programming Languages Low-level languages: • Assembly Language • It consists of symbolic codes or abbreviations known as mnemonics. • It was developed to make programming easier than machine language. • Abbreviations are easy to learn and understand. • It must be converted into machine language by a translator called an assembler.
  • 9. 8.1.3 Programming Languages • Characteristics of Assembly language • It allows programmers to have access to all the special features of the computer they are using. Such operations which is not possible in high level language. • It requires less storage and less running time. • Still best choice in some application but its use is gradually declining.
  • 10. 8.1.3 Programming Languages High-Level Language • It is an English-oriented language. • Commonly used to write a computer program. • It uses English language words. • E.g. Print, Input, Go, If, End, etc. • Compiler or interpreter is required to translate high-level language into machine language. • It can be classified into: • Procedural language • Structured language • object-oriented programming language.
  • 11. High Level Language • Procedural Language • It is based on the concept of modular programming. • In modular programming, programs are divided into smaller parts known as modules. • It consists of one or more modules. • Module has a group of statements that can be performed one or more times in a program. • It is easy to design, modify and debug a program written in a procedural language. • E.g. Fortran, Pascal, C and Basic. 8.1.3 Programming Languages
  • 12. Structured Languages • It consists of three fundamental elements which are sequence, selection, and repetition. • Sequence • it means writing statements in a logical sequence. Each statement logically progresses to the next without producing any undesirable effects. • Selection • It allows the selection of any number of statements based on the result of the evaluation of a condition that may be true or false. E.g if-then-else • Repetition (loop) • It means executing one or more statements a number o times until a condition is satisfied. E.g for-next, while-wend. E.g. ALGOL, PL/1,Ada, Pascal 8.1.3ProgrammingLanguages High Level Language
  • 13. Object-OrientedProgramming(OOPS) • It refers to a programming method that is based on objects such as student, vehicle, building, etc. • OOP language provide a set of rules for defining and managing objects. • An object can be considered a thing which can perform set of activities. • E.G a vehicle have number of wheels, number of doors and seats etc. • The activities which can be performed on this object is Steer, Accelerate, Brake etc. • Large and complicated programs are difficult to design develop, maintain and debug so OOP is used. • E.g. C++,C# and Java High Level Language 8.1.3ProgrammingLanguages
  • 14. 8.1.4 Characteristics of High-Level Language • Make programming easier • Not machine dependable • Must be translated into machine language by a compiler or an interpreter • Processing of finding and removing error is called debugging and it is easier in high level languages • It is highly structured. Can be programmed in modules and tested and run independently
  • 15. 8.1.5 Assembler • An assembler is a program that takes basic computer instructions and converts them into a pattern of bits that the computer's processor can use to perform its basic operations. • It produces binary code in form of 0s and 1s.
  • 16. 8.1.5 Compiler • A compiler is computer software that translates source program into object program. • Source code/program is written in any high level programming language, example C, Java. • It is created once it is translated by a compiler. • It is understandable by computer but difficult to understand and read by a human. Source Object Compiler
  • 17. 8.1.5 Interpreter • It is also a software which translate high level language in low level language. • It translate source code line by line or one instruction at a time. • It executes instruction immediately then translate next instruction. • E.g Basic, Java Script etc.
  • 18. Compiler Interpreter Assembler 8.1.5 Differentiate among assembler, compiler and an interpreter;
  • 19. 8.2 Programming Environment • It is the set of processes and programming tools used to develop computer programs. • In the past separate tools were used for single programs e.g. editor, compiler, linker, debugger, etc. • It was difficult and time-consuming. • Now programmers use IDE – Integrated Development Environment. • It’s an application package which is consist of all these options in one place.
  • 20. 8.2.1 IDE - IntegratedDevelopmentEnvironment • Most of the new programming languages used IDE. • It’s a computer program/software which brings all the tools in one place required to create and run a computer program. • It makes a programmer’s life easier by grouping together all the tasks needed for building applications. • Modern IDE are very and have a graphical user interface (GUI) user-friendly
  • 21. 8.2 Programming environment of C language: • A C language IDE consists of the following modules: • Text editor • Compiler • Linker • Loader • Debugger
  • 22. Programming environment of C language: • Text editor • A text editor is a simple word- processor that is used to create and edit source code of a program. • These are plain text files. • It highlights compile errors to simplify removing them.
  • 23. Programming environment of C language: • Text editor
  • 24. Programmingenvironmentof C language: • Compiler: • A compiler is computer software that translates a C language program (source program) into machine code (object program) that can be understood and executed by the computer. • It also detects syntax errors in the source program. • A program written in a C language has the extension .c (for example first.c) • And after compilation the object program extension will be .obj (For example first.obj)
  • 25. Programmingenvironmentof C language: •Compiler: Source program Compiler software Object program Linker software C library functions Executable Program (.exe file) The process of creating an executable program
  • 26. Programmingenvironmentof C language: Linker: • Linker is a software that translates object program into a single executable program. Loader: • It is a software that loads programs into memory and then executes them. Debugger: • It is a software that executes a program line by line, examines the values stored in variables, and helps in finding and removing errors in the program.
  • 27. 8.2.2 Use the following menus in C programming IDE: • File • Edit • Search • Compile • Debug • Project • Options • Window • Help SLO: CA – Classroom Activity
  • 28. 8.3 Programming Basics • C is a popular and widely used programming language for creating computer programs. Language character set ➢Letters • A to Z in capital letters • a to z in small letters • In C programming small and caps letter are distinct. ➢Digits • 0 to 9 digits
  • 29. 8.3 Programming Basics • C is a popular and widely used programming language for creating computer programs. Language character set ➢Special characters
  • 30. 8.3 Programming Basics 8.3.1 Define header files: • In C language, header files contain a set of predefined standard library functions. • All the header files have a ‘.h’ extension. By including a header file, we can use its contents in our program. • Library file contains header files and each header file contains a set of functions. • Some commonly used header files are stdio.h, conio.h and math.h • Common functions included in stdio.h are printf(), scanf(), etc • Common functions included in conio.h are clrscr(),getch() and getche(). • Math.h header files contains sqrt(), pow(), log(), sin(), cos() and tan().
  • 31. 8.3.2 Structure of a C program: Preprocessor directives
  • 32. 8.3.3 Comments in C language • It makes it easy for others to understand. • These are ignored by the compiler. • Generally written at the beginning of the program. • Single Line comment • start with two forward slashes (//). • Any text between // and the end of the line is ignored by the compiler (will not be executed).
  • 33. 8.3.3 Comments in C language Multiple line comment • Start with /* and ends with */. • Any text between /* and */ will be ignored by the compiler
  • 34. 8.4 Constants and variables • 8.4.1 Differentiate between a constant and a variable in C programming language;
  • 35. Find constant and variables in the given expression. (i) 3x = 9 (ii) x – y = -10 Solution: (i) In 3x = 9, x is the variable and 9 is the constant. If we further simplify the equation, 3x = 9 x = 9/3 = 3 then x is the variable and 3 is the constant. (ii) x-y = -10 Here, x and y are the variables and -10 is the constant value.
  • 36. 8.4.2 Variables in C language: • In programming, a variable is a container (storage area) to hold data. • To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. • In C, there are different types of variables (defined with different keywords), for example: • int - stores integers (whole numbers), without decimals, such as 123 or -123 • float - stores floating point numbers, with decimals, such as 19.99 or -19.99 • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • 37. Declaring Variables To create a variable, specify the type and assign it a value: Syntax: type variableName = value; Example Create a variable called myNum of type int and assign the value 15 to it: int myNum = 15;
  • 38. Declaring Variables • You can also declare a variable without assigning the value, and assign the value later: Example int myNum; myNum = 15;
  • 39. Format Specifiers • Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. • It is basically a placeholder for the variable value. • A format specifier starts with a percentage sign %, followed by a character. • For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes, inside the printf() function: Example int myNum = 15; printf("%d", myNum); // Outputs 15
  • 40.
  • 41. 8.4.2 General rules for specifying variables in C The general rules for naming variables are: • Names can contain letters, digits, and underscores • Names must begin with a letter or an underscore (_) • Names are case sensitive (myVar and myvar are different variables) • Names cannot contain whitespaces or special characters like !, #, %, etc. • Reserved words (such as int) cannot be used as names
  • 42. 8.4.3. Data typesin C 1. Integer data type
  • 43. 8.4.3. Data typesin C 2. Floating-point data type
  • 44. 8.4.3. Data typesin C 3. Character data type
  • 45.
  • 46. 8.4.5 What are implicit and explicit type conversions in C language? • Implicit type casting means conversion of data types without losing their original meaning. This type of typecasting is essential when you want to change data types without changing the significance of the values stored inside the variable.
  • 47.
  • 48. 8.4.5 What are implicit and explicit type conversions in C language? In implicit type conversion, the data type is converted automatically. There are some scenarios in which we may have to force-type conversion. Suppose we have a variable div that stores the division of two operands which are declared as an int data type. int result, var1=10, var2=3; result=var1/var2; In this case, after the division performed on variables var1 and var2 the result stored in the variable “result” will be in an integer format. Whenever this happens, the value stored in the variable “result” loses its meaning because it does not consider the fraction part which is normally obtained in the division of two numbers. To force the type conversion in such situations, we use explicit type casting.
  • 49. 1. We have initialized a variable ‘a’ of type float. 2. Next, we have another variable ‘b’ of integer data type. Since the variables ‘a’ and ‘b’ are of different data types, ‘C’ won’t allow the use of such an expression and it will raise an error. In some versions of ‘C,’ the expression will be evaluated but the result will not be desired. 3. To avoid such situations, we have to typecast the variable ‘a’ of type float. By using explicit type casting methods, we have successfully converted float into data type integer. 4. We have printed value of ‘a’ which is still a float 5. After typecasting, the result will always be an integer ‘b.’ In this way, we can implement explicit type casting in C programming.
  • 50. • Task 1: • Write a simple program using printf command to print your name, school, class without initializing the variables. • Use n to print on each output on a new line. • Make the flowchart of the same. • Copy down the program in your notebook along with the flowchart. • Task 2: • Now make the same program by declaring variables. • Copy down the program in your notebook along with the flowchart.