Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Data in C
1
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Objectives
2
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Learning Outcomes
At the end of this chapter students will be able to
write simple C programs with,
proper declaration of variables,
and the correct use of formatted input-output
statements.
3
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Digits
Digits are 0 to 9.
Special characters
Most of the special characters used in C are listed in Table 3.1.
White spaces
White spaces are ignored by the compiler until they are a part of
string.
Examples - blank space, horizontal tab, carriage return, new line
and form feed.
4
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Special character
Comma
&
Ampersand
Period
Caret
Semicolon
Asterisk
Colon
Minus sign
Question mark
Plus sign
'
Apostrophe
<
Opening angle
"
Quotation mark
>
Closing angle
Exclamation mark
Left parenthesis
Vertical bar
Right parenthesis
Slash
Left bracket
Backslash
Right bracket
Tilde
Left brace
Underscore
Right brace
Dollar sign
Number sign
Percentage sign
5
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
auto
do
goto
signed
unsigned
break
double
if
sizeof
void
case
else
int
static
volatile
char
enum
long
struct
while
const
extern
register
switch
continue
float
return
typedef
default
for
short
union
6
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Identifiers
7
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
3.3 Constants
A constant is an item whose value does not
change during the execution of a program.
constants in C
Integer constants
Real constants
Single character constants
String constants
8
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Integer constants
is a sequence of digits.
types:
decimal integer
consists of a set of digits 0 to 9 preceded by an optional + or
- sign.
octal integer
consists of any combination of digits from 0 through 7 with an
O at the beginning.
Examples
Decimal Integer
123
-51
0
54321
+100
Octal Integer
O12
O576
O100
Hexadecimal
integer
0X6
0x9A
0Xbcd
10
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Real constants
consist of a fractional part in their representation
Examples:
0.0012
-0.87
435.29
+1237.0
can also be represented by exponential notation
(scientific notation) when they are very large or
very small
12.5E09
5E10
5e-12
3.2e+6
//value is 12500000000
//value is 50000000000
//value is 0.000000000005
//value is 3200000
11
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
String constants
is a set of characters enclosed in double
quotation marks.
may be consisting of alphabets, numbers,
special characters and blank spaces.
Examples:
"MMU"
"1234"
"God Bless"
"!.....?"
13
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
3.4 Variables
is an identifier whose value can change at any
time.
is a memory location used to store a data value.
A variable name should be carefully chosen by
the programmer so that its use is reflected in a
useful way in the entire program.
14
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
//rule 1 violated
//rule 3 violated
//rule 4 violated
//rule 5 violated
15
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
16
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
18
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
19
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Size (bytes)
Range
char
128 to 127
int
-2147483648 to 2147483647
unsigned int
0 to 4294967295
short int
-32768 to 32767
0 to 65535
long int
-2147483648 to 2147483647
0 to 4294967295
float
double
1.7e308 to 1.7e+308
20
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Declaration examples
int strength, age, tvs_sold;
declares variables strength, age, and tvs_sold as integer.
double
moon_distance, virus_size;
char
gender, result;
23
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
// meaningful declaration
declares meter to be integer and allows meter to
be used as a user-defined data type later in the
program.
24
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
person = male;
First C statement declared an enumerated data type
gender with possible values male and female.
The second one declares a variable person of gender
type.
Now the variable gender can have either male or female
as its values.
26
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Integer format
The general format for reading an integer number is %xd
Example 1 :
scanf (%d %d, &age, &strength);
If the values input are 20 and 160, value 20 is assigned
to age and 160 to strength.
Example 2:
scanf (%2d, &age);
Here, 2 specifies the width of the field, 2 digits only.
Suppose the input data was 101, the number 10 will be
assigned to age instead of 101.
28
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Float format
The general format for reading a float number is
%f
Example:
scanf (%f %f , &mark, &temperature);
Character format
The general format for reading a character is %c
Example:
scanf (%c, &gender);
Suppose that the input given is m, then m is
assigned to gender.
30
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
31
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
32
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Description
x or X
h or l (lower case of L)
e or E
Displays an unsigned hexadecimal integer. X causes the digits 0-9 and the letters A-F to be
displayed and x causes the digits 0-9 and a-f to be displayed.
Places before any integer conversion specifier to indicate that a short or long integer is displayed
respectively. Letters h and l are more precisely called length modifiers.
Displays a floating point number in exponential notation (computer's version of scientific notation).
Example: 250.3 2.503 x 10 in scientific form and 2.503E+02 in exponential form. The E (or e)
stands for exponent.
g or G
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
33
35
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Output
printf( "(%2d)", i );
(123)
printf( "(%5d)", i );
( 123)
printf( "%.4d", i );
0123
printf( "%.7d", i );
0000123
printf( "%.3f", f );
123.457
printf( "%.3e", f );
1.235e+002
printf( "%.3g", f );
123
(Multimedia )
printf( "(%5.4d)", i );
( 0123)
printf( "(%8.3f)", f );
( 123.457)
printf( "(%7.5s)", s );
( Multi)
( 123)
(123 )
(+0123)
(+123.457)
int i = 123;
double f = 123.45678;
char s[] = "Multimedia
University";
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
37
38
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Action
\'
\"
\?
\\
\a
\b
\f
\n
\r
\t
\v
39
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Programming exercise
Write a C program to find area of rectangle whose sides are given
by the user.
Solution
The inputs to the problem are the sides of a rectangle, i.e. the length
and the breadth. We have to get the values of length and breadth
from the user using scanf statement. These inputs are of data type
float since sides of a rectangle is normally with fraction (floating
point format).
The expected output is the area of the rectangle which is also of
data type float. The area could be calculated by using the following
formula,
area = length * breadth.
40
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Initialisation phase
No variable need to be initialised (as the values of length and
breadth will be given by the user at run time).
Processing phase
Read the values of length and breadth from the user and store
them into variables length and breadth respectively. Calculate
the area of the rectangle as length * breadth and store the result
into the variable area.
Termination phase
Display the calculated value of the variable area with
appropriate message.
41
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
The Program
//header file required for the scanf and printf functions
#include <stdio.h>
int main( )
{
//declaration part
float length, breadth;
float area;
//reading the input and storing them into variables length
//and breadth
printf("Enter the two sides of a rectangle: ");
scanf ("%f %f", &length, &breadth);
//calculating the area as length x breadth
area = length * breadth;
//displaying the output with suitable message
printf("The area of the rectangle = %f square meters\n", area);
return 0;
}
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
42
Sample output
Enter the two sides of a rectangle: 2.3 3.2
The area of the rectangle = 7.360000 square meters
Common errors
44
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
3.9 Summary
1. For each variable, you have to attach some data type.
The data type defines the amount of storage allocated
to variables and the values that they can accept.
2. There are two types of data, built-in and user-defined.
3. Every variable has to be declared before it is used.
4. Values to declared variables can be input from outside
the program using scanf function. The results of the
program can be output by using printf function.
5. The input to the program and the output from the
program can be formatted according to the user
requirements using features like conversion specifier,
flag, field width and precision.
45
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)