CCPROG1
LOGIC FORMULATION AND
INTRODUCTORY
PROGRAMMING
Variable, Operators,
Expressions, and the
Assignment Statement
Introduction
• C is a general-purpose procedural
programming language
• Used to create operating systems like
UNIX and Windows as well as other
popular software applications
• The words in C are called tokens and the
grammar is called syntax
Tokens
Brian W. Kernighan and Dennis M. Ritchie. (1988). The C Programming Language (2nd. ed.). Prentice-Hall Professional
Technical Reference.
Constants
• Numeric Constants / Literals
• Character Constants / Literals
abcdefghi
01234567
Numeric Constants
• Integers (whole numbers)
• Examples:
• 1
• -5
• 12345
• Floating-point numbers (real numbers)
• Examples:
• 2.55
• -1234.0
• 2.5e3 (is the same as 2.5 x 103)
Numeric Constants
• In defining numeric constants, the following
rules should be followed:
• No comma.
• No space between the unary sign (+ or -) and
the digits.
• Must begin and end with a digit.
Character Constants
• is a single letter or character that is
enclosed in single quotes(‘)
• Examples:
• ‘a’
• ‘B’
• ‘+’
• ‘2’
Character Constants
• Special Characters
Escape Character Meaning
'\n' newline
'\a' alert
'\t' tab
'\0' null character
'\’' singe quote
%% percent symbol
String Literals
• a series of characters.
• should be enclosed in double-quotes.
• Examples:
• “De La Salle University”
• “a string with double quotes \” within”
• “a single backslash \\ is in this string”
Identifiers
• are descriptive names that are given to the
variables, constants, and other entities in
our program
• can be used for variables and constants
Identifiers
• It must consist only of letters, digits, and
underscores.
• Examples: Invalid Examples:
• my_variable • this is my var
• input1 • tel#
• _MAX • money$
•x • input-number
Identifiers
• An identifier cannot begin with a digit.
• Examples: Invalid Examples:
• variable1 • 1stvariable
• seven11 • 7eleven
• people200 • 200people
Identifiers
• An identifier defined in the C standard
library should not be redefined, keywords
are also invalid identifiers.
• Examples: Invalid Examples:
• for_loop_var • for
• signed10 • signed
• bBreak • break
• if
Identifiers
• Identifiers are case sensitive; meaning
uppercase is not equal to the lowercase..
• Examples
• ans ≠ Ans ≠ aNs ≠ anS ≠ ANs ≠ ANS
Variables
• are entities that can store a value that can
be changed during the execution of a
program
Variables
• The variable declarations tell the C
compiler what type of data will be stored
in each variable and how that data will be
represented in memory
• Syntax:
<data type> <variable or variable list>;
Variables
Data Type Range
int -2147483648 to +2147483647
long -2147483648 to +2147483647
float 1.175494e-038 to 3.402823e+038
double 2.225074e-308 to 1.797693e+308
For Dev C++ using GCC 9.2.0 32/64bit
Hungarian Notation
Data Type Prefix Examples:
int n int nAge;
long l float fTotal;
float f double dAmount;
char cGender;
double d
char c
Charles Simonyi (Links to an external site.) (1999). "Hungarian Notation". MSDN Library. Microsoft Corp.
Variables
• a series of variables of the same type can
be declared at the same time
• the variables are separated by commas
• Examples:
int nNum1, x, nMaxVal;
float fValue, fAverage;
double dCapital, dProfit;
char cFirstInit, cLast, ch1;
Constants
• Constants are entities that can store a
value but cannot be changed.
• Usually, constant identifiers are written
using all capital letters with underscores
between each word.
• Syntax:
#define <constant identifier> <literal>
Constants
• #define is actually a preprocessor directive used to define
macros.
• A preprocessor directives is an instruction executed before
compilation
• A macro is used to give a name to a block of code or value.
• Example:
#define SCHOOL "De La Salle University"
#define PI 3.1416
#define MID_INITIAL ‘T’
#define MAX_STUDENTS 45
Operators
• is a symbol in C that is used to perform
certain operations on values or variables.
• Examples:
• * : multiplication
• / : division
• % : modulo
• + : addition
• - : subtraction
Addition
Rule Example
int + int = int 5+2=7
int + float = float 5 + 2.0 = 7.0
float+ int = float 5.0 + 2 = 7.0
float+ float = float 5.0 + 2.0 = 7.0
Subtraction
Rule Example
int - int = int 5-2=3
int - float = float 5 - 2.0 = 3.0
float - int = float 5.0 - 2 = 3.0
float - float = float 5.0 - 2.0 = 3.0
Multiplication
Rule Example
int * int = int 5 * 2 = 10
int * float = float 5 * 2.0 = 10.0
float * int = float 5.0 * 2 = 10.0
float * float = float 5.0 * 2.0 = 10.0
Division
Rule Example
int / int = int 5/2=2
int / float = float 5 / 2.0 = 2.5
float / int = float 5.0 / 2 = 2.5
float / float = float 5.0 / 2.0 = 2.5
Modulo
Rule Example Example
int % int = int 17 % 5 = 2 5%2=1
3%5=3 -5 % 2 = -1
1234 % 10 = 4 5 % -2 = 1
0%2=0 -5 % -2 = -1
Modulo
Arithmetic Expressions
Operator Description Precedence Associativity
() parenthesis highest left-to-right
unary + positive sign
high right-to-left
unary - negative sign
* multiplication
/ division low left-to-right
% remainder or modulo
+ addition
lowest left-to-right
- subtraction
Example
given z = 8, a = 3, b = 9, w = 2 and y = -5
z – ( a + b / 2 ) + w * -y
8 - ( 3 + 9 / 2 ) + 2 * --5
8 - ( 3 + 4 ) + 2 * --5
8 - 7 + 2 * --5
8 - 7 + 2 * 5
8 - 7 + 10
1 + 10
11
= 11
Converting Math Equations
Assignment Operator
• stores a value or a computational result in
a variable
• Syntax:
<variable> = <expression>;
Assignment Operator
• Examples:
int x;
float fValue;
char ch1, cLast;
x = 1; /* x is assigned a numeric literal*/
fValue = PI; /* fValue is assigned the value of a constant*/
cLast = 'z’; /* cLast is assigned the character constant 'z'*/
ch1 = cLast; /* ch1 gets the value of the variable cLast,
as such both will stor ethe value 'z' */
Variable Initialization
• declare and initialize the variable
• Syntax:
<datatype> <variable> = <expression>;
• Example:
int nMiles = 2;
float fVal1 = PI; /*assume PI is a constant*/
char cCh = ‘N’;
Shortcuts
• shortcut for increment
• Before:
<variable> = <variable> + 1;
• After:
<variable>++;
Shortcuts
• shortcut for increment
• Before:
<variable> = <variable> - 1;
• After:
<variable>--;
Shortcuts
Examples: Screen Output:
int i = 1; 2
i++;
3
printf(“%d\n”, i);
i++; 2
printf(“%d\n”, i);
i--;
printf(“%d\n”, i);
Shortcuts
• shortcuts for assignment operators
• Before:
<variable> = <variable> <operator> <expr>;
• After:
<variable> <operator>= <expr>;
Shortcuts
Examples:
• i = i + 2; is equivalent to i += 2;
• x = x – 15; is equivalent to x -= 15;
• amt = amt * (1 + rate); is equal to
amt *= 1+ rate;
• share = share / 4; is equal to share /= 4;
• j = j % 2; is equivalent to j %= 2;
Keywords
• Keywords are words that have a special meaning in C.
• Reserved words are words that cannot be used as
identifiers.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct
double
Separators
• are some symbols in C that are used to separate tokens
• spaces, tabs, and newlines (also called white spaces) are
also used to separate tokens are not tokens themselves
• Examples
• ,
• ;
• :
Comments
• are not processed as tokens and are
usually ignored by the compiler
• to make the code more readable and
understandable
• Syntax:
/* <comments> */
// <comments>
single line comments are only possible in version C99 and later
Comments
• Examples:
/* Juan dela Cruz
* Last Modified : September 30, 2019
* The program will compute my Grade.
*/
int age; // this is the age of the student
Self-Evaluation Exercises
• Determine if the following identifiers are valid or invalid.
• L8r
• star*ting
• num_Values
• 4u
• one_i_aren’t
• Evaluate the following expressions
• 10 + 23 % (17 – 4 * 2) / (24 – (7 + 15 % 2))
• 150 - (-6 + 8 * 4 – 22 % 4) – (5 – (15.2 / 2))
46
Self-Evaluation Exercises
• Convert the following mathematical equations
to C expressions without adding unnecessary
parenthesis
47
References:
Aho, A.V., Lam, M. S., Sethi, R., & Ullman, J. D. (2006). Compilers: Principles, Techniques, and Tools (2nd
ed.). Addison Wesley.
Hanly, J. and Koffman, E. (2012). Problem Solving and Program Design in C (7th Edition). Pearson.
Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd. ed.). Pearson.
Simonyi, C. (1999). Hungarian Notation. MSDN Library. Microsoft Corp. Retrieved
from: [Link]
Acknowledgments:
This course material is a revised edition of the Fundamentals of C Programming, a course material for
CMPROG1 by Nathalie Lim-Cheng in 2002. The development of this material was funded by the
University Research Coordination Office (URCO) of De La Salle University, Manila.
slides by: [Link]@[Link]