This document discusses data types in C programming. It introduces the four basic data types - integer, float, char, and void. Integer is used for whole numbers, float for numbers with decimals, char for single characters, and void for functions without return values. Variables must be declared with a data type before use. Standard input and output functions like scanf and printf are demonstrated for getting user input and displaying output.
2. DATA TYPES
Topic Outcomes
• The students are able to identify four
standard types of data in C
• The students are able to declare the
variables according to the data types
• The students are able to create C
programs with various of data types
2
3. INTRODUCTION
Data
types
int float char void
There are four basic data types :
• Int
• Float
• Char
• Void
3
4. INTEGER
• Integer is number without the fraction part
• Support three different sizes (1 byte, 2
bytes and 4 bytes)
• An integer constant must have at least
one digit
4
5. CONT..
• It must not have a decimal point
• It can be either positive or negative
• If no sign precedes an integer
constant it is assumed to be positive
• No commas or blanks are allowed
within an integer constant
• Examples :
426 +782 -8000 -7605
5
6. CHAR
• The letters of the alphabet
• Most computers use the American Standard
Code for Information Interchange (ASCII)
• The maximum length of a character constant
can be 1 character
• Examples :
• 'A'
• 'I'
• '5'
• '='
6
7. FLOAT
• Number with a fractional part, such as 43.32
• Support three different sizes of floating point data types: float,
double and long double
• Double provides twice the precision of float
• Floats typically take up 4 bytes
• Doubles takes up 8 bytes
• Long double takes 10 bytes
7
8. VOID
• The void type has no values therefore we cannot
declare it as variable as we did in case of integer
and float.
• The void data type is usually used with function
to specify its type.
• Like in in the first C program we declared
“main()” as void type because it does not return
any value.
8
10. VARIABLES
• Variables are named memory
locations that have a type.
• Must be declared and defined
• Declaration is used to name an
object such as a variable
• Definitions are used to create the
object
• A variable cannot be type void
10
12. CONT..
In order to use a variable in our program we must first
declare it
HOW? A declaration statement has the following format:
type variable name;
–type : what kind of data will be stored in that location
(integer?character? floating point?)
–variable name: what is the name of the variable?
–semi-colon : indicates this is a statement!
int num_students; // declaration
num_students= 22; // initialization
12
13. EXAMPLE 2.1
#include <stdio.h>
main()
{
int num1, num2, sum;
printf(“Enter one decimal number:”);
scanf(“%d”, &num1);
printf(“Enter another decimal number:”);
scanf(“%d”, &num2);
sum = num1 + num2;
printf(“nSum of %d and %d is %d ”, num1, num2, sum);
return 0;
}
13
14. EXAMPLE 2.2
#include <stdio.h>
main()
{
float area_rectangle, width, length;
printf(“Enter the length:”);
scanf(“%f”, &length);
printf(“Enter the width:”);
scanf(“%f”, &width);
area_rectangle = length * width;
printf(“nArea of the rectangular is %f ”, area_rectangle);
return 0;
}
14
15. EXAMPLE 2.3
#include <stdio.h>
main()
{
double pi, height, radius, base, volume;
pi = 3.142;
printf(“Enter the height the cone:”);
scanf(“%lf”, &height);
printf(“Enter the radius of the cone:”);
scanf(“%lf”, &radius);
base = pi * radius * radius;
volume = (1.0/3.0) * base * height;
printf(“nThe volume of a cone is %f ”, volume);
return 0;
}
15
16. #define
/*You may also associate constant using #define preprocessor directive*/
#include <stdio.h>
#define pi 3.142
main()
{
double height, radius, base, volume;
printf(“Enter the height the cone:”);
scanf(“%lf”, &height);
printf(“Enter the radius of the cone:”);
scanf(“%lf”, &radius);
base = pi * radius * radius;
volume = (1.0/3.0) * base * height;
printf(“nThe volume of a cone is %f ”, volume);
return 0;
}
16
18. STANDARD OUTPUT
• printf Function
• prints information to the screen
• requires two arguments
• control string
• conversion specifier
Example
• double angle = 45.5;
• printf(“Angle = %.2f degrees n”, angle);
Output:
• Angle = 45.50 degrees
19. STANDARD INPUT
• scanf Function
• inputs values from the keyboard
• required arguments
• control string
• memory locations that correspond to
the specifiers in the control string
Example:
• double distance;
• scanf("%lf", &distance);
20. SUMMARY
• There are four standard data types can be use in
C program which are:
• Integer
• Float
• Char
• Void
• Everytime you want to use a variable, the
declaration must be made
• To associate the variables with the data types,
you can use standard input and output function
20
21. REFERENCE
Hanly, J. R. & Koffman, E. B (2001). C
Program Design for Engineers. Addison
Wesley Longman.
Deitel, P & Deitel H (2008). C How to
Program. Pearson Education Inc.
21