SlideShare a Scribd company logo
Overview to C Language
11
2.1 Brief History of C
C’s ancestors include Combined Programming Language (CPL), Basic Combined
Programming Language (BCPL), B, and ALGOL 68. CPL was developed at
Cambridge University in the early 1960’s. BCPL is a simple systems language
developed by Martin Richards in 1967 [SEBE96]. A systems language is a
programming language used to create operating systems (like Windows and DOS)
and compilers (like Visual Basic). ALGOL68 or Algorithmic Language, on the other
hand, was developed for scientific applications.
The first high-level language implemented under the UNIX operating system
was designed and implemented in 1970 in Bell Laboratories by Ken Thompson. This
was B, which was based on BCPL.
Neither BCPL nor B is a typed language. Being untyped means that all data
are considered machine words, which may be simple but leads to many
complications. Because of this complication and several other problems, it led to the
development of a new typed language based on B. Originally called NB (New B) but
later named C, it was designed and implemented by Dennis Ritchie at Bell
Laboratories in 1972. In some cases through BCPL, and in other cases directly, C
was influenced by ALGOL 68.
For more than a decade since C’s inception, the only standard was the book by
Kernighan and Ritchie. Over time, several implementers added different features thus
creating different versions of C. Between 1982 and 1988, ANSI produced a new
official description of C which included many of the features that implementors had
already incorporated into the language.
2.2 Types of Data
A C program is composed of a sequence of characters that are grouped by a compiler
into identifiable tokens. These tokens can be classified as literals, identifiers, and
keywords.
Timeline:
1963 1967 1968 1970 1972
CPL BCPL ALGOL68 B C
Chapter 2
12
2.2.1 Literals
Literals are classified under numeric and non-numeric literals.
2.2.1.1 Numeric Literals
Numeric literals can be further subdivided into whole numbers and real numbers.
Whole numbers are also called integers. These types of numbers do not contain any
fractional part and it should not have a decimal point. Real numbers, on the other
hand, are also called floating point numbers. These numbers have fractional parts and
it could also be expressed using scientific notation.
Exponential representation may be used to represent real numbers. An
exponential representation consists of an integer or real value followed by e then an
integer value in the exponential base 10.
Example. 2.5e2 is the same as 2.5 x 10
2
, which is equivalent to 250.0
2e4 is the same as 2 x 10
4
, which is equivalent to 20000.0
2e0 is the same as 2 x 10
0
, which is equivalent to 2.0
2.e5 is the same as 2.0 x 10
5
, which is equivalent to 200000.0
In defining numeric literals, the following rules should be followed:
1. No comma.
2. No space between the unary sign (+ or -) and the digits.
3. Must begin and end with a digit.
2.2.1.2 Non-Numeric Literals
Non-numeric literals may be in the form of a character or a series of characters. A
series of characters is called a string. A string should be enclosed in double quotes,
while a character is enclosed in single quotes. Some special characters may be used,
but it should be preceded by the escape character  (backslash).
Example. ‘a’
‘I’
‘+’
‘2’
“De La Salle University”
“a string with double quotes ” within”
“a single backslash  is in this string”
Overview to C Language
13
Other escape characters are listed below.
Escape Character Meaning
a alert
t tab
0 null character
’ single quote
% percent symbol
2.2.2 Identifiers
Identifiers are composed of a sequence of letters, digits, and the special character _
(underscore). Identifiers are defined by the programmer, thus they should be
descriptive. Avoid using names that are too short or too long. Limit the identifiers
from 8 to 15 characters only.
Some rules must be followed for defining identifiers. They are as follows:
1. It must consist only of letters, digits, and underscores.
Correct Example Incorrect Example
_duh x-1
thisisanidentifier2 num#
a large num
num_1 y?
2. An identifier cannot begin with a digit.
Incorrect Identifier
1name
3to4
3. An identifier defined in the C standard library should not be redefined.
Incorrect Identifier
printf
scanf
4. Identifiers are case sensitive; meaning uppercase is not equal to the
lowercase.
Example. ans ≠ Ans ≠ aNs ≠ anS ≠ ANs ≠ ANS
Chapter 2
14
2.2.2.1 Variables
Variables are identifiers that can store a value that can be changed. These can be of
different data types. They can store literals or some type of structured data.
Structured data can contain one or more values or data.
2.2.2.2 Constants
Constants are identifiers that can store a value that cannot be changed. Usually,
constant identifiers are all capital letters with underscores between each word to
distinguish them from variables.
2.2.3 Keywords
Keywords are words that have a strict meaning in C. These are special words
“reserved” by the programming language for processing, thus these may not be
redefined by the programmer. Some keywords are listed below.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
2.3 C Expressions
In the C language, expressions may either be arithmetic or logical, but the result
would be an integer or a real value.
2.3.1 Arithmetic Expressions
The following table will show the different operators and their uses. The rule shows
the type of operands where the operator can be used and the resulting data type given
the operands.
Overview to C Language
15
Operator Meaning Rule Example
+ addition integer + integer = integer
integer + real = real
real + integer = real
real + real = real
5 + 2 = 7
5 + 2.0 = 7.0
5.0 + 2 = 7.0
5.0 + 2.0 = 7.0
- subtraction integer - integer = integer
integer - real = real
real - integer = real
real - real = real
5 - 2 = 3
5 - 2.0 = 3.0
5.0 - 2 = 3.0
5.0 - 2.0 = 3.0
* multiplication integer * integer = integer
integer * real = real
real * integer = real
real * real = real
5 * 2 = 10
5 * 2.0 = 10.0
5.0 * 2 = 10.0
5.0 * 2.0 = 10.0
/ division integer / integer = integer
integer / real = real
real / integer = real
real / real = real
5 / 2 = 2
5 / 2.0 = 2.5
5.0 / 2 = 2.5
5.0 / 2.0 = 2.5
% remainder integer % integer = integer 5 % 2 = 1
-5 % 2 = -1
5 % -2 = 1
-5 % -2 = -1
In evaluating arithmetic expressions, the following rules should follow:
1. Parenthesis First. Evaluate expressions that are enclosed in parenthesis.
If there are nested parenthesis, evaluate from inside out.
2. Operator Precedence. Evaluation of operators should follow a hierarchy
of priorities. Evaluate expressions with higher priority operators first.
unary +, - (positive and negative) highest
*, /, %
binary +, - (addition and subtraction) lowest
3. Associativity Rule. If expression to be evaluated have operators that are in
the same precedence level, evaluate the expression from left to right.
Chapter 2
16
Example. z = 8 a = 3 b = 9 w = 2 y = -5
z – ( a + b / 2 ) + w * -y
8 3 9 2 -5
4
7
5
10
1
11
2.3.2 Logical and Relational Expressions
In C, evaluation of logical and relational expressions return 0 for false and 1 (or any
nonzero value) for true.
The relational and equality operators are shown below.
Relational Operators Equality Operators
< less than == equal
<= less than or equal != not equal
> greater than
>= greater than or equal
The following shows the logical operators and their corresponding truth
tables.
Logical Operator Meaning Truth Table
&& and true && true = true
true && false = false
false && true = false
false && false = false
|| or true || true = true
true || false = true
false || true = true
false || false = false
! not !(true) = false
!(false) = true
Overview to C Language
17
Example.
salary < MIN_SALARY || dependents > 5
temperature > 90.0 && humidity > 0.90
n >= 0 && n <= 100
!(0 <= n && n <= 100)
Rules in evaluating logical and relational expressions are similar with
evaluating arithmetic expressions. These operators also follow precedence rules. The
updated list is as follows:
Operator Precedence
( ) highest
!, unary +, -
*, /, %
binary +, -
<, <=, >, >=
==, !=
&&
|| lowest
Example. flag = 0 y = 4.0 z = 2.0 x = 3.0
!flag || ( y + z >= x – z )
0 4.0 2.0 3.0 2.0
6.0
1.0
1
1
1
2.3.3 Converting Mathematical Formula to C Expression
To solve mathematical problems using the computer, the formula should be translated
to the programming language to be used. In this case, arithmetic operations should be
in C expressions.
Example.
b
2
– 4ac b * b – 4 * a * c
a+b
c+d
(a + b) / (c + d)
1
1+x
2 1 / (1 + x * x)
a x –(b + c) a * -(b + c)
Chapter 2
18
2.3.4 Converting English Conditions to C Expression
Solutions to problems may sometimes depend on a set of conditions. To use the
computer to solve such problems, these conditions should be converted to the C.
Example.
x and y are greater than z x > z && y > z
x is equal to 1.0 or 3.0 x == 1.0 || x == 3.0
x is in the range of z to y, inclusive z <= x && x <= y
x is outside the range of z to y !(z <= x && x <= y)
z > x || x > y
Self Evaluation Exercises
1. Determine if the following identifiers are valid or invalid.
a) L8r
b) star*ting
c) num_Values
d) 4u
e) one_i_aren’t
2. Evaluate the following expressions
a.) 10 + 23 % (17 – 4 * 2) / (24 – (7 + 15 % 2))
b.) 150 - (-6 + 8 * 4 – 22 % 4) – (5 – (15.2 / 2))
c.) (7 == 7.0) && ( 15 > 3) || !((7 >4) || (7 > 3))
d.) (8 > 13 % 3) || (7 > 22 % 3) && (5 == 30 / 6)
3. Convert the following mathematical equations to C expressions without adding
unnecessary parenthesis
a.) 1 + X
1 + 1
6 8
b.) (X)(Y)(Z)
(X
2
– Y
2
) + Y
4 + X
2 – Z
4. Convert the following statements to C expressions
a.) X is neither 6 nor 8
b.) X is any number except 1, 2, and 3
c.) REVENUE is at most 80% of SALES
d.) contestant’s HEIGHT is at least 175 cm and AGE is between 18 and 23,
inclusive
e.) X is between 100 and 200, exclusive, except 120, 130, and 180
Overview to C Language
19
5. Write the C statement that will convert an amount in dollars to its peso equivalent.
Assume that PhP1 is equal to $51.75.
Chapter Exercises
1. Determine if the following identifiers are valid or invalid.
a) 3id
b) 1_i_am
c) R3D3
d) int
e) per-capita
f) o_no_o_no
g) me_to-2
h) xYshouldI
i) phone#
j) MAX_SPEED
k) G
l) __yes
2. Determine if the following are valid or invalid whole number literals.
a) -10500
b) 435
c) 2,020
d) +50,000
e) 21.5
3. Determine if the following are valid or invalid real literals
a) 2.34e2
b) 15e-0.3
c) 125
d) 34,500.99
e) 0.005
4. Determine if the following are valid or invalid character literals.
a) ‘M’
b) ‘n1’
c) ‘’
d) ‘”’
e) ‘+’
f) ‘&’
5. Given x = 2.0 and y = 3.0, evaluate the following:
a) 2 – 4 * 3 + 26 / 2
b) (3 + 4) * x / 2 + y
c) 5 + 6.6 / 2.2 * 0.5
Chapter 2
20
6. Given i = 1, j = 2, k = 3, x = 5.5, and y = 7.7, evaluate the following whether they
yield a value of TRUE/FALSE:
a) i < (j – k)
b) (x – y) <= ((j – k) – 1)
c) (k + j) != (i + 1 * 4)
d) ! (1 == 1)
e) i && j
f) i == j && i + j == k || y == x + 2
g) –i <= j – k && !j
7. Assume the values a = 1, b = 2, c = 0, d = 5.0, and e = 25. What is the output of the
following:
a) a + b * c && a
b) 3 / a + b / e || c
c) 10 + 15 || 0 && 5 > 3 + 3
d) 1 + 2 > 3 * 4 || 5 && 3 > 4 == 0
e) 1 % 2 + 1 == 0 + 1 && 2
8. Convert the following conditions to C expressions:
a) Commission = (sales – sales X .10) * .25
b) Commission = (sales – sales X 10/100) * 25/100
c) Interest = Amount X Rate
d) Semiannual Interest = 600/10%
e) age is from 18 to 21 inclusive
f) water is less than 1.5 and also greater than 0.1
g) year is divisible by 4
h) speed is not greater than 55
i) y is greater than x and less than z
j) w is either equal to 6 or not greater than 3
9. Write the C statement that will compute for the area of a triangle given the base and
the height.
10. Write the C statement that will convert a Fahrenheit (F) measure to a Celsius (C)
measure. (C = 5/9 x (F – 32))
11. Write the C statement that will convert an amount in peso to its dollar equivalent.
Assume that PhP1 is equal to $51.75.
12. Write the C statement(s) that will compute the least number of Php5 and Php1 coins
given an amount. Example: There are 3 Php5 and 2 Php1 in Php17.
Ad

