Introduction to C
Who, What, When, Where, Why, How?
A Brief History
C is a low programming language that has been around since 1972. It was developed by a man named Dennis M. Ritchie, and first used on the DEC PDP-11 computer (which was first produced in 1968). One of the first publicly available descriptions of C was released by Ritchie and his colleague, Brian Kernighan, back in 1978. This description was known as the K&R Standard. The K&R Standard is a bracketing style where braces are left out of statements if they are not required.
Why Use C?
C is commonly used as a system development language, due to its code running nearly as fast as the code written in assembly language. Since C is a low level programming language, it is possible to generate code that can run before filesystems, virtual memory, and processes. For these reasons, C is commonly used to create operating systems for hardware such as printers, computers, phones, etc.
Who Uses C?
The following examples of programming languages, operating systems , and games/game engines are all fully or partially based upon C:
Differences between C and JAVA
C and JAVA contain a lot of similarities. If you can understand how one of these languages work, the other one becomes easier to learn. However, there are quite a few differences between the two languages. Here is only a few:
- C is a procedural language, JAVA is an object oriented language.
- C is a low level language, JAVA is a high level language.
- C requires the creation of pointers to reference objects, JAVA does not.
- Memory in C is managed by the user, JAVA uses the garbage collector.
- JAVA supports method overloading, C does not support function overloading.
- JAVA contains exception handling, C does not (however there are ways to deal with errors).
Makeup of a C Program
C programs typically consist of five main components. These components are:
- Preprocessor Commands: Code that transforms the program prior to compilation. Ex. #include <stdio.h>
- Functions: A group of statements that perform a task.
- Variables: Named locations in the memory that hold a value.
- Statements and Expressions: Code that makes up the body of functions.
- Comments: Helpful text inside a program to assist in readability.
Simple C Program
The Hello, World! program is a simple (and cliche) application that will simply print out the phrase “Hello, World!” to the console. In C, this is what the code would look like:
As we can see from this code, there is a preprocessor command to include the library stdio.h. This allows the programmer to include basic input and output functions in their application, such as printf (as used in the Hello,World! example).
When a C program is executed, the first function that always runs is the main() function. The return value for this function is an int data type with a value of 0 or 1 to indicate if the program has run successfully (shown in the “return 0;” statement). 0 means the program ran successfully, 1 means the program ran with errors.
The result from running this program successfully will be “Hello, World!” printed to the console.