0% found this document useful (0 votes)
3 views

LESSON 5

This document outlines the fundamentals of variables and data types in C programming, including character sets, keywords, variable declarations, and data types. It explains the rules for naming variables, their initialization, scope, and the use of qualifiers and constants. Additionally, it provides examples of input and output operations, along with activities and assignments for practical application.

Uploaded by

wawudadora97
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LESSON 5

This document outlines the fundamentals of variables and data types in C programming, including character sets, keywords, variable declarations, and data types. It explains the rules for naming variables, their initialization, scope, and the use of qualifiers and constants. Additionally, it provides examples of input and output operations, along with activities and assignments for practical application.

Uploaded by

wawudadora97
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Variables and data types

Lesson objectives

• By the end of this lesson, you will be expected to:


• Identify various characters used in C program
• Declare and use variables appropriately
• Describe various basic data types in C
• Describe various qualifiers used to modify data types
• Define and use a constants in a program
• Describe types of statements and expressions in C
Character set

• This consists of a set of alphabets, letters, white spaces and


some special characters that are valid in C language.
1. Alphabets
– Uppercase: A B C…………………… XYZ
– Lowercase: a b c……………. xyz
2. Digits 012345689
3. Special Characters:
Character set

4. White space Characters:


– White space refers to spaces and blank lines in your source
code.
Keywords:
• These are the reserved words used in programming.
Each keyword has fixed meaning that cannot be
changed by user.
• C programming is case sensitive and all keywords must
be written in lowercase.
Keywords:
Variables

• A variable is a named memory location in computer


memory used to store data.
• Each variable should be given a unique name called
identifier. Variable names are symbolic representation
of a memory location.
– Examples of valid variables names: pay, age, gender.
Rules for writing a variable name in c

• A variable can be composed of letters, digits and


underscore '_'
• The first character of variable should be a letter.
• Variable names must be meaningful, easy to remember
and work with.
• The variable names should not match a reserved
keyword.
Variable declarations

• Variable declaration is a statement that tells the


compiler the variables name, and what data type it is.
• The variables must be declared before they are used.
Variable declarations

• The data type must be a valid C data type including


char, int, float, double, or any user defined data type
etc.
• The variable_list may consist of one or more identifier
names separated by commas. Examples:
int a,b; char e;
Initialization of variables
• Variables can be initialized (assigned an initial value) in
their declaration. The initializer consists of an equal
sign followed by a constant expression as follows:

variable name= value;


Variables can be initialized in two ways:

1. Declare and initialize the variable(s).


int x=8; char d=’A’;
2. Declare and initialize in the variables in separate
statements:
Int x;
x=8;
char d;
d=’A’;
Variable scope

• A scope is a region of the program.


• There are three places where variables can be
declared:
1. Inside a function or a block e.g. local variables,
2. In the definition of function parameters e.g. formal parameters.
3. Outside of all functions e.g. global variables.
Data Types
• Data types are the keywords, which are used for assigning a type to a
variable. The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted.
• A variable has four characteristics.

int x=8
Classification of Data Types
1. Basic Data Types
• Integer types
• Floating Type
• Char type
2. Derived Data Types
• Arrays
• Pointers
• Structures
• Enumeration
Integer Types

• An integer is a whole number that can be positive,


negative, or zero.
– Example int x=7;
Floating-Point Types

• A float-point is used to represent real numbers. That is


numbers that can consist of the integer part and
decimal part.
• Example
– float x, y; //Declaration of float type
– y=3.45; //Initialization of float variable
Char data type

• Used to represent data of character type. The keyword


char is used to declare character type variables.
– char a,b,d;
– a=’A’;
Using printf( ) to output values
• The main output code used with printf() and the
format they represent are listed below.
Code Format
%c Character
%d or %i Integer
%f Floating point numbers (float, double)
%s String of characters
Inputting values from the keyboard using scanf( )

• To use scanf( ) to read an integer value from the


keyboard, use the general form:
scanf(“%d”, &int_var-name);

• The fragment below, for example, reads an integer entered


from the keyboard.
int num;
scanf(“%d”, &num);
Inputting values from the keyboard using scanf( )

• The table below codes and formats used with scanf()


for basic types.
Code Format
%c Read a single character
%d or %i Read an integer
%lf Read a double
%s Read a string
Activity

1. Write a C program that prompts the user to enter two


values. The program should calculate the sum of the
values entered.
2. Write a C program that prompts the user to enter two
values. The program should calculate the difference
of the values entered.
Activity

• Write a C program that calculates the area of a


rectangle. Given:
Area=length * width
Assignment One (5 Marks)

• Write a C program that prompts the user to enter the


radius of a circle and then calculates the area of a
circle. Given that:

Area=(pi*r2)
Qualifiers

• This is a keyword used to alter the meaning of base


data types to yield a new data type.
• Types of Qualifiers include:
1. Size qualifiers
2. Sign qualifiers
3. Constant qualifiers
4. Volatile qualifiers
Size qualifiers
• Size qualifiers alter the size of basic data type. The
keywords long and short. long int i;
• The size of int is either 2 bytes or 4 bytes but, when
long keyword is used, that variable will be either 4
bytes of 8 bytes.
Sign qualifiers
• Determine whether a variable can hold positive value,
negative value or both values. Keywords signed and
unsigned are used for sign qualifiers.
unsigned int a;
• Unsigned variable can hold zero and positive values only.
• It is not necessary to define variable using keyword
signed because; a variable is signed by default. Sign
qualifiers can be applied to only int and char data types.
Constant qualifiers

• Constant qualifiers can be declared with keyword


const.
• An object declared by const cannot be modified.
const int p=20;
• The value of p cannot be changed in the program.
Volatile qualifiers

• A variable should be declared volatile whenever its


value can be changed by some external sources
outside program. Keyword volatile is used to indicate
volatile variable.
Constants
• A constant is a data storage location used by a program. Unlike a
variable, the value stored in a constant can’t be changed during
program execution. The constants refer to fixed values that the
program may not alter during its execution.
• C has two types of constants, each with its own specific uses:
a) Literal Constants
b) Symbolic Constants
Literal Constants
• A literal constant is a value that is typed directly into the source
code wherever it is needed.
• Literal constants can be of any of the basic data types like:
1. integer constant
2. a floating constant
3. a character constant
4. a string literal
5. enumeration constants
integer constant
• An integer literal can be a decimal (base10), octal (base 8), or
hexadecimal(base 16) constant.
1. Decimal digits: 0 1 2 3 4 5 6 7 8 9
2. Octal digits: 0 1 2 3 4 5 6 7
3. Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
• Examples:
a) Decimal constants: 0, -4, 12 etc
b) Octal constants: 021, 077, 033 etc
c) Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

You might also like