More Related Content

Similar to Overview of C Language (20)

C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
HNDE Labuduwa Galle
 
C program
C programC program
C program
AJAL A J
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
HINAPARVEENAlXC
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming Introduction
Honey Sharma
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
Walepak Ubi
 
Fundamentals of computers - C Programming
Fundamentals of computers - C ProgrammingFundamentals of computers - C Programming
Fundamentals of computers - C Programming
MSridhar18
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
SONU KUMAR
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
fundamentals of c
fundamentals of cfundamentals of c
fundamentals of c
Vijayalaxmi Wakode
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
Fiaz Khokhar
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
Tareq Hasan
 
C basics
C basicsC basics
C basics
sridevi5983
 
C basics
C basicsC basics
C basics
sridevi5983
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
C fundamental
C fundamentalC fundamental
C fundamental
Selvam Edwin
 
Week 02_Development Environment of C++.pdf
Week 02_Development Environment of C++.pdfWeek 02_Development Environment of C++.pdf
Week 02_Development Environment of C++.pdf
salmankhizar3
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming Introduction
Honey Sharma
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
Walepak Ubi
 
Fundamentals of computers - C Programming
Fundamentals of computers - C ProgrammingFundamentals of computers - C Programming
Fundamentals of computers - C Programming
MSridhar18
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
SONU KUMAR
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
Fiaz Khokhar
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
Tareq Hasan
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
Week 02_Development Environment of C++.pdf
Week 02_Development Environment of C++.pdfWeek 02_Development Environment of C++.pdf
Week 02_Development Environment of C++.pdf
salmankhizar3
 

