Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax of C Programming Language #Lesson 2 Sen Zhang.

Similar presentations


Presentation on theme: "Syntax of C Programming Language #Lesson 2 Sen Zhang."— Presentation transcript:

1 Syntax of C Programming Language #Lesson 2 Sen Zhang

2 Topics Covered Lexical elements Lexical elements Grammar Grammar Case sensitive Case sensitive Reserved Words Reserved Words Identifiers Identifiers Data Types Data Types Variables Variables Constants Constants Operations on data Operations on data

3 Lexical elements If you look at any program, you will see that a program simply consists of reserved words, identifiers, constants (literals or constant symbols), operators glue them together and punctuators to delimit them. If you look at any program, you will see that a program simply consists of reserved words, identifiers, constants (literals or constant symbols), operators glue them together and punctuators to delimit them. The above tokens (separable units) are put together under certain rules (both syntactic and logical). The above tokens (separable units) are put together under certain rules (both syntactic and logical).

4 Natural Language Grammar Grammar Syntax (sentence, paragraph, article.) Syntax (sentence, paragraph, article.) Lexical (Vocabulary, verb, noun, etc.) Lexical (Vocabulary, verb, noun, etc.) Reserved Vocabulary Reserved Vocabulary Data Data Constant, symbol Constant, symbol New Vocabulary New Vocabulary New data New data New function New function

5 Syntax and Semantics Syntax is the structure of a language: Syntax is the structure of a language: “What programs can I write?” “What programs can I write?” “Will the compiler accept this program?” “Will the compiler accept this program?” Semantics is the meaning of a language: Semantics is the meaning of a language: “How do programs work?” “How do programs work?” “What will this program do?” “What will this program do?”

6 Syntax Trees A grammar says how a syntax tree can be built: A grammar says how a syntax tree can be built: sentence sentence clause clause noun-phrase noun-phrase noun verb prep article noun noun verb prep article noun “Time flies like an arrow.” “Time flies like an arrow.”

7 A comparison A document (an instruction manual) -> program A document (an instruction manual) -> program A paragraph -> a block or a function A paragraph -> a block or a function A sentence or subclause -> a statement A sentence or subclause -> a statement A phrase -> an expression A phrase -> an expression A word or term identifier -> a constant or a variable name A word or term identifier -> a constant or a variable name

8 Lexical Elements A lexical element refers to a character or groupings of characters that may legally appear in a source file. A lexical element refers to a character or groupings of characters that may legally appear in a source file.

9 Source program character set The following lists the basic source character sets that are available at both compile time and run time: The following lists the basic source character sets that are available at both compile time and run time: The uppercase and lowercase letters of the English alphabet: The uppercase and lowercase letters of the English alphabet: a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z The decimal digits: 0 1 2 3 4 5 6 7 8 9 The decimal digits: 0 1 2 3 4 5 6 7 8 9 The following graphic characters: The following graphic characters: ! " # % & ' ( ) * +, -. / : ; ? [ \ ] _ { } ^ |~ ! " # % & ' ( ) * +, -. / : ; ? [ \ ] _ { } ^ |~ The space character The space character The control characters representing new-line, horizontal tab, vertical tab, form feed, end of string (NULL character), alert, backspace, and carriage return. The control characters representing new-line, horizontal tab, vertical tab, form feed, end of string (NULL character), alert, backspace, and carriage return. ASCII codes ASCII codes

10 Tokens Source code is treated during preprocessing and compilation as a sequence of tokens. Source code is treated during preprocessing and compilation as a sequence of tokens. A token is the smallest independent unit of meaning in a program, as defined by the compiler. A token is the smallest independent unit of meaning in a program, as defined by the compiler. There are five different types of tokens: There are five different types of tokens: Keywords Keywords Identifiers Identifiers Literals Literals Punctuator Punctuator operators operators

