SlideShare a Scribd company logo
1 of 30
CMSC 104, Version 8/06 1
L08IntroToC.ppt
Introduction to C
Topics
• Compilation
• Using the gcc Compiler
• The Anatomy of a C Program
• 104 C Programming Standards and Indentation
Styles
Reading
• Sections 2.1 - 2.4
CMSC 104, Version 8/06 2
L08IntroToC.ppt
Writing C Programs
• A programmer uses a text editor to create or
modify files containing C code.
• Code is also known as source code.
• A file containing source code is called a source
file.
• After a C source file has been created, the
programmer must invoke the C compiler before
the program can be executed (run).
CMSC 104, Version 8/06 3
L08IntroToC.ppt
Using the C Compiler at UMBC
• Invoking the compiler is system dependent.
o At UMBC, we have two C compilers available on the GL
system, cc and gcc. There is also a C compiler on the
CD in your book. Additionally, you can download a free
version of cygwin that has a gcc compiler.
o For this class, we will use the gcc compiler as it is the
compiler available on the Linux system.
o All grading is down on linux.gl.umbc.edu. If you use
any other compiler, run it first on the system where the
grading is being done. There are differences
between the compilers and we only support the gcc
compiler on linux.gl.umbc.edu version!
CMSC 104, Version 8/06 4
L08IntroToC.ppt
Invoking the gcc Compiler
At the prompt, type
gcc -ansi -Wall pgm.c
where pgm.c is the C program source
file.
• -ansi is a compiler option that tells the
compiler to adhere to the ANSI C standard.
• -Wall is an option to turn on all compiler
warnings (best for new programmers).
CMSC 104, Version 8/06 5
L08IntroToC.ppt
The Result : a.out
• If there are no errors in pgm.c, this
command produces an executable file,
which is one that can be executed (run).
• The gcc compiler names the executable file
a.out .
• To execute the program, at the prompt, type
a.out
• Although we call this process “compiling a
program,” what actually happens is more
complicated.
CMSC 104, Version 8/06 6
L08IntroToC.ppt
3 Stages of Compilation
Stage 1: Preprocessing
o Performed by a program called the preprocessor
o Modifies the source code (in RAM) according to
preprocessor directives (preprocessor
commands) embedded in the source code
o Strips comments and white space from the code
o The source code as stored on disk is not modified.
CMSC 104, Version 8/06 7
L08IntroToC.ppt
3 Stages of Compilation (con’t)
Stage 2: Compilation
o Performed by a program called the compiler
o Translates the preprocessor-modified source
code into object code (machine code)
o Checks for syntax errors and warnings
o Saves the object code to a disk file, if instructed
to do so (we will not do this).
o If any compiler errors are received, no object code
file will be generated.
o An object code file will be generated if only
warnings, not errors, are received.
CMSC 104, Version 8/06 8
L08IntroToC.ppt
3 Stages of Compilation (con’t)
Stage 3: Linking
o Combines the program object code with other
object code to produce the executable file.
o The other object code can come from the Run-
Time Library, other libraries, or object files that
you have created.
o Saves the executable code to a disk file. On
the Linux system, that file is called a.out.
o If any linker errors are received, no executable file
will be generated.
CMSC 104, Version 8/06 9
L08IntroToC.ppt
Program Development Using gcc
Source File pgm.c
Program Object Code File pgm.o
Executable File a.out
Preprocessor
Modified Source Code in RAM
Compiler
Linker
Other Object Code Files (if any)
Editor
CMSC 104, Version 8/06 10
L08IntroToC.ppt
A Simple C Program
/* Filename: hello.c
Author: Brian Kernighan & Dennis Ritchie
Date written: ?/?/1978
Description: This program prints the greeting
“Hello, World!”
*/
#include <stdio.h>
int main ( void )
{
printf ( “Hello, World!n” ) ;
return 0 ;
}
CMSC 104, Version 8/06 11
L08IntroToC.ppt
Anatomy of a C Program
program header comment
preprocessor directives (if any)
int main ( void )
{
statement(s)
return 0 ;
}
CMSC 104, Version 8/06 12
L08IntroToC.ppt
Program Header Comment
• A comment is descriptive text used to help a
reader of the program understand its
content.
• All comments must begin with the characters
/* and end with the characters */
• These are called comment delimiters
• The program header comment always
comes first.
• Look at the class web page for the required
contents of our header comment.
CMSC 104, Version 8/06 13
L08IntroToC.ppt
Preprocessor Directives
• Lines that begin with a # in column 1 are
called preprocessor directives
(commands).
• Example: the #include <stdio.h> directive
causes the preprocessor to include a copy of
the standard input/output header file stdio.h at
this point in the code.
• This header file was included because it
contains information about the printf ( )
function that is used in this program.
CMSC 104, Version 8/06 14
L08IntroToC.ppt
stdio.h
• When we write our programs, there are
libraries of functions to help us so that we
do not have to write the same code over
and over.
• Some of the functions are very complex and
long. Not having to write them ourselves
make it easier and faster to write programs.
• Using the functions will also make it easier
to learn to program!
CMSC 104, Version 8/06 15
L08IntroToC.ppt
int main ( void )
• Every program must have a function called
main. This is where program execution begins.
• main() is placed in the source code file as the first
function for readability. There must be a function
with this name or gcc can not successful compile
and link your program.
• The reserved word “int” indicates that main()
returns an integer value.
• The parentheses following the reserved word
“main” indicate that it is a function.
• The reserved word “void” means nothing is there.
CMSC 104, Version 8/06 16
L08IntroToC.ppt
The Function Body
• A left brace (curly bracket) -- { -- begins the
body of every function. A corresponding
right brace -- } -- ends the function body.
• The style is to place these braces on
separate lines in column 1 and to indent the
entire function body 3 to 5 spaces.
CMSC 104, Version 8/06 17
L08IntroToC.ppt
printf (“Hello, World!n”) ;
• This line is a C statement.
• It is a call to the function printf ( ) with a
single argument (parameter), namely the
string “Hello, World!n”.
• Even though a string may contain many
characters, the string itself should be
thought of as a single quantity.
• Notice that this line ends with a semicolon.
All statements in C end with a semicolon.
CMSC 104, Version 8/06 18
L08IntroToC.ppt
return 0 ;
• Because function main() returns an integer value,
there must be a statement that indicates what this
value is.
• The statement
return 0 ;
indicates that main() returns a value of zero to
the operating system.
• A value of 0 indicates that the program successfully
terminated execution.
• Do not worry about this concept now. Just
remember to use the statement.
CMSC 104, Version 8/06 19
L08IntroToC.ppt
Another C Program
/*****************************************
** File: proj1.c
** Author: Joe Student
** Date: 9/15/01
** SSN: 123-45-6789
** Section: 0304
** E-mail: jstudent22@umbc.edu
**
** This program prompts the user for two integer values then displays
** their product.
**
***********************************************/
CMSC 104, Version 8/06 20
L08IntroToC.ppt
Another C Program (con’t)
#include <stdio.h>
int main( void )
{
int value1, value2, product ;
printf(“Enter two integer values: “) ;
scanf(“%d%d”, &value1, &value2) ;
product = value1 * value2 ;
printf(“Product = %dn”, product) ;
return 0 ;
}
CMSC 104, Version 8/06 21
L08IntroToC.ppt
Good Programming Practices
• C programming standards and indentation styles
are available on the 104 course homepage.
• You are expected to conform to these standards
for all programming projects in this class and in
CMSC 201. (This will be part of your grade for
each project!)
• The program just shown conforms to these
standards, but is uncommented (later).
• Subsequent lectures will include more “Good
Programming Practices” slides.
CMSC 104, Version 8/06 22
L08IntroToC.ppt
Tokens
• The smallest element in the C language is
the token.
• It may be a single character or a sequence
of characters to form a single item.
CMSC 104, Version 8/06 23
L08IntroToC.ppt
Tokens are:
• Tokens can be:
o Numeric constants
o Character constants
o String constants
o Keywords
o Names (identifiers)
o Punctuation
o Operators
CMSC 104, Version 8/06 24
L08IntroToC.ppt
Numeric Constants
• Numeric constants are an uninterrupted
sequence of digits (and may contain a
period). They never contain a comma.
• Examples:
o 123
o 98.6
o 1000000
CMSC 104, Version 8/06 25
L08IntroToC.ppt
Character Constants
• Singular!
• One character defined character set.
• Surrounded on the single quotation mark.
• Examples:
o ‘A’
o ‘a’
o ‘$’
o ‘4’
CMSC 104, Version 8/06 26
L08IntroToC.ppt
String Constants
• A sequence characters surrounded by
double quotation marks.
• Considered a single item.
• Examples:
o “UMBC”
o “I like ice cream.”
o “123”
o “CAR”
o “car”
CMSC 104, Version 8/06 27
L08IntroToC.ppt
Keywords
• Sometimes called reserved words.
• Are defined as a part of the C language.
• Can not be used for anything else!
• Examples:
o int
o while
o for
CMSC 104, Version 8/06 28
L08IntroToC.ppt
Names
• Sometimes called identifiers or labels.
• Can be of anything length, but on the first
31 are significant (too long is as bad as too
short).
• Are case sensitive:
o abc is different from ABC
• Must begin with a letter and the rest can be
letters, digits, and underscores.
• Must follow the standards for this course!
CMSC 104, Version 8/06 29
L08IntroToC.ppt
Punctuation
• Semicolons, colons, commas, apostrophes,
quotation marks, braces, brackets, and
parentheses.
• ; : , ‘ “ [ ] { } ( )
CMSC 104, Version 8/06 30
L08IntroToC.ppt
Operators
• There are operators for:
o assignments
o mathematical operations
o relational operations
o Boolean operations
o bitwise operations
o shifting values
o calling functions
o subscripting
o obtaining the size of an object
o obtaining the address of an object
o referencing an object through its address
o choosing between alternate subexpressions