More from Prof. Erwin Globio (20)

Embedded System Presentation
Embedded System PresentationEmbedded System Presentation
Embedded System Presentation
Prof. Erwin Globio
 
Internet of Things
Internet of ThingsInternet of Things
Internet of Things
Prof. Erwin Globio
 
Sq lite presentation
Sq lite presentationSq lite presentation
Sq lite presentation
Prof. Erwin Globio
 
Ethics for IT Professionals
Ethics for IT ProfessionalsEthics for IT Professionals
Ethics for IT Professionals
Prof. Erwin Globio
 
Cisco Router Basic Configuration
Cisco Router Basic ConfigurationCisco Router Basic Configuration
Cisco Router Basic Configuration
Prof. Erwin Globio
 
Introduction to iOS Apps Development
Introduction to iOS Apps DevelopmentIntroduction to iOS Apps Development
Introduction to iOS Apps Development
Prof. Erwin Globio
 
Cloud Computing Latest
Cloud Computing LatestCloud Computing Latest
Cloud Computing Latest
Prof. Erwin Globio
 
Introduction to Android Development Latest
Introduction to Android Development LatestIntroduction to Android Development Latest
Introduction to Android Development Latest
Prof. Erwin Globio
 
iOS Apps Development (SQLite Tutorial Part 2)
iOS Apps Development (SQLite Tutorial Part 2)iOS Apps Development (SQLite Tutorial Part 2)
iOS Apps Development (SQLite Tutorial Part 2)
Prof. Erwin Globio
 
