0% found this document useful (0 votes)
26 views10 pages

SLP Week01 Lab Cfundamentals

This document describes a series of C++ programming exercises to understand data types and memory usage. The objectives are to understand how characters, integers, arrays and pointers are stored in memory. A series of programs are presented to: 1) Determine the memory size of different data types like char, int, float, arrays of each type. 2) Display decimal, octal and hexadecimal representations of characters. 3) Dump the memory contents of variables to see how they are stored. 4) Demonstrate the use of arrays and pointers by printing values and addresses. The student is asked to modify sample programs, observe outputs, and fill in tables summarizing data type sizes and memory addresses of variables

Uploaded by

Web TachTalk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

SLP Week01 Lab Cfundamentals

This document describes a series of C++ programming exercises to understand data types and memory usage. The objectives are to understand how characters, integers, arrays and pointers are stored in memory. A series of programs are presented to: 1) Determine the memory size of different data types like char, int, float, arrays of each type. 2) Display decimal, octal and hexadecimal representations of characters. 3) Dump the memory contents of variables to see how they are stored. 4) Demonstrate the use of arrays and pointers by printing values and addresses. The student is asked to modify sample programs, observe outputs, and fill in tables summarizing data type sizes and memory addresses of variables

Uploaded by

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

System Level Programming Lab

C++ fundamentals and Type conversion


Student ID _______2018521460123______________
Student Name _____MD RIAZ HASAN_______________
Start Time __________09/08/2020______________
Finish Time __________09/12/2020______________

1. Objectives:
To write simple C++ programs covering:
 Understand how a character is stored in memory;
 Understand how an integer is stored in memory;
 Understand array and pointer; and
 Perform conversion from eight bits (character) to eight bytes to be displayed on
screen.

2. Type and Format in memory – character, integer, short and float

2.1
This exercise is to determine the memory size of different type.

lab1_1.cpp
//Determine the memory size of declaration and variable type
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char c;
char s[128];
short i;
short n[64];
printf("%3d %3d\n", sizeof(c), sizeof(char));
printf("%3d %3d\n", sizeof(s), sizeof(char[128]));
printf("%3d %3d\n", sizeof(i), sizeof(short));
printf("%3d %3d\n", sizeof(n), sizeof(short[64]));
}

The display is:

Now execute the above program and fill in the following:


2021/1/1 - - 1
System Level Programming Lab

Type Size in byte Size in bit


Short 2 16
Char 1 8
Short[32] 64 512
Char[64] 64 512

2.2
Use the approach of the above, determine the memory size of integer, float, integer[128],
float[16] (lab1_2.cpp)

Write a program to verify your answer and fill in the following: [hint, use sizeof(…) ]
Type Size in byte Size in bit
Int 4 32
Float 4 32
Int[128] 512 4096
Float[16] 64 512

Write down your codes below:

#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
int c;
int s[128];
float i;
float n[16];
printf("%3d %3d\n", sizeof(c), sizeof(int));
printf("%3d %3d\n", sizeof(s), sizeof(int[128]));
printf("%3d %3d\n", sizeof(i), sizeof(float));
printf("%3d %3d\n", sizeof(n), sizeof(float[16]));
}

Write down your expression amongst the difference in size between character, integer, float,
short, char[8], int[8], short[8], float[8]

character < short < integer = float < char[8] < short[8] < int[8] = float[8]

2021/1/1 - - 2
System Level Programming Lab
char = 1 byte = 8 bit
int = 4 byte = 32 bit
float = 4 byte = 32 bit
short = 2 byte = 16 bit
char[8] = 8 byte = 64 bit
int[8] = 32 byte = 256 bit
short[8] = 16 byte = 128 bit
float[8] = 32 byte = 256 bit

2.3
This exercise is to display the decimal and octal values so that you know how it is stored in
memory.

lab1_3.cpp
//Determine the memory size of declaration and variable type
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
for (char i = 30; i <41; ++i)
printf("i: dec=%d oct=%o \n", i, i);
}

the output is:

2021/1/1 - - 3
System Level Programming Lab
Now modify the above programme to display decimal, octal, hex and unsigned as well.
(Hints: hex, %x, unsigned %u)

Write down your codes below:

#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
for (char i = 30; i <41; ++i)
printf("i: dec=%d oct=%o hex=%x unsigned=%u \n", i, i, i, i);
}

Write down the output of the first 4 lines so that you understand the difference among them.

i: dec=30 oct=36 hex=1e unsigned=30


i: dec=31 oct=37 hex=1f unsigned=31
i: dec=32 oct=40 hex=20 unsigned=32
i: dec=33 oct=41 hex=21 unsigned=33

