Chapter 2. Lexical Elements & Operators

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

Chapter 4. Flow of Control

Lecture 3. More About C

Chapter 3. Fundamental Data Types

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

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

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

Differentiate Between Keywords and Identifiers

UNIT- 3 Introduction to C++

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

LECTURE 02 INTRODUCTION TO C++

Full file at

1 Lexical Considerations

Basics of Java Programming

Chapter 2: Introduction to C++

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

BITG 1233: Introduction to C++

Chapter 2 Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Programming for Engineers Introduction to C

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

Lexical Considerations

PYTHON- AN INNOVATION

Typescript on LLVM Language Reference Manual

Visual C# Instructor s Manual Table of Contents

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 2: Using Data

Lexical Considerations

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

The C++ Language. Arizona State University 1

Program Fundamentals

INTRODUCTION 1 AND REVIEW

Data Types and Variables in C language

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

C: How to Program. Week /Mar/05

DEPARTMENT OF MATHS, MJ COLLEGE

Fundamental of C programming. - Ompal Singh

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

4. Inputting data or messages to a function is called passing data to the function.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 2 - Introduction to C Programming

ARG! Language Reference Manual

Operators & Expressions

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

6.096 Introduction to C++ January (IAP) 2009

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

LEXICAL 2 CONVENTIONS

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

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

CHAPTER 3 BASIC INSTRUCTION OF C++

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to Programming

DaMPL. Language Reference Manual. Henrique Grando

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

Operators. Java operators are classified into three categories:

Lecture 2 Tao Wang 1

Chapter 3 Structure of a C Program

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Basics of Programming

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

Chapter 17. Fundamental Concepts Expressed in JavaScript

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Fundamentals of Programming. Lecture 3: Introduction to C Programming

YOLOP Language Reference Manual

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Computer Programming : C++

Fundamental of Programming (C)

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Fundamentals of Programming Session 4

Objectives. In this chapter, you will:

VARIABLES AND CONSTANTS

COMP Primitive and Class Types. Yi Hong May 14, 2015

CHAPTER-6 GETTING STARTED WITH C++

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

THE FUNDAMENTAL DATA TYPES

C++ Programming: From Problem Analysis to Program Design, Third Edition

C - Basic Introduction

IT 374 C# and Applications/ IT695 C# Data Structures

Variables, Constants, and Data Types

L-System Fractal Generator: Language Reference Manual

4 Programming Fundamentals. Introduction to Programming 1 1

Chapter 1 Introduction to Computers and C++ Programming

Transcription:

Chapter 2. Lexical Elements & Operators Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr

The C System C Language Preprocessor Compiler (Standard) Library Linker Debugger Editor

Syntax of the language C Compiler Rules for putting together words and punctuation to make correct, or legal, programs Compiler A program that checks on the legality of C code If errors exist, compiler prints error messages and stops If there is no errors, compiler translates the code into object code

C program C Program A sequence of characters that will be converted by a C compiler to object code Compilers first collects the characters of the C program into tokens. 6 kinds of tokens Keywords, Identifiers, Constants, String constants, Operators, Punctuators

Lowercase letters a b c Uppercase letters A B C Digits 1 2 3 4 5 6 7 8 9 Other special characters Characters used in a C Prog. + -* / = ( ) { } [ ] < >! # % & _ ^ ~ \., ; :? Whitespace characters blank, newline, linefeed, tab, etc.

Comments Comment A sequence of characters used for memo or documentation which are ignored by the compiler Single line Starts with // Closed Format Start with /* and ends with */ Multiline of comments are possible. // blah blah I m free from the codes printf( hello ); // print hello!! /* this is comment!! blah blah blah blah blah blah */ printf( hello ); /* print hello!! */

Documentation Comments Codes are frequently shared with other people or communities. Comments must provide clarity to the C source code. Always write comments for clarity even if the codes are not shared. It will help debugging. // calculate the area for given radius and prints it area = 3.14 * r * r ; printf( area: %f, area);

Reserved words Keywords Keywords have a strict meaning as individual tokens in C. Keywords can not be redefined or used in other contexts.

Identifiers Identifiers A token composed of a sequence of letters, digits, and the special character _ (underscore) A letter or underscore must be the first character of an identifier Lowercase and uppercase are distinct. < legal> knot _id101 iamanidentifier2 so_am_i < illegal> not#2 101_south -plus

Identifiers Give unique names to objects in a program. Objects : functions, variables, etc Keywords can be thought of as identifiers that are reserved to have special meaning. (Note: Keywords are not identifiers.) The identifier main is special. It is identified as a main function(entry point) of a program by the compiler. Choose names that are meaningful! tax_rate

Identifiers main miles yards kilometers printf

Integer constants 0, -7, 17 Floating constants 1.0, 3.14159 Character constants Constants a, b, c Special character constants \n (newline) \t (tab) \\ (backslash \ ) Backslash is the escape character. It has an alternative interpretation.

Integer constants Decimal integers 17 Octal integers 017 Hexadecimal integers 0x17 Constants

String Constants A sequence of characters enclosed in a pair of double-quote marks abc collected as a single token a and a are NOT the same. /* this is not a string */ /* this is not a comment */

Punctuators Punctuators parentheses, braces, commas, semicolons, etc Operators and punctuators, along with white space, serve to separate language elements int main(void) { int a, b = 2, c = 3; a = 17 * (b + c);

Operators Arithmetic Operators +, -, *, /,% % is a modulus operator a % b is a remainder when a is divided by b Operators can be used to separate identifiers a + b Some symbols have meanings that depend on context printf( %d, a); // % is used for formatted string a = b%7; // % is used as a modulus operator

relational operators Relational Operators ==,!=, >, <, >=, <= Result value of the relational operator is one(true) or zero(false). Operator Description == Equal!= Not equal > Greater than < Less than >= Greater or equal than <= Less than or equal than if (a == b) { print( a, b are equal!! ); }

logical operators Logical Operators &&,,! Result value of the logical operator is one(true) or zero(false). Operator Description && AND OR! Negation (1==1) && (3 > 2) true (1!=1) && (3 > 2) false! (0 > 1) (2 < 1) true 0 0 false

++, -- Increment and Decrement Operators Increase(decrease) an operand by one Operands must be variables. ++a; // same as a = a + 1 --b; // same as b = b - 1 Prefix and postfix z = ++a; a = a + 1; z = a; z = a++; z = a; a = a + 1;

Binary & Unary Operators Binary Binary operators have two operands. a + b, 100 / 65 Unary Unary operators have a single operand. -2, ++a

Precedence and Associativity of Operators Precedence: order of an operator 1 + 2 * 3 1 + (2 * 3) Associativity: direction of an operator when precedence are same 1 + 2-3 + 4-5 (((1+2) - 3) + 4) 5 note that binary + and binary have same precedence Parentheses can be used to clarify or change the order in which operators are performed. (1 + 2) * 3

Assignment Operator a = b + c; /* assignment statement */ = is treated as an operator Its precedence is lower than all others right to left associativity variable = right_side right_side is itself expression The value of right_side is assigned to variable. Result value of the assignment operator is the value of right_side. a = b = c = 3 a = b = (c = 3) a = b = 3 a = 3 3 z = 1 + (x = 3 2) z = 1 + (x = 1) z = 1 + 1 z = 2 2

Assignment Operator Some complex assignment operators +=, -=, *=, /=, %= a += b is same as a = a + b Assignment could be used in the variable declaration statements. int a = 3; float price = 3000, discount = 0.5;