iOS Apps Development (SQLite Tutorial Part 1)
iOS Apps Development (SQLite Tutorial Part 1)iOS Apps Development (SQLite Tutorial Part 1)
iOS Apps Development (SQLite Tutorial Part 1)
Prof. Erwin Globio
 
A tutorial on C++ Programming
A tutorial on C++ ProgrammingA tutorial on C++ Programming
A tutorial on C++ Programming
Prof. Erwin Globio
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
Prof. Erwin Globio
 
Android Fragments
Android FragmentsAndroid Fragments
Android Fragments
Prof. Erwin Globio
 
Solutions to Common Android Problems
Solutions to Common Android ProblemsSolutions to Common Android Problems
Solutions to Common Android Problems
Prof. Erwin Globio
 
Android Development Tools and Installation
Android Development Tools and InstallationAndroid Development Tools and Installation
Android Development Tools and Installation
Prof. Erwin Globio
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
Action Bar in Android
Action Bar in AndroidAction Bar in Android
Action Bar in Android
Prof. Erwin Globio
 
Resource Speaker
Resource SpeakerResource Speaker
Resource Speaker
Prof. Erwin Globio
 
Guidelines to Qualitative Researches
Guidelines to Qualitative ResearchesGuidelines to Qualitative Researches
Guidelines to Qualitative Researches
Prof. Erwin Globio
 