More Related Content

Similar to L08IntroToC.ppt

Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdfRajb54
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
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
 
Object oriented programming 7 first steps in oop using c++
Object oriented programming 7 first steps in oop using  c++Object oriented programming 7 first steps in oop using  c++
Object oriented programming 7 first steps in oop using c++Vaibhav Khanna
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptANISHYAPIT
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...bhargavi804095
 

Similar to L08IntroToC.ppt (20)

C in7-days
C in7-daysC in7-days
C in7-days
 
C in7-days
C in7-daysC in7-days
C in7-days
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
chapter 1.pptx
chapter 1.pptxchapter 1.pptx
chapter 1.pptx
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
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
 
Object oriented programming 7 first steps in oop using c++
Object oriented programming 7 first steps in oop using  c++Object oriented programming 7 first steps in oop using  c++
Object oriented programming 7 first steps in oop using c++
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Rr
RrRr
Rr
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
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
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
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
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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...
 
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 ...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

L08IntroToC.ppt

  • 1. CMSC 104, Version 8/06 1 L08IntroToC.ppt Introduction to C Topics • Compilation • Using the gcc Compiler • The Anatomy of a C Program • 104 C Programming Standards and Indentation Styles Reading • Sections 2.1 - 2.4
  • 2. CMSC 104, Version 8/06 2 L08IntroToC.ppt Writing C Programs • A programmer uses a text editor to create or modify files containing C code. • Code is also known as source code. • A file containing source code is called a source file. • After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).
  • 3. CMSC 104, Version 8/06 3 L08IntroToC.ppt Using the C Compiler at UMBC • Invoking the compiler is system dependent. o At UMBC, we have two C compilers available on the GL system, cc and gcc. There is also a C compiler on the CD in your book. Additionally, you can download a free version of cygwin that has a gcc compiler. o For this class, we will use the gcc compiler as it is the compiler available on the Linux system. o All grading is down on linux.gl.umbc.edu. If you use any other compiler, run it first on the system where the grading is being done. There are differences between the compilers and we only support the gcc compiler on linux.gl.umbc.edu version!
  • 4. CMSC 104, Version 8/06 4 L08IntroToC.ppt Invoking the gcc Compiler At the prompt, type gcc -ansi -Wall pgm.c where pgm.c is the C program source file. • -ansi is a compiler option that tells the compiler to adhere to the ANSI C standard. • -Wall is an option to turn on all compiler warnings (best for new programmers).
  • 5. CMSC 104, Version 8/06 5 L08IntroToC.ppt The Result : a.out • If there are no errors in pgm.c, this command produces an executable file, which is one that can be executed (run). • The gcc compiler names the executable file a.out . • To execute the program, at the prompt, type a.out • Although we call this process “compiling a program,” what actually happens is more complicated.
  • 6. CMSC 104, Version 8/06 6 L08IntroToC.ppt 3 Stages of Compilation Stage 1: Preprocessing o Performed by a program called the preprocessor o Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code o Strips comments and white space from the code o The source code as stored on disk is not modified.
  • 7. CMSC 104, Version 8/06 7 L08IntroToC.ppt 3 Stages of Compilation (con’t) Stage 2: Compilation o Performed by a program called the compiler o Translates the preprocessor-modified source code into object code (machine code) o Checks for syntax errors and warnings o Saves the object code to a disk file, if instructed to do so (we will not do this). o If any compiler errors are received, no object code file will be generated. o An object code file will be generated if only warnings, not errors, are received.
  • 8. CMSC 104, Version 8/06 8 L08IntroToC.ppt 3 Stages of Compilation (con’t) Stage 3: Linking o Combines the program object code with other object code to produce the executable file. o The other object code can come from the Run- Time Library, other libraries, or object files that you have created. o Saves the executable code to a disk file. On the Linux system, that file is called a.out. o If any linker errors are received, no executable file will be generated.
  • 9. CMSC 104, Version 8/06 9 L08IntroToC.ppt Program Development Using gcc Source File pgm.c Program Object Code File pgm.o Executable File a.out Preprocessor Modified Source Code in RAM Compiler Linker Other Object Code Files (if any) Editor
  • 10. CMSC 104, Version 8/06 10 L08IntroToC.ppt A Simple C Program /* Filename: hello.c Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting “Hello, World!” */ #include <stdio.h> int main ( void ) { printf ( “Hello, World!n” ) ; return 0 ; }
  • 11. CMSC 104, Version 8/06 11 L08IntroToC.ppt Anatomy of a C Program program header comment preprocessor directives (if any) int main ( void ) { statement(s) return 0 ; }
  • 12. CMSC 104, Version 8/06 12 L08IntroToC.ppt Program Header Comment • A comment is descriptive text used to help a reader of the program understand its content. • All comments must begin with the characters /* and end with the characters */ • These are called comment delimiters • The program header comment always comes first. • Look at the class web page for the required contents of our header comment.
  • 13. CMSC 104, Version 8/06 13 L08IntroToC.ppt Preprocessor Directives • Lines that begin with a # in column 1 are called preprocessor directives (commands). • Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. • This header file was included because it contains information about the printf ( ) function that is used in this program.
  • 14. CMSC 104, Version 8/06 14 L08IntroToC.ppt stdio.h • When we write our programs, there are libraries of functions to help us so that we do not have to write the same code over and over. • Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. • Using the functions will also make it easier to learn to program!
  • 15. CMSC 104, Version 8/06 15 L08IntroToC.ppt int main ( void ) • Every program must have a function called main. This is where program execution begins. • main() is placed in the source code file as the first function for readability. There must be a function with this name or gcc can not successful compile and link your program. • The reserved word “int” indicates that main() returns an integer value. • The parentheses following the reserved word “main” indicate that it is a function. • The reserved word “void” means nothing is there.
  • 16. CMSC 104, Version 8/06 16 L08IntroToC.ppt The Function Body • A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. • The style is to place these braces on separate lines in column 1 and to indent the entire function body 3 to 5 spaces.
  • 17. CMSC 104, Version 8/06 17 L08IntroToC.ppt printf (“Hello, World!n”) ; • This line is a C statement. • It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!n”. • Even though a string may contain many characters, the string itself should be thought of as a single quantity. • Notice that this line ends with a semicolon. All statements in C end with a semicolon.
  • 18. CMSC 104, Version 8/06 18 L08IntroToC.ppt return 0 ; • Because function main() returns an integer value, there must be a statement that indicates what this value is. • The statement return 0 ; indicates that main() returns a value of zero to the operating system. • A value of 0 indicates that the program successfully terminated execution. • Do not worry about this concept now. Just remember to use the statement.
  • 19. CMSC 104, Version 8/06 19 L08IntroToC.ppt Another C Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: 123-45-6789 ** Section: 0304 ** E-mail: jstudent22@umbc.edu ** ** This program prompts the user for two integer values then displays ** their product. ** ***********************************************/
  • 20. CMSC 104, Version 8/06 20 L08IntroToC.ppt Another C Program (con’t) #include <stdio.h> int main( void ) { int value1, value2, product ; printf(“Enter two integer values: “) ; scanf(“%d%d”, &value1, &value2) ; product = value1 * value2 ; printf(“Product = %dn”, product) ; return 0 ; }
  • 21. CMSC 104, Version 8/06 21 L08IntroToC.ppt Good Programming Practices • C programming standards and indentation styles are available on the 104 course homepage. • You are expected to conform to these standards for all programming projects in this class and in CMSC 201. (This will be part of your grade for each project!) • The program just shown conforms to these standards, but is uncommented (later). • Subsequent lectures will include more “Good Programming Practices” slides.
  • 22. CMSC 104, Version 8/06 22 L08IntroToC.ppt Tokens • The smallest element in the C language is the token. • It may be a single character or a sequence of characters to form a single item.
  • 23. CMSC 104, Version 8/06 23 L08IntroToC.ppt Tokens are: • Tokens can be: o Numeric constants o Character constants o String constants o Keywords o Names (identifiers) o Punctuation o Operators
  • 24. CMSC 104, Version 8/06 24 L08IntroToC.ppt Numeric Constants • Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. • Examples: o 123 o 98.6 o 1000000
  • 25. CMSC 104, Version 8/06 25 L08IntroToC.ppt Character Constants • Singular! • One character defined character set. • Surrounded on the single quotation mark. • Examples: o ‘A’ o ‘a’ o ‘$’ o ‘4’
  • 26. CMSC 104, Version 8/06 26 L08IntroToC.ppt String Constants • A sequence characters surrounded by double quotation marks. • Considered a single item. • Examples: o “UMBC” o “I like ice cream.” o “123” o “CAR” o “car”
  • 27. CMSC 104, Version 8/06 27 L08IntroToC.ppt Keywords • Sometimes called reserved words. • Are defined as a part of the C language. • Can not be used for anything else! • Examples: o int o while o for
  • 28. CMSC 104, Version 8/06 28 L08IntroToC.ppt Names • Sometimes called identifiers or labels. • Can be of anything length, but on the first 31 are significant (too long is as bad as too short). • Are case sensitive: o abc is different from ABC • Must begin with a letter and the rest can be letters, digits, and underscores. • Must follow the standards for this course!
  • 29. CMSC 104, Version 8/06 29 L08IntroToC.ppt Punctuation • Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. • ; : , ‘ “ [ ] { } ( )
  • 30. CMSC 104, Version 8/06 30 L08IntroToC.ppt Operators • There are operators for: o assignments o mathematical operations o relational operations o Boolean operations o bitwise operations o shifting values o calling functions o subscripting o obtaining the size of an object o obtaining the address of an object o referencing an object through its address o choosing between alternate subexpressions