C Syntax and Semantics and the Program Development Process - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

C Syntax and Semantics and the Program Development Process

Description:

Department of Computer Science. University of Houston. Goals. The structure of C program ... const string TITLE='Dr.'; // Salutary title ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 26
Provided by: jhy9
Category:

less

Transcript and Presenter's Notes

Title: C Syntax and Semantics and the Program Development Process


1
C Syntax and Semantics and the Program
Development Process
  • Jianhua Yang
  • Department of Computer Science
  • University of Houston

2
Goals
  • The structure of C program
  • Syntax templates
  • C identifiers
  • Char and string
  • Reserved words in C
  • Assigning values to variables
  • String expressions
  • Output statement/format
  • Comments
  • Simple C programs

3
2.1 The Elements of C Program
  • 1. C program structure
  • Composed of one or more functions plus
    declaration part
  • One and only one main function

4
2.1 The Elements of C Program
  • Example

include ltiostreamgt using namespace std int
Square(int ) int Cube(int ) int main()
coutltltThe square of 27 isltltSquare(27)ltltendl
coutltlt and the cube of 27 isltltCube(27)ltltendl
return 0 int Squre ( int n) return nn
int Cube( int n ) return nnn
5
2.1 The Elements of C Program
  • 2. Syntax and Semantics
  • Syntax Grammar
  • Semantics Meaning

the formal rules governing how valid
instructions are written
The set of rules that determines the meaning of
instructions
6
2.1 The Elements of C Program
  • 3. Syntax templates
  • A syntax template is a generic example of the C
    construct being defined.
  • Example MainFunction, Identifier

int main() Statement
7
2.1 The Elements of C Program
  • 4. Identifiers
  • A name associated with a function or data object
    and used to refer to that function or data object
  • Are made up of letters, digits, and underscore
    character, but must begin with a letter or
    underscore

8
2.1 The Elements of C Program
  • Valid identifiers
  • Invalid identifiers

sum_of_squares J9 box_22A GetData Bin4D
40Hours Get Data box-22 cost_in_ int
9
2.1 The Elements of C Program
  • 5. Data and Data types

Storage attribute
Standard type int float char User-defined
10
2.1 The Elements of C Program
  • char
  • Built-in
  • Consisting of one alphanumeric
  • string
  • Program-defined
  • Is a sequence of characters

A, a, 8,,,
Problem Solving, C, ,
11
2.1 The Elements of C Program
  • 6. Declarations
  • Variable
  • Constant

A statement that associates an identifier with a
data object, a function, or a data type so that
the programmer can refer to that item by name
A location in memory, referenced by an
identifier, that contains a data value that can
be changed
Is a value that can not be changed, including
literal value, and named constant
12
2.1 The Elements of C Program
  • Variable
  • int empNum
  • char ch
  • Constant
  • A, _at_, Programming in C
  • const string STARS
  • const char BLANK

13
2.1 The Elements of C Program
  • 7. Executable statements

The statements that work (react) on running time
14
2.1 The Elements of C Program
  • Assignment statement
  • Function
  • Syntax template
  • Example
  • Some points

To store a value to a variable
VariableExpression
firstNameAbraham TitlePresident i5 X123
.12
  • consistent type
  • Left hand must be variable
  • Right hand must be expression

15
2.1 The Elements of C Program
  • Expression
  • An arrangement of identifiers, literals, and
    operators that can be evaluated to compute a
    value of a given type

16
2.1 The Elements of C Program
  • Invalid assignment statement

string firstName string lastName char
letter char middleInitial middleInitialA. l
etterfirstName EdisomlastName lastName
17
2.1 The Elements of C Program
  • String expression and operator

Operator Joining or concatenating
string bookTitle string phrase1 string
phrase2 phrase1Programming and phrase2Prob
lem Solving bookTitlephrase1 phrase2 in
C .
18
2.1 The Elements of C Program
  • 8. Output
  • Syntax template
  • examples

coutltltexpression
2
char ch2 string fName, lName fNameMarie
lNameCurie coutltltch coutltltfName
lName coutltltfNameltltlName coutltltfNameltltendlltltlNam
e
Marie Cure
Marie Curie
MarieCurie
19
2.1 The Elements of C Program
  • 9. Comments
  • /xxxxxxx/
  • /xxxxx
  • xxxxx
  • xxxxx/
  • //xxxxxxx

20
2.2 Program Construction
  • Brief review
  • Identifiers
  • Declarations
  • Variables
  • Constants
  • Expressions
  • Statements
  • comments

21
2.2 Program Construction
  • 1. Program Construction
  • Program
  • Function
  • Block
  • Statement

Declaration FunctionDefination FunctionDefinatio
n
Heading Statement
Statement
22
2.2 Program Construction
  • 2. C preprocessor
  • Include directive
  • Syntax template
  • Example

Physically insert the contents of the named file
into your source program
include ltfile_namegt
include ltiostreamgt include ltstringgt
23
2.2 Program Construction
  • 3. Namspaces
  • Why do we use Namespaces?
  • Two accessing ways
  • Examples

Some programmer-defined identifiers are declared
in a namespace with a specific name
include ltiostreamgt using namespace std int
main() coutltltNamespace Testingltlt endl
return 0
  • Direct way stdcout
  • 2. Indirect way using namespace std

include ltiostreamgt int main()
stdcoutltltNamespace Testingltlt stdendl
return 0
24
2.3 Reviewa real program
  • //
  • //This is a real program. It would help you to
  • // understand what we have learned in this
    chapter.
  • //
  • include ltiostreamgt
  • include ltstringgt
  • using nmaespace std
  • const string TITLEDr.
    // Salutary title
  • const string F_NAMEMargaret //First
    name of addressee
  • const string M_INITIALH
  • const string L_NAMESklaznick

25
2.3 Reviewa real program
  • int main()
  • string first // Holds the first name plus
    a blank
  • string FullName //complete name,
    including title
  • string firstLast
  • string titleLast
  • //Creat first name with blank
  • first F_NAME
  • // Create full name
  • fullnameTITLE first M_INITIAL
  • fullnamefullName.L_NAME
  • coutltltfullNameltlt is a GRAND PRIZE
    WINNER!!!ltltendlltltendl
Write a Comment
User Comments (0)
About PowerShow.com