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

Lab Hands On 2 - Data Representation Part 1 (AKIA)

This laboratory session focuses on data representation of integer numbers in C programming. It demonstrates how different integer data types like short, unsigned short, int and unsigned int are represented and their ranges. The session includes programs to determine the size and output values of integer data types and analyze overflow cases.

Uploaded by

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

Lab Hands On 2 - Data Representation Part 1 (AKIA)

This laboratory session focuses on data representation of integer numbers in C programming. It demonstrates how different integer data types like short, unsigned short, int and unsigned int are represented and their ranges. The session includes programs to determine the size and output values of integer data types and analyze overflow cases.

Uploaded by

Dylan Akia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

De La Salle University

Computer Technology Department

LBYCMSY

Members: Dylan Lee B. Akia Group No.:

Date: Jan. 23, 2024

Laboratory Hands-On 2 – Data Representation Part 1


Description
This laboratory session focuses on the data representation for integer numbers. The C programming
language is to be used to demonstrate how software interprets binary or hexadecimal numbers as integer
numbers. There are four types of integer-based data types in the C programming language: short,
unsigned short, int and unsigned int. Data types in C can have different sizes and range.
The programs in these laboratory session also uses the printf() function to interpret the values in the
data types. The format specifier can be used to interpret the hexadecimal value of the data type. Below
are format specifiers used in this laboratory session:

d or i : Signed decimal integer

u : Unsigned decimal integer

x or X : Hexadecimal integer

Procedures
1. Using a browser, research on the Internet the size of the following data types in the C programming
language:
Data Type Data Size (bits)
Short : 16 bits
Unsigned Short : 16 bits

Int : 32 bits
Unsigned Int : 32 bits

2. Using the Dev C++ compiler, execute the program:


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
printf ("Size of signed short data type %d bits\n",sizeof(short)*8);
printf ("Size of unsigned short data type %d bits\n",sizeof(unsigned short)*8);
printf ("Size of signed short data type %d bits\n",sizeof(int)*8);
printf ("Size of unsigned short data type %d bits\n",sizeof(unsigned int)*8);
}

1
3. Write down the size of the following data types of the C programming language from the output of the
previous program:
Data Type Data Size (bits)
Short : 16 bits
Unsigned Short : 16 bits

Int : 32 bits
Long : 32 bits

4. Using pen and paper method, determine the range of the data type:
Upper Range Lower Range
16-bit Short : 32,767 -32,767

16-bit Unsigned Short : 65,535 0

32-bit Signed Integer : 2,147,483,647 -2,147,483,647

32-bit Unsigned Integer : 4,294,967,295 0

Yes
5. Is the information from the Internet the same with the output of the program? ___________________
6. What is the implication of the number of bits of a data type in relation to data size and range?

In summary, the number of bits in a data type affects the size of the data in memory, the range of
values it can represent, the precision of representation, and the performance of operations involving
that data type. The selection of an appropriate data type depends on the requirements of the specific
application, considering factors such as memory usage, range of values, and computational efficiency.

7. Using pen and paper method (calculators are allowed), write down the value in hexadecimal (write
down XXX if cannot be represented):
Value 32-bit Signed Integer 32-bit Unsigned Integer
234 : EA EA

-234 : FFFFFF16 FFFFFF16

123456 : 1E240 1E240

-123456 : FFFE1DC0 FFFE1DC0

2147483647 : 7FFFFFFF 7FFFFFFF

-2147483647 : 80000001 80000001

2
8. Using Dev C++ compiler, execute the program and enter values from the previous procedure:
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or
input loop */

int main(int argc, char *argv[]) {

int a;

printf("Enter an integer value: ");


scanf("%d", &a);

printf ("Number of bits: %d bits\n", sizeof(int)*8);


printf ("A in signed integer %d\n", a);
printf ("A in unsigned integer %u\n",a);
printf ("A in hexdecimal %X\n",a);

return 0;
}