2.4
This exercise is to dump the content of memory of different type so that you know how it is
stored in memory.
lab1_4.cpp
//Determine the memory size of declaration and variable type
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char a = '0'; // in hex 0x30, not 0
int i = 0x00000013; //in decimal is (1 x 16 + 3) = 19
short j = 18; //occupies two bytes,
float f = 1.25; //occupies 4 bytes
}

In order to display, you have to set a break point by pressing “F9” beside the line, the
program will display a red circle as follows:

2021/1/1 - - 4
System Level Programming Lab

Now, type the address of each variable as shown in the left-bottom frame to locate whether
they are. Here, we dump the address of variable ‘a’. Since, visual C++ reserves 4 bytes but
char c uses one byte, you can see that the rest three bytes are set to CC CC CC (means
10101010 10101010 10101010 in binary, the default setting).

1 memory location of a is 0x0065fdf4, you have to type &a


2 then in the Address, key in 0x0065fdf4,
3 it displays 30 CC CC CC

0x0065FDF4, the hexadecimal is 0x30 (ASCII)

2021/1/1 - - 5
System Level Programming Lab
Now, type and execute the above program and fill in the following, note that your addresses
might be different from what I have below.

Variable Memory address Hexadecimal value


a 0x0065FDF4
i
j
f

3. Array and Pointer

3.1

The following is a very simple array definition. It defines an array called char a[4] which
occupies 4 bytes, the value is 0x30 0x31 0x32 0x00. Note that 0x30 (ASCII) is 0 in decimal.
The last character must be terminated by 0x00 or is called null. (ref. lab1_5.cpp)

lab1_5.cpp
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char a[4] = "012";
}

0x30 Null, 0x00


or 0

Based on the above, note that the address of array a is 0x0065fdf4. Now fill in the following:

Address Type Value


0x0065FF4 a[0] 0x30

2021/1/1 - - 6
System Level Programming Lab
3.2
The following program will display the content of an array a[11].

#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char a[11] = "0123456789";
for (int i = 0; i <10; ++i)
printf("The value of %d is %c in hex %x \n", i, a[i], a[i]);

Now modify the above program to display the address of each location in hexadecimal and
the value a[12], a[13], explain why you can display a[12] and [13] as you only defined them
up to a[10] as follows. (Hint: The address of a[0] is &a[0])

(note that your value might be different.)


Write down the program

#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char a[11] = "0123456789";
for (int i = 0; i <14; ++i)
printf("The value of %d is %x in hex %x \n", i, &a[i], a[i]);
printf("The value of %d is %x in hex %x \n", 12, &a[12], a[12]);
printf("The value of %d is %x in hex %x \n", 13, &a[13], a[13]);

2021/1/1 - - 7
System Level Programming Lab
Write down your explanation (hint: dump the memory location and look at the location of
a[11], a[12] and a[13] etc. you then realise why?)

The value of 11 is 12ff7f in hex ffffffcc


The value of 12 is 12ff80 in hex ffffffc0
The value of 13 is 12ff81 in hex ffffffff

So, a[11],a[12] and a[13] beyond boundary

3.3
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char a[11] = "0123456789";
char *ptr;
ptr = &a[0]; // it can be &a with same effect
for (int i = 0; i <11; ++i, ++ptr)
printf("The value of %d is %x in hex %x \n", i, ptr, *ptr);
}

Now explain the meaning of the following of the above program.

%x: output integer in hexadecimal form


\n: equivalent to the enter key
i: integer variable
ptr: pointer variable, representing the address

2021/1/1 - - 8
System Level Programming Lab
*ptr: the value of the pointer to the address

3.4

The following is about the integer array.

#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
int a[7] = {12, 21, 31, 41, 51, 61};
for (int i = 0; i <6; ++i)
printf("The value of %d is %x in hex %d \n", i, &a[i], a[i]);

}
Now explain increment of address is 4 instead of 1.

Because an Integer takes 4 bytes in memory. Instead of a Char, which takes 1 byte, the next
integer is stored in a memory 4 addresses apart.

3.5

Modify the above program with the same output with pointer instead of array a[].

Write down the program

#include <iostream.h>
#include <stdio.h>#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
int a[7] = { 12, 21, 31, 41, 51, 61 };
int *ptr;
ptr = &a[0];

2021/1/1 - - 9
System Level Programming Lab
for (int i = 0; i < 6; ++i, ++ptr)
printf("The value of %d is %x in hex %d \n", i, ptr, *ptr);
}

2021/1/1 - - 10

You might also like