LEXICAL 2 CONVENTIONS

Similar documents
INTRODUCTION 1 AND REVIEW

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

Tokens, Expressions and Control Structures

6.096 Introduction to C++ January (IAP) 2009

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

UNIT- 3 Introduction to C++

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

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

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

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

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

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

Basic Elements of C. Staff Incharge: S.Sasirekha

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

DEPARTMENT OF MATHS, MJ COLLEGE

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

Basic Types, Variables, Literals, Constants

BITG 1233: Introduction to C++

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

A Fast Review of C Essentials Part I

UNIT-2 Introduction to C++

Review of the C Programming Language for Principles of Operating Systems

Data Types and Variables in C language

Variables. Data Types.

VARIABLES AND CONSTANTS

Fundamental of Programming (C)

Chapter 2. Lexical Elements & Operators

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

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

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

Introduction to Programming

C: How to Program. Week /Mar/05

EEE145 Computer Programming

The C++ Language. Arizona State University 1

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

ANSI C Programming Simple Programs

CHAPTER-6 GETTING STARTED WITH C++

Fundamental of C programming. - Ompal Singh

Review of the C Programming Language

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2 - Introduction to C Programming

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

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction to C++ Systems Programming

BLM2031 Structured Programming. Zeyneb KURT

C - Basic Introduction

Chapter 12 Variables and Operators

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

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

HP C/iX Reference Manual

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

1 Lexical Considerations

Information Science 1

Fundamentals of Programming

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

Work relative to other classes

UEE1302 (1102) F10: Introduction to Computers and Programming

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

Lexical Considerations

PYTHON- AN INNOVATION

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Language Fundamentals Summary

JAVA Programming Fundamentals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Programming. C++ Basics

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

C Reference Manual. Dennis M. Ritchie Bell Telephone Laboratories Murray Hill, New Jersey 07974

Chapter 12 Variables and Operators

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

Lecture 02 C FUNDAMENTALS

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

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 15 - C++ As A "Better C"

CSC 1107: Structured Programming

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

Programming and Data Structures

Lexical Considerations

Language Reference Manual simplicity

Chapter 12 Variables and Operators

Basics of Java Programming

Introduction to C# Applications

C Language, Token, Keywords, Constant, variable

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Java Notes. 10th ICSE. Saravanan Ganesh

C++ INDEX. Introduction: Instructions for use. Basics of C++: Structure of a program Variables. Data Types. Constants Operators Basic Input/Output

Lecture 3. More About C

Syntax and Variables

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

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

The Arithmetic Operators

Full file at C How to Program, 6/e Multiple Choice Test Bank

Programming for Engineers Introduction to C

Transcription:

LEXIAL 2 ONVENTIONS hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN.

++ Programming Lexical onventions Objectives You will learn: Operators. Punctuators. omments. Identifiers. Literals. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page i

++ Programming Lexical onventions 1 Operators The operators in ++ are defined as being one of the following: [ ] ( ). -> ++ -- & * + - ~! sizeof / % << >> < > <= >= ==!= ^ &&?: = *= /= %= += -= <<= >>= &= ^= =, # ## And the following operators specific to ++: ::.* ->* The operators # and ## are used only by the preprocessor. Depending on context, the same operator can have more than one meaning. The ampersand (&) can be interpreted as: a bitwise AND (A & B) an address operator (&A) in ++, a reference modifier In the first case, the & is a binary operator; in the second, the & is a unary operator. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 1

++ Programming Lexical onventions Unary operators operate on one operand. Turbo ++ provides the following unary operators: 1.1 Unary operators & Address! Logical negation * Indirection ++ Increment ~ Bitwise complement -- Decrement - Unary minus + Unary plus The syntax for unary operators is as follows: <unary-operator> <unary expression> OR <unary-operator> <type><unary expression> SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 2

