SlideShare a Scribd company logo
1 of 26
Download to read offline
1
Programming in C
Hello World!
Soon I will
control the world!
2
Introduction to C
 C language
 Facilitates a structured and disciplined approach to
computer program design
 Provides low-level access
 Highly portable
3
Program Basics
 The source code for a program is the set of instructions
written in a high-level, human readable language.
X = 0;
MOVE 0 TO X.
X := 0
 The source code is transformed into object code by a
compiler. Object code is a machine usable format.
 The computer executes a program in response to a
command.
4
Basics of a Typical C Environment
Phases of C Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Loader
Primary
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Editor
Preprocessor
Linker
CPU
Primary
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
5
GCC Program Basics
 The basic program writing sequence:
1. create or modify a file of instructions using an editor
 Unix: Pico, vi, gEdit, emacs, …
2. compile the instructions with GCC
3. execute or run the compiled program
 repeat the sequence if there are mistakes
Pico:
http://www.bgsu.edu/departments/compsci/docs/pico.html
6
Structure of a C Program
Every C program must
have a main function
.
.
.
main function
function 1
function n
7
Functions
 Each function consists of a header
followed by a basic block.
 General format:
<return-type> fn-name (parameter-list)
basic block
header
8
The Basic Block
 A semi-colon (;) is used to terminate a statement
 A block consists of zero or more statements
 Nesting of blocks is legal and common
 Each interior block may include variable declarations
{
declaration of variables
executable statements
}
9
Return statement
 return expression
1. Sets the return value to the value of the expression
2. Returns to the caller / invoker
 Example:
10
SSH Secure Shell
 On-Campus / VPN
 SSH to one of the
machines in the list
 machine.cs.clemson.edu
 Off-Campus
 SSH to access.cs.clemson.edu
 ssh machine.cs.clemson.edu
11
Unix Commands: mkdir & cd
mkdir cpsc1110
 Creates a new directory / folder
cd cpsc1110
 Changes the current directory
pico ch02First.c
 Runs the pico editor to edit file ch02First.c
12
Go Tigers!!!
Our First Program
13
Compiling and Running a Program
 To compile and print all warning messages, type
gcc –Wall prog-name.c
 If using math library (math.h), type
gcc –Wall prog-name.c -lm
 By default, the compiler produces the file a.out
After
14
Compiling and Running a Program
 To execute the program type
./a.out
 The ./ indicates the current directory
 To specify the file for the object code,
for example, p1.o, type
gcc –Wall prog1.c –o p1.o
then type
./p1.o
to execute the program
15
Comments
 Make programs easy to read and modify
 Ignored by the C compiler
 Two methods:
1. // - line comment
- everything on the line following // is ignored
2. /* */ - block comment
- everything between /* */ is ignored
16
Preprocessor Directive: #include
 A C program line beginning with # that is processed by
the compiler before translation begins.
 #include pulls another file into the source
 causes the contents of the
named file, stdio.h, to be inserted where the #
appears. File is commonly called a header file.
 <>’s indicate that it is a compiler standard header file.
 causes the contents of
myfunctions.h to be inserted
 “’s indicate that it is a user file from current or specified
directory
#include: Chapter 12 p. 311
17
Introduction to Input/Output
 Input data is read into variables
 Output data is written from variables.
 Initially, we will assume that the user
 enters data via the terminal keyboard
 views output data in a terminal window on the screen
18
Program Input / Output
 The C run-time system automatically opens two files
for you at the time your program starts:
 stdin – standard input (from the keyboard)
 stdout – standard output (to the terminal window in
which the program started)
 Later, how to read and write files on disk
1. Using stdin and stdout
2. Using FILE’s
19
Console Input/Output
 Defined in the C library included in <stdio.h>
 Must have this line near start of file:
#include <stdio.h>
 Includes input functions scanf, fscanf, …
 Includes output functions printf, fprintf, …
