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

C programming variables

The document explains the concept of variables in C programming, detailing their declaration, which consists of a data type and an identifier. It describes how different data types (int, char, float) occupy varying amounts of memory and the roles of the compiler and linker in managing these variables. Additionally, it includes activities and questions to reinforce understanding of the material.

Uploaded by

Fernanda Castro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C programming variables

The document explains the concept of variables in C programming, detailing their declaration, which consists of a data type and an identifier. It describes how different data types (int, char, float) occupy varying amounts of memory and the roles of the compiler and linker in managing these variables. Additionally, it includes activities and questions to reinforce understanding of the material.

Uploaded by

Fernanda Castro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C Programming Variables- READING ACTIVITY

Variable
A variable is the combination of a data types and an identifier (name) that
represents one or more memory locations used to hold a program's data.

A variable must be declared before it can be used. The declaration of a


variable consists of two parts:

1. A data type (or just simply a type) that serves two purposes:
1. Tells the compiler how to handle or manipulate the data
stored in the variable. In the example, the word float is one of
several data types we'll discuss later in this section.
2. Tells the linker how much memory to reserve to store the
contents of the variable.
2. An identifier (or name) that will be used to uniquely identify the
variable whenever we want to access or modify its contents. The
words radius and area in the example are identifiers.

To the right is a visual representation of how three variables might be


stored in the 16-bit wide data memory of a PIC24 family device (there
wouldn't actually be gaps between them).
The first variable, warp_factor, is of type int which is defined as a 16-bit
value in the MPLAB® XC16 Compiler, so it takes one complete word (two
bytes) of data memory.
The second variable letter is of type char which is almost always an 8-bit
value and only compilers with Unicode support might define it otherwise. So,
it takes up one byte or half of a word of data memory. If other char type
variables were declared, a second one would be packed into the same word
as the first, so both bytes would be occupied.
The third variable length is of type float which is usually a 32-bit value with
most compilers. It takes up two full words or 4-bytes of data memory. Its
data is also encoded (not shown) in a modified form of the IEEE-754 floating
point format, so it needs to be handled differently from int and char type
variables.

1) Pre-reading activity: Work in pairs to look up definitions and


discuss the meanings.

Variable
Data Type
Identifier
Compiler
Linker
Memory
Int
Char
Float
2) Comprehension Questions
What are the two parts of a variable declaration?
How does the compiler use the data type of a variable?
Why is it important to reserve memory for variables?
Explain the difference between int, char, and float variables in terms
of memory usage.

3) Post-Reading Activity: Fill-in-the-Gap Exercise

Fill in the gaps with the correct terms from the text:

1. A variable is a combination of a _________ and an _________ that


represents memory locations used to hold a program's data.
2. A variable must be _________ before it can be used.
3. The two parts of a variable declaration are the _________ and the
_________.
4. The _________ tells the compiler how to handle the data stored in
the variable.
5. The _________ tells the linker how much _________ to reserve for
the variable.
6. An _________ is used to uniquely identify the variable.
7. The data type _________ takes one complete word of data
memory in the MPLAB® XC16 Compiler.
8. The data type _________ is almost always an 8-bit value.
9. The data type _________ usually takes up two full words or 4-
bytes of data memory.

4) True or False

1. A variable can be used in a program without being declared


first.
2. An identifier tells the linker how much memory to reserve for a
variable.
3. The char data type usually takes up one complete word of data
memory.
4. The float data type is typically a 32-bit value.
5. The int data type in the MPLAB® XC16 Compiler is defined as a
16-bit value.
Key Terms and Definitions
Variable
A variable is a combination of a data type and an identifier (name) that
represents one or more memory locations used to hold a program's data.
Data Type
A data type specifies the type of data that a variable can hold, such as
integer, character, or floating-point values. It tells the compiler how to
handle the data and the linker how much memory to reserve.
Identifier
An identifier is a name used to uniquely identify a variable, allowing the
programmer to access and modify the variable's contents in the program.
Compiler
A compiler is a software tool that translates the code written in a high-level
programming language into machine code that the computer's processor
can execute.
Linker
A linker is a software tool that combines multiple object files generated by
the compiler into a single executable program. It also resolves references
between the object files and allocates memory for variables.
Memory
Memory refers to the storage space in a computer where data is held for
processing. It is used to store program data, variables, and instructions
during the execution of a program.
int

int is a data type used to represent integer values. In the MPLAB® XC16
Compiler, an int is defined as a 16-bit value, occupying two bytes of
memory.
char
char is a data type used to represent single characters. It is typically an 8-
bit value, occupying one byte of memory. In some cases, compilers with
Unicode support might define it differently.
float
float is a data type used to represent floating-point (decimal) values. It is
usually a 32-bit value, occupying four bytes of memory, and is stored using
a modified form of the IEEE-754 floating point format.
These definitions should help clarify the terms and concepts related to C
programming variables.
Answer Key for True or False

1. False
2. False
3. False
4. True

Comprehension Questions

1.The two parts of a variable declaration are:


A data type that specifies the type of data the variable will hold and instructs
the compiler on how to handle the data.
An identifier, which is the name used to uniquely identify the variable whenever
it is accessed or modified.

2.The compiler uses the data type of a variable to determine how to handle or
manipulate the data stored in the variable. This includes operations that can be
performed on the data and how the data is stored in memory.

3. It is important to reserve memory for variables to ensure that there is enough


space to store the variable's data. Reserving memory allows the program to
allocate the correct amount of space needed to store the variable's value and
ensures that other data is not overwritten.

4.The difference between int, char, and float variables in terms of memory
usage is as follows:
An int variable is typically a 16-bit value in the MPLAB® XC16 Compiler, which
means it takes up one complete word (two bytes) of data memory.
A char variable is almost always an 8-bit value, taking up one byte (half of a
word) of data memory. If multiple char variables are declared, they can be
packed into the same word, with each char occupying one byte.
A float variable is usually a 32-bit value, requiring two full words (four bytes) of
data memory. The data for a float variable is encoded in a modified form of the
IEEE-754 floating point format, necessitating different handling compared to int
and char variables.

You might also like