Title: Introduction to Computer C Programming
1Introduction to Computer (C Programming)
- Chapter 2
- Constants, Variables, and Data Types
2Roadmap
- 2.1 Learning by example
- 2.2 Perspectives of the programming process
- 2.3 Program
- 2.4 Variables
- 2.5 Data types
- 2.6 Declaration of variables
- 2.7 Assigning Values to Variables
32.1 Learning by example
- The program add2.c asks the user to enter two
numbers, adds those numbers together, and then
displays the sum.
42.1 Learning by example
- The program add2.c asks the user to enter two
numbers, adds those numbers together, and then
displays the sum.
include ltstdio.hgt main() int n1, n2,
total printf(This program adds two
numbers.\n) printf(1st number?) scanf(d,
n1) printf(2nd number?) scanf(d,n2) t
otal n1n2 printf(The total is d.\n,
total)
52.1 Learning by example Cont.
int n1, n2, total
- Variable
- A variable is a placeholder for some piece of
data whose value is unknown when the program is
written. - In the add2.c, the variables n1 and n2 represent
the numbers to be added, and the variable total
represents the sum. - Variable names are usually chosen to be usable.
62.1 Learning by example Cont.
int n1, n2, total
- Variable declaration
- Declaring a variable tells the C compiler that
you are introducing a new variable name and
specifies the type of data that variable can
hold. - Variable n1, n2 and total indicate to the
compiler that each holds a value of type int
(stands for integer).
72.1 Learning by example Cont.
include ltstdio.hgt main() int n1, n2,
total printf(This program adds two
numbers.\n) printf(1st number?) scanf(d,
n1) printf(2nd number?) scanf(d,n2)
total n1n2 printf(The total is d.\n,
total)
Input
Computation
Output
82.1 Learning by example Cont.
prompt
printf(1st number?) scanf(d,n1) printf(2n
d number?) scanf(d,n2)
read number
d means that the input should be a decimal
integer.
- General format of scanf is as follows
- scanf(control string, variable1,
variable2,)
contains the format of data being received
Must use the operator before the name of
variable
92.1 Learning by example Cont.
- Computation phase
- Computation is specified by writing an expression
that indicates the necessary operations. - The result of the expression is stored in a
variable using an assignment statement so that
the result can be used in subsequent parts of the
program. - In C, an assignment statement stores a value
written to the right of an equal sign in a
variable written to the left of that equal sign.
total n1n2
102.1 Learning by example Cont.
- Output phase
- The output phase of the program consists of
displaying the computed result. - General format of printf is as follows
- printf(control string, variable1,
variable2,) - Note there is no before variables
- d means that the output should be displayed as
a decimal integer.
printf(The total is d.\n, total)
112.2 Perspectives of the programming process
- Reductionism is the philosophical principle that
the whole of an object can best be understood by
understanding the parts that make is up. - Holism recognizes that the whole is often more
than the sum of its parts. - In learning about programming, the best approach
is usually to alternate between these two
perspectives.
122.3 Program
- A programming language is designed to help
process certain kinds of data and to provide
useful output known as information. - The task of processing data is accomplished by
executing a sequence of precise instructions
called a program. - These instructions are formed using certain
symbols and words according to some rigid rules
known as syntax rules (or grammar).
132.3 Program Cont.
- Character set the characters in C are grouped
into the following categories - Letters AZ, az
- Digits 09
- Special characters
- , . - ? lt
- White spaces
- Blank space Horizontal tab Carriage
return - New line Form feed
142.3 Program Cont.
- Trigraph Characters each trigraph sequence
consists of three characters. - ?? number sign ??( left bracket
- ??) right bracket ??lt left brace
- ??gt right brace ??! vertical bar
- ??/ \ back slash ??/ caret
- ??- tilde
152.3 Program Cont.
- C tokens individual words and punctuation marks,
the smallest individual units in a C program.
C TOKENS
Keywords
Constants
Strings
Operators
Special Symbols
Identifiers
162.3.1 Keyword
- Keywords serve as basic building blocks for
program statements. - 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 unsigned union
void - volatile while
Note all keywords must be written in lowercase.
172.3.2 Identifiers
- Identifiers refer to names of variables,
functions and array. - Identifiers are user-defined names consist of a
sequence of letters, digits and underscore, with
a letter as first character. - Both uppercase and lowercase letters are
permitted, although lowercase letters are
commonly used.
182.3.2 Identifiers Cont.
Rules for Identifiers
- First character must be an alphabet (or
underscore). - Must consist of only letters, digits or
underscore. - Only first 31 characters are significant.
- Cannot use a keyword.
- Must not contain white space.
- Example
- sum Sum M.D.John day Date 3days
student_name - 33 lotus_1_2_3 char agtb _above
123
192.3.3 Constants
- Constants in C refer to fixed values that do not
change during the execution of a program.
CONSTANTS
Numeric constants
Character constants
Real constants
Single character constants
String constants
Integer constants
202.3.3.1 Integer Constants
- An integer constant refers to a sequence of
digits. There are three types - decimal integers
- Consist of a set of digits, 0 through 9,
preceded by an optional or sign. - Example 123 -321 0 654321 78
- octal integer constant
- Consists of any combination of digits from
the set 0 through 7, with a leading 0. - Example 037 0 0435 0551
212.3.3.1 Integer Constants Cont.
- An integer constant refers to a sequence of
digits. There are three types - hexadecimal integer
- A sequence of digits preceded by 0x or 0X.
- Example 0X2 0x9F 0Xbcd
222.3.3.1 Integer Constants Cont.
- The largest integer value that can be stored is
machine-dependent. - It is possible to store larger integer constants
on these machines by appending qualifiers such as
U,L and UL to the constants. - Example
- 56789U or 56789u
- 987612347UL or 98761234ul
- 9876543L or 9876543l
- U/u is unsigned, L/l is long.
232.3.3 Integer Constants Cont.
- Representation of integer constants on a 16-bit
computer.
include ltstdio.hgt main() printf(Integer
values\n\n) printf(d d d\n,32767,327671,3
276710) printf(\n) printf(Long
integer values\n\n) printf(ld ld
ld\n,32767L,32767L1L,32767L10L)
242.3.3.2 Real Constants
- Real constants represent numbers containing
fractional parts. - Decimal notation
- Example 0.0083 -0.75 435.36 247.0
215. - .95 -.70 .5
- Note it is possible to omit digits before the
decimal point, or digits after the decimal point. - Exponential notation mantissa e exponent
- Example 0.65e4 12e-2 1.5e5 3.18E3
-1.2E-1
252.3.3.3 Single Character Constants
- Single character constant contains a single
character enclosed in pair of single quote marks. - Example 5 X
- Character constants have integer values known as
ASCII values, so character constants 5 is
different from integer constants 5.
262.3.3.4 String Constants
- A string constant is a sequence of characters
enclosed in double quotes. - Example Hello! 1987 WELL DONE
?...! 53 X - The characters in a string constant may be
letters, numbers, special characters and blank
space. - Note a single character constant (x) is not
equivalent to the single character string (x).
272.3.3.5 Backslash Character Constants
- C supports some special backslash character
constants that are used in output functions. - \a bell \v vertical
tab - \b back space \ single quotes
- \f form feed \ double quote
- \n new line \? question mark
- \r carriage return \\ backslash
- \t horizontal tab \0 null
- Note each one of them represents one character.
These characters combinations are known as escape
sequences.
282.4 Variables
- A variable is a data name that may be used to
store a data value. - Constants remain unchanged, while a variable may
take different at different times during
execution. - A variable name can be chosen by the programmer
in a meaningful way so as to reflect its function
or nature in the program. - Example Average, height, Total, Counter_1
292.4 Variables cont.
- Variable names subject to the following
conditions - They must begin with a letter.
- ANSI standard recognizes a length of 31
characters. - Uppercase and lowercase are significant.
- It should not be a keyword.
- White space is not allowed.
- Examples Valid? Invalid?
- John Value T_raise Delphi x1 ph_value mark
sum1 - distance 123 (area) 25th
302.5 Data Types
- To be useful in a wide variety of applications,
programs must be able to store many different
types of data. - ANSI C supports three classes of data types
- Primary (or fundamental) data types
- Derived data types
- User-defined data types
312.5 Data Types cont.
322.5.1 Integer Types
- Integers are whole numbers with a range of values
supported by a particular machine. - Integers occupy one word of storage (typically,
16 or 32 bits). - If we use a 16 bit word length, the size of the
integer value is -215215-1.
332.5.1 Integer Types cont.
- In order to provide some control over the range
of numbers and storage space, C has three classes
of integer storage - short int half storage of int
- int occupy one word
- long int twice storage of int.
- We declare long and unsigned integers to increase
the range of values. - The default declaration is signed.
342.5.1 Integer Types cont.
- Size and range of integer type on a 16-bit
machine - Type Size (bits)
Range - (signed) int 16 -32,76832,767
- unsigned int 16 065535
- (signed) short int 8 -128127
- unsigned short int 8 0255
- (signed) long int 32
-2,147,483,6482,147,483,647 - unsigned long int 32 04,294,967,295
352.5.2 Floating Types
- Floating point (or real) numbers are stored in 32
bits (on all 16 bit and 32 bit machines), with 6
digits of precision. - Double data type can provide higher accuracy,
which use 64 bits giving a precision of 14
digits. - To extend the precision further, we may use long
double which uses 80 bits.
362.5.2 Floating Types cont.
- Size and range of floating type on a 16-bit
machine - Type Size (bits)
Range - float 32
3.4E-383.4E38 - double 64
1.7E-3081.7E308 - long double 80
3.4E-49323.4E4932
372.5.3 Void Type
- The void type has no value.
- It is usually used to specify the type of
functions. The type of a function is said to be
void when it does not return any value to the
calling function.
382.5.4 Character Types
- A single character can be defined as a character
(char) type data. - Characters are usually stored in 8 bits (one
byte) of internal storage. - Size and range of integer type on a 16-bit
machine - Type Size (bits)
Range - (signed) char 8
-128127 - unsigned char 8
0255
392.5.5 Test data type length
- A program testing data type length of your
computer.
include ltstdio.hgt main() printf("Data type
Number of bytes\n") printf("------------
---------------------\n") printf(char
d\n", sizeof(char)) printf("int
d\n", sizeof(int)) printf("short int
d\n", sizeof(short)) printf("long int
d\n", sizeof(long)) printf("float
d\n", sizeof(float)) printf("double
d\n", sizeof(double))
402.6 Declaration of variables
- After designing suitable variable names, we must
declare them to the compiler. Declaration does
two things - It tells the compiler what the variable name is.
- It specifies what type of data the variable will
hold.
412.6 Declaration of variables cont.
- The syntax for declaring a primary type variable
is as follows - data-type variable_1,variable_2,
- Example
- int count
- int number, total
- double ratio
422.6 Declaration of variables cont.
- Data types and their keywords
- Data type keyword
equivalent - Character
char - Unsigned character
unsigned char - Signed character
signed char - Signed integer
(signed) int - Signed short integer
(signed) short (int) - Signed long integer
(signed) long (int) - Unsigned integer
unsigned (int) - Unsigned short integer
unsigned short (int) - Unsigned long integer unsigned long
(int) - Floating point
float - Double-precision floating point
double - Extended double-precisionfloating point long
double
432.6 Declaration of variables cont.
- Declaration of variables in program
main /Declaration/
float x, y int code
short int count long int amount
double deviation unsigned n char
c /Computation/
442.7 Assigning Values to Variables
- You can assign values to variables in the
following ways - Use an assignment statement
- Read the value from keyboard.
452.7.1 Assignment statement
- Values can be assigned to variables using the
assignment operator as follows - variable expression
- variable is the variable you wish to set
- expression specifies the value.
- Example
- initial_value 0 balance
75.84 - yes x p q
s 0 - sum num1 num2 sub num1 num2
- mul num1 num2 div num1/num2
462.7.1 Assignment statement cont.
- Initialization
- data-type variable expression
- Example
- int final_value 100
- double balance 75.84
- char yes x
472.7.2 Reading data from keyboard
- General format of scanf is as follows
- scanf(control string, variable1,
variable2,)
contains the format of data being received
Must use the operator before the name of
variable
48Just Remember
- Every complete C program contains a function
main. When the program is run, the statements in
the body of main are executed in order. - Many programs are composed of the following three
phases input, computation, and output. - Constants are used to specify values that do not
change within a program. - Variables have three attributes a name, a value,
and a type. All variables used in a C program
must be declared. - Variables are given values through the use of
assignment statements.
49Homework
- Write a program that reads in two numbers an
account balance and an annual interest rate
expressed as a percentage. Your program should
then display the new balance after a year. There
are no deposits or withdrawals-just the interest
payment. Your program should be able to reproduce
the following sample run
Interest calculation program. Starting
balance?6000 Annual interest rate
percentage?4.25 Balance after one year 6255
50Homework
- Hints the syntax of using scanf to read a float
number from keyboard is - scanf(f, variable)
- Extend the program you wrote in homework1 so that
it also displays the balance after two years have
elapsed, as shown in the following sample run
Interest calculation program. Starting
balance?6000 Annual interest rate
percentage?4.25 Balance after one year
6255 Balance after two years 6520.84