PSITE Letter for Prof Globio
PSITE Letter for Prof GlobioPSITE Letter for Prof Globio
PSITE Letter for Prof Globio
Prof. Erwin Globio
 
Cisco Router Basic Configuration
Cisco Router Basic ConfigurationCisco Router Basic Configuration
Cisco Router Basic Configuration
Prof. Erwin Globio
 
Introduction to iOS Apps Development
Introduction to iOS Apps DevelopmentIntroduction to iOS Apps Development
Introduction to iOS Apps Development
Prof. Erwin Globio
 
Introduction to Android Development Latest
Introduction to Android Development LatestIntroduction to Android Development Latest
Introduction to Android Development Latest
Prof. Erwin Globio
 
iOS Apps Development (SQLite Tutorial Part 2)
iOS Apps Development (SQLite Tutorial Part 2)iOS Apps Development (SQLite Tutorial Part 2)
iOS Apps Development (SQLite Tutorial Part 2)
Prof. Erwin Globio
 
iOS Apps Development (SQLite Tutorial Part 1)
iOS Apps Development (SQLite Tutorial Part 1)iOS Apps Development (SQLite Tutorial Part 1)
iOS Apps Development (SQLite Tutorial Part 1)
Prof. Erwin Globio
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
Prof. Erwin Globio
 
Solutions to Common Android Problems
Solutions to Common Android ProblemsSolutions to Common Android Problems
Solutions to Common Android Problems
Prof. Erwin Globio
 
Android Development Tools and Installation
Android Development Tools and InstallationAndroid Development Tools and Installation
Android Development Tools and Installation
Prof. Erwin Globio
 
Guidelines to Qualitative Researches
Guidelines to Qualitative ResearchesGuidelines to Qualitative Researches
Guidelines to Qualitative Researches
Prof. Erwin Globio
 
Ad

Recently uploaded (20)

U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Ad

