Data Types
Data Types
Outline
2.1 Introduction
2.2 A Simple C Program: Printing a Line of Text
2.3 Memory Concepts
2.4 Naming Convention of Variables
2.5 Arithmetic in C
2.6 Type Conversion
Computer was modelled
after Human
Sensory Memory
( <1 sec)
http://www.human-memory.net/types_sensory.html
A simple model of the computer
Short term
Main
Memory
Memory
Sensory
Memory
Output
Input Buffer Processor Devices
Devices
Permanent
Long term
Storage
Memory
Definition: Computer Program
New Balance
Rate
Processing
Year
OUTPUT
INPUT
COMPUTATION
Introduction
• C programming language
– Structured and disciplined approach to program design
– Story Paragraph Sentence
– Program Function Statement
A Simple C Program: Printing a Line of
Text
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 int main()
6 {
7 printf( "Welcome to CSE 115!\n" );
8
9 return 0;
10 }
• Comments
– Text surrounded by /* and */ is ignored by computer
– Used to describe program
• #include <stdio.h>
– Preprocessor directive - tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operations
A Simple C Program: Printing a Line of
Text (II)
• int main()
– C programs contain one or more functions, exactly one of
which must be main
– Parenthesis used to indicate a function
– int means that main "returns" an integer value
– Braces indicate a block
• The bodies of all functions must be contained in braces
A Simple C Program: Printing a Line of
Text (III)
• printf( "Welcome to C!\n" );
– Instructs computer to perform an action
• Specifically, prints string of characters within quotes
– Entire line called a statement
• All statements must end with a semicolon
– \ - escape character
• Indicates that printf should do something out of the
ordinary
• \n is the newline character
A Simple C Program: Printing a Line of
Text (IV)
• return 0;
– A way to exit a function
– return 0, in this case, means that the program terminated
normally
• Right brace }
– Indicates end of main has been reached
Basic Data types
• Variables
– Variable names correspond to locations in the
computer's memory.
– Every variable has a name, a type, a size and a value.
– Whenever a new value is placed into a variable
(through scanf, for example), it replaces (and
destroys) previous value
– Reading variables from memory does not change them
• A visual representation
integer1 45
Using Variables: Output
int a, b, c;
a = 3; Output:
b = 8;
11
c = a + b;
printf(“%d” , c);
Suppose we want……
int a, b, c;
a = 3; Output:
b = 8;
The value of c: 11
c = a + b;
printf(“%d” , c);
You can do……
int a, b, c;
a = 3; Output:
b = 8;
The value of c: 11
c = a + b;
printf(“The value of c:”);
printf(“The value of c: %d”,c);
printf(“%d” , c);
Or….
int a, b, c;
a = 3; Output:
b = 8;
The value of c: 11
c = a + b;
printf(“The value of c: %d”,c);
Using Variables: Output
Format specifier Variable
printf(type, what);
char "%c"
int "%d"
"%i"
float "%f"
double "%lf"
Using Variables: Output
int a, b, c; float a = 8.958;
b = 8; double a = -9.8;
int a; double c;
scanf("%i", &a); scanf("%lf", &c);
or char d;
scanf("%d", &a); scanf("%c", &d);
Little QUIZ (What is the output if user
enters 0)
int a; char a;
scanf("%i", &a); scanf("%c", &a);
printf("%d", a); printf("%d", a);
Naming convention of a variable
Keyw ord s
• First_tag
• char
• Price$
• group one
• average_number
• int_
• 8boys
Declaring variables
• Two ways:
– Using scanf
int c;
scanf(“%d”,&c);
– Using assignment operator
int c;
c = 10;
Arithmetic operators on int and char
• Arithmetic operators:
C o p era tio n Arithm etic Alg eb ra ic C exp ressio n
o p era to r exp ressio n
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
5+6*7-8/7 = 46
5*4/3 = 6
5/(6+7) – 8%8 = 0
Working with characters
and
c = 20;
c++;
printf(“%d”,c);
c = 5;
printf( "%d", ++c); Prints 6
printf( "%d", c++); Prints 5
int a, b, c;
b = 10;
c = 20;
a = b+++--c;
printf("%d %d %d", a, b, c);
• Arithmetic operators:
Fractional division!!
Working with fractions: Example 1
x = 1.0 y = 1 x = 1.6667 y = 1
Type conversion (example)
Floor(x)
x
: The largest integer not exceeding x
Ceil(x)
x
: The smallest integer not less than x
Round(x)
x
: The nearest integer (in case of tie take greater one)
Write down a program that will take a positive fractional number as input
and will print its floor, ceil and round.
Problem Solving Methodology
3. Hand example
distance side12 side2 2
side1 = 4 - 1 = 3
side2 = 7 - 5 = 2
distance side12 side22
2 2
distance 3 2
distance 13 3.61
Example 2 (cont’d)
5. Testing
• After compiling your program, run it and see if it
gives the correct result.
• Your program should print out
The distance between two points is 3.61
• If not, what will you do?
Modification to Example 2
How will you find the distance between two other points (2,5) and (10,8)?