Programming in C++ 4. The lexical basis of C++

Similar documents
DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

C Language, Token, Keywords, Constant, variable

Tokens, Expressions and Control Structures

Basic Types, Variables, Literals, Constants

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

6.096 Introduction to C++ January (IAP) 2009

Data Types and Variables in C language

Programming. C++ Basics

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Basic Elements of C. Staff Incharge: S.Sasirekha

Syntax and Variables

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

INTRODUCTION 1 AND REVIEW

VARIABLES AND CONSTANTS

Presented By : Gaurav Juneja

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

UEE1302 (1102) F10: Introduction to Computers and Programming

Introduction to C# Applications

C: How to Program. Week /Mar/05

DEPARTMENT OF MATHS, MJ COLLEGE

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2 - Introduction to C Programming

Variables. Data Types.

LEXICAL 2 CONVENTIONS

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

PROGRAMMAZIONE I A.A. 2018/2019

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Language Fundamentals Summary

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

HP C/iX Reference Manual

EEE145 Computer Programming

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

CSC 1107: Structured Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Java Notes. 10th ICSE. Saravanan Ganesh

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Programming, numerics and optimization

CHAPTER-6 GETTING STARTED WITH C++

ANSI C Programming Simple Programs

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

UNIT-2 Introduction to C++

The MaSH Programming Language At the Statements Level

Typescript on LLVM Language Reference Manual

A Fast Review of C Essentials Part I

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter 1 & 2 Introduction to C Language

Introduction to Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Programming and Data Structures

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Fundamental of Programming (C)

CSCI 123 Introduction to Programming Concepts in C++

BSM540 Basics of C Language

Differentiate Between Keywords and Identifiers

EECS 388 C Introduction. Gary J. Minden August 29, 2016

Imports. Lexicon. Java/Lespérance 1. PROF. Y. LESPÉRANCE Dept. of Electrical Engineering & Computer Science

Procedures, Parameters, Values and Variables. Steven R. Bagley

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

An overview of Java, Data types and variables

Short Notes of CS201

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Fundamentals of Programming

Lecture 02 C FUNDAMENTALS

UNIT- 3 Introduction to C++

Basics of Java Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS201 - Introduction to Programming Glossary By

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Review of the C Programming Language

The C++ Language. Arizona State University 1

C Basics. Chapter 2: C Basics. C Programming Language. v3.3 February 6, 2017

Introduction to the C Programming Language

1 Lexical Considerations

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Creating a C++ Program

VHDL Lexical Elements

BASIC ELEMENTS OF A COMPUTER PROGRAM

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Transcription:

Programming in C++ 4. The lexical basis of C++! Characters and tokens! Permissible characters! Comments & white spaces! Identifiers! Keywords! Constants! Operators! Summary 1

Characters and tokens A C++ program consists of one or more files which contain series of characters. Characters are normally represented as one byte for character. A compiler resolves (parses) the C++ program into a series of tokens. There are five types of tokens:! identifiers! keywords! constants! operators! other separators 2

Permissible characters All permissible characters can be divided into five groups:! lower case letters a b c... x y z! upper case letters A B C... X Y Z! digits 0 1 2 3 4 5 6 7 8 9! special characters! % ^ & ( ) _ - + = \ <, >.? / ~ # : ;! non printing characters blank carriage return new line etc. 3

Comments Comments are an essential feature of good programming practice. In C++ there are two ways of denoting comments. Two forward slashes // indicate the start of a comment which continues until the end of a line. // This is a comment x = 1; // The first part is not a comment An alternative technique is to use /* to indicate the start of a comment which continues until */ is encountered. Such comment can continue over many lines. /* This is a comment */ /* This is a longer comment */ 4

Comments The /* */ technique can include one or more // style comments. /* First comment // Second comment continuation of first comment */ However the /* */ style of comments cannot themselves be nested. This restriction is a minor nuisance. We cannot exclude by commenting a piece of code containing this style of comments!!! Blanks are not allowed within the tokens defining either style of comment. An incomplete /* */ of comments can lead to errors. 5

White spaces Such entities like! comments! blanks! vertical and horizontal tabs! form feeds! new lines are collectively known as white spaces. White space is not allowed in any token, except a in character or string constants. The compiler ignores any white space that occurs between tokens. Such white space is effectively a separator. 6

Identifiers An identifier is a sequence of some combination of letters, digits and the underscore character _. Both lower and upper characters are valid and distinct. In both C and C++ there is a tradition using mainly lower case except e.g. global constants. This tradition makes for easily readable code. There should be no limit on the number of characters in an identifier. An important restriction on identifiers is that they must start with a character or underscore rather then a digit. velocity! // illegal character! 1velocity // starts with 1 v e l o c i t y // illegal spaces initial-velocity // illegal character - Identifiers with a leading underscore or embedded double underscores are not recommended - such identifiers can be produced by compiler. 7

Keywords Keywords are special identifiers which have a significance defined by the language rather than programmer. asm auto break case catch char class const continue default delete do double else enum extern float for friend goto if inline int long new operator private protected public register return short signed sizeof static struct switch template this throw try typedef union unsigned virtual void volatile while Some compilers reserve some more keywords: ada fortran pascal overload huge near far 8

Constants Constants which are also known as literals can be: integer floating point character string. Integer constants Integer constants consist only of sequence of digits. A negative integer is an integer constant expression!!! Octal (base eight) constant starts with 0 and cannot include 8 and 9. Hexadecimal (base sixteen) integer constants starts with 0x or 0X and may include letters a..f or A..F - the case is not significant. Floating point constants Floating point constants include a decimal point and/or an exponent. The value following e or E is the exponent. 9

Constants Character constants A character inside single quotes as a is a character constant and is usually represented internally by one byte. Certain hard to get at characters are represented by using escape sequence, starting with a backslash. This is the complete list of them: new line \n backslash \\ horizontal tab \t question mark \? vertical tab \v single quote \ backspace \b double quote \ carriage return \r octal number \032 form feed \f hex. number \x032 allert (bell) \a The generalized escape sequence consists of up to three octal or as many hexadecimal digits as required. For instance \61 \x31 both represent the character 1. 10

Operators String constants String constants are sequences of characters between double quotes. Internally a string is represented by an array of characters. The escape sequence \0 is used after the last character to denote the end. Operators An operator is a language defined token consisting of one or more characters which instructs the computer to perform some action. (). [] -> :: ->* & * delete new! ~ ++ -- - / % sizeof +.* << >> < <= > >= ==!= ^ &&?: = += -= *= /= %= <<= >>= &= ^= =, Keywords are also operators. Multi-character operators form a single token. White space is not allowed within a token. 11

Summary! A C++ source file consists of a sequence of tokens which are made up of one or more characters.! White space (including blanks, new lines, tabs etc.) is not allowed inside tokens.! There are five types of tokens: identifiers, keywords, constants, operators and other separators.! Use meaningful names for user-defined identifiers, e.g. flow_rate! Identifiers with language-defined significance are known as keywords. The significance of them cannot be altered.! Constants can be integer, floating point, character or string.! Operators are tokens representing operations such as assignment, addition, multiplication. 12