Overview of C Language

  • 1. Overview to C Language 11 2.1 Brief History of C C’s ancestors include Combined Programming Language (CPL), Basic Combined Programming Language (BCPL), B, and ALGOL 68. CPL was developed at Cambridge University in the early 1960’s. BCPL is a simple systems language developed by Martin Richards in 1967 [SEBE96]. A systems language is a programming language used to create operating systems (like Windows and DOS) and compilers (like Visual Basic). ALGOL68 or Algorithmic Language, on the other hand, was developed for scientific applications. The first high-level language implemented under the UNIX operating system was designed and implemented in 1970 in Bell Laboratories by Ken Thompson. This was B, which was based on BCPL. Neither BCPL nor B is a typed language. Being untyped means that all data are considered machine words, which may be simple but leads to many complications. Because of this complication and several other problems, it led to the development of a new typed language based on B. Originally called NB (New B) but later named C, it was designed and implemented by Dennis Ritchie at Bell Laboratories in 1972. In some cases through BCPL, and in other cases directly, C was influenced by ALGOL 68. For more than a decade since C’s inception, the only standard was the book by Kernighan and Ritchie. Over time, several implementers added different features thus creating different versions of C. Between 1982 and 1988, ANSI produced a new official description of C which included many of the features that implementors had already incorporated into the language. 2.2 Types of Data A C program is composed of a sequence of characters that are grouped by a compiler into identifiable tokens. These tokens can be classified as literals, identifiers, and keywords. Timeline: 1963 1967 1968 1970 1972 CPL BCPL ALGOL68 B C
  • 2. Chapter 2 12 2.2.1 Literals Literals are classified under numeric and non-numeric literals. 2.2.1.1 Numeric Literals Numeric literals can be further subdivided into whole numbers and real numbers. Whole numbers are also called integers. These types of numbers do not contain any fractional part and it should not have a decimal point. Real numbers, on the other hand, are also called floating point numbers. These numbers have fractional parts and it could also be expressed using scientific notation. Exponential representation may be used to represent real numbers. An exponential representation consists of an integer or real value followed by e then an integer value in the exponential base 10. Example. 2.5e2 is the same as 2.5 x 10 2 , which is equivalent to 250.0 2e4 is the same as 2 x 10 4 , which is equivalent to 20000.0 2e0 is the same as 2 x 10 0 , which is equivalent to 2.0 2.e5 is the same as 2.0 x 10 5 , which is equivalent to 200000.0 In defining numeric literals, the following rules should be followed: 1. No comma. 2. No space between the unary sign (+ or -) and the digits. 3. Must begin and end with a digit. 2.2.1.2 Non-Numeric Literals Non-numeric literals may be in the form of a character or a series of characters. A series of characters is called a string. A string should be enclosed in double quotes, while a character is enclosed in single quotes. Some special characters may be used, but it should be preceded by the escape character (backslash). Example. ‘a’ ‘I’ ‘+’ ‘2’ “De La Salle University” “a string with double quotes ” within” “a single backslash is in this string”
  • 3. Overview to C Language 13 Other escape characters are listed below. Escape Character Meaning a alert t tab 0 null character ’ single quote % percent symbol 2.2.2 Identifiers Identifiers are composed of a sequence of letters, digits, and the special character _ (underscore). Identifiers are defined by the programmer, thus they should be descriptive. Avoid using names that are too short or too long. Limit the identifiers from 8 to 15 characters only. Some rules must be followed for defining identifiers. They are as follows: 1. It must consist only of letters, digits, and underscores. Correct Example Incorrect Example _duh x-1 thisisanidentifier2 num# a large num num_1 y? 2. An identifier cannot begin with a digit. Incorrect Identifier 1name 3to4 3. An identifier defined in the C standard library should not be redefined. Incorrect Identifier printf scanf 4. Identifiers are case sensitive; meaning uppercase is not equal to the lowercase. Example. ans ≠ Ans ≠ aNs ≠ anS ≠ ANs ≠ ANS
  • 4. Chapter 2 14 2.2.2.1 Variables Variables are identifiers that can store a value that can be changed. These can be of different data types. They can store literals or some type of structured data. Structured data can contain one or more values or data. 2.2.2.2 Constants Constants are identifiers that can store a value that cannot be changed. Usually, constant identifiers are all capital letters with underscores between each word to distinguish them from variables. 2.2.3 Keywords Keywords are words that have a strict meaning in C. These are special words “reserved” by the programming language for processing, thus these may not be redefined by the programmer. Some keywords are listed below. auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 2.3 C Expressions In the C language, expressions may either be arithmetic or logical, but the result would be an integer or a real value. 2.3.1 Arithmetic Expressions The following table will show the different operators and their uses. The rule shows the type of operands where the operator can be used and the resulting data type given the operands.
  • 5. Overview to C Language 15 Operator Meaning Rule Example + addition integer + integer = integer integer + real = real real + integer = real real + real = real 5 + 2 = 7 5 + 2.0 = 7.0 5.0 + 2 = 7.0 5.0 + 2.0 = 7.0 - subtraction integer - integer = integer integer - real = real real - integer = real real - real = real 5 - 2 = 3 5 - 2.0 = 3.0 5.0 - 2 = 3.0 5.0 - 2.0 = 3.0 * multiplication integer * integer = integer integer * real = real real * integer = real real * real = real 5 * 2 = 10 5 * 2.0 = 10.0 5.0 * 2 = 10.0 5.0 * 2.0 = 10.0 / division integer / integer = integer integer / real = real real / integer = real real / real = real 5 / 2 = 2 5 / 2.0 = 2.5 5.0 / 2 = 2.5 5.0 / 2.0 = 2.5 % remainder integer % integer = integer 5 % 2 = 1 -5 % 2 = -1 5 % -2 = 1 -5 % -2 = -1 In evaluating arithmetic expressions, the following rules should follow: 1. Parenthesis First. Evaluate expressions that are enclosed in parenthesis. If there are nested parenthesis, evaluate from inside out. 2. Operator Precedence. Evaluation of operators should follow a hierarchy of priorities. Evaluate expressions with higher priority operators first. unary +, - (positive and negative) highest *, /, % binary +, - (addition and subtraction) lowest 3. Associativity Rule. If expression to be evaluated have operators that are in the same precedence level, evaluate the expression from left to right.
  • 6. Chapter 2 16 Example. z = 8 a = 3 b = 9 w = 2 y = -5 z – ( a + b / 2 ) + w * -y 8 3 9 2 -5 4 7 5 10 1 11 2.3.2 Logical and Relational Expressions In C, evaluation of logical and relational expressions return 0 for false and 1 (or any nonzero value) for true. The relational and equality operators are shown below. Relational Operators Equality Operators < less than == equal <= less than or equal != not equal > greater than >= greater than or equal The following shows the logical operators and their corresponding truth tables. Logical Operator Meaning Truth Table && and true && true = true true && false = false false && true = false false && false = false || or true || true = true true || false = true false || true = true false || false = false ! not !(true) = false !(false) = true
  • 7. Overview to C Language 17 Example. salary < MIN_SALARY || dependents > 5 temperature > 90.0 && humidity > 0.90 n >= 0 && n <= 100 !(0 <= n && n <= 100) Rules in evaluating logical and relational expressions are similar with evaluating arithmetic expressions. These operators also follow precedence rules. The updated list is as follows: Operator Precedence ( ) highest !, unary +, - *, /, % binary +, - <, <=, >, >= ==, != && || lowest Example. flag = 0 y = 4.0 z = 2.0 x = 3.0 !flag || ( y + z >= x – z ) 0 4.0 2.0 3.0 2.0 6.0 1.0 1 1 1 2.3.3 Converting Mathematical Formula to C Expression To solve mathematical problems using the computer, the formula should be translated to the programming language to be used. In this case, arithmetic operations should be in C expressions. Example. b 2 – 4ac b * b – 4 * a * c a+b c+d (a + b) / (c + d) 1 1+x 2 1 / (1 + x * x) a x –(b + c) a * -(b + c)
  • 8. Chapter 2 18 2.3.4 Converting English Conditions to C Expression Solutions to problems may sometimes depend on a set of conditions. To use the computer to solve such problems, these conditions should be converted to the C. Example. x and y are greater than z x > z && y > z x is equal to 1.0 or 3.0 x == 1.0 || x == 3.0 x is in the range of z to y, inclusive z <= x && x <= y x is outside the range of z to y !(z <= x && x <= y) z > x || x > y Self Evaluation Exercises 1. Determine if the following identifiers are valid or invalid. a) L8r b) star*ting c) num_Values d) 4u e) one_i_aren’t 2. Evaluate the following expressions a.) 10 + 23 % (17 – 4 * 2) / (24 – (7 + 15 % 2)) b.) 150 - (-6 + 8 * 4 – 22 % 4) – (5 – (15.2 / 2)) c.) (7 == 7.0) && ( 15 > 3) || !((7 >4) || (7 > 3)) d.) (8 > 13 % 3) || (7 > 22 % 3) && (5 == 30 / 6) 3. Convert the following mathematical equations to C expressions without adding unnecessary parenthesis a.) 1 + X 1 + 1 6 8 b.) (X)(Y)(Z) (X 2 – Y 2 ) + Y 4 + X 2 – Z 4. Convert the following statements to C expressions a.) X is neither 6 nor 8 b.) X is any number except 1, 2, and 3 c.) REVENUE is at most 80% of SALES d.) contestant’s HEIGHT is at least 175 cm and AGE is between 18 and 23, inclusive e.) X is between 100 and 200, exclusive, except 120, 130, and 180
  • 9. Overview to C Language 19 5. Write the C statement that will convert an amount in dollars to its peso equivalent. Assume that PhP1 is equal to $51.75. Chapter Exercises 1. Determine if the following identifiers are valid or invalid. a) 3id b) 1_i_am c) R3D3 d) int e) per-capita f) o_no_o_no g) me_to-2 h) xYshouldI i) phone# j) MAX_SPEED k) G l) __yes 2. Determine if the following are valid or invalid whole number literals. a) -10500 b) 435 c) 2,020 d) +50,000 e) 21.5 3. Determine if the following are valid or invalid real literals a) 2.34e2 b) 15e-0.3 c) 125 d) 34,500.99 e) 0.005 4. Determine if the following are valid or invalid character literals. a) ‘M’ b) ‘n1’ c) ‘’ d) ‘”’ e) ‘+’ f) ‘&’ 5. Given x = 2.0 and y = 3.0, evaluate the following: a) 2 – 4 * 3 + 26 / 2 b) (3 + 4) * x / 2 + y c) 5 + 6.6 / 2.2 * 0.5
  • 10. Chapter 2 20 6. Given i = 1, j = 2, k = 3, x = 5.5, and y = 7.7, evaluate the following whether they yield a value of TRUE/FALSE: a) i < (j – k) b) (x – y) <= ((j – k) – 1) c) (k + j) != (i + 1 * 4) d) ! (1 == 1) e) i && j f) i == j && i + j == k || y == x + 2 g) –i <= j – k && !j 7. Assume the values a = 1, b = 2, c = 0, d = 5.0, and e = 25. What is the output of the following: a) a + b * c && a b) 3 / a + b / e || c c) 10 + 15 || 0 && 5 > 3 + 3 d) 1 + 2 > 3 * 4 || 5 && 3 > 4 == 0 e) 1 % 2 + 1 == 0 + 1 && 2 8. Convert the following conditions to C expressions: a) Commission = (sales – sales X .10) * .25 b) Commission = (sales – sales X 10/100) * 25/100 c) Interest = Amount X Rate d) Semiannual Interest = 600/10% e) age is from 18 to 21 inclusive f) water is less than 1.5 and also greater than 0.1 g) year is divisible by 4 h) speed is not greater than 55 i) y is greater than x and less than z j) w is either equal to 6 or not greater than 3 9. Write the C statement that will compute for the area of a triangle given the base and the height. 10. Write the C statement that will convert a Fahrenheit (F) measure to a Celsius (C) measure. (C = 5/9 x (F – 32)) 11. Write the C statement that will convert an amount in peso to its dollar equivalent. Assume that PhP1 is equal to $51.75. 12. Write the C statement(s) that will compute the least number of Php5 and Php1 coins given an amount. Example: There are 3 Php5 and 2 Php1 in Php17.
  翻译: