PPP Notes
PPP Notes
- Source code files typically have a `.c` extension and are written using a text editor.
- Before a program can be executed by the computer, the source code needs to be compiled
into machine code (executable code) that the computer understands. This is done by a
compiler program.
- A variable is a named storage location in memory that holds a value. It acts as a container
for data used by your program.
- Variables must be declared before they can be used. The declaration specifies the variable's
name and its data type.
- Data types define the kind of data a variable can store. Common data types in C include:
Example:
float pi = 3.14159; // Declares a float variable named 'pi' and initializes it with a value
Memory Allocation:
- Memory allocation refers to the process of assigning a specific amount of memory space in
the computer's memory to store a variable or other data structure.
- In C, there are two main types of memory allocation:
- Static allocation: Memory is allocated for variables declared at the global or static
scope when the program starts. This memory remains allocated for the entire program
execution.
- Dynamic allocation: Memory is allocated during program execution using functions like
`malloc`, `calloc`, and `realloc`. This allows you to allocate memory for data structures whose
size might not be known beforehand or that need to be resized during runtime. However, it's
important to remember to **free** the dynamically allocated memory using the `free` function
to avoid memory leaks.
- Syntax errors: These are errors in the code's structure that violate the grammatical rules of
the C language. Examples include missing semicolons, mismatched parentheses, or typos in
keywords.
- Logical errors: These are errors in the program's logic that cause it to produce incorrect
results even though the code compiles and runs without syntax errors. For instance, using an
incorrect formula or forgetting to include a necessary step in the logic can lead to logical
errors.
- Syntax errors are usually caught by the compiler during compilation, while logical errors can
be trickier to find and may only manifest during program execution.
- Object code: When you compile your C source code, the compiler translates it into machine
code (object code) that can be directly understood by the computer's processor.
- Executable code: The object code file(s) generated by the compiler may need to be linked
with library functions (pre-written code) to create a final executable file. This executable file
can then be run on your computer. The linking process is typically handled by a linker program.
- Object (in some contexts): In C, an object can also refer to a data structure or an instance of
a user-defined type (struct, union, etc.).