C
Table of Contents
- 1 Introduction
- 2 Setting Up The Development Environment
- 3 Basic Program
- 4 Variables and Data Types
- 5 Basic Operators
- 6 Conditionals
- 7 Printing and Reading
- 7.1 Basics of Printing
- 7.2 Format Specifiers
- 7.3 Reading Input
- 8 Loops
- 8.1 While Loop
- 8.2 Do... While Loop
- 8.3 For Loop [3]
- 9 Arrays
- 10 Test Your Knowledge
- 11 Where From Here
- 12 References
- 12.1 Contributors:
The Logo of The C Programming Language [1] |
|---|
Introduction
C is a general-purpose, procedural computer programming language first designed and created by Dennis Ritchie and Bell Labs in 1972. It was designed to be compiled and to provide the developer the option to directly access memory. As of April 2021, C was ranked first in The Importance of Being Earnest (TIOBE) index, an index measuring the most popular programming languages. The C programming language has inspired many future programming languages' basic features, control structures, or just general syntax. This includes C++, C#, Java, Javascript, Objective-C, and many more [2].
This wiki page serves as a very basic introduction to the C language but should not be the sole resource for learning this comparatively difficult and complex programming language. It will go over general syntax and a few features but will not go in depth and will certainly not go over all the features. Readers are strongly recommended to pickup a dedicated book, such as The C Programming Language by Brian Kernighan and Dennis Ritchie or C Programming: A Modern Approach by K. N. King for more comprehensive information. It is also highly suggested that the reader set up their own development environment, such as through an Integrated Development Environment (IDE), to develop their own C code.
Setting Up The Development Environment
For beginners, it is recommended to use an IDE to compile and debug code. For a comprehensive setup tutorial for using the Eclipse IDE to program in C, please refer here.
Otherwise, you can also choose to use a text editor like VSCode and install a compiler such as MinGW-w64 to compile the code from the command line. There are several options available, in terms of code editors and compilers. Plenty of resources explaining these resources and how to install them are available online.
Basic Program
It is essentially tradition to begin learning a new programming language by writing a program that will print "Hello World!". The code for printing "Hello World!" in C goes like this:
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}On the first line, the library stdio.h is imported. The #include is a preprocessor directive that instructs the compiler to include code and functions from the header stdio.h [3]. The stdio.h library includes the standard functions such as printf or scanf but other standard header libraries can be included. If you are importing your own local headers, you can declare it like
#include "header_name_goes_here.h"The C language has things known as functions, which are routines, or blocks of code, which take arguments and return a single value. In this example, we define a function called "main" which is of type "int", or integer, and takes in "void" paramters. "main" is a special function name as it is the entry point for all C code. The "int" indicates that the function returns an integer value. What goes inside the parentheses defines the parameters of the function. Currently, it is stated as "void" which means it takes in nothing [4].
The printf function is used in C to display output to the screen. The quotation marks indicates that whatever contained inside should be printed as-is [3]. \n is known as an escape sequence - a full list of these are available in a later section. Just know for now that it is a newline character and pushes the cursor to the next line. This would be useful if you wished to print sentences on multiple lines. The function is returned at the end with the "return 0" statement. In the main function, the return 0 statement is important as it essentially indicates to the operating system that the program succeeded [3].
You'll also notice that statements such as printf and return 0 are suffixed with semicolons. All statements in C must be followed by a semicolon. As you write more code, this will become more natural.
Variables and Data Types
C is a statically typed langauge so any variable you declare has a type assigned to it [4]. These are some example variable declarations:
int age; // Declares a variable "age" with type int
float height; // Declares a variable "height" with type float
double weight; // Declares a variable "weight" with type doubleNote that comments in C start with a "//". Multiline comments start with a "/*" and end with a "*/". Variable names can contain uppercase/lowercase characters, digits, and the underscore character, but cannot start with a digit [4]. You can also initialize these variables with a value like so:
int age = 20; // Declares a variable "age" with type int and initializes it with 20
float height = 175.5f; // Declares a variable "height" with type float and initializes it with 175.5
double weight = 75; // Declares a variable "weight" with type double and initializes it with 75Note that float types need to append an "f" at the end of the value because the compiler normally interprets floats as doubles. Once a variable is declared, you can change or access its value at any time using the = operator [4]:
age = 50; // age is now 50
height = 156.7f; // height is now 156.7
weight = 60.4; // weight is now 60.4char types store a character although it refers it through its ASCII value. int types store integers while floating-point numbers (float, double, long double) store real numbers. Most programming languages have a dedicated boolean type (true or false) but not C, unless you include the header file <stdbool.h>. C considers zero as false and anything nonzero as true.
Here is a full list of the basic data types [5]:
Data Type | Typical Memory Size in Bytes1 | Typical Range (or what it stores)2 |
|---|---|---|
char | 1 | -128 to 127 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
unsigned short int | 2 | -32,768 to 32,767 |
unsigned int | 4 | 0 to 4,294,967,295 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 | 0 to 4,294,967,295 |
long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
float | 4 | 24 bits of precision [4] |
double | 8 | 53 bits of precision [4] |
long double | 16 | 64 bits of precision [4] |
12Note that the amount of memory required, and ultimately how many digits each data type can store, is entirely dependent on the architecture/environment you're running the code on. The table above provides the standard values. The only guarantee is that a short is not longer than an int, a long is not shorter than an int, etc [4].
With the <stdint.h> header, the programmer can specify the number of bytes for a particular data type.