20
Console Output - printf
 Print to standard output,
typically the screen
 General format (value-list may not be required):
printf("format string", value-list);
21
Console Output
What can be output?
 Any data can be output to display screen
 Literal values
 Variables
 Constants
 Expressions (which can include all of above)
 Note
 Values are passed to printf
 Addresses are passed to scanf
22
Console Output
 We can
 Control vertical spacing with blank lines
 Use the escape sequence "n“, new line
 Should use at the end of all lines unless you are building lines
with multiple printf’s.
 If you printf without a n and the program crashes, you will not
see the output.
 Control horizontal spacing
 Spaces
 Use the escape sequence “t”, tab
 Sometimes undependable.
23
Terminal Output - Examples
 Sends string "Hello World" to display, skipping to next
line
 Displays the lines
Good morning
Ms Smith.
24
Program Output: Escape Character 
 Indicates that a “special” character is to be output
Escape
Sequence
Description
n Newline. Position the screen cursor to the beginning of
the next line.
t Horizontal tab. Move the screen cursor to the next tab
stop.
r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the next
line.
a Alert. Sound the system bell.
 Backslash. Used to print a backslash character.
" Double quote. Used to print a double quote character.
25
Template: a.c
 Starting point for a new program
 Read into (^R in pico) or
 Copy into (cp command) a new file
 Ex: cp a.c prog1.c
26
Programming in C
T H E E N D

More Related Content

Similar to Ch02.pdf

complete data structure
complete data structurecomplete data structure
complete data structureAnuj Arora
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Getachew Ganfur
 
Bsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageBsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageRai University
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c languageRai University
 
introduction to c language
 introduction to c language introduction to c language
introduction to c languageRai University
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageRai University
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageRai University
 
How to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxHow to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxMitali Chugh
 
ICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfssuser33f16f
 
Introduction
IntroductionIntroduction
IntroductionKamran
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRokonuzzaman Rony
 
Programming in c_in_7_days
Programming in c_in_7_daysProgramming in c_in_7_days
Programming in c_in_7_daysAnkit Dubey
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training reportRaushan Pandey
 

Similar to Ch02.pdf (20)

C intro
C introC intro
C intro
 
complete data structure
complete data structurecomplete data structure
complete data structure
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Bsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageBsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c language
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c language
 
basic program
basic programbasic program
basic program
 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c language
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c language
 
How to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxHow to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linux
 
ICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdf
 
C_Intro.ppt
C_Intro.pptC_Intro.ppt
C_Intro.ppt
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C
CC
C
 
01 c
01 c01 c
01 c
 
Programming in c_in_7_days
Programming in c_in_7_daysProgramming in c_in_7_days
Programming in c_in_7_days
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
 

Recently uploaded

Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

