C Introduction
What is C?
C is a general-purpose programming language created by Dennis Ritchie at
the Bell Laboratories in 1972.
It is a very popular language, despite being old. The main reason for its
popularity is because it is a fundamental language in the field of computer
science.
C is strongly associated with UNIX, as it was developed to write the UNIX
operating system.
Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the
syntax is similar
If you know C, you will understand how computer memory works
C is very fast, compared to other programming languages, like Java
and Python
C is very versatile; it can be used in both applications and technologies
C Syntax
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
C Variables
Variables
Variables are containers for storing data values, like numbers and
characters.
In C, there are different types of variables (defined with different keywords),
for example:
int - stores integers (whole numbers), without decimals, such as 123 or
-123
float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
char - stores single characters, such as 'a' or 'B'. Characters are
surrounded by single quotes
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Example
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';
// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);
Student id: 15
Student age: 23
Student fee: 75.250000
Student grade: B
Basic Data Types
The data type specifies the size and type of information the variable will
store.
In this tutorial, we will focus on the most basic ones:
Data Size Description
Example
Type
int 2 or 4 Stores whole numbers, without 1
bytes decimals
floa 4 Stores fractional numbers, containing 1.99
t bytes one or more decimals. Sufficient for
storing 6or7decimal digits
doub 8 Stores fractional numbers, containing 1.99
le bytes one or more decimals. Sufficient for
storing 15 decimal digits
char 1 byte Stores a single 'A'
character/letter/number, or ASCII
values
Basic Format Specifiers
There are different format specifiers for each data type. Here are some of
them:
Format Specifier Data Type
%d or %i int
%f or %F float
%lf double
%c char
%s Used for strings (text), which you will learn more
about in a later chapter
Math Functions
There is also a list of math functions available, that allows you to perform
mathematical tasks on numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
Square Root
To find the square root of a number, use the sqrt() function:
Example
#include <stdio.h>
#include <math.h>
int main() {
printf("%f", sqrt(16));
return 0;
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and the
floor() method rounds a number downwards to its nearest integer, and
returns the result:
Example
#include <stdio.h>
#include <math.h>
int main() {
printf("%f\n", ceil(1.4));
printf("%f\n", floor(1.4));
return 0;
Power
The pow() function returns the value of x to the power of y (xy):
Example
#include <stdio.h>
#include <math.h>
int main() {
printf("%f", pow(4, 3));
return 0;