11 Keywords Keywords are words reserved by the language for special use. Keywords are words reserved by the language for special use. Although you can use them for preprocessor macro names, it is considered poor programming style. (At csci109 and csci116 level, you just do not redefine keywords at anywhere. ) Although you can use them for preprocessor macro names, it is considered poor programming style. (At csci109 and csci116 level, you just do not redefine keywords at anywhere. ) Also called reserved words, cannot be used as identifiers. Also called reserved words, cannot be used as identifiers. Since the language is case sensitive, only the exact spelling of keywords is reserved. For example, auto is reserved but AUTO is not. Since the language is case sensitive, only the exact spelling of keywords is reserved. For example, auto is reserved but AUTO is not.

12 Difference between a keyword and an identifier? Keywords are reserved words like(int, float) which have special meanings for compilers. Keywords are reserved words like(int, float) which have special meanings for compilers. Identifiers are given by programmers to label any variable or any name used to identify an variable, function, constant value, object or entity. Identifiers are given by programmers to label any variable or any name used to identify an variable, function, constant value, object or entity. Hence keywords cannot be used as an identifier Hence keywords cannot be used as an identifier

13 Adjacent identifiers, keywords, and literals must be separated with operators or punctuators or sometimes space. Adjacent identifiers, keywords, and literals must be separated with operators or punctuators or sometimes space. Other tokens should be separated by white space to make the source code more readable. White space includes blanks, horizontal and vertical tabs, new lines, form feeds, and comments. Other tokens should be separated by white space to make the source code more readable. White space includes blanks, horizontal and vertical tabs, new lines, form feeds, and comments.

14 A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. A punctuator can also be a token that is used in the syntax of the preprocessor. A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. A punctuator can also be a token that is used in the syntax of the preprocessor.

15 Reserved Words The reserved words are predefined identifiers or names used internally by C compiler. The reserved words are predefined identifiers or names used internally by C compiler. They are always written in lower case. They are always written in lower case. You shouldn't use them for any other purpose in a C program. You shouldn't use them for any other purpose in a C program. There are 32 words defined as keywords in C. There are 32 words defined as keywords in C.

16 Reserved word cont’ auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while Although main is not in the reserved words list, it should not be taken as a user-defined identifier for your variable or other function names except for the main function itself to avoid confusion. This is because main is the entry point for an independent executable C program. Although main is not in the reserved words list, it should not be taken as a user-defined identifier for your variable or other function names except for the main function itself to avoid confusion. This is because main is the entry point for an independent executable C program.

17 Case sensitive Int is not a keyword, int is. Int is not a keyword, int is. Case matters. Case matters.

18 WHAT IS AN IDENTIFIER? Before you can do anything in any language, you must know how to name an identifier (Noun in natural language, if you tend to think some reserved words verbs. Although this comparison is not always true.) Before you can do anything in any language, you must know how to name an identifier (Noun in natural language, if you tend to think some reserved words verbs. Although this comparison is not always true.) An identifier is used for any variable, function, constant data definition, etc. An identifier is used for any variable, function, constant data definition, etc.

19 Identifier Naming Conventions Identifiers (variable names are kinds of identifiers) must follow these rules: Identifiers (variable names are kinds of identifiers) must follow these rules: consist of a sequence of letters, digits, and underscore. consist of a sequence of letters, digits, and underscore. cannot start with a digit. cannot start with a digit. can be of any length, only the first 31 characters are significant. can be of any length, only the first 31 characters are significant. are case sensitive. are case sensitive. cannot be the same as keywords (reserved words) cannot be the same as keywords (reserved words)

20 Examples of legal Identifier Names Legal variable names Legal variable namesClassSize_previous_valuePercentPerCentPERCENTKdff123kkkkk

21 Examples of illegal identifier names identifiersreasons savings#account Contains the illegal character # Double Is a C keyword rad ius Space is illegal Tax-Rate - is illegal union A reserved word 9winter First character is a digit

22 Two rules must be kept in mind when naming identifiers. Two rules must be kept in mind when naming identifiers. C is case-sensitive. The case of alphabetic characters is significant. The names percent, PERCENT, and Percent would be considered three different variables. C is case-sensitive. The case of alphabetic characters is significant. The names percent, PERCENT, and Percent would be considered three different variables. According to the ANSI-C standard, at least 31 significant characters can be used and will be considered significant by a conforming ANSI-C compiler. If more than 31 are used, all characters beyond the 31st may be ignored by any given compiler. According to the ANSI-C standard, at least 31 significant characters can be used and will be considered significant by a conforming ANSI-C compiler. If more than 31 are used, all characters beyond the 31st may be ignored by any given compiler.

23 Data types 'data types' are used to define a variable before its use. 'data types' are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.

24 Major Data Types in C Scalar Types Scalar Types integer integer float float double double character character Void Void Aggregate Types Aggregate Types Arrays Arrays strings strings structures structures

25 Basic data types void associated with no data type void associated with no data type int integer int integer float floating-point number float floating-point number double double precision floating-point number double double precision floating-point number char character char character

26 Data type is defined by two properties Data type is defined by two properties A set of values, domain, which represents the set of values that are valid elements of that type. A set of values, domain, which represents the set of values that are valid elements of that type. For example, the domain of the integer type includes all integers. For example, the domain of the integer type includes all integers. A set of operations, which defines the operations which are meaningful in that data type domain. A set of operations, which defines the operations which are meaningful in that data type domain. For example you can multiple two integers, but you cannot multiple two characters. For example you can multiple two integers, but you cannot multiple two characters.

27 The Integer Type int is a data type specifier used to declare integer value variables. int is a data type specifier used to declare integer value variables. Includes: int, short, and long Includes: int, short, and long The int type usually occupies a word in memory The int type usually occupies a word in memory If a word has N bits, the range of integers is between -2 N-1 and 2 N-1 - 1 If a word has N bits, the range of integers is between -2 N-1 and 2 N-1 - 1 short <= int <= long short <= int <= long Most of time, int should be used! Most of time, int should be used! Operator can be used are +, -, *, / and %. Operator can be used are +, -, *, / and %.

28 The Floating Point Type A data type for declaring floating point variables, i.e. for numbers that have a fractional part. A data type for declaring floating point variables, i.e. for numbers that have a fractional part. may be expressed with or without the e-notation: may be expressed with or without the e-notation: 123.4561.23456e+02 there are: float, double, and long double. there are: float, double, and long double. By default, floating point constants will be considered double. By default, floating point constants will be considered double.

29 A floating point constant must end with an f or F: 123.45f A floating point constant must end with an f or F: 123.45f A long double constant must end with an l or L: 123.45L A long double constant must end with an l or L: 123.45L Declaration of floating point type: Declaration of floating point type: float x; double y; long double z; The arithmetic operator used with the float type are: + - * / The arithmetic operator used with the float type are: + - * /

30 The relational operator used with the float type are >= != == The relational operator used with the float type are >= != == The logical operator used with the float type are: ! && || The logical operator used with the float type are: ! && ||

31 The Character Type char is a data type used to declare character variables. char is a data type used to declare character variables. The ASCII character set consists of 128 characters. The ASCII character set consists of 128 characters. Each element of the character set is associated with an integer (0 - 127) Each element of the character set is associated with an integer (0 - 127) Use a pair of single quotes to delimit a character. Use a pair of single quotes to delimit a character.

32 variables A variable is a name assigned to a data storage location. Your program uses variables to store various kinds of data during program execution. In C, a variable must be defined before it can be used. A variable is a name assigned to a data storage location. Your program uses variables to store various kinds of data during program execution. In C, a variable must be defined before it can be used. By using a variable's name in your program, you are, in effect, referring to the data stored there. By using a variable's name in your program, you are, in effect, referring to the data stored there. Variables names are a kind of identifiers. Variables names are a kind of identifiers.

33 Variables A variable is a placeholder for a value and has 5 attributes: A variable is a placeholder for a value and has 5 attributes: name or identifier, name or identifier, address, address, Type (size and operations) Type (size and operations) value value scope scope

34 Variable declaration Variables must always be defined before they are used. Variables must always be defined before they are used.defined datatype identifier; datatype identifier; For example For example int i; // defines a variable named as i of data type of integer. int i; // defines a variable named as i of data type of integer. float j; // defines a variable named as j of data type of float. float j; // defines a variable named as j of data type of float.

