'Anyone can write code which a machine can understand - the trick is to write code which another human can understand.' -- Martin Fowler
Introduction
- What is computer programming?
- What is a computer program?
- What is a programming language?
- Differences between languages (Currently popular languages Don't take it too seriously!)
- PYPL PopularitY of Programming Language
- Compilers vs. Interpreters
- Creating a program (Think Lego™ Blocks)
- Editing source (text) files
- Compiling source files into object (binary) files
- Linking object files into executable (binary) files
- Executing the program
- Central Processing Unit (CPU)
- Arithmetic and Logic Units
- Registers
- Cache memory
- Main memory
AirPods Support. Forgot Apple ID or password. Billing and subscriptions. Trade in with Apple. Turn an eligible device into credit towards a new one, or recycle it for free. Apple Trade In is good for you and the planet. In 2001, Apple released Mac OS X, a modern Unix-based operating system which was later rebranded to simply OS X in 2012, and then macOS in 2016. Its final version was macOS Catalina, as Apple went on to release macOS 11 in 2020. The current version is macOS Big Sur, first released on November 12, 2020. Mac OS X Tiger (version 10.4) is the fifth major release of macOS, Apple's desktop and server operating system for Mac computers. Tiger was released to the public on April 29, 2005 for US$ 129.95 as the successor to Mac OS X 10.3 Panther. The first beta of OS X El Capitan was released to developers shortly following the WWDC keynote on June 8, 2015. The first public beta was made available on July 9, 2015. There were multiple betas released after the keynote. OS X El Capitan was released to end users on September 30, 2015, as a free upgrade through the Mac App Store. 👍 In this video, I'm going to show you how to recover data after you reinstalled or updated your MacOS, or after you have formatted the hard disk.
First C Programs (The Chicken and The Egg)
- A C program is essentially a collection of one or more functions.
- There must be a function named main; it must be in all lowercase.
- main is the program's starting point; it can call other functions by name
- There must be exactly one function named main in every program.
Students that think that int main(void) can be changed to int main() are required to read this, as I'm not going to spend any more time on it. (Yes, I know that some good textbooks interchanges them, but you shouldn't.)
Keep it alive (kkrac) mac os. A second program in C that actually does something: Output:
Adding line numbers for clarity. They are not (and cannot be) present in the actual code. Each non-blank line above has significant meaning to the C compiler. Look at stdio.h to see what the pre-processor adds.Editing, Preprocessing, Compiling, Linking, and Executing
Edit/Preprocess/Compile/Execute Loop | Edit/Preprocess/Compile/Execute Loop Extended |
---|
Contrasting Languages at 3 Levels
Let's say we have 4 variables (just like in algebra), where a = 1, b = 2, c = 3, d = 4. We want to set a fifth one, e, to ab(c + d). We can rewrite the multiplication explicitly asa ⋅ b ⋅ (c + d)This will result in e having the value 14.
C code (partial program)
Assembly language (16-bit Intel x86)
Assembly language (compiler-generated) | Same Assembly language with comments |
---|
Hand-coded assembler (32-bit 80386 using GNU's assembler, comments start with # and are in bold)
Here are two other assembler samples output from Microsoft's compiler and GNU's compiler.Machine language (Intel 80386)
Hexadecimal dump (base 16) | Octal dump (base 8) |
---|
Binary dump (base 2) |
---|
Now, which language would you rather work with?
Simple calculation:
Simplified view at runtime showing 3 variables in memory, a CPU with 4 registers and an Arithmetic-Logic Unit: Assembly code (generated by the GNU C compiler, comments added):Hopefully, you can see why people prefer to program in C rather than assembly. The end result is the same, but the amount of effort required from the C programmer is significantly less.
Putting It All Together Lost legend mac os.
Step 1: Edit
Create a text file for this C code named simple.c. The size of this file is about 120 bytes (although this depends on the operating system and the type of whitespace used). You can use any text editor like, such as Notepad++ (Windows only) or Geany (Windows, OS X, Linux, and others).
Step 2: Compile (with Preprocess)
The source code (text) is compiled into object or machine code (binary) and saved in a file named simple.o. The size of this file will probably be between 500 and 2000 bytes (depends on the compiler, version, and options used). Using
Step 3: Link
The object file is linked (combined) with other object code and saved in a file named simple.exe. (The .exe is a Windows requirement as other operating systems don't require it.) The size of this file is going to be tens of thousands of bytes. (Again, depends on the compiler.)
Step 3: Execute
Run the executable file by simply typing the name of the executable file (providing the .exe extension when executing it is optional under Windows):Of course, nothing appears to happen. The program did run and it did perform the calculations. There just aren't any instructions in the program that display anything for you to see.
We can modify it to use the printf (print formatted) function and display the value of e after the calculations: The C program above is a complete program that, when executed, will print the value 14 on the screen.
Step-by-Step
The commands above were really shortcuts for the entire process. Let's look at each step individually to gain a clearer understanding of what is actually taking place. Remember that the C programming language (and all of its associated tools) are case sensitive. 99% of the time, everything should be in lowercase. Exceptions will be noted below.Step 1 - Creating/Editing the source file (.c):
From the command line, invoke a text editor (e.g. notepad++) and create/edit the source file: Just type the code above (or copy/paste) and save the file.
Step 2 - Preprocessing the source file (.i):
The output file (simple.i) contains the original source code, plus a lot of code from the stdio.h header file. The -o option tells cpp what to name the output file. The output file, simple.i must follow immediately after the -o option. (The file extension .i is the conventional way to name a preprocessed file.)Note: The cpp command above stands for Cpre-processor, NOT Cplus p Boreek mac os. lus.
Step 3 - Compiling to an assembly file (.s):
Use the GNU C compiler, gcc, to translate the preprocessed file into assembly code. Notes:- The -fpreprocessed option tells gcc not to run the preprocessor because the file has already been preprocessed.
- The -S option (that's an uppercase 'S') tells gcc to only produce an assembly file instead of creating an object file.
- The -o option tells gcc what to name the output file. The output file, simple.s must follow immediately after this option. (The file extension .s is the conventional way to name an assembly file.)
Step 4 - Assembling into an object file (.o):
Use the GNU assembler (as) to assemble/convert the assembly file to object code. The -o option tells the assembler, as, to name the object file simple.o (The file extension .o is the conventional way to name an object file.) To view the disassembled code, use either of these:
Step 5 - Linking to an executable file (.exe):
Use the GNU linker, ld (that's a lowercase 'L'), to link the object file with code from the standard libraries and create an executable named simple.exeDo NOT copy and paste the text below. The command that you type must be on a single line and the text below is broken into multiple lines. This link here: linker command on one line, has the command as one long line. Copy that text instead.
Notes:- The exact command will depend on the version and location of the GCC C compiler on your computer. (This example is using version 8.1.0 of the compiler which is installed in C:mingw64.)
- This command is Windows-specific and is different on other operating systems.
- You will rarely, if ever, need to type this cryptic line manually. (I've only done it once in my long career, and it was for this example!)
- The output shown from simple.c (simple.isimple.s, simple.o, and simple.exe) was generated under Linux using version 7.1.0 of the GCC C compiler. The output generated from Mac OS X and Windows would not be exactly the same, but would be similar.
- You can clearly see why some files are called text files and others are called binary files.
Step 6 - Executing the program:
At the command prompt you just need to type the name of the executable file. On Windows type: or On Mac or Linux type (or if you're using the Windows Power Shell): Usually, you don't need to supply the .exe file extension. The linker will add that automatically (on Windows) and leave it off for Mac and Linux. By omitting the extension: the linker will 'do the right thing' based on the operating system.
Conclusion
The above step-by-step tutorial was just to show you what's going on behind-the-scenes. Normally for a simple project (meaning 99% of the time) you will simply do this: and that will perform all of the steps above.Additional Compile Options
To enable the compiler to perform a more thorough check of your source code, use the -Wallcommand line option like so: Now, if the compiler detects any potential problems or misuse of the C language, it will alert you with a warning message. For example, if I add the variable f like this: I get this warning from the compiler: or, if I add something like this: I get this warning from the compiler:
Compile and Link in One Step
If you want to perform both compile and link steps with one command, don't provide the -c option. This will compile and then link the program: However, if the compile step fails for any reason, the link step is skipped.
Other Useful Options
If you want to generate the assembly output as above, use -S option: This will produce a text file called simple.s which you can view with any text editor.If you want to generate the preprocessor output directly from gcc, use -E option:
This will produce a ton of information to the screen. To capture the output so you can view it more easily, redirect the output to a file: The causes the output to be written to a file (namedAssembly language (16-bit Intel x86)
Assembly language (compiler-generated) | Same Assembly language with comments |
---|
Hand-coded assembler (32-bit 80386 using GNU's assembler, comments start with # and are in bold)
Here are two other assembler samples output from Microsoft's compiler and GNU's compiler.Machine language (Intel 80386)
Hexadecimal dump (base 16) | Octal dump (base 8) |
---|
Binary dump (base 2) |
---|
Now, which language would you rather work with?
Simple calculation:
Simplified view at runtime showing 3 variables in memory, a CPU with 4 registers and an Arithmetic-Logic Unit: Assembly code (generated by the GNU C compiler, comments added):Hopefully, you can see why people prefer to program in C rather than assembly. The end result is the same, but the amount of effort required from the C programmer is significantly less.
Putting It All Together Lost legend mac os.
Step 1: Edit
Create a text file for this C code named simple.c. The size of this file is about 120 bytes (although this depends on the operating system and the type of whitespace used). You can use any text editor like, such as Notepad++ (Windows only) or Geany (Windows, OS X, Linux, and others).
Step 2: Compile (with Preprocess)
The source code (text) is compiled into object or machine code (binary) and saved in a file named simple.o. The size of this file will probably be between 500 and 2000 bytes (depends on the compiler, version, and options used). Using
Step 3: Link
The object file is linked (combined) with other object code and saved in a file named simple.exe. (The .exe is a Windows requirement as other operating systems don't require it.) The size of this file is going to be tens of thousands of bytes. (Again, depends on the compiler.)
Step 3: Execute
Run the executable file by simply typing the name of the executable file (providing the .exe extension when executing it is optional under Windows):Of course, nothing appears to happen. The program did run and it did perform the calculations. There just aren't any instructions in the program that display anything for you to see.
We can modify it to use the printf (print formatted) function and display the value of e after the calculations: The C program above is a complete program that, when executed, will print the value 14 on the screen.
Step-by-Step
The commands above were really shortcuts for the entire process. Let's look at each step individually to gain a clearer understanding of what is actually taking place. Remember that the C programming language (and all of its associated tools) are case sensitive. 99% of the time, everything should be in lowercase. Exceptions will be noted below.Step 1 - Creating/Editing the source file (.c):
From the command line, invoke a text editor (e.g. notepad++) and create/edit the source file: Just type the code above (or copy/paste) and save the file.
Step 2 - Preprocessing the source file (.i):
The output file (simple.i) contains the original source code, plus a lot of code from the stdio.h header file. The -o option tells cpp what to name the output file. The output file, simple.i must follow immediately after the -o option. (The file extension .i is the conventional way to name a preprocessed file.)Note: The cpp command above stands for Cpre-processor, NOT Cplus p Boreek mac os. lus.
Step 3 - Compiling to an assembly file (.s):
Use the GNU C compiler, gcc, to translate the preprocessed file into assembly code. Notes:- The -fpreprocessed option tells gcc not to run the preprocessor because the file has already been preprocessed.
- The -S option (that's an uppercase 'S') tells gcc to only produce an assembly file instead of creating an object file.
- The -o option tells gcc what to name the output file. The output file, simple.s must follow immediately after this option. (The file extension .s is the conventional way to name an assembly file.)
Step 4 - Assembling into an object file (.o):
Use the GNU assembler (as) to assemble/convert the assembly file to object code. The -o option tells the assembler, as, to name the object file simple.o (The file extension .o is the conventional way to name an object file.) To view the disassembled code, use either of these:
Step 5 - Linking to an executable file (.exe):
Use the GNU linker, ld (that's a lowercase 'L'), to link the object file with code from the standard libraries and create an executable named simple.exeDo NOT copy and paste the text below. The command that you type must be on a single line and the text below is broken into multiple lines. This link here: linker command on one line, has the command as one long line. Copy that text instead.
Notes:- The exact command will depend on the version and location of the GCC C compiler on your computer. (This example is using version 8.1.0 of the compiler which is installed in C:mingw64.)
- This command is Windows-specific and is different on other operating systems.
- You will rarely, if ever, need to type this cryptic line manually. (I've only done it once in my long career, and it was for this example!)
- The output shown from simple.c (simple.isimple.s, simple.o, and simple.exe) was generated under Linux using version 7.1.0 of the GCC C compiler. The output generated from Mac OS X and Windows would not be exactly the same, but would be similar.
- You can clearly see why some files are called text files and others are called binary files.
Step 6 - Executing the program:
At the command prompt you just need to type the name of the executable file. On Windows type: or On Mac or Linux type (or if you're using the Windows Power Shell): Usually, you don't need to supply the .exe file extension. The linker will add that automatically (on Windows) and leave it off for Mac and Linux. By omitting the extension: the linker will 'do the right thing' based on the operating system.
Conclusion
The above step-by-step tutorial was just to show you what's going on behind-the-scenes. Normally for a simple project (meaning 99% of the time) you will simply do this: and that will perform all of the steps above.Additional Compile Options
To enable the compiler to perform a more thorough check of your source code, use the -Wallcommand line option like so: Now, if the compiler detects any potential problems or misuse of the C language, it will alert you with a warning message. For example, if I add the variable f like this: I get this warning from the compiler: or, if I add something like this: I get this warning from the compiler:
Compile and Link in One Step
If you want to perform both compile and link steps with one command, don't provide the -c option. This will compile and then link the program: However, if the compile step fails for any reason, the link step is skipped.
Other Useful Options
If you want to generate the assembly output as above, use -S option: This will produce a text file called simple.s which you can view with any text editor.If you want to generate the preprocessor output directly from gcc, use -E option:
This will produce a ton of information to the screen. To capture the output so you can view it more easily, redirect the output to a file: The causes the output to be written to a file (named simple.out) instead of to the screen. This file is a text file that you can view with any text editor. Note: Modern compilers have two primary goals:
1. To verify that your code is valid C code
2. To convert/translate the C (text) code into machine (binary) code
So, the moral of the story is:
Just because your code compiles, links, (and even runs), doesn't mean it is correct. Havana club game.
Another Example
This example will show constructs such as identifiers, literal constants, defines, expressions, and several others.Without line numbers (to copy and paste): The preprocessed file named marathon.i (generated by: gcc -E marathon.c -o marathon.i)
The assembly file named marathon.s (generated by: gcc -S marathon.c -o marathon.s)
The code above uses a few arithmetic operators. Many operators correspond with the ones you've seen in algebra. Here are a few binary operators:
Operator | Meaning |
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo (Remainder) |
Computer Data Storage (Refresher)
This section is just a short refresher on the binary, decimal, and hexadecimal number systems.- Computers represent information as patterns of bits (0's and 1's), known as base 2; e.g. 1011101000010101000111100011
- People use base 10; doesn't translate well into base 2
- Hexadecimal (base 16) works well with binary; computer programmers often work in base 16
- Group the bits into sets of 4 and translate into hexadecimal using the chart below
0 | 0000 | 4 | 0100 | 8 | 1000 | C | 1100 |
1 | 0001 | 5 | 0101 | 9 | 1001 | D | 1101 |
2 | 0010 | 6 | 0110 | A | 1010 | E | 1110 |
3 | 0011 | 7 | 0111 | B | 1011 | F | 1111 |
The number above, 1011101000010101000111100011, translates into hex (BA151E3) and decimal (195,121,635) as:
Binary | 1011 | 1010 | 0001 | 0101 | 0001 | 1110 | 0011 |
Hexadecimal | B | A | 1 | 5 | 1 | E | 3 |
Decimal | 195121635 |
Brave paradise mac os. Binary/Decimal converter (BinConverter.exe)
Lexical Conventions
C programs are typically stored in one or more files on the disk. These files are given a rather fancy name: translation units. After the pre-processor has removed the directives and performed the appropriate action, the compiler starts its job. The first thing the compiler needs to do is to parse through all of the tokens in the files.There are different classes of tokens (lexical elements). In no particular order they are:
- keywords
- identifiers
- constants
- integers
- characters
- floating point
- enumerations
- string literals (NUL-terminated character arrays)
- operators (Lots!)
- punctuators and separators
- keywords
- identifiers
- constants
- string literals
- operators
- punctuators
We will spend the entire course studying these aspects of the C language.
Identifiers
- Variables, constants, function names, and other elements of a C program are called identifiers; they are used to name (i.e. identify) things.
- There are only two rules that must be followed:
- Identifiers may only contain letters (upper- or lower-case), digits (0-9), and underscores ( _ ).
- Identifiers must begin with a letter or underscore. (Not a digit)
- There are certain words in C that have special meanings (keywords) and can't be used as identifiers. (e.g. int, float, const)
- C is a case sensitive language; upper- and lower-case letters are considered to be different. (e.g. SUM, Sum, sum are all different identifiers)
- Most compilers allow identifiers to be 31 characters or more in length. (You probably won't need more than that.)
- Use meaningful names, you will be glad you did (and your grade in this course will reflect it!)
Valid | Invalid | Invalid Reason |
Starts with a digit (Must start with a letter or underscore) | ||
1. Doesn't start with a letter or underscore 2. $ is illegal character | ||
$ is an illegal character | ||
- is an illegal character (for an identifier) | ||
Can't have spaces in identifier names | ||
int is a keyword |
Good Identifier Names | Bad Identifiier Names |
int rate; int time; int distance; rate = 60; time = 20; distance = rate * time; | int x; int y; int z; x = 60; y = 20; z = x * y; |
#define PI 3.1416F float radius = 5.25F; float sphere_volume; sphere_volume = 4.0F / 3.0F * PI * radius * radius * radius; | #define A 3.1416F float id1 = 5.25F; float id2; id2 = 4.0F / 3.0F * A * id1 * id1 *id1; |
double base = 2.75; double height = 4.8; double area_of_triangle = 0.5 * base * height; | double table = 2.75; double chair = 4.8; double couch = 0.5 * table * chair; |
Keywords
Keywords are identifiers that are reserved for the compiler. You can't use any of these as identifiers:const | double | float | int | short | struct | unsigned | |
break | continue | else | for | long | signed | switch | void |
case | default | enum | goto | register | sizeof | typedef | volatile |
char | do | extern | if | return | static | union | while |
After Azrael Mac Os 11
The keywords in red are not used in this course as they are somewhat more advanced. (The auto keyword is deprecated and shouldn't be used at all. The meaning of auto means something completely different in modern C++. There are also about 92 keywords in C++, which is an enormous language!)Also, remember that C is case-sensitive so these keywords must be typed exactly as shown. int is not the same as Int or INT.
Constants
A literal value is a constant just as you type it in the code: Examples:Constant | Type |
5 | int |
3.14 | double |
3.14F | float |
'A' | int |
'hello' | string |
After Azrael Mac Os Download
Notes:- The literal character 'A' is of type int and not char. ('A' is just an alias for the value 65. ASCII chart near the bottom)
- The string type is technically a constant character pointer which we will talk more about in the future.
- The marathon.c program has 6 literal values. There are 4 integers, 1 double, and 1 string.
Algorithms
A large part of your programming career will deal with algorithms.After Azrael Mac Os Catalina
- One of the most fundamental concepts in computer programming
- A set a steps that defines how a task is accomplished; a recipe
- A computer program represents an algorithm
- Programs = software
- Algorithms have been known and studied for centuries; they were not invented by computer scientists
- Algorithms do not require computers, although computers require algorithms
- There are many famous algorithms; Euclid's Greatest Common Divisor (GCD) is a simple one (e.g. the GCD of 27 and 12 is 3, the GCD of 68 and 12 is 4)
Euclid's algorithm in English (with some algebra thrown in):
Notice that in Step 3 there are two different possibilities. This is typically how algorithms work. There usually needs to be some terminating condition, otherwise the algorithm (program) runs forever.How about the numbers 101 and 27?
Here is how we might write the algorithm in pseudo-code:
- Assign larger value to M
- Assign smaller value to N
- Divide M by N and assign remainder to R
- While remainder, R, is not 0
- Assign N to M
- Assign R to N
- Divide M by N and assign remainder to R
- End While
- The algorithm has terminated and the GCD is N
Euclid's GCD algorithm as high-level computer programs (assuming that m and n are integers and already have values and that m > n)
C/C++/C#/D/Java | Perl | Python | Pascal | BASIC |
---|
After Azrael Mac Os X
The algorithm reprinted: