Skip to content

Programming in C

Jorge Pérez edited this page Dec 5, 2024 · 10 revisions

Courses you can take:

Bitwise operators

We can organize eight bits into a single number, but these bits can also represent eight different things. For example, they can be wired to eight different LED lights. In fact, when you place values in special memory locations called memory-mapped I/O registers, these values turn on or off I/O pins. Since there are eight bits to the register’s byte, a single register can control eight LEDs. (Or, in our case, one LED and seven pins on which we can add more LEDs.)

Operators

This operators allow you to manipulate how each bit of your variable behaves.

AND &

Option B8 B7 B6 B5 B4 B3 B2 B1 B0
Var_A 1 1 0 0 0 0 0 1 0
Var_B 1 0 0 1 0 0 0 1 0
Reslt 1 0 0 0 0 0 0 1 0

OR |

Option B8 B7 B6 B5 B4 B3 B2 B1 B0
Var_A 1 1 0 0 0 0 0 1 0
Var_B 1 0 0 1 0 0 0 1 0
Reslt 1 1 0 1 0 0 0 1 0

XOR ^

Option B8 B7 B6 B5 B4 B3 B2 B1 B0
Var_A 1 1 0 0 0 0 0 1 0
Var_B 1 0 0 1 0 0 0 1 0
Reslt 0 1 0 1 0 0 0 0 0

NOT ~

Option B8 B7 B6 B5 B4 B3 B2 B1 B0
Var_A 1 1 0 0 0 0 0 1 0
Reslt 0 0 1 1 1 1 1 0 1

Pointers

Variables: locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory

For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.

When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored.

image

Address of operator (&)

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator.

foo = &myvar;

Deference Operator *

Pointers can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*).

myvar = 25;

foo = &myvar;

bar = *foo;

image

Declaring Pointers

Due to the ability of a pointer to directly refer to the value that it points to, a pointer has different properties when it points to a char than when it points to an int or a float. Once dereferenced, the type needs to be known. And for that, the declaration of a pointer needs to include the data type the pointer is going to point to.

The declaration of pointers follows this syntax:

type * name;

image


Structs

Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. image image

Structures Declaration

We specify its member variables along with their datatype.

image

The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration. To use structure in our program, we have to define its instance. We can do that by creating variables of the structure type.

image

We can access structure members by using the [( . ) dot operator.] structure_name.member1; strcuture_name.member2;

Initialize structure members

Default Initialization

struct Point { int x; int y; }; struct Point p = {0}; // Both x and y are initialized to 0

Initialization using Assignment Operator

struct structure_name str; str.member1 = value1; str.member2 = value2; str.member3 = value3;

Initialization using Initializer List

struct structure_name str = { value1, value2, value3 };

typedef

The typedef keyword is used to define an alias for the already existing datatype. In structures, we have to use the struct keyword along with the structure name to define the variables. Sometimes, this increases the length and complexity of the code. We can use the typedef to define some new shorter name for the structure.

image

Struct vs Union

A union is a user-defined type in which all members share the same memory location. This definition means that at any given time, a union can contain no more than one object from its list of members. image


Enum

An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.

image