Ch02.pdf

  • 1. 1 Programming in C Hello World! Soon I will control the world!
  • 2. 2 Introduction to C  C language  Facilitates a structured and disciplined approach to computer program design  Provides low-level access  Highly portable
  • 3. 3 Program Basics  The source code for a program is the set of instructions written in a high-level, human readable language. X = 0; MOVE 0 TO X. X := 0  The source code is transformed into object code by a compiler. Object code is a machine usable format.  The computer executes a program in response to a command.
  • 4. 4 Basics of a Typical C Environment Phases of C Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Loader Primary Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Editor Preprocessor Linker CPU Primary Memory . . . . . . . . . . . . Disk Disk Disk Disk Disk
  • 5. 5 GCC Program Basics  The basic program writing sequence: 1. create or modify a file of instructions using an editor  Unix: Pico, vi, gEdit, emacs, … 2. compile the instructions with GCC 3. execute or run the compiled program  repeat the sequence if there are mistakes Pico: http://www.bgsu.edu/departments/compsci/docs/pico.html
  • 6. 6 Structure of a C Program Every C program must have a main function . . . main function function 1 function n
  • 7. 7 Functions  Each function consists of a header followed by a basic block.  General format: <return-type> fn-name (parameter-list) basic block header
  • 8. 8 The Basic Block  A semi-colon (;) is used to terminate a statement  A block consists of zero or more statements  Nesting of blocks is legal and common  Each interior block may include variable declarations { declaration of variables executable statements }
  • 9. 9 Return statement  return expression 1. Sets the return value to the value of the expression 2. Returns to the caller / invoker  Example:
  • 10. 10 SSH Secure Shell  On-Campus / VPN  SSH to one of the machines in the list  machine.cs.clemson.edu  Off-Campus  SSH to access.cs.clemson.edu  ssh machine.cs.clemson.edu
  • 11. 11 Unix Commands: mkdir & cd mkdir cpsc1110  Creates a new directory / folder cd cpsc1110  Changes the current directory pico ch02First.c  Runs the pico editor to edit file ch02First.c
  • 13. 13 Compiling and Running a Program  To compile and print all warning messages, type gcc –Wall prog-name.c  If using math library (math.h), type gcc –Wall prog-name.c -lm  By default, the compiler produces the file a.out After
  • 14. 14 Compiling and Running a Program  To execute the program type ./a.out  The ./ indicates the current directory  To specify the file for the object code, for example, p1.o, type gcc –Wall prog1.c –o p1.o then type ./p1.o to execute the program
  • 15. 15 Comments  Make programs easy to read and modify  Ignored by the C compiler  Two methods: 1. // - line comment - everything on the line following // is ignored 2. /* */ - block comment - everything between /* */ is ignored
  • 16. 16 Preprocessor Directive: #include  A C program line beginning with # that is processed by the compiler before translation begins.  #include pulls another file into the source  causes the contents of the named file, stdio.h, to be inserted where the # appears. File is commonly called a header file.  <>’s indicate that it is a compiler standard header file.  causes the contents of myfunctions.h to be inserted  “’s indicate that it is a user file from current or specified directory #include: Chapter 12 p. 311
  • 17. 17 Introduction to Input/Output  Input data is read into variables  Output data is written from variables.  Initially, we will assume that the user  enters data via the terminal keyboard  views output data in a terminal window on the screen
  • 18. 18 Program Input / Output  The C run-time system automatically opens two files for you at the time your program starts:  stdin – standard input (from the keyboard)  stdout – standard output (to the terminal window in which the program started)  Later, how to read and write files on disk 1. Using stdin and stdout 2. Using FILE’s
  • 19. 19 Console Input/Output  Defined in the C library included in <stdio.h>  Must have this line near start of file: #include <stdio.h>  Includes input functions scanf, fscanf, …  Includes output functions printf, fprintf, …
  • 20. 20 Console Output - printf  Print to standard output, typically the screen  General format (value-list may not be required): printf("format string", value-list);
  • 21. 21 Console Output What can be output?  Any data can be output to display screen  Literal values  Variables  Constants  Expressions (which can include all of above)  Note  Values are passed to printf  Addresses are passed to scanf
  • 22. 22 Console Output  We can  Control vertical spacing with blank lines  Use the escape sequence "n“, new line  Should use at the end of all lines unless you are building lines with multiple printf’s.  If you printf without a n and the program crashes, you will not see the output.  Control horizontal spacing  Spaces  Use the escape sequence “t”, tab  Sometimes undependable.
  • 23. 23 Terminal Output - Examples  Sends string "Hello World" to display, skipping to next line  Displays the lines Good morning Ms Smith.
  • 24. 24 Program Output: Escape Character  Indicates that a “special” character is to be output Escape Sequence Description n Newline. Position the screen cursor to the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. a Alert. Sound the system bell. Backslash. Used to print a backslash character. " Double quote. Used to print a double quote character.
  • 25. 25 Template: a.c  Starting point for a new program  Read into (^R in pico) or  Copy into (cp command) a new file  Ex: cp a.c prog1.c