++ Programming Lexical onventions 1.2 Overloaded Operators Overloaded operators are those that have been redefined within a ++ class using the keyword operator followed by an operator symbol. When an operator is thus overloaded, the resulting symbol is called the operator function name. 1.3 Precedence of Operators In the following table of operator precedence, the Turbo ++ operators are divided into 16 categories. The #1 category has the highest precedence; category #2 (Unary operators) takes second precedence, and so on to the omma operator, which has lowest precedence. The operators within each category have equal precedence. The Unary (category #2), onditional (category #14), and Assignment (category #15) operators associate right-to-left; all other operators associate left-to-right. # ategory Operator What it is (or does) 1. Highest () Function call [] Array subscript -> ++ indirect component selector :: ++ scope access/resolution. ++ direct component selector 2. Unary! Logical negation (NOT) ~ Bitwise (1's) complement + Unary plus - Unary minus ++ Preincrement or postincrement -- Predecrement or postdecrement & Address * Indirection sizeof new delete (returns size of operand, in bytes) (dynamically allocates ++ storage) (dynamically deallocates ++ storage) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 3

++ Programming Lexical onventions # ategory Operator What it is (or does) 3. Multiplicative * Multiply / Divide % Remainder (modulus) 4. Member access.* ++ dereference ->* ++ dereference 5. Additive + Binary plus - Binary minus 6. Shift << Shift left >> Shift right 7. Relational < Less than <= Less than or equal to > Greater than >= Greater than or equal to 8. Equality == Equal to!= Not equal to 9. & Bitwise AND 10. ^ Bitwise XOR 11. Bitwise OR 12. && Logical AND 13. Logical OR 14. onditional?: (a? x : y means "if a then x, else y") SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 4

++ Programming Lexical onventions # ategory Operator What it is (or does) 15. Assignment = Simple assignment *= Assign product /= Assign quotient %= Assign remainder (modulus) += Assign sum -= Assign difference &= Assign bitwise AND ^= Assign bitwise XOR = Assign bitwise OR <<= Assign left shift >>= Assign right shift 16. omma, Evaluate All of the operators in this table can be overloaded except the following:. ++ direct component selector.* ++ dereference :: ++ scope access/resolution?: onditional SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 5

++ Programming Lexical onventions 2 Punctuators The Turbo ++ punctuators (also known as separators) are: [ ] ( ) { }, ; :... * = # Most of these also function as operators. 2.1 Braces ( { } ) The { } braces indicate the start and end of a compound statement. The semicolon is a statement terminator. 2.2 Semicolon ( ; ) Any legal expression (including the empty expression) followed by ; is interpreted as a statement, known as an expression statement. The expression is evaluated and its value is discarded. If the expression statement has no side effects, Turbo ++ can ignore it. Semicolons are often used to create an empty statement. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 6

++ Programming Lexical onventions Use the colon (:) to indicate a labeled statement: 2.3 olon ( : ) start: x=0;... goto start;... switch (a) { case 1: puts("one"); break; case 2: puts("two"); break;... default: puts("none of the above!"); break; } 2.4 Ellipsis (... ) An ellipsis (...) consists of three successive periods with no whitespace intervening. You can use an ellipsis in the formal argument lists of function prototypes to indicate a variable number of arguments, or arguments with varying types. void func(int n, char ch,...); This declaration indicates that func will be defined in such a way that calls must have at least two arguments, an int and a char, but can also have any number of additional arguments. In ++, you can omit the comma preceding the ellipsis. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 7

++ Programming Lexical onventions 2.5 Equal Sign ( = ) The = (equal sign) separates variable declarations from initialization lists. char array[5] = { 1, 2, 3, 4, 5 }; int x = 5; Declarations of any type can appear, with some restrictions, at any point within the code. Function argument list, the equal sign indicates the default value for a parameter: int f(int i = 0) {... } // parameter i has default value of zero The equal sign is also used as the assignment operator in expressions: a = b + c; 2.6 Pound Sign ( # ) The # (pound sign) indicates a preprocessor directive when it occurs as the first non-whitespace character on a line. It signifies a compiler action, not necessarily associated with code generation. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 8

++ Programming Lexical onventions 3 omments A comment is text that the compiler ignores. omments are normally used to annotate code for future reference. The compiler treats them as white space. omments can be used by programmer in testing to make certain lines of code inactive. In most cases, however, #if/#endif preprocessor directives should be used because although comments cannot benested. 3.1 ANSI Style omment The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. omments using this syntax cannot be nested. /* Intent: omment out this block of code. Problem: Nested comments on each line of code are illegal. FileName = String( "hello.dat" ); /* Initialize file string */ cout << "File: " << FileName << "\n"; /* Print status message */ */ 3.2 Single Line omment A single line commets consists of // (two slashes) characters, followed by any sequence of characters. A new line not immediately preceded by a backslash terminates this form of comment. Therefore, it is commonly called a single-line comment. The single-line form (//) of a comment followed by the line-continuation token (\) can have surprising effects. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 9

