4. 32 KEYWORDS
• auto
• break
• case
• char
• const
• continue
• default
• do
• double
• else
• enum
• extern
• float
• for
• goto
• if
• int
• long
• register
• return
• short
• signed
• sizeof
• static
• struct
• switch
• typedef
• union
• unsigned
• void
• volatile
• while
5. LITERALS
• values that identifiers can hold.
• Numeric literals – accepts numeric values
• No comma
• No space between unary sign and the digits
• Must begin and end with a digit
6. • Non-numeric literals - may be a character or sequence of characters
• Example:
‘a’
‘+’
‘B’
“De La Salle University”
“BCS”
7. IDENTIFIERS
• are the names that are used to reference variables, function labels and various
user-defined objects.
8. RULES FOR NAMING VALID IDENTIFIERS
1. An identifier in Turbo C can vary from 1 to 32 characters.
2. The first letter must be a letter or an underscore (_), followed optionally by sequence of letters, digits
and/or underscore.
9. RULES FOR NAMING VALID IDENTIFIERS
3. Turbo C also allows the $ to be used in an identifier name, but this is non-standard so it’s use is not
recommended.
4. An identifier should not include embedded blanks.
5. You cannot use any of the Turbo C keyword as your variable or identifier name.
6. You should not call your variable by the same name as other functions.
10. EXERCISES
Identify the if the identifier is valid or invalid.
1. _
2. a$
3. Hello_World
4. _1
5. A
6. main
7. scanf
8. num1
9. tot sales
10. x-1
11. lname
12. Void
Invalid: 2,3,6,7,9,10
Valid: 1,4,5,8,11,12
11. integer1 45
integer2 72
sum 117
VARIABLES
• are identifiers in C where we want to store values (data).
Variables are important since they contain the values need for
manipulation and evaluation. Variable names are stored in the
computer’s memory.
12. TYPE BITWIDTH RANGE
char 8 0 to 255
int 16 -32768 to 32767
float 32 3.4E-32 to 3.4 E+38
double 64 1.7E-308 to 1.7 E+308
void 0 valueless
DATA TYPES
type of data that a variable can hold
13. TYPE EXAMPLES
char ‘A’ ‘b’ ‘$’ ‘9’ ‘ab’‘10’
int 1 250 4500
float 3.5 42.56 345.6789
double 3.5647290… 486.145875...
void valueless
MORE ON DATA TYPES
14. TYPE MODIFIERS
is used to alter the meaning of the base type to fit the needs of various situations more precisely.
The four type modifiers in C are:
signed unsigned
long short
Note:
Type modifiers can be applied to char and int except long which can also be applied to double
15. TYPE BITWIDTH RANGE
long int 32 -2147483648 to 2147483647
short int 16 -32768 to 32767
signed 32 -2147483648 to 2147483647
long int
unsigned 32 0 to 4294967295
long int
SOME COMBINATIONS OF C’S BASIC
DATA TYPES AND MODIFIERS
16. DECLARATION OF VARIABLES
• All variables must be declared before they are used.
• Syntax:
type variable_list;
• where:
type is a valid data type
variable_list is 1 or more identifier names with comma separator
17. Local variables are variables that are declared inside a function.
They are also called automatic variables. Local variables can
only be referenced by statements that are inside the block in
which the variables are declared.
Example:
#include <stdio.h>
main ()
{
int a,b,c;
}
Function block
LOCAL VARIABLES
18. Example:
#include <stdio.h>
int a,b,c;
main ()
{
}
GLOBAL VARIABLES
• variables are known throughout the entire program and may be used by any
piece of code. Also, they will hold their values during the entire execution of the
program. Global variables are created by declaring them outside any function.
20. Example:
#include <stdio.h>
main ()
{
}
test_function (int a, float b);
{
}
FORMAL PARAMETERS
• behave like local variables in a function. Their
declaration occurs inside parentheses that follow the
function name.
21. CONSTANTS
• Constants refer to fixed values that may not be
altered by the program.
• Turbo C supports one other type of constant in
addition to those of the pre-defined data types. This
is known as the string. All string constants are
enclosed in double quote (“”).
• Example: #define TEXT “Hello World”
•
22. DECLARATION OF CONSTANT
• are identifiers that can store a value that cannot be changed during program
execution.
const type iden_name = value;
where:
type is a valid data type
iden_name is a valid identifier
value is a constant value of the identifier
23. ASSIGNMENT STATEMENT
Recall the assignment statement in flowcharting
The general form of an assignment statement is
var_name = expression
where:
var_name should be a variable, not a function or
constant.
expression may be a single constant or a
complex combination of variables, operators and
constants.
Number = 5
24. Examples:
char ch = ‘a’;
int first = 0;
float num = 1.5;
type var_name = constant
You can give variables a value at the time they are declared
by placing an equal sign (=) and a constant after the variable
name. This is called initialization and it’s general form is:
semicolon
VARIABLE INITIALIZATION
25. • Global variables are initialized at the start of
the program
• Local variables are initialized each time the
block in which they are declared is entered
• All global and local variables are initialized to
zero (0) if no other initialization is specified.
REMINDERS IN INITIALIZATION
26. Variables of type const may not be changed
during execution of the program. Variables
of this type get value from initialization or by
some hardware-dependent means.
The modifier volatile is used to tell the
compiler that a variable’s value can be
changed in ways not explicitly specified by
the program.
ACCESS MODIFIERS
27. REVIEW EXERCISES
1. Variables of type ___________are used to hold
integer quantities.
2. Values of type character are used to hold
________characters or any 8-bit quantity.
3. __________in C are reserved words that have
special meaning.
4. Values of type ________ and ________ are used
to hold real numbers.
5. Real numbers have both an ________ and a
fractional component.
int
ASCII
Keywords
float double
integer
28. 6. Identifiers are composed of ________, ________, and underscore.
7. Variables that are declared inside a function are called
______________.
8. _________ are identifiers that can store a value that cannot be
changed.
letters
digits
local variables
Constants
29. OPERATORS
• Symbol that tells the compiler to perform specific mathematical or logical manipulations.
• Classification
• arithmetic operators
• relational operators
• logical operators
30. A. ARITHMETIC OPERATORS
- subtraction, unary minus
+addition
*multiplication
/ division
% modulus division
-- decrement
++ incrementNote:
• When / is applied to an integer, any remainder is truncated
• % cannot be used on type float or double
31. B. RELATIONAL & LOGICAL OPERATORS
• Relational Operators shows the relationship values have with one another.
• Logical operators show the ways these relationships can be connected together using rules of formal
logic.
36. EVALUATE THE FOLLOWING:
1. Given: z = 5; a = 3, b = 9, w = 2, y = -5
Expression:
z – a * b / 2 + w * y
2. Given: a = 5, b = 2, y = 3, c = 4, x = 1
Expression:
(a * b + 2) * -y / ( c + x )
-18
7
37. 3. Given: dei = 0; y = 4.0; z = 2.0; x = 3.0
Expression:
!dei || ( y + z >= x – z )
4. Given: x = 3; y = 2; j = 5; k = 3
Expression:
(x-y) <= (j-k ==3)
1
0