SlideShare a Scribd company logo
Chapter 1
Fundamentals of C
1
Dr. A. PHILIP AROKIADOSS
Assistant Professor
Department of Statistics
St. Joseph’s College (Autonomous)
Tiruchirappalli-620 002.
INTRODUCTION
This chapter serves as a formal introduction to the
C programming language.
The fundamental elements of the C language.
Algorithms requires as least five statement types;
input, output, arithmetic calculations, selection,
and repetition.
2
PROGRAM 1: A C Program that Computes City Tax
Requirements Specification Develop a program
that does the following
1. Prints on the monitor screen a brief description
of the program’s purpose.
2. Prompts the user to enter a value for gross
annual income using the terminal keyboard.
3. Reads a value for gross income.
4. Computes the city income tax for the city of
Oxford, Ohio. The city income tax is 1.75
percent of the gross annual income.
5. Prints the computed city income tax.
3
Analysis
Input. Gross annual income in dollars.
Output. The computed city income tax in
dollars.
Formulas. The city income tax is computed
using the formula.
income_tax = 0.0175 * gross_income
4
Design
print “A PROGRAM THAT COMPUTES
CITY INCOME TAX”
print “Enter gross income:”
read gross_income
compute city_tax = 0.0175 * gross_income
print city_tax
5
Implementation
A PROGRAM THAT COMPUTES CITY INCOME TAX
Enter gross income : 18657
City tax is 326.497500 dollars.
The five lines that we have just explained are
examples of C statements. Notice that they all
terminate with a semicolon.
6
LANGUAGE CHARACTER SET AND TOKENS
types of tokens
1. Reserved words (keywords)
2. Identifiers
3. Constants
4. String literals
5. Punctuators
6. Operators
7
1. Reserved words :
Identify language entities, they have special
meanings to the compiler. C reserved words must
be typed fully in lowercase. Some examples of
reserved words from the program are const,
double, int, and return.
8
2. Identifiers
programmer-defined words. Needed for program
variables, functions, and other program constructs.
gross_income and city_tax are examples. Must be
unique within the same scope
1. A to Z , a to z , 0 to 9 , and the underscore “_”
2. The first character must be a letter or an underscore.
3. Only the first 32 characters as significant.
4. There can be no embedded blanks.
5. Reserved words cannot be used as identifiers.
6. Identifiers are case sensitive.
9
3. Constants
fixed values CITY_TAX_RATE = 0.0175 is an
example of a constant.
Integer Constants
commas are not allowed in integer constants.
Floating-Point Constants
either in conventional or scientific notation. For
example, 20.35; 0.2035E+2
Character Constants and Escape Sequences
a character enclosed in single quotation marks.
Precede the single quotation mark by a backslash,
printf(“%c”, ‘”);
Escape sequence
causes a new line during printing. n
10
4. String Literals
characters surrounded by double quotation
marks.
format specifier for output converts the internal
representation of data to readable characters.( %f )
for example,
City tax is 450.000000 dollars.
precede it with a backslash as
“Jim ”Mac” MacDonald”
backslash character can be used as a continuation
character
printf(THIS PROGRAM COMPUTES  CITY INCOME
TAX”);
11
5. Punctuators
[ ] ( ) { } , ; : ………* #
6. Operators
result in some kind of computation or action
city_tax = CITY_TAX_TATE * gross_income ;
operators act on operands.
12
THE STRUCTURE OF A C PROGRAM
C program consists of following components:
1. Program comments
2. Preprocessor directives
3. Type declarations
4. Named constants
5. Statements
6. Function declarations (prototypes)
7. Function definitions
8. Function calls
13
1. Program Comments
use /* and */ to surround comments, or // to begin comment
lines.
2. Preprocessor Directives
Lines that begin with a pound sign, #,
A preprocessor directive is can instruction to the
preprocessor. Named file inclusion is concerned with adding the
content of a header file to a source program file. Standard
header files. For example,
#include <stdio.h>
#include causes a headerfile to be copied into the code.
programmer-defined header file surrounded by double quotation
marks. #include <d:header1.h>
to advantage in partitioning large programs into several files.
14
3. Data Types and Type Declarations
double gross_income;
double city_tax;
variable’s type determines
1. How it is stored internally
2. What operations can be applied to it
3. How such operations are interpreted
15
declare a variable to be of type integer, the compiler
allocates a memory location for that variable. The size of
this memory location depends on the type of the
compiler.
int is 2 bytes the range –32768 through 32768 designed
to perform arithmetic operations and assignment
operations. Two classes of data types:
1. Fundamental data types
2. Programmer-defined data types
to classes of built-in data types:
1. Fundamental data types
2. Derived data types
Examples of derived data types are arrays, strings, and
structures.
16
Data Type int
Data Type char
Data Type double
Data initialization
be initialized in two ways,
1. Compile-time initialization
2. Run-time initialization
Strings as a Derived Data Type
A string is a sequence of characters that is
treated as a single data item. A string variable is a
variable that stores a string constant.
17
how to declare string variables.
1. Begin the declaration with the keyword char,
Char report_header [41]
2. To initialize a string variable at complie time,
char report_header [41] = “Annual Report”
18
4. Named Constants
const double CITY_TAX_RATE = 0.0175;
is an identifier whose value is fixed and does not
change during the execution of a program in which
it appears.
In C the declaration of a named constant begins
with the keyword const.
During execution, the processor replaces every
occurrence of the named constant .
19
5. Statements
A statement is a specification of an action to be
taken by the computer as the program executes.
Compound Statements
is a list of statements enclosed in braces, { }
20
A FIRST LOOK AT FUNCTIONS
as a block of code that performs a specific task.
The function main( )
int main(void) {
Statement;
Statement;
……
……
return 0;
}
21
return statement ensures that the constant value 0,
the program status code, is returned to the program
or the operating system that has triggered the
execution of this function main.
Each C program must have one main function.
The type specifier for functions can be int, double,
char, void, and so on, depending on the type of data
that it returns.
22
BUILDING A MINIMUM LANGUAGE SUBSET
An expression is a syntactically correct and meaningful
combination of operators and operands.
city_tax = CITY_TAX_RATE * gross_income
An expression statement is any expression followed by
a semicolon.
city_tax = CITY_TAX_RATE * gross_income
23
Example 2
area ? short_side 10.05 long_side 20.00
area = short_side * long_side
area 210.00 short_side 10.05 long_side 20.00
24
The Standard Output Function printf
This statement is a function call to the standard
library function printf. The parentheses ( ) are known
as the function call operator.
Following compilation, the linker fetches the object
code corresponding to printf from the standard C
library and combines it with your object program.
25
Quantity Type printf Format Specifier
int %d
double %f or % lf
char %c
printf(“Your year of birth is %d, and in 2000 you
will be %d years old.” , year_of_birth, 2000 –
year_of_birth);
26
Variable Type scanf Format Specifier
int %d
double %lf
char %c
printf(“Type your weight in pounds: “);
scanf(“%d” , &weight_in_pounds);
27
Input of String Variables
char string1 [31];
scanf(“%s” , string1);
The reason is that scanf skips whitespace during
string input and picks string values delimited by
whitespace.
the input string values that contain whitespace,
we can use several techniques in C. We will explain
one easy way, which requires the use of the gets
function.
28
PREPARING C SOURCE PROGRAM FILES
Here are some style conventions
1. Insert blank lines between consecutive program
sections.
2. Make liberal use of clear and help comments.
3. Keep your comments separate from the program
statements.
4. Type each statement or declaration on a single line.
29
5. Avoid running a statement over multiple lines.
6. Avoid line splicing.
7. Indent all lines that form a compound
statement by the same amount.
8. Type the beginning and end braces, { }, for
compound statements
9. Use whitespace in typing statements.
10. Conclude each function by a comment to
mark its end.
30
EXAMPLE PROGRAM 2 : A C Program that Converts
Height and Weight to Metric Units
Enter your first name : Kelly
Enter your last name : Johnson
Enter your height in “inches” : 64
Enter your weight in “pounds” : 110
Kelly Johnson, your height is 162.560000
centimeters, and your weight is 49.894900
kilograms.
31
PROGRAM DEBUGGING
1 #include <stdio.h>
2
3 int main (void) {
4 double number;
5
6 printf(“Enter a number : “)
7 scanf(“%lf” , &number);
8 Inverse = 1.0 / number ;
9 printf(“Inverse of %f is %f” , number, inverse);
32
---Configuration : debug – Win32 Debug ---
Compiling …
Debug.c
D:cprogsdebug.c(7) : error C2146: cyntax error :
missing ‘;’ before identifier ‘scanf’
D:cprogsdebug.c(8) : error C2065 ‘inverse’ :
undeclared identifier.
D:cprogsdebug.c(8) : warning C4244 : ‘=‘ :
conversion from ‘const double ‘ to ‘ int ‘ , possible
loss of data.
D:cprogsdebug.c(10) : fatal error C1004 :
unexpected end of file found
Error executing c1.exe
Debug.exe – 3 error(s), 1 warning(s)
33
Debugging for Warning Diagnostics
do not force it to stop the compilation.
Debugging Run-Time Errors
Enter a number : 0
Floating point error : Divide by 0 .
Abnormal program termination .
34
if number is equal to zero
print “Zero does not have a finite inverse.”
else
compute inverse = 1 / number
end_if
35
Ad

More Related Content

Similar to C-Programming Fundamentals of C (1).ppt (20)

programming for problem solving in C and C++.pptx
programming for problem solving in C and C++.pptxprogramming for problem solving in C and C++.pptx
programming for problem solving in C and C++.pptx
BamaSivasubramanianP
 
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptxKMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
Tekle12
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
C programming
C programmingC programming
C programming
PralhadKhanal1
 
C notes
C notesC notes
C notes
Raunak Sodhi
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
madhurij54
 
UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
Revathiparamanathan
 
C Language ppt create by Anand & Sager.pptx
C Language ppt create by Anand & Sager.pptxC Language ppt create by Anand & Sager.pptx
C Language ppt create by Anand & Sager.pptx
kumaranand07297
 
C programming
C programmingC programming
C programming
saniabhalla
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD
 
Module 1 PCD.docx
Module 1 PCD.docxModule 1 PCD.docx
Module 1 PCD.docx
VijayKumar886687
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
BalamuruganV28
 
Cnotes
CnotesCnotes
Cnotes
Muthuganesh S
 
Chapter2
Chapter2Chapter2
Chapter2
Anees999
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
Emad Helal
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
Rai University
 
programming for problem solving in C and C++.pptx
programming for problem solving in C and C++.pptxprogramming for problem solving in C and C++.pptx
programming for problem solving in C and C++.pptx
BamaSivasubramanianP
 
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptxKMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
Tekle12
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
madhurij54
 
C Language ppt create by Anand & Sager.pptx
C Language ppt create by Anand & Sager.pptxC Language ppt create by Anand & Sager.pptx
C Language ppt create by Anand & Sager.pptx
kumaranand07297
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
BalamuruganV28
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
Emad Helal
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
Rai University
 

Recently uploaded (20)

Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Ad

C-Programming Fundamentals of C (1).ppt

  • 1. Chapter 1 Fundamentals of C 1 Dr. A. PHILIP AROKIADOSS Assistant Professor Department of Statistics St. Joseph’s College (Autonomous) Tiruchirappalli-620 002.
  • 2. INTRODUCTION This chapter serves as a formal introduction to the C programming language. The fundamental elements of the C language. Algorithms requires as least five statement types; input, output, arithmetic calculations, selection, and repetition. 2
  • 3. PROGRAM 1: A C Program that Computes City Tax Requirements Specification Develop a program that does the following 1. Prints on the monitor screen a brief description of the program’s purpose. 2. Prompts the user to enter a value for gross annual income using the terminal keyboard. 3. Reads a value for gross income. 4. Computes the city income tax for the city of Oxford, Ohio. The city income tax is 1.75 percent of the gross annual income. 5. Prints the computed city income tax. 3
  • 4. Analysis Input. Gross annual income in dollars. Output. The computed city income tax in dollars. Formulas. The city income tax is computed using the formula. income_tax = 0.0175 * gross_income 4
  • 5. Design print “A PROGRAM THAT COMPUTES CITY INCOME TAX” print “Enter gross income:” read gross_income compute city_tax = 0.0175 * gross_income print city_tax 5
  • 6. Implementation A PROGRAM THAT COMPUTES CITY INCOME TAX Enter gross income : 18657 City tax is 326.497500 dollars. The five lines that we have just explained are examples of C statements. Notice that they all terminate with a semicolon. 6
  • 7. LANGUAGE CHARACTER SET AND TOKENS types of tokens 1. Reserved words (keywords) 2. Identifiers 3. Constants 4. String literals 5. Punctuators 6. Operators 7
  • 8. 1. Reserved words : Identify language entities, they have special meanings to the compiler. C reserved words must be typed fully in lowercase. Some examples of reserved words from the program are const, double, int, and return. 8
  • 9. 2. Identifiers programmer-defined words. Needed for program variables, functions, and other program constructs. gross_income and city_tax are examples. Must be unique within the same scope 1. A to Z , a to z , 0 to 9 , and the underscore “_” 2. The first character must be a letter or an underscore. 3. Only the first 32 characters as significant. 4. There can be no embedded blanks. 5. Reserved words cannot be used as identifiers. 6. Identifiers are case sensitive. 9
  • 10. 3. Constants fixed values CITY_TAX_RATE = 0.0175 is an example of a constant. Integer Constants commas are not allowed in integer constants. Floating-Point Constants either in conventional or scientific notation. For example, 20.35; 0.2035E+2 Character Constants and Escape Sequences a character enclosed in single quotation marks. Precede the single quotation mark by a backslash, printf(“%c”, ‘”); Escape sequence causes a new line during printing. n 10
  • 11. 4. String Literals characters surrounded by double quotation marks. format specifier for output converts the internal representation of data to readable characters.( %f ) for example, City tax is 450.000000 dollars. precede it with a backslash as “Jim ”Mac” MacDonald” backslash character can be used as a continuation character printf(THIS PROGRAM COMPUTES CITY INCOME TAX”); 11
  • 12. 5. Punctuators [ ] ( ) { } , ; : ………* # 6. Operators result in some kind of computation or action city_tax = CITY_TAX_TATE * gross_income ; operators act on operands. 12
  • 13. THE STRUCTURE OF A C PROGRAM C program consists of following components: 1. Program comments 2. Preprocessor directives 3. Type declarations 4. Named constants 5. Statements 6. Function declarations (prototypes) 7. Function definitions 8. Function calls 13
  • 14. 1. Program Comments use /* and */ to surround comments, or // to begin comment lines. 2. Preprocessor Directives Lines that begin with a pound sign, #, A preprocessor directive is can instruction to the preprocessor. Named file inclusion is concerned with adding the content of a header file to a source program file. Standard header files. For example, #include <stdio.h> #include causes a headerfile to be copied into the code. programmer-defined header file surrounded by double quotation marks. #include <d:header1.h> to advantage in partitioning large programs into several files. 14
  • 15. 3. Data Types and Type Declarations double gross_income; double city_tax; variable’s type determines 1. How it is stored internally 2. What operations can be applied to it 3. How such operations are interpreted 15
  • 16. declare a variable to be of type integer, the compiler allocates a memory location for that variable. The size of this memory location depends on the type of the compiler. int is 2 bytes the range –32768 through 32768 designed to perform arithmetic operations and assignment operations. Two classes of data types: 1. Fundamental data types 2. Programmer-defined data types to classes of built-in data types: 1. Fundamental data types 2. Derived data types Examples of derived data types are arrays, strings, and structures. 16
  • 17. Data Type int Data Type char Data Type double Data initialization be initialized in two ways, 1. Compile-time initialization 2. Run-time initialization Strings as a Derived Data Type A string is a sequence of characters that is treated as a single data item. A string variable is a variable that stores a string constant. 17
  • 18. how to declare string variables. 1. Begin the declaration with the keyword char, Char report_header [41] 2. To initialize a string variable at complie time, char report_header [41] = “Annual Report” 18
  • 19. 4. Named Constants const double CITY_TAX_RATE = 0.0175; is an identifier whose value is fixed and does not change during the execution of a program in which it appears. In C the declaration of a named constant begins with the keyword const. During execution, the processor replaces every occurrence of the named constant . 19
  • 20. 5. Statements A statement is a specification of an action to be taken by the computer as the program executes. Compound Statements is a list of statements enclosed in braces, { } 20
  • 21. A FIRST LOOK AT FUNCTIONS as a block of code that performs a specific task. The function main( ) int main(void) { Statement; Statement; …… …… return 0; } 21
  • 22. return statement ensures that the constant value 0, the program status code, is returned to the program or the operating system that has triggered the execution of this function main. Each C program must have one main function. The type specifier for functions can be int, double, char, void, and so on, depending on the type of data that it returns. 22
  • 23. BUILDING A MINIMUM LANGUAGE SUBSET An expression is a syntactically correct and meaningful combination of operators and operands. city_tax = CITY_TAX_RATE * gross_income An expression statement is any expression followed by a semicolon. city_tax = CITY_TAX_RATE * gross_income 23
  • 24. Example 2 area ? short_side 10.05 long_side 20.00 area = short_side * long_side area 210.00 short_side 10.05 long_side 20.00 24
  • 25. The Standard Output Function printf This statement is a function call to the standard library function printf. The parentheses ( ) are known as the function call operator. Following compilation, the linker fetches the object code corresponding to printf from the standard C library and combines it with your object program. 25
  • 26. Quantity Type printf Format Specifier int %d double %f or % lf char %c printf(“Your year of birth is %d, and in 2000 you will be %d years old.” , year_of_birth, 2000 – year_of_birth); 26
  • 27. Variable Type scanf Format Specifier int %d double %lf char %c printf(“Type your weight in pounds: “); scanf(“%d” , &weight_in_pounds); 27
  • 28. Input of String Variables char string1 [31]; scanf(“%s” , string1); The reason is that scanf skips whitespace during string input and picks string values delimited by whitespace. the input string values that contain whitespace, we can use several techniques in C. We will explain one easy way, which requires the use of the gets function. 28
  • 29. PREPARING C SOURCE PROGRAM FILES Here are some style conventions 1. Insert blank lines between consecutive program sections. 2. Make liberal use of clear and help comments. 3. Keep your comments separate from the program statements. 4. Type each statement or declaration on a single line. 29
  • 30. 5. Avoid running a statement over multiple lines. 6. Avoid line splicing. 7. Indent all lines that form a compound statement by the same amount. 8. Type the beginning and end braces, { }, for compound statements 9. Use whitespace in typing statements. 10. Conclude each function by a comment to mark its end. 30
  • 31. EXAMPLE PROGRAM 2 : A C Program that Converts Height and Weight to Metric Units Enter your first name : Kelly Enter your last name : Johnson Enter your height in “inches” : 64 Enter your weight in “pounds” : 110 Kelly Johnson, your height is 162.560000 centimeters, and your weight is 49.894900 kilograms. 31
  • 32. PROGRAM DEBUGGING 1 #include <stdio.h> 2 3 int main (void) { 4 double number; 5 6 printf(“Enter a number : “) 7 scanf(“%lf” , &number); 8 Inverse = 1.0 / number ; 9 printf(“Inverse of %f is %f” , number, inverse); 32
  • 33. ---Configuration : debug – Win32 Debug --- Compiling … Debug.c D:cprogsdebug.c(7) : error C2146: cyntax error : missing ‘;’ before identifier ‘scanf’ D:cprogsdebug.c(8) : error C2065 ‘inverse’ : undeclared identifier. D:cprogsdebug.c(8) : warning C4244 : ‘=‘ : conversion from ‘const double ‘ to ‘ int ‘ , possible loss of data. D:cprogsdebug.c(10) : fatal error C1004 : unexpected end of file found Error executing c1.exe Debug.exe – 3 error(s), 1 warning(s) 33
  • 34. Debugging for Warning Diagnostics do not force it to stop the compilation. Debugging Run-Time Errors Enter a number : 0 Floating point error : Divide by 0 . Abnormal program termination . 34
  • 35. if number is equal to zero print “Zero does not have a finite inverse.” else compute inverse = 1 / number end_if 35