++ Programming Lexical onventions onsider this code: 1 2 3 4 5 6 7 #include <stdio.h> void main() { printf( "This is a number %d", // \ 5 ); } After preprocessing, the preceding code contains errors and appears as follows: 1 2 3 4 5 6 #include <stdio.h> void main() { printf( "This is a number %d", } SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 10

++ Programming Lexical onventions 4 Identifiers An identifier is a sequence of characters used to denote one of the following: Object or variable name. lass, structure, or union name. Enumerated type name. Member of a class, structure, union, or enumeration. Function or class-member function. typedef name. Label name. Macro name. Macro parameter. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 11

++ Programming Lexical onventions 4.1 Naming Rules 1. An identifier can contain: upper or lower case alphabet digit underscore 2. The first character of an identifier must be an alphabetic character, either uppercase or lowercase, or an underscore ( _ ). 3. An identifier must not be a /++ keyword. Use of two sequential underscore characters ( ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for ++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers. 4.2 ase ++ identifiers are case sensitive: filename is different from FileName. Identifiers cannot be exactly the same spelling and case as keywords. Identifiers that contain keywords are legal. Pint is a legal identifier, even though it contains int, which is a keyword. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 12

++ Programming Lexical onventions 4.3 Keywords Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. The following keywords are reserved for ++: asm auto bad_cast bad_typeid break case catch char class const const_cast continue default delete do double dynamic_cast else enum except extern finally float for friend goto if inline int long namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw try type_info typedef typeid union unsigned using virtual void volatile while xalloc SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 13

++ Programming Lexical onventions 5 Literals Invariant program elements are called literals or constants. Literals fall into four major categories: integer character floating-point string literals 5.1 Integer onstants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. Decimal To specify a decimal constant, begin the specification with a nonzero digit. int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal Octal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. int i = 0377; int j = 0397; // Octal constant // Error: 9 is not an octal digit SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 14

++ Programming Lexical onventions Hexadecimal To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the x does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. int i = 0x3fff; int j = 0X3FFF; // Hexadecimal constant // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. unsigned uval = 328u; long lval = 0x7FFFFFL; // Unsigned value // Long value specified // as hex constant unsigned long ulval = 0776745ul; // Unsigned long value 5.2 haracters onstants haracter constants are one or more members of the source character set, the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the execution character set, the character set on the machine where the program executes. There are three kinds of character constants: Normal character constants Multicharacter constants Wide-character constants SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 15

++ Programming Lexical onventions Use wide-character constants in place of multicharacter constants to ensure portability. haracter constants are specified as one or more characters enclosed in single quotation marks. char ch = 'x'; int mbch = 'ab'; wchar_t wcch = L'ab'; // Specify normal character constant. // Specify system-dependent // multicharacter constant. // Specify wide-character constant. The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. char schar = 'x'; wchar_t wchar = L'\x81\x19'; // Normal character constant // Wide-character constant SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 16

++ Programming Lexical onventions ++ Reserved or Nongraphic haracters haracter ASII Representation ASII Value Escape Sequence Newline NL (LF) 10 or 0x0a \n Horizontal tab HT 9 \t Vertical tab VT 11 or 0x0b \v Backspace BS 8 \b arriage return R 13 or 0x0d \r Formfeed FF 12 or 0x0c \f Alert BEL 7 \a Backslash \ 92 or 0x5c \\ Question mark? 63 or 0x3f \? Single quotation mark ' 39 or 0x27 \' Double quotation mark " 34 or 0x22 \" Octal number ooo \ooo Hexadecimal number hhh \xhhh Null character NUL 0 \0 SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 17

++ Programming Lexical onventions 5.3 Floating-point onstants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. Floating-point constants have: mantissa exponent optional suffix specifies the value of the number. specifies the magnitude of the number. that specifies the constant s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10. 18.46e0 // 18.46 18.46e1 // 184.6 If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 18

++ Programming Lexical onventions 5.4 String Literal A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string. ++ strings have these types: Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string. Array of wchar_t, for wide-character strings The result of modifying a string constant is undefined. char *szstr = "1234"; szstr[2] = 'A'; // Results undefined SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (PP - 4.3) h 2: Page 19