1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.

Slides:



Advertisements
Similar presentations
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Lexical Elements Chapter 2 in ABC. 2 Tokens Token = word / symbol, does not contain white spaces. Tokens in C are either: –keywords –identifiers –constants.
C programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
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.
Basic Elements of C++ Chapter 2.
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.
Introduction to C Language
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
Computer Science 210 Computer Organization Introduction to C.
COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Minimal standard C program int main(void) { return 0 ; }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
A Sample Program #include using namespace std; int main(void) { cout
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Computer Science 210 Computer Organization
Chapter 2 Introduction to C++ Programming
ECE Application Programming
Chapter 2 - Introduction to C Programming
Tokens in C Keywords Identifiers Constants
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
Lexical Elements, Operators, and the C Cystem
A First Book of ANSI C Fourth Edition
Introduction to Java Applications
Lexical Elements & Operators
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

1 Lexical Elements, Operators, and the C System

2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String Constants

3 Outline (continued) Operators and Punctuations Precedence and Association of Operators Increment and Decrement Operators Assignment Operators Example The C System (preprocessor and standard library)

4 Characters and Lexical Elements Rule of program is called syntax The program that check the legality of C code is called the compiler Compiler collects the characters of the program into tokens Six tokens: keywords, identifiers, constants, string constant, operators, and punctuations

5 C Program Group characters into token Translate tokens into target code The Compilation Process

6 Characters and Lexical Elements Basic characters being used in a C program These characters are collected by the compiler into syntactic units called tokens. Lowercase: a b c … z Uppercase: A B C … Z Digits: … 9 Others: + - * / ( ) { } [ ] ' " ! # $ % ^ ~ & | \ ; :,. / ? Space character: blank space, new-line, tab, etc.

7 /* Read in two integers and print their 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; } Input two integers: = 6 Comment line using /* */ Preprocessor directive which causes stdio.h to be included, in order to use printf() and scanf() Result

8 main is identifier, as a function name; () is an operator; int is a keyword; {, }, ;, and, are punctuations; a,b,and sum are identifiers. printf and scanf are identifiers as function names " is punctuation Sequence characters in the double quotes is a string constant & is an operator for memory address = and + are operators, one for assignment and another one for arithmetic adding C compiler ignore white space

9 Comments Comments are not token. Compiler ignores the comments statement You can put comment statement in either blank line or white space after the C statement, such as Different comments area = length*height; /*calculate area of rectangle */

10 /* a comment */ /*** another comment ***/ /**********************/ /* * A comment can be written in this fashion * to set it off from the surrounding code. / /********************************* * If you wish, you can * * put comments in a box. * *********************************/

11 Keywords C has less reserved keywords autodogotosignedunsigned breakdoubleifsizeofvoid caseelseintstaticvolatile charenumlongstructwhile constexternregisterswitchcontinue floatreturntypedefdefaultfor shortunion

12 Identifier --- naming Identifier is a token It composed of a sequence of letters, digits, and underscore _ Identifier is case-sensitive (i.e., age is different from Age) k _id (not advised) kamanidentifiers so_am_i not#me 101_south -plus add another legal illegal

13 Identifier --- naming Be unique identifier No keyword can be used as identifier Size of identifier depends on systems (at least 31 in ANSI C). Always choose meaningful identifier for naming variables, such as tax_rate, price, … Underscore is used to create a single name, but has the meaning of word, such as C_class_student.

14 Constants Character constant Integer constant Floating number constant 'a', 'G', '\n', '\t', 43, -54, , 13.23e+2

15 String Constants string constants are identified by double quote " " a string of text" "" " "a = b+c;" "/* this is not a comment */" "a string with double quotes \" within" "a single backslash \\ is in this string" "abd" "efg" /* "this is not a string" */ "and Neither is this " 'dgashgahg' legal illegal

16 Operators and Punctuators Arithmetic operators ( + - * / %) + and – has four meanings; one is as arithmetic operator for addition, the second is for changing the sign of value, such as c= -a – b; The third one is used as increment or decrement operator such as ++ and – The fourth one is used as additional operator

17 Precedence and Associativity of Operators Operators have rules of precedence and associativity that are used to determine how expressions are calculated and what is the order of operations Example 1+ 2*3 1 +(2*3) (1+2)*3

18 Precedence and Associativity of Operators Left-to-right rule as associativity rule (((1+2)-3)+4)-5

19 Operator precedence and associativity Operatorassociativity () ++(postfix) --(postfix)left to right + (unary) – (unary) ++ (prefix) – (prefix) right to left * / %left to right + -left to right = += -= *= /= %= etc.Right to left -a*b-c((-a)*b)-c

20 Increment and Decrement Operators Increment and decrement by 1 They are unary operators They both can be used as both postfix and prefix They have higher precedence ++int_variable-- int_ variable int_variable++int_ variable-- ++i cnt (a*b-1) not:

21 Increment and Decrement Operators The difference between postfix and prefix int a,b,c=0; a=++c; b=c++; printf(%d %d %d\n", a,b,++c); ++i; i++; i=i+1; ++a*b-c--((++a)*b)-(c--) 7—b*++d7-((-b)*(++d))

22 Assignment Operator "=" is assignment operator variable=right_side (expr); b=2; c=3; a=b+c; a=(b=2) + (c=3); a=b=c=0; a=(b=(c=0));

23 Assignment Operator Additional assignment operators +=-=*=/=%= >>=<<=&=^= |= variable op=expressionvariable =variable op expression j*=k+3j=j*(k+3);

24 Assignment Operator More example Power of 2 example i+=j+ki+=(j+k)i=i+(j+k) j*k=m+5k*=(k=(m+5))j=j*(k=(m+5))

/* 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; }

26 C System Preprocessor using preprocessor directive # – #include#define #include #incluse #include "headerfile.h" Search in other (System) places Search in the same directory

27 C System Usually, header file contains function definitions On UNIX, the header files are located in the directory /usr/include On Borland C, it may be found in c:\bc\include Put function prototypes or function registration in the header file int printf(const char * format, … ); int scanf(const char *format, …);

28 C System Standard library – C has many useful built-in and useful functions – The collection of functions is called "library" – Example of using functions built in the existing library

29 #include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed 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; } Width of field for print is 7 Needs srand(time(Null));

Result: If you run the code again, you will get the same result Why? We have not generate seed yet.

31 C System Seed the random-number generator Include Add the following line before using rand() function srand(time(NULL);

32 #include int main(void) { int i, n, seed; /* seed=time(NULL); srand(seed); alternatively we can use the following line. */ srand(time(NULL));

33 printf("\n%s\n%s", "Some randomly distributed 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; }

Execution 1: Execution 2: