welcome to cpsc 206
DESCRIPTION
Welcome to CPSC 206. Structured Programming in C. Lecture Information. http://people.cs.tamu.edu/ychen/Teaching/CPSC206. 0. Introduction to Computer Science 1. Overview of CCh 1, 2. Lecture Topics:. Features of C:. 2. Flow of control and functionsCh 3, 4 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/1.jpg)
1
Structured Programming in CStructured Programming in C
Welcome to CPSC 206Welcome to CPSC 206
![Page 2: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/2.jpg)
2
Lecture Information
http://people.cs.tamu.edu/ychen/Teaching/CPSC206
![Page 3: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/3.jpg)
3
Lecture Topics:
0. Introduction to Computer Science1. Overview of C Ch 1, 2
2. Flow of control and functions Ch 3, 43. Character processing & fundamental data types Ch 5, 64. File I/O Ch 135. Pointers, Arrays, and Strings Ch 8, 9, 106. Structures, and linked lists Ch 12
Features of C:Features of C:
7. Enumeration type and storage classes Ch 7, 88. Recursion Ch 11
![Page 4: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/4.jpg)
4
Review of Class on Sept. 14, Tuesday
![Page 5: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/5.jpg)
5
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
![Page 6: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/6.jpg)
6
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
The general form of a programThe general form of a programpreprocessing directivesint main (){
declarations statements
}
![Page 7: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/7.jpg)
7
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
DeclarationsDeclarationsPurpose: Tell the compiler what kind of data can be stored in each of the variables.
The compiler can set aside the appropriate amount of memory to hold the data.
Format:Data_Type Variable1, Variable2;
![Page 8: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/8.jpg)
8
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
VariablesVariablesA variable name can be a sequence of letters, digits, and underscores.A variable name may not begin with a digit.Certain keywords, also called reserved words, cannot be used as names of variables.
![Page 9: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/9.jpg)
9
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
ExpressionsExpressionsHow to use expressions?
on the right side of assignmentArguments to functions
How to construct an expression?ConstantsThe name of a variableMeaningful combinations of operators with variables and constants
![Page 10: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/10.jpg)
10
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
Integer:Integer:Division a/b: an integer expression divided by another integer expression yields an integer value.
Any fractional part is discarded.Modulus a%b: the remainder after a is divided by b.If a or b is negative, the results of division and modulus are system-dependentIn a/b and a%b, the value of b cannot be zero.
![Page 11: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/11.jpg)
11
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
Character:Character:Constants are written within single quotesExample:
‘A’ ‘1’
![Page 12: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/12.jpg)
12
An Overview of C— Chapter 1
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and AssignmentsInitialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
A Depth Program: Algorithm and codeDissection of the depth program
Declarationmain Functionprintf function
Basics of C ProgrammingGeneral form of a program
DeclarationVariableExpressionData Type and Operation
Floating types:Floating types:float: an F suffix
1.22Fdouble: unsuffixed floating constant
1.22long double: an L suffix
1.22L
![Page 13: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/13.jpg)
13
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
InitializationInitializationA variable can be initialized when it is declared.Constants or constant expressions can be used to initialize a variable.Declared variables can be used to initialize a variable.
a variable cannot be used before it has been declared.
![Page 14: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/14.jpg)
14
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
Preprocessing directives:Preprocessing directives:
#include “filename”The preprocessor replaces the line with a copy of the named file.
![Page 15: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/15.jpg)
15
An Overview of C— Outline A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
Preprocessing directives:Preprocessing directives:
#define A BIt affects only those lines in the file that come after it.All occurrences (after this line) of the identifier A, except in quoted string, will be changed to B.
![Page 16: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/16.jpg)
16
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
Print and Read formatted dataPrint and Read formatted data
Arguments: control_string and other_arguments
control_string contains formats, called conversion specifications, which are matched with other arguments.
![Page 17: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/17.jpg)
17
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
Print and Read formatted dataPrint and Read formatted data
How to specify format using conversion specification?
%field_widthconversion_character%field_width.precisionconversion_characterconversion_character: how the data is
printed?o c: as a charactero d: as a decimal integero f: as a floating-point number
![Page 18: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/18.jpg)
18
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
Print and Read formatted dataPrint and Read formatted data
Example: scanf(“%d”, &x); &: address operator%d — how to interprete the input stream
the input characters typed at the keyboard is interpreted as a decimal integer
&x — where to store the valuethe value of the decimal integer is stored at the address of x.
![Page 19: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/19.jpg)
19
An Overview of C— Outline
A brief history of C Features of C Get Ready to Program A First Program The Basics of C Programming
Variables, Expressions, and Assignments Initialization Preprocessing directives: #define and #include Print and Read formatted data: printf() and scanf() The while statement
Problem Solving: Computing Sums Style, Programming Errors and System Considerations.
The while statementThe while statementwhile (expression)
statementstatement is executed as long as the value of expression is true
![Page 20: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/20.jpg)
20
Review of Class on Sept. 14, Tuesday
End
![Page 21: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/21.jpg)
21
Chapter 2:
Lexical Elements, Operators, and the C System
![Page 22: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/22.jpg)
22
Introduction
C is a language.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
C has an alphabet
C has rules for putting together words and punctuation to make legal programs.
![Page 23: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/23.jpg)
23
Introduction
C is a language. C has an alphabet
Lowercase letters: a b c …… zUppercase letters: A B C …… ZDigits: 0 1 2 3 4 5 6 7 8 9 Other characters:
o + - * / = ( ) { } [ ] < > ‘ “ ! @ # $ % & _ | ^ ~ \ . , ; : ?
White space characters: o blank, newline, tab, etc.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 24: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/24.jpg)
24
Introduction
C is a language. C has rules for putting together words and
punctuation to make legal programs. These rules are called syntax
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 25: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/25.jpg)
25
Introduction
C is a language Alphabet syntax
What is C program?A C program is a sequence of characters
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
How a computer understands this sequence of characters?
![Page 26: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/26.jpg)
26
Introduction
Compiler
C code Preprocessor
object code
errors
Loader
Machine code
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
How does a compiler know whether a C program is correct or not?
![Page 27: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/27.jpg)
27
Introduction
How to check a C program is correct? Compiler
The program that checks the legality of a program.
How compiler checks the legality of a program?The characters are collected by the
compiler into syntactic units called tokensThe compiler checks whether the tokens
can be formed into legal strings according to the syntax of C language.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 28: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/28.jpg)
28
Introduction
In C language, there are six kinds of tokens:
Keywords Identifiers Constants String constants Operators Punctuators
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 29: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/29.jpg)
29
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 30: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/30.jpg)
30
An Example — Characters and Lexical Elements
/* Read in two scores and print their sum. */
#include <stdio.h>
int main(void){ int score_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum.cComment: be replaced with a single blank
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 31: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/31.jpg)
31
An Example — Characters and Lexical Elements
#include <stdio.h>
int main(void){ int score_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum.c
File stdio.h is included
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 32: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/32.jpg)
32
An Example — Characters and Lexical Elements
int main(void){ int score_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum.c
A Copy of stdio.h
Keyword: int void
Identifier: mainscore_1score_2sum
Operator: Parentheses ( )
Punctuators: ; ,{
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 33: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/33.jpg)
33
An Example — Character and Lexical Elements
How the compiler distinguishs tokens? white space
For example: int score_1s
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
int main(void){ int score_1, score_2, sum;
What if we write as follows?intscore_1, score_2, sum;
The compiler considers intscore_1 as a single token.
![Page 34: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/34.jpg)
34
An Example — Characters and Lexical Elements
/* Read in two scores and print their sum. */
#include <stdio.h>
int main(void){ intscore_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum1.c
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
% gcc sum1.csum1.c: In function `main':sum1.c:8: error: `intscore_1' undeclared (first use in this function)sum1.c:8: error: (Each undeclared identifier is reported only oncesum1.c:8: error: for each function it appears in.)sum1.c:8: error: `score_2' undeclared (first use in this function)sum1.c:8: error: `sum' undeclared (first use in this function)sum1.c:11: error: `score_1' undeclared (first use in this function)
![Page 35: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/35.jpg)
35
An Example — Characters and Lexical Elements
int main(void){ int score_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum.c
A Copy of stdio.h
Identifier: printfscanf
Operator: ( )&
Punctuators: ,;
String ConstantA series of characters enclosed in “ ”
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 36: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/36.jpg)
36
An Example — Characters and Lexical Elements
int main(void){ int score_1, score_2, sum;
printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0;}
sum.c
A Copy of stdio.h
Identifier: sumscore_1score_2
Operator: + =
Punctuators: ;
Constant0
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 37: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/37.jpg)
37
An Example — Characters and lexical Elements
Summary Tokens:
Keyword: int voidIdentifier: printf scanf sumOperator: ( ) &Punctuators: ;}Constant: 0String Constant: “Input two scores as
integers: “ Separate two tokens: white space
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 38: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/38.jpg)
38
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 39: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/39.jpg)
39
Comments
What is comment? Arbitrary strings of symbols placed between
the delimiters /* and */. Single line comment: // text
How compiler processes comments? The compiler changes each comment into a
single blank character.Why comment?
Comments are used by the programmer as a documentation aid.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 40: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/40.jpg)
40
Comments
Rule1: Where can we place comments? Anywhere?
NoCompiler replaces each comment by a
single blank character o Comments can be placed wherever a single
blank character can appear. Example
x = 10+/* add the numbers */5;
x = 10+ 5;
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 41: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/41.jpg)
41
Comments
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
#include <stdio.h>
int main(void){ int/* a comment*/x;
x=10+5; printf("x=%d\n",x);
return 0;}
comment.c
int x;
% gcc comment.c% a.outx=15
Lexical Lexical ElementsElements
![Page 42: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/42.jpg)
42
Comments
Question: Can a comment appear in the middle of a keyword or identifier? Compiler will insert a single blank
character into a keyword or identifier Since a blank is used to separate two
tokens, the keyword or identifier will be considered as two tokens.
No
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 43: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/43.jpg)
43
Comments
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
#include <stdio.h>
int main(void){ int x, x/* a comment */y; return 0;}
comment1.c
% gcc comment1.ccomment1.c: In function `main':comment1.c:6: error: redeclaration of `x'comment1.c:6: error: `x' previously declared herecomment1.c:6: error: parse error before "y"
int x, x y;
Lexical Lexical ElementsElements
![Page 44: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/44.jpg)
44
Comments
Rule2: nested comments Multi-Line comments may not be nested.
Example:/* outer comment /* inner comment */ */
A single-line (//) comment can be nested within a multi-line comment.Example:
/* a //test of nested comments. */
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 45: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/45.jpg)
45
CommentsAn understanding of Rule2:Usually when a compiler encounters a pair /*,
it considers text between this /* and the first occurrence of */ after this /* as a comment;
this comment is replaced by a blank character.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
/* outer comment /* inner comment */ */
/* a //test of nested comments */
Lexical Lexical ElementsElements
*/
![Page 46: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/46.jpg)
46
Comments
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
#include <stdio.h>
int main(void){ int x=1; /* This is a comment /* */ return x;}
comment3.c#include <stdio.h>
int main(void){ int x; /* //*/ return 0;}
comment2.c
Lexical Lexical ElementsElements
![Page 47: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/47.jpg)
47
Comment
Summary What is comment?
Arbitrary strings of symbols placed between the delimiters /* and */.
Single line comment: // text The compiler changes each comment into a
single black character. Rules:
Multi-line comments cannot be placed in the middle of a keyword or identifier.
Multi-line comments may not be nested.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 48: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/48.jpg)
48
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 49: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/49.jpg)
49
Keywords
What is Keywords? Keywords are explicitly reserved words that
have a strict meaning as individual tokens in C.
Examples of Keywords Data Type: int, char, long, short
Compared to other major languages, C has only a small number of keywords.
C is small
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 50: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/50.jpg)
50
Keywords
Rules: Keywords cannot be redefined or used in
other contexts. Example:
keywords cannot be used as variable names.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 51: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/51.jpg)
51
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 52: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/52.jpg)
52
Identifiers
What is identifier? The names of variables, functions, labels
and other user-defined items are called identifier.
Examplesint main(void)int score_1, score_2, sum;
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 53: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/53.jpg)
53
Identifiers
Special identifier Keywords can be thought of as identifiers
that are reserved to have special meaning in the C language.
The names of functions in standard library would not be redefined.
The identifier main is special — C programs always begin execution at the function called main.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 54: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/54.jpg)
54
Identifiers
Rules:1. An identifier is a token that is composed of
a sequence of letters, digits, and underscore _ .
2. A letter or underscore must be the first character of an identifier. Valid identifier: a, b, c Invalid identifier: a_...b, 1a
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 55: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/55.jpg)
55
Identifiers
Rules (contd.):3. Case-sensitive
Identifiers C and c are different4. Would not be defined as a keyword. It is not recommended to define a variable
name as the name of a function in the standard library and the identifier main. Example: define a variable name as
printf;5. Identifiers should be chosen to reflect their
use in the program.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 56: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/56.jpg)
56
Identifiers
Examples of invalide identifiers: Rule1:
o int a_...b; Rule2:
o int 1a; Rule3:
o int c;o C =0;
Rule4: o int char;o int printf;
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 57: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/57.jpg)
57
Identifiers
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
#include <stdio.h>int main(void){ int a_...b; int 1a; int c; int char; int printf; C=0; printf("A test of identifiers\n"); return 0;}
identifier.c
% gcc identifier.cidentifier.c: In function `main':identifier.c:4: error: syntax error before '...' tokenidentifier.c:5:13: invalid suffix "a" on integer constantidentifier.c:7: warning: useless keyword or type name in empty declarationidentifier.c:7: warning: empty declarationidentifier.c:9: error: `C' undeclared (first use in this function)identifier.c:9: error: (Each undeclared identifier is reported only onceidentifier.c:9: error: for each function it appears in.)identifier.c:10: error: called object is not a function
Lexical Lexical ElementsElements
![Page 58: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/58.jpg)
58
Identifiers
Summary What is identifier?
The names of variables, functions, labels and other user-defined items are called identifier.
Special identifier Keywords, names of functions in C library,
main Rules:
1. composed of letters, digits, and underscore _ .2. The first character must be a letter or
underscore.3. case-sensitive4. would not be defined as the special identifiers.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 59: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/59.jpg)
59
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 60: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/60.jpg)
60
Constants
What is a constant? Constants refer to fixed values that the
program may not alter. Examples:
Integer constants: 0, 17 Floating constants: float, double, long double
1.0, 3.14 Character constants: (enclosed between single
quotes)‘a’, ‘A’‘\n’ : newline
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 61: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/61.jpg)
61
Constants
Integer constants Decimal integer Octal integer Hexadecimal integer
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 62: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/62.jpg)
62
Constants
Octal Integer: What is the decimal value of an octal integer?
An octal integer a= ik-1 ik-2 …..i0o The decimal value is
ik-1* 8k-1 + ik-2 * 8k-2 + …..+ i0 * 80
Example:o The decimal value of an octal integer
a = i3i2i1i0=7121 is 7 * 83 + 1 * 82 + 2* 81 + 1 * 80 =3665
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 63: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/63.jpg)
63
Constants
Hexadecimal Integer: What is the decimal value of a hexadecimal
integer? A hexadecimal integer a= ik-1 ik-2 …..i0
o The decimal value is ik-1* 16k-1 + ik-2 * 16k-2 + …..+ i0 * 160
Example:o The decimal value of a hexadecimal integer
a = i3i2i1i0=7121 is 7 * 163 + 1 * 162 + 2* 161 + 1 * 160 =28961
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 64: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/64.jpg)
64
Constants
Representation of Integer constants Decimal integer: 17 Octal integer:017 Hexadecimal integer: 0x17
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 65: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/65.jpg)
65
Constants
Integer constants
#include <stdio.h>int main(void){ printf("17=%d 017=%d 0x17=%d\n", 17, 017, 0x17); return 0;}
constant.c
% gcc constant.c% a.out17=17 017=15 0x17=23
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 66: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/66.jpg)
66
Constants
An integer may be too large to be stored in a machine word. 123456798900 may be too large
The maximum integer is defined in head file <limits.h> INT_MAX
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 67: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/67.jpg)
67
Constants#include <limits.h>#include <stdio.h>int main(void){ printf("123456789000=%d\n", 12345678900); printf("The maximum integer is %d \n", INT_MAX); return 0;} % gcc constant1.c
constant1.c: In function `main':constant1.c:5: warning: integer constant is too large for "long" type% a.out123456789000=2The maximum integer is 2147483647
constant1.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 68: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/68.jpg)
68
Constants
Summary Integer constants: 0, 17
Decimal integer: 17Octal integer: 017Hexadecimal integer: 0x17An integer may be too large to be stored
in a machine word. Floating constants: 1.0, 3.14 Character constants: (enclosed between single
quotes)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 69: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/69.jpg)
69
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 70: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/70.jpg)
70
String Constants
What is string constant? A sequence of characters enclosed in a pair
of double quote marks.
Example: “a string of text” “” // the null string “ ” // a string of a blank character
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 71: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/71.jpg)
71
String Constants
String constants are stored by the compiler as arrays of characters, ended by the null character ‘\0’. “Hello” | H | e | l | l | o | \0 |
String constants are differently form character constants. “a” ≠‘a’
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 72: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/72.jpg)
72
String Constants
#include <stdio.h>int main(void){ char c; c = "a string"; return 0;}
% gcc string_char.cstring_char.c: In function `main':string_char.c:5: warning: assignment makes integer from pointer without a cast
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 73: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/73.jpg)
73
String Constants
Special characters in string constants Double quote “
\”Example: “a string with double quotes \””
Backslash \\\Example: “a string with backslash \\”
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 74: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/74.jpg)
74
String Constants
#include <stdio.h>int main(void){ printf("%s\n", "a string with double quotes \""); printf("%s\n", "a string with backslash \\"); return 0;}
% gcc string.c% a.outa string with double quotes "a string with backslash \
string.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 75: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/75.jpg)
75
String Constants
Rules: You mustn't split a string constant across
lines (the beginning and ending double quotes must be on the same line).
Two string constants that are separated only by white space are concatenated by the compiler into a single string.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 76: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/76.jpg)
76
String Constants
#include <stdio.h>int main(void){ printf("%s\n", "a string with double quotes \""); return 0;}
string1.c
% gcc string1.cstring1.c:4:24: missing terminating " characterstring1.c: In function `main':string1.c:5: error: parse error before "double"string1.c:5: error: stray '\' in programYou mustn't split a string constant across lines (the
beginning and ending double quotes must be on the same line).
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 77: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/77.jpg)
77
String Constants
Two string constants that are separated only by white space are concatenated by the compiler not a single string.
#include <stdio.h>int main(void){ printf("%s\n", "abc" "def"); return 0;}
string2.c
gcc string2.c% a.outabcdef
What is the output?1. abc2. abc def3. abcdef
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 78: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/78.jpg)
78
String Constants
Summary String constant is a sequence of characters
enclosed in a pair of double quote marks. String constants are differently form
character constants. Special characters: \”, \\ You mustn't split a string constant across
lines Two string constants that are separated
only by white space are concatenated by the compiler into a single string.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 79: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/79.jpg)
79
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 80: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/80.jpg)
80
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 81: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/81.jpg)
81
Operators and Punctuators— Examples of Operators and Punctuators
Examples of Arithmetic Operators + addition - subtraction * multiplication / division % modulus
Operators can be used to separate identifiers. a+b
Some symbols have meanings that depend on context
printf(“%d”, b%7);
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 82: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/82.jpg)
82
Operators and Punctuators— Examples of Operators and Punctuators
Examples of punctuators Parentheses () Braces {} Commas , Semicolons ;
Punctuators also serve to separate language elements.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 83: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/83.jpg)
83
Operators and Punctuators— Examples of Operators and Punctuators
Summary Examples of Operators and Punctuators. They are collected by the compiler as
tokens and along with white space, they sever to separate language elements.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 84: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/84.jpg)
84
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 85: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/85.jpg)
85
Operators and Punctuators— Precedence and Associativity of Operators
Why Precedence and Associativity? Computers have to be told very precisely
what to do. Precedence and Associativity determine
precisely how expressions are evaluated.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 86: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/86.jpg)
86
Operators and Punctuators— Precedence and Associativity of Operators
What is Precedence? A characteristic of operators that indicates
when they will be evaluatedwhen they will be evaluated when they appear in complex expressions.
Operators with high precedence are evaluated before operators with low precedence. Example: 2+3*4 = 14, not 20
You can override precedence rules by using parenthesesparentheses. Example: (2+3)*4 would evaluate to 20.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 87: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/87.jpg)
87
Operators and Punctuators— Precedence and Associativity of Operators
What is Associativity? Operators associate with either the expression
on their left or the expression on their right; this is called "associativity."
Associativity rule “left to right”Operations are performed from left to rightExample: +,-,/,%
Associativity rule “right to left”Operations are performed from right to left++(prefix), --(prefix)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 88: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/88.jpg)
88
Operators and Punctuators— Precedence and Associativity of Operators
Example: 1+2-3+4-5 Precedence and Associativity Rules:
Precedence: o + and – have the same precedence
Associativity rule: o “left to right”
(((1+2)-3)+4)-5
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 89: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/89.jpg)
89
Operators and Punctuators— Precedence and Associativity of Operators
Summary Precedence and Associativity determine
precisely how expressions are evaluated. Precedence of operators indicates when they when they
will be evaluatedwill be evaluated. Associativity
“left to right”: Operations are performed from left to right
o Examples: +,-,/,%“right to left”: Operations are performed from right
to lefto Examples: ++(prefix), --(prefix)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 90: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/90.jpg)
90
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 91: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/91.jpg)
91
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 92: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/92.jpg)
92
Operators and Punctuators — Increment ++ and Decrement Operators --
Increment ++i, i++ Each causes the stored value of i in memory
to be incremented by 1. Each of the expressions has a value.
Example: o a = ++i;
The value of expression ++i is assigned to variable a.
o a = i++; The value of expression i++ is assigned to
variable a.
What is the value of each expression?
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 93: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/93.jpg)
93
Operators and Punctuators — Increment ++ and Decrement Operators --
The value of expressions ++i and i++ ++i
the stored value of i is incremented firstthe expression takes as its value the new
stored value of i i++
the expression takes as its value the current stored value of i
the stored value of i is incremented
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 94: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/94.jpg)
94
Operators and Punctuators — Increment ++ and Decrement Operators --
#include <stdio.h>int main(void){ int i, j, a, b; i=0; j=0; a = ++i; b = j++; printf("a=%d, b=%d\n",a,b); return 0;}
% gcc id.c% a.outa=1, b=0
id.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 95: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/95.jpg)
95
Operators and Punctuators — Increment ++ and Decrement Operators --
++ and + ++
Cause the value of a variable in memory to be changed
+Does not change the value of a
variable.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 96: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/96.jpg)
96
Operators and Punctuators — Increment ++ and Decrement Operators --
Decrement Operator i-- and --i The value of i is decremented by 1. Each expression has a value.
--io the stored value of i is decremented by 1o the expression takes as its value the new
stored valued of ii--
o the expression takes as its value the current stored valued of i
o the stored value of i is decremented by 1
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 97: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/97.jpg)
97
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 98: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/98.jpg)
98
Operators and Punctuators — Increment ++ and Decrement Operators --
Precedence and Associativity
Associativity ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix) Right
to left * / % Left to right + - Left to right
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 99: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/99.jpg)
99
Operators and Punctuators — Increment ++ and Decrement Operators --
Precedence and Associativity +(unary) –(unary) ++(prefix) --(prefix) Right to left
#include <stdio.h>int main(void){ int a=2; int result; result = - --a; printf("a=2, - --a = %d\n",result); return 0;}
% gcc id2.c% a.outa=2, - --a = -1
id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
Question1: What is the value of a after the operation --a?
Question2: What is the value of the expression --a?
![Page 100: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/100.jpg)
100
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a * b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0;}
++ a * b – c --
++ (postfix) -- (postfix) Left to right+(unary) –(unary) ++(prefix) --(prefix) right to left* / % left to right+ - left to right
Precedence and Associativity
)()( )( ++ a * b – 3 2 * b – 3 4 – 3
1
)( )()(id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 101: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/101.jpg)
101
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0;}
7 - - b * ++ d
++ (postfix) -- (postfix) Left to right+(unary) –(unary) ++(prefix) --(prefix) right to left* / % left to right+ - left to right
Precedence and Associativity
7 - (-2) * 5 7 - (-10) 17
)( )(
)(id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 102: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/102.jpg)
102
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b* ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b* ++ d = %d\n",result2); return 0;}
% gcc id1.c% a.out++ a * b - c -- = 1 7 - - b* ++ d = 17
id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 103: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/103.jpg)
103
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 104: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/104.jpg)
104
Operators and Punctuators — Increment ++ and Decrement Operators --
Rules Applied to variables but not
to constants or ordinary expressions
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 105: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/105.jpg)
105
Operators and Punctuators— Increment ++ and Decrement Operators --
Examples:
#include <stdio.h>int main(void){ int a, result1, result2; a = 1; result1 = ++1; result2 = -- -a; return 0;}
% gcc id3.cid3.c: In function `main':id3.c:6: error: invalid lvalue in incrementid3.c:7: error: invalid lvalue in decrement
id3.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 106: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/106.jpg)
106
Operators and Punctuators— Increment ++ and Decrement Operators --
Summary ++
++i: the stored value of i is incremented; the expression takes as its value the new stored valued of i
i++: the expression takes as its value the current stored valued of i; the stored value of i is incremented by 1.
-- --i: the stored value of i is decremented by 1; the
expression takes as its value the new stored valued of i i--: the expression takes as its value the current stored
valued of i; the stored value of i is decremented by 1. Applied to variables but not to constants or
ordinary expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 107: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/107.jpg)
107
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 108: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/108.jpg)
108
Operators and Punctuators— Assignment Operators
Example: An assignment expression with = Format: variable = right_side
Two operands o variable o right_side: an expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 109: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/109.jpg)
109
Operators and Punctuators— Assignment Operators
Example: An assignment expression with = Format: variable = right_side Result:
The value of right_side is assigned to variable
Assignment expression has a value.o The value of right_side is the value of the
assignment expression.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 110: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/110.jpg)
110
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int a, b, c; int a1, b1, c1; b = 2; c = 3; a = b + c; printf("b = %d, c = %d, a = %d \n",b, c, a);
a1=(b1 = 2) + (c1 = 3); printf("b1 = %d, c1 = %d, a1 = %d \n",b1, c1, a1);
return 0;}
% gcc ass1.c% a.outb = 2, c = 3, a = 5b1 = 2, c1 = 3, a1 = 5
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 111: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/111.jpg)
111
Operators and Punctuators— Assignment Operators
Assignment operators = op=:
+=, -=, *=, / =, %=, >>=, <<=, &=, ^=, |=
Semantics: Variable op= expression
Variable = variable op (expression)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 112: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/112.jpg)
112
Operators and Punctuators— Assignment Operators
Assignment operators Precedence:
all the assignment operators have the same precedence
Lower than all the other operators which have been introduced (such as + - )
Associativity:right to left
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 113: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/113.jpg)
113
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int a, b, c; a = b = c = 0; printf("b = %d, c = %d, a = %d \n", b, c, a); return 0;} % gcc ass2.c
% a.outb = 0, c = 0, a = 0
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 114: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/114.jpg)
114
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int i=1, j=2, k=3, m=4; i += j + k; printf(" j = %d, k = %d, i += j+k = %d \n",j, k, i); printf(" m = %d, k = %d, ",m, k); j *= k = m + 5; printf("j *= k = m + 5 = %d \n",j); printf("k = %d \n",k); return 0;}
% gcc ass3.c% a.out j = 2, k = 3, i += j+k = 6 m = 4, k = 3, j *= k = m + 5 = 18 k = 9
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 115: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/115.jpg)
115
Operators and Punctuators— Assignment Operators
Summary Assignment operators
Precedence: they have the same precedence
o Lower than all the other operators which have been introduced (such as + - )
Associativity: right to left Variable op= expression
Variable = variable op (expression)The value of the expression is the value
of the expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 116: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/116.jpg)
116
Operators and Punctuators— Summary
Examples of Operators and PunctuatorsPrecedence and Associativity of Operators Increment and Decrement Operators
i++, i++ i--, --i
Assignment Operators Variable op= expression
Variable = variable op (expression)The value of the expression is the value of the
expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 117: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/117.jpg)
117
Lexical Elements
Summary Comments Keywords Identifiers Constants String Constants Operators and Punctuators
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 118: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/118.jpg)
118
Lexical Elements
Comment What is comment?
Arbitrary strings of symbols placed between the delimiters /* and */.
Single line comment: // text The compiler changes each comment into a
single black character. Rules:
Multi-line comments cannot be placed in the middle of a keyword or identifier.
Multi-line comments may not be nested.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 119: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/119.jpg)
119
Lexical Elements
Keywords What is Keywords?
Keywords are explicitly reserved words that have a strict meaning as individual tokens in C.
Examples of KeywordsData Type: int, char, long, short
Keywords cannot be redefined or used in other contexts.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 120: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/120.jpg)
120
Lexical Elements
Identifiers What is identifier?
The names of variables, functions, labels and other user-defined items are called identifier.
Special identifier Keywords, names of functions in C library,
main Rules:
1. composed of letters, digits, and underscore _ .2. The first character must be a letter or
underscore.3. case-sensitive4. would not be defined as the special identifiers:
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
![Page 121: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/121.jpg)
121
Lexical Element
Constants Integer constants: 0, 17
Decimal integer: 17Octal integer: 017Hexadecimal integer: 0x17An integer may be too large to be stored
in a machine word. Floating constants: 1.0, 3.14 Character constants: (enclosed between single
quotes)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 122: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/122.jpg)
122
Lexical Elements
String Constants String constant is a sequence of characters
enclosed in a pair of double quote marks. String constants are differently form
character constants. Special characters: \”, \\ You mustn't split a string constant across
lines Two string constants that are separated
only by white space are concatenated by the compiler not a single string.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 123: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/123.jpg)
123
Lexical Elements
Operators and Punctuators Precedence and Associativity of Operators Increment and Decrement Operators
i++, i++i--, --i
Assignment OperatorsVariable op= expression
Variable = variable op (expression)o The value of the expression is the value of the
expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 124: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/124.jpg)
124
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 125: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/125.jpg)
125
An Example: Computing Powers of 2— Outline
Program pow_of_2 Dissection of the pow_of_2 Program
CommentsKeywordsIdentifiersConstantsString ConstantsOperators and Punctuators
![Page 126: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/126.jpg)
126
An Example: Computing Powers of 2— power_of_2.c
/* Some powers of 2 are printed. */
#include <stdio.h>
int main(void){ int exponent = 0, power_of_two = 1;
while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0;}
pow_of_2.c
CommentsKeywordsIdentifiersConstantsString ConstantsOperators and Punctuators
How many times the body of the loop is executed?
power_of_two = power_of_two * 2
![Page 127: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/127.jpg)
127
An Example: Computing Powers of 2— power_of_2.c
/* Some powers of 2 are printed. */
#include <stdio.h>
int main(void){ int exponent = 0, power_of_two = 1;
while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0;}
% gcc pow_of_2.c% a.out 2 4 8 16 32 64 128 256 512 1024%
Purpose: prints on a line some powers of 2.
pow_of_2.c
![Page 128: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/128.jpg)
128
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
![Page 129: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/129.jpg)
129
The C System
C is a small language The core language is small Non-essential functionality, such as math functions
or file handling, is provided by a standardized set of library routines.The standard library contains many useful
functions that add considerable power and flexibility to the c System.
What is standard library?How to use a function in standard library?
![Page 130: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/130.jpg)
130
The C System— Outline
The Standard library What is standard library? How to use a function in standard library?
#include
An Example: A prn_rand Program
![Page 131: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/131.jpg)
131
The C System— The Standard Library
What is Standard library? The C standard library is a collection of
header files and library routines
used to implement common operations, such as input/output and string handling.
![Page 132: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/132.jpg)
132
The C System— The Standard Library
What is Standard library? (Contd.) Header file:
The names and characteristics of functions are included into computer files called header file.
Library file: The actual implementation of functions
are separated into a library file.The library contains compiled code that is
unreadable to humans.
![Page 133: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/133.jpg)
133
The C System— The Standard Library
Examples of header files: <math.h>: For computing common
mathematical functions <stdio.h>: Provides the core input and
output capabilities of the C language. This file includes the venerable printf function.
<stdlib.h>: For performing a variety of operations, including conversion, pseudo-random numbers, memory allocation, process control, environment, signalling, searching, and sorting.
<string.h>: For manipulating several kinds of strings.
Check Appendix A for Details
![Page 134: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/134.jpg)
134
The C System— The Standard Library
How to use a function in the standard library? The programmer needs to provide the
function prototype.Including appropriate header files.
Do we need to locate the function in the library file? No. The system knows where to find the code
that corresponds to functions from the standard library.
![Page 135: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/135.jpg)
135
The C System— The Standard Library
How to use a function in the standard library? Find the header file which contains the
prototype of the function. Use #include preprocessing directive to
include the appropriate file.
![Page 136: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/136.jpg)
136
The C System— The Preprocessor: #include
#include “filename” A search for this file is made first in the
current directory and then in other system-dependent places
#include <filename> The preprocessor looks for the file only in
the system-dependent places and not in the current directory.
![Page 137: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/137.jpg)
137
The C System— Outline
The Standard library What is standard library? How to use a function in standard library?
#include
An Example: A prn_rand Program
![Page 138: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/138.jpg)
138
The C System— An example: prn_rand Program
Purpose of prn_rand program: Use rand() to generate some randomly
distributed integers.
Where is rand()? stdlib.h
![Page 139: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/139.jpg)
139
The C System— An example: prn_rand Program/*Printing random numbers. */#include <stdio.h>#include <stdlib.h>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%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0;}
Hearder files are included
The characters typed in are received and converted into the format of decimal integeter and placed at the address of n
i=0;while (i++<n){……}
![Page 140: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/140.jpg)
140
The C System— An example: prn_rand Program/*Printing random numbers. */#include <stdio.h>#include <stdlib.h>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%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0;}
% gcc prn_rand.c% a.out
Some randomly distributed integers will be printedHow many do you want to see? 17
16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089%
![Page 141: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/141.jpg)
141
The C System— Summary
The Standard library How to use a function in standard library?
#include
The Preprocessor and #includeAn Example: A prn_rand Program
![Page 142: Welcome to CPSC 206](https://reader036.vdocument.in/reader036/viewer/2022062305/5681500d550346895dbdee36/html5/thumbnails/142.jpg)
142
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System