Chapter2_Basics of C and C++_part1
Chapter2_Basics of C and C++_part1
© HĐC 2024.1
Content
return 0;
}
Object program
Executable program
Break point
Break point
Registers
Program Data
Memory Memory
Program Memory
Non-volatile type
Stores instructions in memory slots
CPU reads and executes instructions
Data MemoryVolatile type
Stores temporary data in memory slots
Very fast
Cache, Registers, and RAM (SRAM, DRAM, DDRAM...)
© HĐC 2024.1 Chapter 2: Basic of C & C++ 13
Memory allocation of C/C++ Programs
Operating system
Other programs
Top of the stack
Code (Text) SP
n
Global variables Matrix_A
f
Free memory space (Heap) k
i
Stack (arguments, local count
variables)
a
Characters Let’s find the full table and extended table yourself!
float in C
Variable name
int months;
Proper values: 1, 17, -32, 0 Not like these 1.5, 2.0, ‘A’
double pi;
Values are numbers with decimal point: 3.14, -27.5, 6.02e23,
5.0 Not like this 3
char first_initial, marital_status;
Represent the character of the keyboards: ‘a’, ‘b’, ‘M’, ‘0’ , ‘9’ ,
‘#’ , ‘ ’ Not like this “Bill”
Variable can be declared with initial values:
int month = 1;
double weight = 62.6;
char c = “N”;
Global variable
Local variables
Paramter
int main(void)
{
double fahrenheit, celsius;
return 0;
}
int main(void)
{
double fahrenheit, celsius;
printf("Enter a Fahrenheit temperature: ");
scanf("%lf", &fahrenheit);
celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
printf("That equals %f degrees Celsius.", celsius);
return 0;
}
#include <stdio.h>
int main(void)
{
double fahrenheit, celsius;
printf("Enter a Fahrenheit temperature: ");
scanf("%lf", &fahrenheit);
celsius = fahrenheit - 32.0;
celsius = celsius * 5.0 / 9.0;
printf("That equals %f degrees Celsius.", celsius);
return 0;
}
Mathematical formula
An operation in C
(- b + sqrt (b*b - 4.0*a*c) ) / (2.0*a)
count = 10;
Types Values
Enumeration
Constant
Pointer
Array
Reference (C++)
j, *p j, *p i, *p i, *p
0 3.0 In C:
1 3.8 sensor[0] = 3.0;
double 1.7
.
sensor[7]; . 2.0 sensor[6] = 3.2;
. 2.5 2.0*sensor[3] = 4.0;
2.1
6 3.2 ...
#define DATA_SIZE 7
int main() {
double sensor[DATA_SIZE];
int index;
index = 9;
...
sensor[index] = 3.5; /* Out of range?? */
if (0<=index && index<DATA_SIZE) {
sensor[index ] = 3.5;
} else {
printf(“Index %d out of range.\n”, index);
}
}
© HĐC 2024.1 Chapter 2: Basic of C & C++ 64
Special values
-1
This value CANNOT
-1
be a legal value
(humidity value
-1
in this case)
-1
MAX_DATA_SIZE - 1 -1
for (k = n; k >= 1; k = k - 1)
x[k] = x[k-1];
x[0] = new;
n = n + 1;
parameters
value 0 1 2 3
node 0 22 15 25 25 Expression in C:
node 1 12 12 25 20 value[0][0] = 22;
node 2 5 17 25 24
node 3 15 19 25 13 value[6][3] = 12;
node 4 2 0 25 25
2*value[3][0] = 30;
node 5 25 22 24 21
node 6 8 4 25 12
#define MAX_NODES 80
#define MAX_PARAMETERS 6
...
int value[MAX_NODES][MAX_PARAMETERS];
int num_nodes, num_params, i, j;
void main() {
int a[5]; // a has 5 elements with
// uncertain values
int* p = a; // p points to a[0]
p[0] = 1; // a[0]=1
p[1] = 2; // a[1]=2
p+= 2; // now p points to a[2]
p[0] = 3; // a[2]=3
p[1] = 4; // a[3]=4
p[3] = 6;// a[5]=6, Now is a BIG BIG problem!
}
void main() {
Time classTime = {6,45,0};
Time lunchTime = {12};
Date myBirthday, yourBirthday = {30,4,1975};
Student I= {"Nguyen Van A", {2,9,1975}};
//...
}
struct Time {
int hour = 0; // error, initialization not allowed
int minute, // error, use semicolon (;) instead
int second // error, missing semicolon (;)
} // error, missing semicolon (;)
//...
void main() {
Date d;
d = {11, 9, 2001}; // error, {...} is an initialization list, not a structure
Date.hour = 0; // error, Date is a type, not a var.
struct Date2{ int day, month, year; };
Date2 d2 = d; // error, Date is not compatible to Date2
}
union test3 {
int arr[10];
char y;
} Test3;
int main() {
printf(“sizeof(test1) = %lu, sizeof(test2) = %lu, sizeof(test3) = %lu,
sizeof(Test1), sizeof(Test2), sizeof(Test3));
return 0;
}