C Programming
Hello World
The Hello World program in C below prints “Hello, world!” to the terminal. It also includes a few basic elements of C programming, such as the #include
directive, the main
function, and the printf
function.
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
}
Comments
Why are comments important?
Comments are used to explain the code and make it easier to understand. They are ignored by the compiler and are for the programmer’s benefit (you might to come back to your code later and wonder what you were thinking or you might be working with a team and want to explain your code to them).
// this is a single line comment
/*
this is a
multi-line
comment
*/
#include
You can include libraries in your C program using the #include
directive. The #include
directive tells the compiler to include the contents of the specified file in the program.
#include <stdio.h>
Some common libraries you might include are:
stdio.h
- standard input/output library (for functions likeprintf
)cs50.h
- CS50 library (for functions likeget_int
)stdlib.h
- standard library (for functions likemalloc
)math.h
- math library (for functions likesqrt
)string.h
- string library (for functions likestrlen
)
For a reference of the functions in each library, you can check the CS50 Manual.
main
Function
The main
function is the entry point of a C program. When you run a C program, the operating system calls the main
function to start the program. The main
function is required in every C program.
int main(void)
{
// code goes here
}
Note that int
is the return type of the function, and void
is the parameter list. void
means that the function does not take any parameters.
Variables
Data Types
int
- integerfloat
- floating point numberdouble
- double-precision floating point numberchar
- characterstring
- string of characters (array of characters, technically)bool
- booleanvoid
- no valuelong
- long integer
Variable Declaration
int num = 2;
float pi = 3.14;
char letter = 'a';
string name = "Alice"; // must include #include <string.h> or #include <cs50.h>
bool is_true = true;
// you can also declare multiple variables of the same type on the same line
int x, y, z;
// you can also declare and initialize variables without specifying the type
int a = 2, b = 3;
Variable Assignment
You can assign a value to a variable using the assignment operator =
.
int num;
num = 2;
Variable Naming
- Variable names must start with a letter or an underscore.
- Variable names can contain letters, numbers, and underscores.
- Variable names are case-sensitive.
- Variable names cannot be keywords (e.g.,
int
,float
,char
).
Variable Scope
Variables can have different scopes depending on where they are declared:
- Global - declared outside of any function
- Global variables are accessible from any function in the program.
- Local - declared inside a function
- Local variables are only accessible within the function where they are declared.
Here is a simple example to illustrate variable scope:
int global_var = 2;
int main(void)
{
int local_var = 3;
printf("%i\n", global_var); // prints 2
printf("%i\n", local_var); // prints 3
}
void another_function(void)
{
printf("%i\n", global_var); // prints 2
printf("%i\n", local_var); // error: local_var is not accessible
}
Constants
Constants are variables whose values cannot be changed once they are assigned. You can define a constant using the const
keyword.
const int NUM = 2;
Operators
Arithmetic Operators
+
- addition-
- subtraction*
- multiplication/
- division%
- modulo
int sum = 2 + 3;
int difference = 5 - 2;
int product = 2 * 3;
int quotient = 10 / 2;
int remainder = 10 % 3;
A few notes about operators:
- modulo operator
%
returns the remainder of a division operation. - division operator
/
returns a float if one of the operands is a float, and an integer if both operands are integers.
Relational Operators
Relational operators are used to compare two values and return a boolean result (true or false).
==
- equal to
int x = 2;
int y = 3;
if (x == y)
{
printf("x is equal to y\n");
}
!=
- not equal to
if (x != y)
{
printf("x is not equal to y\n");
}
>
- greater than
if (x > y)
{
printf("x is greater than y\n");
}
<
- less than
if (x < y)
{
printf("x is less than y\n");
}
>=
- greater than or equal to
if (x >= y)
{
printf("x is greater than or equal to y\n");
}
<=
- less than or equal to
if (x <= y)
{
printf("x is less than or equal to y\n");
}
Logical Operators
Logical operators are used to combine multiple conditions and return a boolean result.
&&
- and||
- or!
- not
int x = 2;
int y = 3;
if (x > 0 && y > 0)
{
printf("x and y are positive\n");
}
if (x > 0 || y > 0)
{
printf("x or y is positive\n");
}
if (!(x > 0))
{
printf("x is not positive\n");
}
Assignment Operators
Assignment operators are used to assign/update values to variables. Besides the basic assignment operator (=
), they often combine an arithmetic operation with assignment.
=
- assignment+=
- addition assignment-=
- subtraction assignment*=
- multiplication assignment/=
- division assignment%=
- modulo assignment++
- increment--
- decrement
int x = 2;
x += 3; // x is now 5
int y = 5;
y -= 2; // y is now 3
int z = 3;
z *= 2; // z is now 6
int a = 10;
a /= 2; // a is now 5
int b = 10;
b %= 3; // b is now 1
int c = 2;
c++; // c is now 3
int d = 3;
d--; // d is now 2
// you can even combine operators
int e = 2;
e *= 3 + 2; // e is now 10
Input/Output and Printing
printf
printf
prints a formatted string to the terminal:
// print text
printf("hi\nhi")
// print a new line
printf("\n")
// print the value of variables
int num = 2
printf("the value of this number is %i", num)
/*
can also do for
strings, doubles, floats, etc.
using %s, %d, %f, etc.,
as seen below
*/
Format Codes
Format codes are used to specify the type of data that is being printed. These are placeholders that are replaced by the actual values when the printf
function is executed.
%i
- integer%f
- float%c
- char%s
- string%b
- boolean
Escape Sequences
Escape sequences are special characters that are used to format the output of a string. They are preceded by a backslash \
.
- \n create a new line
- \r return to the start of a line
- \” print a double quote
- \` print a single quote
- \\ print a backslash
get_int
, get_float
, get_string
get_int
, get_float
, and get_string
are functions from the CS50 library that prompt the user for input and return the value as an integer, float, or string, respectively.
int num = get_int("Enter an integer: ");
float pi = get_float("Enter a float: ");
string name = get_string("Enter a string: ");
You must include the cs50.h
library to use these functions.
scanf
scanf
reads input from the user and stores it in a variable:
int num;
scanf("%i", &num);
The &
operator is used to get the memory address of the variable.
Loops and Conditionals
if
Statements
if
statements are used to execute a block of code if a condition is true. You can also use else if
and else
to specify alternative conditions.
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
for
Loops
for
loops are used to execute a block of code a specified number of times. The loop has three parts: initialization, condition, and update.
for (int i = 0; i < 10; i++)
{
printf("%i\n", i);
}
while
Loops
while
loops are used to execute a block of code as long as a condition is true. It is important to update the condition inside the loop to avoid an infinite loop.
int i = 0;
while (i < 10)
{
printf("%i\n", i);
i++;
}
do while
loops
do while
loops are similar to while
loops, but the condition is checked at the end of the loop. This means that the loop will always execute at least once.
int input;
do
{
input = get_int("Enter an integer: ");
}
while (input < 0)
Functions
Function Declaration
To declare a function, you need to specify the return type (e.g., int
), function name (e.g., add
), and parameters (if any). For each parameter, you need to specify the type and name.
int add(int a, int b);
Function Definition
Below is an example of a function definition that adds two integers and returns the result.
int add(int a, int b)
{
return a + b;
}
Function Call
If a function returns a value, you can store the result in a variable.
int sum = add(2, 3);
However, if the function does not return a value, you can call it like this:
void print_hello(void)
{
printf("Hello!\n");
}
int main(void)
{
print_hello();
}
Function Prototypes
To use a function before it is defined, you can declare a function prototype at the beginning of the file.
// function prototype
int add(int a, int b);
// main function
int main(void)
{
int sum = add(2, 3);
printf("%i\n", sum);
}
// full function definition
int add(int a, int b)
{
return a + b;
}
Arrays
Array Declaration
You can declare an array by specifying the data type and the number of elements in the array.
int numbers[5];
Array Initialization
You can initialize an array by specifying the values of the elements.
int numbers[5] = {1, 2, 3, 4, 5};
You can also initialize an array with a for loop.
int numbers[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = i + 1;
}
Accessing Array Elements
You can access elements of an array using the index (starting from 0).
int numbers[5] = {1, 2, 3, 4, 5};
printf("%i\n", numbers[0]); // prints 1
printf("%i\n", numbers[2]); // prints 3
Array Length
You can get the length of an array using the sizeof
operator.
int numbers[5] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]);
printf("%i\n", length); // prints 5
Multidimensional Arrays
You can create multidimensional arrays by specifying the number of rows and columns.
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
To access elements of a multidimensional array, you need to specify the row and column.
int element = matrix[1][2]; // gets the element in the second row and third column (6)
Strings as Arrays
Strings in C are arrays of characters. You can declare a string by specifying the characters enclosed in double quotes.
char name[6] = "Alice";
String Array Functions
You can use string functions from the string.h
library to manipulate strings.
strlen
- get the length of a stringstrcpy
- copy one string to anotherstrcat
- concatenate two stringsstrcmp
- compare two strings
#include <string.h>
int main(void)
{
char name[] = "Alice";
int length = strlen(name);
printf("%i\n", length); // prints 5
}
Here is an example of using the strcpy
function:
#include <string.h>
int main(void)
{
char source[] = "Hello";
char destination[6];
strcpy(destination, source);
printf("%s\n", destination); // prints "Hello"
}