0% found this document useful (0 votes)
35 views6 pages

CHAPTER 2 C Language

The document discusses the key lexical elements, operators, and components of the C programming language system. It defines characters, lexical elements, comments, keywords, identifiers, literal constants, operators, and punctuators. It also describes C syntax rules, precedence and associativity of operators, standard library functions, and provides examples of comments, random number generation, and power of two calculations in C code.

Uploaded by

Mawi Carlos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

CHAPTER 2 C Language

The document discusses the key lexical elements, operators, and components of the C programming language system. It defines characters, lexical elements, comments, keywords, identifiers, literal constants, operators, and punctuators. It also describes C syntax rules, precedence and associativity of operators, standard library functions, and provides examples of comments, random number generation, and power of two calculations in C code.

Uploaded by

Mawi Carlos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CHAPTER 2

LEXICAL ELEMENTS, OPERATORS, AND THE C SYSTEM

Objectives

• Identify and describe the C lexical elements


• Describe the C syntax rules
• Identify the symbols used for making comments in a C source code.
• Write correct one-line or variable length comments.
• Identify the C keywords and giving a brief overview of what they are for.
• Describe what identifiers are
• Tell the rules or guidelines for selecting a correct and relevant identifier
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Character and Lexical elements

- a character is a single valid symbol available in the standard set of characters from ASCII table.

- on page 703 of “A book on C”

-Lexical elements are formed by combination of characters into tokens or syntactic units:

-comments -Literal Constants

-keywords -Operators and Punctuators

-identifiers

SYNTAX RULES

• Better termed as Grammar of Grammatical Rules.


• The rules how syntactic units are defined and used
• Expressed using the Baccus-Naur Form (BNF)
• Used by / Guides the compiler for its translation of the source code into machine language.
Example:
Digit ::= 0|1|2|3|4|5|6|7|8|9
- complete rules is on P.685 of “A Book on C”

COMMENTS
-Arbitrary strings placed after symbol
// (Single-line comment)
Example: //This is an example
Or Between /* and */ (var-length comment)
Example: /* This is a comment
Using this sh*t */

KEYWORDS
-Words reserved for specific usage in C
Data Type Data Type Memory Class Control Structure, and Flow Makers/
Specifiers Modifiers Specifiers Special Operators Redirectors
char const auto if else
double enum ext ern switch case
float signed register for default
int struct static while do
long unsigned type def
break
short union
size of continue
signed volatile
go to
unsigned
run
void

IDENTIFIERS
-Strings used to have variables named constants, custom functions in the program.
-Should be chosen to aid easier understanding of source codes.
Examples of valid Identifiers:
x AN_IDENTIFIER
_id IsValid()
Counter1to61 This_is_an_identifier

LITERAL CONSTANTS
-combination of characters that are considered as literal values

Numbers
- Integers, Real numbers
Example: 2019, 54321.23
Character
Example: ‘A’ , ‘?’ , ‘ ‘ , ‘’
Strings
Example: “235”, “This is a string”, “/”Think Different”/”

Operators
Arithmetic operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division – remainder

(Algebraic) Unary Operators


+ Positive y = +x is same as y=x
- Negative y = -x
++ Increment by 1 x++ increment upon next value
-- Decrement by 1

Assignment Operators
= (Simple) assignment {x + 3} is {x = x + 3}
+= Addition, assignment {x+=3} is x=x+3
-= Subtraction, assignment {x-=3} is {x=x-3}
*= Multiplication, assignment {x*=3} is {x=x*3}
/= Division, assignment {x/=3} is {x=x/3}
%= Modular Division, assignment {x%=3} is {x=x%3}

*note: Bitwise assignment operators are discussed in Chapter 7

Comparison/Relational Operators
== Equality 5==5 (result to 1)
< less than 5<5 (result to 0)
> Greater than 5>4 (result to 1)
<= less than or equal 4<=5 (result to 0)
>= greater than or equal 4>=5 (result to 0)
!= Not equal 5!=5 (result to 0)
&& AND 1&&0 (result to 0)
|| OR 1||0 (result to 1)
! Not !0 (result to 1)

OPERATORS
Bitwise operator chapter 7
~ Tilde Unary Complement
& Ampersand AND
| Vertical Bar OR
^ Caret XOR
<< Double Less than sign Left Shift
>> Double Greater than sign Right Shift
>>= Bitwise right-shift assignment
<<= Bitwise left-shift assignment
&= Bitwise AND operation assignment
|= Bitwise OR operation, assignment
^= Bitwise XOR operation, assignment
Note: @exam, “anong klaseng operator ito?”
Special Operators
() Function call
[] Array Element Reference
- Direct member accessor
-> Indirect member accessor
?: Ternary member accessor
, Comma operator
Note: Might be in exam /

Precedence and Associativity of Operators


Operators Order of Evaluation
() ++ (post fix) -- (prefix) left to right
+(unary) – (unary) ++(prefix) – (prefix) Right to left
*/% left to right
+- left to right
= += -= *= /= %= Right to left

Punctuators

Symbol Use
() Grouping
{} Codeblock, (Array initial elements maker)
[] Array size marker
<> Standard header file indicator
; Statement terminator
// , /* */ Comment marker
# Preprocessor directive marker
‘ Variable or values separator
‘’ Character constants marker
“” String constant marker
% Output format marker
\ ……..Yen Escape character marker
: Label marker
More Lexical elements
-Expressions (Chapter 3)
-Clauses (Chapter 4)
-Function Headers (Chapter 5)
-Statements
-Preprocessor directives
-Declarations (chapter 3)
-variables (Chapter 3)
-functions (Chapter 5)
-Assignments (Chapter 3)
-Function calls (Chapter 5)
-Combinations

Labels
Example 2a: powers of 2
-a program that displays 6-digit power-of-two values starting from 2^0 to 2^10

//comments here
#include <stdio.h>
main () {
int i = 0
int power =1;
while (++i <=10)
printf(“%6d”, power*=2);
}

THE C SYSTEM
Language
-The grammar rules integrated with the compiler
Preprocessor
-Invoked when # is used in the preamble
-communicates with OS for preparing pre-compiler operations
Compiler
-converts source codes into machine instructions
Standard Library
-Pre-compiled functions saved in locations known by preprocessor
Header Files
Assert.h, ctype.h, errno.h, float.h, limits.h, math.h, setjmp.h, signal.h, stdarg.h,
stddef.h, stdio.h, stdlib.h

Example 2b: RANDOM VALUES


- a program that asks the user how many random values he/she llikes to be
generated and displayed. It accepts the value by the use through the keyboard and displays the
random values with at most 10 of them displayed in regular spacing per line.
//req. comments
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Main () {
Int I, n ;
Char* prompt = “ How many randon values do you like? “ ;
printf(prompt);
scanff(“%d”, &n );
ssrand(time(NULL));
for ( i = 0 ; I < n; ++i) {
if (I % 10 == 0) putchar(‘\n’);
printf(“%8d”, rand () ) ;
}
g}

You might also like