C
Compiling
- MinGW:
gcc app.c -o app.exe
To compile an EXE from Linux:
x86_64-w64-mingw32-gcc hello.c -o hello.exe
i686-w64-mingw32-gcc hello.c -lws2_32 -o hello.exe
To compile an ELF from Windows:
x86_64-linux-gnu-gcc hello.c -o hello
- VSCode:
C/C++ Extension
Terminal > Run Build Task > gcc build active file
- Visual Studio:
Create pertinent project, then Build > Release > Build Solution
If you want to debug it, press the play debug boton.
Variables
These are named entities in your program that are used to store data of a particular type. When you declare a variable, you specify its data type, which tells the compiler how much memory to allocate for the variable and what kind of data it will hold.
Data Types
These are the definitions provided by the programming language to tell the compiler or interpreter how you intend to use the data. Data types determine the size and layout of the data storage; the range of values that can be stored within that data type; and the set of operations that can be applied to the data type.
Arrays
An array is a collection of elements, all of the same type, stored in contiguous memory locations. It serves to organize and manage multiple items using a single identifier, facilitating efficient data manipulation and access.
Printf
Numbers
Constants
Getting User Input
Structures
Structures or Structs are user-defined data types that allow the programmer to group related data items of different data types into a single unit. Structs can be used to store data related to a particular object. Structs help organize large amounts of related data in a way that can be easily accessed and manipulated. Each item within a struct is called a "member" or "element".
- Structure Declaration
Structures can be declared with the use of typedef
keyword to give a structure an alias.
For example, the structure below is created with the name _STRUCTURE_NAME
but typedef
adds two other names, STRUCTURE_NAME
and *PSTRUCTURE_NAME
.
The STRUCTURE_NAME
alias refers to the structure name, whereas PSTRUCTURE_NAME
represents a pointer to that structure. Microsoft generally uses the P
prefix to indicate a pointer type.
- Structure Initialization
Initializing the actual structure type (following the previous example, it would be the same when using _STRUCTURE_NAME
or STRUCTURE_NAME
):
Initializing the structure pointer, PSTRUCTURE_NAME
.
- Initializing and Accessing Structures Members
To initialize structure's members directly via the dot operator (.
):
To initialize structure's members using designated initializer syntax to specify which members of the structure to initialize:
To access and initialize a structure through its pointer via the arrow operator (->
):
structpointer->ID
is equivalent to (*structpointer).ID
.
Enumeration
The enum or enumeration data type is used to define a set of named constants.
The compiler automatically assigns values to the constants, starting with 0 and increasing by 1 for each subsequent constant. In this course, enums can be seen representing the state of specific data, error codes or return values.
Enum lists cannot be modified or accessed using the dot (.) operator. Instead, each element is accessed directly using its named constant value.
Union
Union is a data type that permits the storage of various data types in the same memory location. Unions provide an efficient way to use a single memory location for multiple purposes.
To access the members of a union in C, one can use the dot operator.
Bitwise Operators
Bitwise operators are operators that manipulate the individual bits of a binary value, performing operations on each corresponding bit position.
Right shift (
>>
):
The right shift (>>) operator is used to shift the bits of a binary number to the right by a specified number of positions.
For example, 10100111
shifted right by 2, to become 00101001
.
Left shift (
<<
):
The left shift (<<) operators is used to shift the bits of a binary number to the left by a specified number of positions.
For example, 10100111
shifted left by 2, to become 10011100
.
Bitwise OR (
|
):
Logical operation that involves two binary values at the bit level. It evaluates each bit of the first operand against the corresponding bit of the second operand, generating a new binary value. The new binary value contains a 1 in any bit position where either one or both of the corresponding bits in the original values are 1 (only 0 is the output when both inputs are 0).
Bitwise AND (
&
):
Logical operation that involves two binary values at the bit level. This operation sets the bits of the new binary value to 1 only in the case where the corresponding bits of both input operands are 1.
Bitwise XOR (
^
):
XOR operation (also known as exclusive OR) is a logical operation that involves two binary values at the bit level. If only one of the bits is 1, the result in each position is 1. Conversely, if both bits are 0 or 1, the output is 0.
Bitwise NOT (
~
):
The bitwise NOT operation takes one binary number and flips all its bits. In other words, it changes all 0s to 1s and all 1s to 0s.
- Passing By Value
Method in which the value of the object is copied and the function can only modify its local copy of the object's value, not the original object itself, allowing it to operate on local copies without changing the original variables.
Passing By Reference
"Passing by reference" means when you give a function something (an argument), you're not giving it a copy of that thing's value. Instead, you're giving it a way to find and interact with the original thing directly. It's like telling someone, "Here's where you can find this thing," rather than giving them a copy of the thing itself.
So, when you pass something by reference in a function, you're sharing its memory address (a way to locate it in the computer's memory) with the function. This allows the function to work with the original thing without making its own copy. This is useful when you want to change the original thing inside the function and have those changes affect the original outside the function.
Dereferencing is the act of accessing the actual value stored at the memory location pointed to by a pointer. "Passing by reference" is more about how you pass arguments to a function, while "dereferencing" is about how you access values through pointers.
Getting the memory address of a variable:
Passing variables by reference to functions:
Header Files
For example, for debugging:
To create a function or functions in a separate .c
file to be called by another file (i.e. the main):
Hola.c
:
Hola.h
:
Last updated