COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
1 Structured Programming in C Welcome to CPSC 206.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Syntax of C Programming Language #Lesson 2 Sen Zhang.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Object Oriented Programming with C++ Diploma in Computer System Design.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Data types Data types Basic types
LESSON 3 IO, Variables and Operators
Introduction to C Programming Language
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
Lexical Elements, Operators, and the C Cystem
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Lexical Elements, Operators, and the C Cystem
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Variables in C Declaring , Naming, and Using Variables.
WEEK-2.
Programming Language C Language.
Data Type.
Module 2 Variables, Data Types and Arithmetic
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
INTRODUCTION TO C.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering Korea University

Natural Language Processing Lab, Korea University 2 Compilers and Tokens  C is a language  An alphabet  Syntax: rules for putting together words and punctuations to make legal programs  Compiler  Checks the legality of the source code (program)  Translate the source code into object code (object code is translated into executable code (i.e. target language, machine language)  Collects the characters of the program into tokens  Tokens in C (ANSI C)  The basic vocabulary of C  keywords, identifiers, constants, string constants, operators, punctuators

Natural Language Processing Lab, Korea University 3 Contents 2.1 Characters and Lexical Elements 2.2 Syntax Rule( 구문 규칙 ) 2.3 Comments( 주석 ), 2.4 keywords 2.5 Identifiers( 식별자 ), 2.6 Constants( 상수 ) 2.7 String Constants( 문자열 상수 ) 2.8 Operators and Punctuators( 연산자와 구두점 ) 2.9 Precedence and Associativity of Operators ( 연산자의 우선 순위와 결합 법칙 ) 2.10 Increment and Decrement Operators ( 증가 연산자와 감소 연산자 ) 2.11 Assignment Operators ( 배정 연산자 ) 2.12 An example: Computing Powers of The C system

Natural Language Processing Lab, Korea University Characters and Lexical Elements  A programmer construct C program as a sequence of characters  Characters that can be used in a C program  Lowercase letters : a b c... z  Uppercase letters : A B C... Z  Digits :  Other characters : + - * / = ( ) { } [ ] ’ # $ % ^ & _ | \ ~., ; : ?  White space characters : blank, newline, tab  Compiler collects characters into tokens

Natural Language Processing Lab, Korea University 5 An Example C Source Code – sum.c /* Read in two integers and print the sum. */ #include int main(void) { int a, b, sum; printf(”Input two integers : ”); scanf(”%d%d”, &a, &b); sum = a + b; printf(”%d + %d = %d\n”, a, b, sum); return 0; }

Natural Language Processing Lab, Korea University 6 An Example C Source Code – sum.c  /* Read in two integers and print their sum. */  comments : /* 부터 */ 까지는 공백으로 대치  #include  전처리 지시자 : the standard header file stdio.h is included  stdio.h contains the function prototypes for printf() and scanf()  int main(void) { int a, b, sum; => The compiler groups these characters into four kinds of tokens:  키워드 : int, void  식별자 : main, a, b, sum  연산자 : ( ) -> tells the compiler that main is a function  구두점 : “{”, “,”, “;”

Natural Language Processing Lab, Korea University 7 An Example C Source Code – sum.c  printf(“Input two integers: “); scanf(“%d%d”, &a, &b);  printf and scanf are identifier  () after them tells the compiler that they are functions  "Input two integers : "  String constant : 큰 따옴표로 둘러싸인 문자들  &a, &b  & is the address operator  sum = a + b;  = and + are operators

Natural Language Processing Lab, Korea University Syntax Rules  BNF(Backus-Naur Form)  Can be used to describe a programming language  Productions or rewriting rules  Symbols used in productions italicsnon-terminals (syntactic categories) ::=LHS can be rewritten as RHS |choice { } 1 choose one item { } 0+ repeat 0 or more times { } 1+ repeat 1 or more times { } opt optional othersterminal symbols

Natural Language Processing Lab, Korea University 9 BNF Examples - 1  Any lowercase or uppercase alphabet or any digit digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 lowercase_letter ::= a | b | c |... | z uppercase_letter ::= A | B | C |... | Z letter ::= lowercase_letter | uppercase_letter letter_or_digit ::= letter | digit  e.g) “a”, “3”, …  Any sequence of letter or digits alphanumeric_string ::= {letter | digit} 1+  e.g) “3”, “ab777c”, …

Natural Language Processing Lab, Korea University 10 BNF Examples - 2  Any sequence of letter or digits that start with a uppercase letter  Any sequence of letter or digits that start with either a uppercase letter or a digit  Any sequence of letter or digits that may end with “.” u_alpha_string ::= uppercase_letter {letter | digit} 0+ p_name ::= {uppercase_letter | digit} 1 {letter | digit} 0+ f_word ::= {letter | digit} 1+ {.} opt