35 Descriptive variable name DO use variable names that are descriptive. For example, a program that calculates loan payments could store the value of the prime interest rate in a variable named interest_rate. DO use variable names that are descriptive. For example, a program that calculates loan payments could store the value of the prime interest rate in a variable named interest_rate. The variable name helps make its usage clear. You could also have created a variable named x or even my_carson; it doesn't matter to the C compiler. But bad names won’t help others to read your code. The variable name helps make its usage clear. You could also have created a variable named x or even my_carson; it doesn't matter to the C compiler. But bad names won’t help others to read your code. Though it might take a little more time to type descriptive variable names, the improvements in program clarity make it worthwhile. Though it might take a little more time to type descriptive variable names, the improvements in program clarity make it worthwhile.

36 Constant literals float k=0; float k=0; k=5+56.89; // 5 and 56.89 are called constant literals k=5+56.89; // 5 and 56.89 are called constant literals

37 #define preprocessor directives #define symbol value Will define a constant value. Will define a constant value. It means whenever the symbol appears anywhere in the program after #define is introduced, the specified value is substituted in place of the symbol. It means whenever the symbol appears anywhere in the program after #define is introduced, the specified value is substituted in place of the symbol. You only need to modify one place to change the value of the symbol. You only need to modify one place to change the value of the symbol.

38 Constants #defineSIZE100 int main( ) { int i; for (i=0; i< SIZE; i++) …}

39 Another way to define constant is to use const quantifier const int SIZE=10; const int SIZE=10;

40 Constants constint SIZE=100; // global scope int main( ) { int i; for (i=0; i< SIZE; i++) …}

41 Constants int main( ) { constint SIZE=100; // local scope to main int i; for (i=0; i< SIZE; i++) …}

42 Operations on data Operators Operators Built – in library functions Built – in library functions User –defined functions User –defined functions

43 A simple c program illustrate many big ideas. A simple c program illustrate many big ideas.

44 A simple program get input from keyboard, the do calculation, and output the result! #include #include int main() { int a, b, c; printf("Enter the first value:"); scanf("%d", &a); printf("Enter the second value:"); scanf("%d", &b); c = a + b; printf("%d + %d = %d\n", a, b, c); return 0; }

45 How this program works when you execute it

46 Comments A comment is text replaced during preprocessing by a single space character; the compiler therefore ignores all comments. A comment is text replaced during preprocessing by a single space character; the compiler therefore ignores all comments. There are two kinds of comments: There are two kinds of comments: The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This kind of comment is commonly called a C- style comment. The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This kind of comment is commonly called a C- style comment. The // (two slashes) characters followed by any sequence of characters. A new line not immediately preceded by a backslash terminates this form of comment. This kind of comment is commonly called a single-line comment or a C++ comment. The // (two slashes) characters followed by any sequence of characters. A new line not immediately preceded by a backslash terminates this form of comment. This kind of comment is commonly called a single-line comment or a C++ comment. A C++ comment can span more than one physical source line if it is joined into one logical source line with line-continuation (\) characters. A C++ comment can span more than one physical source line if it is joined into one logical source line with line-continuation (\) characters. You can put comments anywhere the language allows white space. You can put comments anywhere the language allows white space.

47 You cannot nest C-style comments inside other C-style comments. Each comment ends at the first occurrence of */. You cannot nest C-style comments inside other C-style comments. Each comment ends at the first occurrence of */. The /* or */ characters found in a character constant or string literal do not start or end comments. The /* or */ characters found in a character constant or string literal do not start or end comments. Such as “/*” or “*/”, they are not comments Such as “/*” or “*/”, they are not comments

48 You do not need to know at this level You can also include multibyte characters; to instruct the compiler to recognize multibyte characters in the source code, compile with the -qmbcs option. You can also include multibyte characters; to instruct the compiler to recognize multibyte characters in the source code, compile with the -qmbcs option. -qmbcs

49 END Lesson #2


Download ppt "Syntax of C Programming Language #Lesson 2 Sen Zhang."

Similar presentations


Ads by Google