9. Write down the output of the C program in procedure no 3:


Value Signed Integer Output Unsigned Integer Output Hexadecimal Output
234 234 EA
234 :
-234 4294967062 FFFFFF16
-234 :
123456 123456 1E240
123456 :
-123456 4294843840 FFFE1DC0
-123456 :
2147483647 2147483647 7FFFFFFF
2147483647 :
-2147483647 2147483647 80000001
-2147483647 :

10. Did the program output mathematically correct values for signed integer? Why?

Yes because it matched my answers in number 7.

3
11. Did the program output mathematically correct values for unsigned integer? Why?

Yes because it matched my answers in number 7.

12. How did the C programming language represent negative numbers?

It interprets the bits of the signed integer as an unsigned integer making changes in the representation

13. Analyze the program below and determine the output if the value to be entered for variable “a“ is
32767 (Note: Do not execute yet!):
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or
input loop */

int main(int argc, char *argv[]) {

short a;

printf("Enter an integer value: ");


scanf("%d", &a);

a=a+1;

printf ("Number of bits: %d bits\n", sizeof(short)*8);


printf ("A in signed integer %d\n", a);
printf ("A in unsigned integer %u\n",a);
printf ("A in hexdecimal %X\n",a);

return 0;
}

32768
14. What is the probable value of variable “a” after the code “a=a+1” in the program? ________________
15. Calculate for the binary value of 32767 in the specified notation. Write down the probable display
value of the program:
Value Signed Integer Output Unsigned Integer Output Hexadecimal Output

32767 : 32767 32767 7FFF

4
16. Execute the program in procedure no. 13 using Dev C++ and write down the output:
Value Signed Integer Output Unsigned Integer Output Hexadecimal Output

32767 : -32768 4294934528 FFFF8000

17. Is the output of the C program for signed integer correct? Was there an overflow? Why?

Yes but there is an overflow because of the line code "a=a+1" and the maximum value for signed
short is only 32767.

18. Is the output of the C program for unsigned integer correct? Why is the output such a high value?

Yes. The outcome of the output is because it's converted into it's positive equivalent by %u.

19. Analyze the program below and determine the output if the value to be entered for variable “a“ is
32767 (Note: Do not execute yet!):
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or
input loop */

int main(int argc, char *argv[]) {

unsigned short a;

printf("Enter an integer value: ");


scanf("%d", &a);

a=a+1;

printf ("Number of bits: %d bits\n", sizeof(unsigned short)*8);


printf ("A in signed integer %d\n", a);
printf ("A in unsigned integer %u\n",a);
printf ("A in hexdecimal %X\n",a);

return 0;
}

5
20. Execute the program in procedure no. 19 using Dev C++ and write down the output:
Value Signed Integer Output Unsigned Integer Output Hexadecimal Output

32767 : 32768 32768 8000

Yes
21. Is the output of the C program different from the earlier program? _____________
22. Explain why the program has a different output from before?

It's different because we are now using the unsigned short data type which is different from signed
short data type.

23. Run the program in procedure no. 19 and use the value 65535 as the input. Write down the result of
the output.
Value Signed Integer Output Unsigned Integer Output Hexadecimal Output
65357 65357 FF4D
65356 :

24. Is the output of the program different from last time? Explain why.

Yes because the range of signed and unsigned short is different which is causing the output of the
program to be different from one another.

25. What can you conclude from the output of the programs in procedure no.8, 13, and 19 in relation to
data size, range, accuracy and overflow?

What I can conclude from this activity is that we need to be careful in assigning numbers to different
data types because it may result to overflow or inconsistencies in the output of the program because
of the limitations of each data types in range.

26. How and why should you take data size into account when writing programs?

Taking data size into account is essential for writing efficient, portable, and correct programs.
It involves choosing appropriate data types based on the requirements of your program and
understanding how data is represented in memory on the target platform.

You might also like