SLP Week01 Lab Cfundamentals
SLP Week01 Lab Cfundamentals
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.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]));
}
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
#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);
}
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)
#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.
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).
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.
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";
}
Based on the above, note that the address of array a is 0x0065fdf4. Now fill in the following:
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])
#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?)
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);
}
2021/1/1 - - 8
System Level Programming Lab
*ptr: the value of the pointer to the address
3.4
#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[].
#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