Natural Language Processing Lab, Korea University Comments( 주석 )  C style comments  Any strings placed between “ /* ” and “ */ ”  /* here is comments */  /** this is also comments **/  /* what if ”*/” comes in the middle */  C++ style comments  // form here to the end of the line

Natural Language Processing Lab, Korea University Keywords( 키워드 )  Reserved words in C (They can not be redefined or used in other contexts)  Additional keywords in Borland C  asm, cdecl, far, huge, interrupt, near, pascal auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Natural Language Processing Lab, Korea University Identifiers( 식별자 )  Identifiers  Names of variables or functions  identifier ::= { letter | _ } 1 { letter | digit | _ } 0+  e.g.) k, _id, iamanidentifier2, so_am_i not#me, 101_south, -plus (X)  First 31 characters of identifier are discriminated (ANSI C)  Good programming style: choose meaningful names  Caution: Identifiers that begin with _ can conflict with system names.

Natural Language Processing Lab, Korea University Constants( 상수 )  Integer  e.g.) 0, 17, 017, 0x17  Floating numbers  e.g.) 1.0, , 3.14E+03  Character constants  e.g.) ’a’, ’b’, ’+’, ’\n’  String constants  e.g.) ”abc”, ””, ” ”, ”\n”, ”\””, ”\\”

Natural Language Processing Lab, Korea University Operators,Punctuators( 연산자, 구두점 )  Operators  e.g.) +, -, *, /, %  Punctuators  e.g.) “ ( ”, “ ) ”, “ { ”, “ } ”, “, ”, “ ; ”  Example int main(void) { int a, b, c = 3; a = 17 * ( b + c ); printf(”%d\n”, a); }

Natural Language Processing Lab, Korea University Precedence and Associativity ( 우선순위와 결합법칙 )  Precedence and associativity  The order in which operations are performed  e.g.) * 3  1 + (2 * 3) – 3  ((1 + 2) – 3)  Precedence and associativity of arithmetic operators OperatorAssociativity () ++(postfix) --(postfix) +(unary) –(unary) ++(prefix) --(prefix) * / % + - = += -= *= /= etc. left to right right to left left to right right to left

Natural Language Processing Lab, Korea University Increment and Decrement Operators ( 증가 연산자와 감소 연산자 )  Increment and decrement operators  ++i;  i = i + 1;  i++;  --i;  i = i – 1;  i--;  Both operators can be either prefix or postfix  Exercise int a, b, c=0, d=0; a = ++c; b = d++; printf(”%d %d %d %d\n”, a, b, c--, --d);  a=1, b=0, c=1, d=1 (before printf)  is printed  a=1, b=0, c=0, d=0 (after printf)

Natural Language Processing Lab, Korea University Assignment Operators ( 배정연산자 )  Assignment operators  Change the value of a variable  e.g.) a = 1; a = 2 + 3; a = b + c; a = ( b = 2 ) + ( c = 3 ); a = b = c = 0;  More assignment operators  +=, -=, *=, /=, %=, >=, |=, &=, ^=  e.g.) k += 2;  k = k + 2; j *= k + 3;  j = j * (k + 3);

Natural Language Processing Lab, Korea University 19 Assignment operator( 배정 연산자 )  다른 언어와는 달리 C 는 = 를 연산자로 다룸 a = ( b = 2 ) + ( c = 3);  배정 연산자 =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= ( 주의 ) j *= k + 3 은 j = j * k + 3 이 아니라, j = j * (k + 3) 임 선언 및 초기화 int i = 1, j = 2, k = 3, m = 4; 수식 동일한 수식 동일한 수식 결과 i += j + k i += (j + k) i = (i + (j + k)) 6 j *= k = m + 5 j *= (k = (m + 5)) j = (j * (k = (m + 5))) 18

Natural Language Processing Lab, Korea University Example – Computing P owers of 2 /* Some powers of 2 are printed. */ #include int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%-6d", power *= 2); printf("\n"); return 0; } 

Natural Language Processing Lab, Korea University The C system Preprocessor and Standard Library  C 시스템  C 언어, 전처리기, 컴파일러, 라이브러리, 편집기 등으로 구성  전처리기  # 으로 시작하는 행을 전처리 지시자라고 함 #include #include "filename" #define PI  표준 라이브러리  프로그램에 유용한 함수들로 C 시스템이 제공함  printf(), scanf(), 등  사용자가 알아서 해당 헤더파일을 포함시켜야함

Natural Language Processing Lab, Korea University 22 Practice – prn_rand.c #include int main(void) { int i, n; printf("\n%s\n%s", "Random integers will be printed.", "How many do you want to see?"); scanf("%d", &n); for (i = 0; i < n; ++i) { if (i % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; }