SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Ähnlich wie Overview of C Language

c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdfHome
 
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++ Unit2Tekendra Nath Yogi
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming IntroductionHoney Sharma
 
introductory concepts
introductory conceptsintroductory concepts
introductory conceptsWalepak Ubi
 
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.pptxSONU KUMAR
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Programming presentation
Programming presentationProgramming presentation
Programming presentationFiaz Khokhar
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 

Ähnlich wie Overview of C Language (20)

c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
C program
C programC program
C program
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
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
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming Introduction
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
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
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
fundamentals of c
fundamentals of cfundamentals of c
fundamentals of c
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
 

Mehr von Prof. Erwin Globio

Cisco Router Basic Configuration
Cisco Router Basic ConfigurationCisco Router Basic Configuration
Cisco Router Basic ConfigurationProf. Erwin Globio
 
Introduction to iOS Apps Development
Introduction to iOS Apps DevelopmentIntroduction to iOS Apps Development
Introduction to iOS Apps DevelopmentProf. Erwin Globio
 
Introduction to Android Development Latest
Introduction to Android Development LatestIntroduction to Android Development Latest
Introduction to Android Development LatestProf. 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 ProgrammingProf. Erwin Globio
 
Solutions to Common Android Problems
Solutions to Common Android ProblemsSolutions to Common Android Problems
Solutions to Common Android ProblemsProf. Erwin Globio
 
Android Development Tools and Installation
Android Development Tools and InstallationAndroid Development Tools and Installation
Android Development Tools and InstallationProf. Erwin Globio
 
Guidelines to Qualitative Researches
Guidelines to Qualitative ResearchesGuidelines to Qualitative Researches
Guidelines to Qualitative ResearchesProf. Erwin Globio
 

Mehr von Prof. Erwin Globio (20)

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

Kürzlich hochgeladen

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
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
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
 
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
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
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
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Kürzlich hochgeladen (20)

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
 
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...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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
 
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
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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.