Title: C Syntax and Semantics and the Program Development Process
1C Syntax and Semantics and the Program
Development Process
- Jianhua Yang
- Department of Computer Science
- University of Houston
2Goals
- 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
32.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
42.1 The Elements of C Program
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
52.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
62.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
72.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
82.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
92.1 The Elements of C Program
Storage attribute
Standard type int float char User-defined
102.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, ,
112.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
122.1 The Elements of C Program
- Variable
- int empNum
- char ch
- Constant
- A, _at_, Programming in C
- const string STARS
- const char BLANK
132.1 The Elements of C Program
The statements that work (react) on running time
142.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
152.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
162.1 The Elements of C Program
- Invalid assignment statement
string firstName string lastName char
letter char middleInitial middleInitialA. l
etterfirstName EdisomlastName lastName
172.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 .
182.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
192.1 The Elements of C Program
- 9. Comments
- /xxxxxxx/
- /xxxxx
- xxxxx
- xxxxx/
- //xxxxxxx
202.2 Program Construction
- Brief review
- Identifiers
- Declarations
- Variables
- Constants
- Expressions
- Statements
- comments
212.2 Program Construction
- 1. Program Construction
- Program
- Function
- Block
- Statement
Declaration FunctionDefination FunctionDefinatio
n
Heading Statement
Statement
222.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
232.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
242.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
252.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 -