You are on page 1of 26

BASIC COMPUTER ARCHITECTURE

Introduction to SYNTAX
C/C++ FIRST PROGRAM
DATA TYPES
Basic Computer Main components of a
Architecture computer

The parts of a C
Syntax program

Topics
Write and run our first
First Program program

Variable
Data Types representation in
memory
General Purpose
Computer
Processor
I/O Drivers
PCIe Ethernet
Wifi
OS
Memory
Management
Thread File system Drivers
Scheduling Drivers
Storage Graphic
File System USB
Drivers (HDD, SDD) Card
Program code
+ variables
Main Memory
(RAM)
Embedded
Microcontroller
Processor Program Program
code Data Comm Comm Comm Ports
FLASH SDRAM UART SPI I2C GPIO
OS Memory Memory
Optional
Memory
Management
Thread
Scheduling External
File System Storage
Drivers
(EEPROM
FLASH, SD
CARD)
Basic Computer Main components of
Architecture a computer

The parts of a C
Syntax program

Topics
Write and run our
First Program first program

Variable
Data Types representation in
memory
C Program Example
A C program basically consists of the following parts −
•Preprocessor Commands
•Functions #include <stdio.h>
•Variables
•Statements & Expressions int main() {
/* my first program in C */
•Comments printf("Hello, World! \n");
return 0;
}
Syntax
Tokens and Statements
Statements consists of a list of Tokens printf("Hello, World! \n");

Tokens
◦ Keywords printf
◦ (
Identifiers
"Hello, World! \n"
◦ Constants )
◦ Variables ;
◦ Symbols

Semicolon ‘;’ is the statement terminator


Program is a sequence of statements
Syntax
Keywords
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
Syntax
Identifiers
Name for
◦ Functions
◦ Constants
◦ Variables mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
◦ User defined items

Can contain
◦ Letters A..Z
◦ Letters a..z
◦ Underscores ‘_’
◦ Digits 0..9

Cannot contain white spaces or operators: + , - = . / < > @ # $ % ^ & ! ~ ‘ “ [ ] { } ( )


Syntax
Comments
Block comments
/*
Line 1
Line 2
Line 3
*/

Line comments
//Line1
//Line 2
Syntax
White Spaces
White space
◦ blanks
◦ tabs
int age;
◦ newline characters
◦ Comments

Lines of white spaces are ignored


Necessary to separate
◦ Identifiers
◦ key words fruit = apples + oranges; // get the total fruit

fruit=apples+oranges;//get the total fruit


Basic Computer Main components of
Architecture a computer

The parts of a C
Syntax program

Topics
Write and run our
First Program first program

Variable
Data Types representation in
memory
First Program
Exercise
Create a Visual Studio C++ Console application project

Write a program that prints “Hello world”

Change the program to


◦ Read 2 numbers
◦ Add the numbers
◦ Print the result
Basic Computer Main components of
Architecture a computer

The parts of a C
Syntax program

Topics
Write and run our
First Program first program

Variable
Data Types representation in
memory
Memory Organization
Smallest addressable unit is a Byte
Memory is an array of bytes
Address size dictates address space
◦ Memory may be smaller than the address space

A byte has 8 bits


Variable types -> memory footprint
Program memory
◦ Code
◦ Data
◦ Stack
◦ Heap
Variable Types
Basic types
◦ Integer
◦ Floating point

Enumerated types
◦ A finite subset of the integer type

Void type
◦ No value

Derived types
◦ Pointers
◦ Arrays
◦ Structures/Unions
◦ Function pointers
Basic Types
Integers
Sign Modifier Type Size [B] Range
signed char 1 -128 to 127
unsigned char 1 0 to 255
signed short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65535
signed int 2/4
unsigned int 2/4
signed long int 4/8 -2,147,483,648 to 2,147,483,647
unsigned long int 4/8  0 to 4,294,967,295
signed long long int 8 -9223372036854775808 to 9223372036854775807
unsigned long long int 8 0 to 18446744073709551615
Removing ambiguity
Include the stdint.h Include the stdbool.h
Types Types
◦ int8_t ◦ bool
◦ uint8_t
Values
◦ int16_t ◦ true
◦ uint16_t ◦ false
◦ int 32_t
◦ uint32_t
◦ int64_t
◦ uint64_t
Integer representation
Unsigned
Integer representation
Unsigned
Hexadecimal codes
0xFFA50187

FF = 1111 1111
A5 = 1010 0101
01 = 0000 0001
87 = 1000 0111

See calculator in programmer mode


Integer representation
Signed
Use first bit for sign
◦ Two codes for +0/-0
◦ 00000000/10000000

Two’s complement
◦ 0 = 0000 0000
◦ -1 = 1111 1111
◦ -128 = 1000 0000
◦ 127 = 0111 1111

Example
◦ 010 -> 101 + 1 -> 110
Floating point representation
IEEE 754
Type Size [B] Range Precision
float 4 1.2E-38 to 3.4E+38 6 decimal places
double 8 2.3E-308 to 1.7E+308 15 decimal places
long double 10 3.4E-4932 to 1.1E+4932 19 decimal places
Endianness
Homework
Write a program that prints the range for the basic integer and floating-point data types
◦ See material on next slide
Materials
C Tutorial:
https://www.tutorialspoint.com/cprogramming/index.htm
C/C++ Library description
https://www.cplusplus.com/

You might also like