The C Programming Handbook For Beginners
The C Programming Handbook For Beginners
Forum Donate
The C Programming
Handbook for Beginners
Dionysia Lemonaki
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 1/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
will need for the rest of your programming career. Forum Donate
Chapter 3: Operators in C
Chapter 5: Loops in C
Chapter 6: Arrays in C
Chapter 7: Strings in C
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 2/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Programming
Learn to code — free 3,000-hour curriculum
You will also learn the basics of C syntax and familiarize yourself
with the general structure of all C programs.
You will have successfully written, compiled, and executed your first
simple C program that prints the text "Hello, world!" to the screen.
You will have also learned some core C language features, such as
comments for documenting and explaining your code and escape
sequences for representing nonprintable characters in text.
What Is Programming?
Computers are not that smart.
Even though they can process data tirelessly and can perform
operations at a very high speed, they cannot think for themselves.
They need someone to tell them what to do next.
And the humans who write the instructions and supply them to the
computer are known as programmers.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 4/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
With that said, they tend to be slower, consume more memory, and
make it harder to work with low-level hardware and systems
because of how abstract they are.
Forum Donate
A procedural language is a type of programming language that
Learn to code
follows a step-by-step — freeto
approach 3,000-hour
solving a curriculum
problem.
So, C programs are divided into smaller, more specific functions that
accomplish a certain task and get executed sequentially, one after
another, following a top-down approach.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 6/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Most modern and popular languages used today either use C under
the hood or are inspired by it.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 7/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
A C Compiler
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 8/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The compiler reads the entire source code, checks it for errors, and
then translates the entire program into machine code. This is a
language the computer can understand and it's directly associated
with the particular instructions of the computer.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 9/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
gcc --version
Learn to code — free 3,000-hour curriculum
If you're using macOS and have not installed the command line
developer tools, a dialog box will pop-up asking you to install them –
so if you see that, go ahead and do so.
If you have already installed the command line tools, you will see an
output with the version of the compiler, which will look similar to
the following:
If you are using Windows, you can check out Code::Blocks or look
into installing Linux on Windows with WSL. Feel free to pick
whatever programming environment works best for you.
An IDE is where you write, edit, save, run, and debug your C
programs. You can think of it like a word processor but for writing
code.
Visual Studio Code is a great editor for writing code, and offers
many IDE-like features.
Once you have downloaded Visual Studio Code, install the C/C++
extension.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 10/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
If you want to learn more, you can look through the Visual Studio
Code documentation
Learn tofor C/C++.
code — free 3,000-hour curriculum
With your local machine all set up, you are ready to write your first
C program!
Give this folder a name, for example, c-practice , and then select
"Create" -> “Open".
You should now have the c-practice folder open in Visual Studio
Code.
Hold down the Command key and press N on macOS or hold down
the Control and press N for Windows/Linux to create an
Untitled-1 file.
Hold down the Command key and press S on macOS or hold down
the Control key and press S for Windows/Linux, and save the file
as a main.c file.
Make sure that you save the file you created with a .c extension, or
it won’t be a valid C file.
You should now have the main.c file you just created open in Visual
Studio Code.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 11/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
}
Let’s go over each line and explain what is happening in the program.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 12/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
So, this line is necessary for the function we have later on in our
program, printf() , to work.
If you don't include the stdio.h file at the top of your code, the
compiler will not understand what the printf() function is.
And the void keyword inside the main() function indicates that
the function receives no arguments.
This line acts as a boilerplate and starting point for all C programs. It
lets the computer know where to begin reading the code when it
executes your programs.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 13/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Having comments in your source code is also helpful for your future
self. So when you come back to the code in a few months and don't
remember how the code works, these comments can help.
Single-line comments
Multi-line comments
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 14/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
// I am a single-line comment
Forum Donate
Any text written after the forward slashes and on the same line gets
ignored by the compiler.
/*
This is
a multi-line
comment
*/
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 15/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 16/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
#include <stdio.h>
printf("Hello, world!\n");
}
The compiler will read the program and translate it into a format
closer to the computer’s native language and make your program
suitable for execution.
You will be able to see the output of your program, which should be
Hello, world! .
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 17/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum
For example, when the preprocessor finds the line #include Donate
<stdio.h> , theLearn to codetells
#include the3,000-hour
— free preprocessor to include all the
curriculum
code from the stdio.h header file.
So, it replaces the #include <stdio.h> line with the actual contents
of the stdio.h file.
If there are any errors, compilation will fail, and you will need to fix
the errors to continue.
The next step is the assembly phase, where the assembler converts
the generated assembly code statements into machine code
instructions.
Now, let’s go over the commands you need to enter to compile your
main.c file.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 18/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
gcc main.c
The gcc part of the command refers to the C compiler, and main.c
is the file that contains the C code that you want to compile.
ls
a.out main.c
The a.out is the default name of the executable file created during
the compilation process.
./a.out
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 19/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
This command tells the computer to look in the current directory, Donate
Forum
./ , for a file named a.out .
Learn to code — free 3,000-hour curriculum
Hello, world!
You also have the option to name the executable file instead of
leaving it with the default a.out name.
This command with the -o option (which stands for output) tells
the gcc compiler to create an executable file named helloWorld .
To run the new executable file that you just created, enter the
following command:
./helloWorld
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 20/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Hello, world!
Learn to code — free 3,000-hour curriculum
Note that whenever you make a change to your source code file, you
have to repeat the process of compiling and running your program
from the beginning to see the changes you made.
By the end of this chapter, you will know how to declare and
initialize variables.
You will also have learned about various data types available in C,
such as integers, floating-point numbers, and characters, which
dictate how information is processed and stored within a program's
memory.
What Is a Variable in C?
Variables store different kind of data in the computer's memory, and
take up a certain amount of space.
Forum Donate
The stored data is given a name, and that is how you are able to
access it whenLearn to code
you need it. — free 3,000-hour curriculum
Once you have specified the data type, you give the variable a name.
The general syntax for declaring variables looks something like this:
data_type variable_name;
#include <stdio.h>
int main(void) {
int age;
}
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 22/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 23/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
Forum Donate
int age;
age = 29;
}
With that said, you can combine the initialization and declaration
steps instead of performing them separately:
#include <stdio.h>
int main(void) {
// declaration + initialization
int age = 29;
}
For example, you can change the value of age without having to
specify its type again.
#include <stdio.h>
int main(void) {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 24/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Note that the data type of the new value being assigned must match
the declared data type of the variable.
If it doesn't, the program will not run as expected. The compiler will
raise an error during compilation time.
#include <stdio.h>
int main(void) {
/*
trying to assign a floating-point value
to a variable with type int
will cause an error in your program
*/
age = 29.5;
}
There are various built-in data types in C such as char , int , and
float .
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 25/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Signed data types can represent both positive and negative values.
On the other hand, unsigned data types can represent only non-
negative values (zero and positive values).
Use signed data types when you need to represent both positive and
negative values, such as when working with numbers that can have
positive and negative variations.
And use unsigned data types when you want to ensure that a
variable can only hold non-negative values, such as when dealing
with quantities.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 26/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
It can also store symbols such as '!' , and digits such Forum
as '7' . Donate
#include <stdio.h>
int main(void) {
This is because you can't use double quotes when working with
char s.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 27/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void) {
So, the range of numbers for unsigned ints that take up 2 bytes of
memory is [0 to 65,535] and the range is [0 to 4,294,967,295]
for those that take up 4 bytes.
To represent smaller numbers, you can use another data type – the
short int . It typically takes up 2 bytes (or 16 bits) of memory.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 28/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Use a short when you want to work with smaller integers, or when
memory optimisation is critically important.
If you need to work with larger integers, you can also use other data
types like long int or long long int , which provide a larger range
and higher precision.
The long long int data type is able to use even larger numbers
than a long int . It usually takes up 8 bytes (or 64 bits) of memory.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 29/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum
Here is how you create a variable that will hold a float value: Donate
#include <stdio.h>
int main(void) {
Here is how you create a variable that will hold a double value:
#include <stdio.h>
int main(void) {
Forum Donate
If you require higher precision and accuracy for your calculations
Learnis
and memory usage tonot
code — free you
critical, 3,000-hour
can usecurriculum
a double .
They tell the program how to format or interpret the data being
passed to or read from the scanf() and printf() functions.
The syntax for format codes is the % character and the format
specifier for the data type of the variable.
#include<stdio.h>
int main(void)
{
int age = 29;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 31/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Here is a table with the format specifiers for each data type:
%c char
%c unsigned char
%u unsigned int
%f float
%lf double
But what happens when you want to receive user input? This is
where the scanf() function comes in.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 32/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
The scanf() function reads user input, which is typically entered
Learn to code — free 3,000-hour curriculum
via a keyboard.
The user enters a value, presses the Enter key, and the value is saved
in a variable.
The general syntax for using scanf() looks something similar to the
following:
scanf("format_string", &variable);
#include <stdio.h>
int main(void) {
int number;
scanf("%i", &number);
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 33/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
In the exampleLearn
above, I first—have
to code free to include the
3,000-hour stdio.h header file,
curriculum
which provides input and output functions in C.
Next, I use scanf() to read and save the value that the user enters.
Note also the & symbol before the variable name. Forgetting to add
it will cause an error.
Lastly, after receiving the input, I display the received value to the
console using another printf() function.
With that said, there may be times when you don’t want a value to
be changed. This is where constants come in handy.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 34/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
It is also best practice to use all upper case letters when declaring a
constant’s name.
#include <stdio.h>
int main(void) {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 35/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
If you try to change its value, the C compiler will generate an error
indicating that you are attempting to modify a constant.
#include <stdio.h>
int main(void) {
Chapter 3: Operators
Operators are essential building blocks in all programming
languages.
And they let you compare variables and values against each other
for decision-making computatons.
In this chapter, you will learn about the most common operators in C
programming.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 36/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
And you will learn about logical operators, which allow you to make
decisions based on conditions.
By the end of this chapter, you will have a solid grasp of how to use
different operators to manipulate data.
OPERATOR OPERATION
+ Addition
- Subtraction
* Multiplication
/ Division
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 37/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Let's see examples of each one in action.
Learn to code — free 3,000-hour curriculum
#include <stdio.h>
int main(void) {
int a = 5;
int b = 3;
int sum = a + b;
#include <stdio.h>
int main(void) {
int a = 10;
int b = 5;
int difference = a - b;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 38/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
#include <stdio.h>
int main(void) {
int a = 4;
int b = 3;
int product = a * b;
#include <stdio.h>
int main(void) {
int a = 10;
int b = 2;
int quotient = a / b;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 39/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void) {
int a = 10;
int b = 3;
int remainder = a % b;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 40/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Learn==
to code — free
Equal3,000-hour
to curriculum
!= Not equal to
Note that you use the comparisson operator (two equal signs – == )
and not the assignment operator ( = ) which is used for assigning a
value to a variable.
#include <stdio.h>
int main(void) {
int a = 5;
int b = 5;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 41/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void) {
int a = 5;
int b = 3;
#include <stdio.h>
int main(void) {
int a = 10;
int b = 5;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 42/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
The result is 1 (true), because a is greater than b .
Learn to code — free 3,000-hour curriculum
#include <stdio.h>
int main(void) {
int a = 10;
int b = 5;
#include <stdio.h>
int main(void) {
int a = 5;
int b = 5;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 43/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
printf("Result: %i\n", result); // Output: Result: 1
}
Learn to code — free 3,000-hour curriculum
#include <stdio.h>
int main(void) {
int a = 1;
int b = 5;
Logical Operators
Logical operators operate on Boolean values and return a Boolean
value.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 44/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Learn to &&
code — freeLogical
3,000-hour
AND curriculum
|| Logical OR
! Logical NOT
Here is the truth table for the AND ( && ) operator when you are
working with two operands:
The result of (10 == 10) && (20 == 20) is true because both
operands are true .
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 45/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The result is true only when at least one of the operands is true .
Here is the truth table for the OR ( || ) operator when you are
working with two operands:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 46/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
You may want to use the NOT operator when when you want to flip
the value of a condition and return the opposite of what the
condition evaluates to.
OPERAND RESULT
true false
false true
Forum Donate
What Is the
LearnAssignement Operator
to code — free 3,000-hour curriculum in C?
The assignment operator is used to assign a value to a variable.
#include <stdio.h>
int main(void) {
The type of data assigned should match the data type of the
variable.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 48/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
This can make your code more concise and easier to read.
Learn to code — free 3,000-hour curriculum
Some common compound assignment operators in C include:
#include <stdio.h>
int main(void) {
num += 5;
The line num += 5 increments the value of num by 5, and the result
(15) is stored back into num in one step.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 49/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Note that the num += 5; line works exactly the same Forum
as doing numDonate
= num + 5 , which would mean num = 10 + 5 , but with fewer lines
Learn to code — free 3,000-hour curriculum
of code.
#include <stdio.h>
int main(void) {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 50/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
Forum Donate
Chapter 4: Conditional
Statements
The examples you have seen so far all execute line by line, from top
to bottom.
They are not flexible and dynamic and do not adapt according to
user behavior or specific situations.
In this chapter, you will learn how to make decisions and control the
flow of a program.
You get to set the rules on what happens next in your programs by
setting conditions using conditional statements.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 51/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The program will decide what the next steps should beForum
based on Donate
whether the conditions are met or not.
Learn to code — free 3,000-hour curriculum
Certain parts of the program may not run depending on the results
or depending on certain user input. The user can go down different
paths depending on the various forks in the road that come up
during a program's life.
You will also learn about the else if and else statements that are
added to the if statement to provide additional flexibility to the
program.
You will then learn about the ternary operator which allows you to
condense decision-making logic into a single line of code and
improve the readability of your program.
If the given condition evaluates to true only then is the code inside
the if block executed.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 52/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
if (condition) {
Forum Donate
// run this code if condition is true
Learn to code — free 3,000-hour curriculum
}
#include <stdio.h>
int main(void) {
// variable age
int age;
I then prompted the user to enter their age and stored the answer in
the variable age .
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 53/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
If so, I want a message printed to the console letting the user knowDonate
Forum
that to proceed, the user should be at least 18 years of age.
Learn to code — free 3,000-hour curriculum
When asked for my age and I enter 16 , I'd get the following output:
#output
This time, when asked for my age, say I enter 28 , but I don't get any
output:
#output
I have also not specified what should happen in the case that the
user's age is greater than 18.
To specify what happens in case the user's age is greater than 18, I
can use an if else statement.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 54/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
But if that condition evaluates to false , the code inside the else
block will execute.
The else keyword is the solution for when the if condition is false
and the code inside the if block doesn't run. It provides an
alternative.
if (condition) {
// run this code if condition is true
} else {
// if the condition above is false, run this code
}
Now, let's revisit the example from the previous section, and specify
what should happen if the user's age is greater than 18:
#include <stdio.h>
int main(void) {
int age;
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 55/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#output
If the condition is false the code in the if block is skipped and the
code in the else block runs instead:
#output
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 56/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
But what happens when you want to have more than one conditionDonate
Forum
to choose from?
Learn to code — free 3,000-hour curriculum
If you wish to chose between more than one option you can
introduce an else if statement.
if (condition) {
// if condition is true run this code
} else if(another_condition) {
// if the above condition was false and this condition is true
// run the code in this block
} else {
// if the two above conditions are false run this code
}
#include <stdio.h>
int main(void) {
int age;
scanf("%i", &age);
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 57/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
If the first if statement is true, the rest of the block will not run:
#output
If that is true the code inside the else if block executes and the
rest of the block doesn't run:
#output
If both of the previous conditions are all false, then the last resort is
the else block which is the one to execute:
#output
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 58/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
It can provide a way of writing more readable and concise code and
comes in handy when writing simple conditional expressions.
You would want to use it when you are making making simple
decisions and want to keep your code concise and on one line.
The general syntax for the ternary operator looks something similar
to the following:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 59/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void) {
int x = 10;
Chapter 5: Loops
In this chapter you will learn about loops, which are essential for
automating repetitive tasks without having to write the same code
multiple times.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 60/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Loops allow you to execute a specific block of code instructions
Learn
repeatedly over andtoover
codeagain
— free 3,000-hour
until curriculum
a certain condition is met.
You will learn about the different types of loops, such as the for ,
while and do-while loops, and understand their syntax and when
you should use each one.
You will also learn about the break statement, which allows you to
control the execution flow within loops in specific ways.
It's useful when you know how many times you want to repeat a
certain action.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 61/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
false , the loop terminates. The loop will run as long as the
Forum Donate
condition remains true.
Learn to code — free 3,000-hour curriculum
increment/decrement is the part responsible for changing
the loop control variable after each iteration. It can be an
increment ( ++ ), a decrement ( -- ), or any other
modification.
#include <stdio.h>
int main() {
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 62/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The while loop is useful when you want to repeat an action based
on a condition but don't know the exact number of iterations
beforehand.
while (condition) {
// Code to be executed in each iteration
}
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 63/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Something to note with while loops is that the code in the loop's
body is not guaranteed to run even at least one time if a condition is
not met.
#include <stdio.h>
int main() {
int count = 1;
count++;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 64/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main(void)
{
while(true)
{
printf("Hello world");
}
}
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 65/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum
As the answer is always yes (since the condition it needs to check isDonate
always true each and
Learn toevery time),
code — it runs the curriculum
free 3,000-hour code again and again
and again.
The way to stop the program and escape from the endless loop is
running Ctrl C in the terminal.
So, the do-while loop is useful when you want to ensure that the
loop's body is executed at least once before the condition is
checked.
do {
// Code to be executed in each iteration
} while (condition);
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 66/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
#include <stdio.h>
int count = 1;
do {
printf("Iteration %i\n", count);
count++;
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
So, the block of code inside the loop is executed at least one time.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 67/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum
After the iteration where count is 6 , the condition becomes Donate
It's a control flow statement that allows you to interrupt the normal
loop execution and move on to the code after the loop.
#include <stdio.h>
int main() {
int target = 5;
if (i == target) {
printf("Target value reached. Exiting loop.\n");
break; // Exit the loop
}
}
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 68/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
The program will continue executing the code that is after the loop.
Chapter 6: Arrays
Arrays offer a versatile and organized way to store multiple pieces
of related data that are arranged in an ordered sequence.
They allow you to store multiple values of the same data type under
a single identifier and perform repetitive tasks on each element.
In this chapter, you will learn how to declare and initialize arrays.
You will also learn how to access individual elements within an array
using index notation and modify them.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 69/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
In addition, you will learn how to use loops to iterate through array
Learn to code
elements and perform — free 3,000-hour
operations curriculum
on each element.
This means you can create arrays of type int , float , char , and so
on.
You then specify the array's name, followed by the array's size in
square brackets.
The size of the array is the number of elements that it can hold. This
number must be a positive integer.
Keep in mind that arrays have a fixed size, and once declared, you
cannot change it later on.
data_type array_name[array_size];
#include <stdio.h>
int main() {
int grades[5];
}
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 70/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
In the example above, I created an array named grades that can
Learn to code — free 3,000-hour curriculum
store 5 int numbers.
The curly braces will enclose the values, and each value needs to be
separated by a comma.
#include <stdio.h>
int main() {
Keep in mind that the number of values should match the array size,
otherwise you will encounter errors.
Something to note here is that you can also partially initialize the
array:
#include <stdio.h>
int main() {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 71/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main() {
#include <stdio.h>
int main() {
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 72/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Learn
Size of array: 20tobytes
code — free 3,000-hour curriculum
Here is how you can check how much memory each int occupies
using the sizeof operator:
#include <stdio.h>
int main() {
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 73/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
By dividing the total size of the array by the size of a single element,
you can calculate the number of elements in the array, which is
equal to the array's length:
#include <stdio.h>
int main() {
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 74/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
array_name[index];
#include <stdio.h>
int main() {
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 75/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Element at index 0: 50
Element at index 1: 75
Learn to code — free 3,000-hour curriculum
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
In the example above, to access each item from the integer array
grades , I have to specify the array's name along with the item's
position in the array inside square brackets.
Note that if you try to access an element with an index number that
is higher than array_size - 1 , the compiler will return a random
number:
#include <stdio.h>
int main() {
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 76/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Learn
Once you know howtotocode — free
access 3,000-hour
array curriculum
elements, you can then modify
them.
The general syntax for modifying an array element looks like this:
array_name[index] = new_value;
#include <stdio.h>
int main() {
#include <stdio.h>
int main() {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 77/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
Output:
Learn to code — free 3,000-hour curriculum
Element at index 1: 85
When modifying arrays, keep in mind that the new value must
match the declared data type of the array.
#include <stdio.h>
int main() {
Output:
Element at index 0: 50
Element at index 1: 75
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 78/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
When using a for loop to loop through an array, you have to specify
Learn to code — free 3,000-hour curriculum
the index as the loop variable, and then use the index to access each
array element.
The %i placeholders are replaced with the current index i and the
value at that index in the grades array, respectively.
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
Output:
Element at index 0: 50
Element at index 1: 75
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
When using a while loop to loop through an array, you will need an
index variable, int i = 0 , to keep track of the current position in
the array.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 79/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
The loop checks the condition (i < 5) and prints the index of the
Learn to code — free 3,000-hour curriculum
grade as well as the actual grade value.
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Element at index %i: %i\n", i, grades[i]);
i++;
} while (i < 5);
}
You can also use the sizeof operator to loop through an array.
#include <stdio.h>
int main() {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 80/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The length is calculated by dividing the total size (in bytes) of the
array by the size of a single element grades[0] . The result is stored
in the length variable.
The loop then iterates through the array using this length value.
For each iteration, it prints the index i and the value of the grade at
that index grades[i] .
Chapter 7: Strings
In the previous chapter, you learned the basics of arrays in C.
In this chapter, you will learn about strings in C and how they are
stored as arrays of characters.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 81/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
This terminator lets the computer know where the string ends.
So, the string ' Hello ' in C is stored as ' Hello\0 ' in memory.
The array will contain the characters that make up the string.
Here is how you would initialize an array to create the string 'Hello':
#include <stdio.h>
int main() {
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 82/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Let's look at how you would create the string 'Hello world':
#include <stdio.h>
int main() {
char phrase[12] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
}
In this example, there is a space between the word 'Hello' and the
word 'world'.
To print the string, you use the printf() function, the %s format
code and the name of the array:
#include <stdio.h>
int main() {
char phrase[] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd
printf("%s\n", phrase);
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 83/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
#include <stdio.h>
int main() {
char word[] = "Hello";
However, you may want to use character arrays when you want to
modify the string's content. String literals are read-only, meaning
the content is fixed.
Forum Donate
int main() {
char phrase[] = "Hello";
Output:
String length: 5
Note that the result does not include the null terminator, \0 .
You may want to copy a string in C when you need to make changes
to it without modifying it. It comes in handy when you need to keep
the original string's content intact.
The general syntax for the strcpy() function looks like this:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 85/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
strcpy(destination_string, original_string);
Learn to code — free 3,000-hour curriculum
One thing to note here is that you need to make sure the destination
array has enough space for the original string:
#include <stdio.h>
#include <string.h>
int main() {
strcpy(destination, original);
Output:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 86/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
The general syntax for the strcat() function looks something like
the following:
strcat(destination_string, original_string);
The strcat() function takes the original string and adds it to the
end of destination string.
#include <stdio.h>
#include <string.h>
int main(void) {
strcat(greeting, name);
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 87/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
}
Output:
The general syntax for the strcmp() function looks like this:
strcmp(string1, string2);
#include <stdio.h>
#include <string.h>
int main() {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 88/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
If the return value of strcmp() is less than 0 , then it means the first
word comes before the second:
#include <stdio.h>
#include <string.h>
int main() {
#include <stdio.h>
#include <string.h>
int main() {
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 89/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Forum Donate
FurtherLearn
learning: Advanced
to code — free 3,000-hour curriculum C
Topics
While this handbook has covered a wide range of topics, there is still
so much to learn, as programming is so vast.
You may also want to learn about pointers. Pointers in C are like
arrows that show you where a specific piece of information is stored
in the computer's memory.
Lastly, you may want to learn how to work with files. Working with
files in C allows you to read from and write to files. This is useful for
tasks like saving user data, reading configuration settings, or sharing
data between different programs.
These suggestions are not a definitive guide – just a few ideas for
you to continue your C programming learning journey.
If you are interested in learning more, you can check out the
following freeCodeCamp resources:
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 90/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Conclusion
This marks the end of this introduction to the C programming
language.
Thank you so much for sticking with it and making it until the end.
You learned how to work with variables, various data types, and
operators.
Happy coding!
Dionysia Lemonaki
Read more posts.
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 91/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 92/93
4/16/24, 8:14 AM The C Programming Handbook for Beginners
https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/ 93/93