2. In this tutorial we will be looking at the basics of
C programming language.
This tutorial comprises of chapters, short explanation
on them and examples of ran codes.
The chapters are as follows:
1. Introduction to programming.
2. Introduction to C programming
3. 3. Diving into C programming.
Introduction to C programming
Program structure
Basic syntax
Data types
Variables
Operators
Conditional Statements
Loops
Functions
Arrays
4. INTRODUCTION TO PROGRAMMING
Computer programming is the process of designing and writing computer programs. As a
skill set, it includes a wide variety of different tasks and techniques.
Most of us have heard that CPU is called the brain of our computer because it accepts
data, provides temporary memory space to it until it is stored(saved) on the hard disk,
performs logical operations on it and hence processes(here also means converts) data
into information. We all know that a computer consists of hardware and software.
Software is a set of programs that performs multiple tasks together. An operating system is
also software (system software) that helps humans to interact with the computer system.
A program is a set of instructions given to a computer to perform a specific operation.
While executing the program, raw data is processed into the desired output format.
These computer programs are written in a programming language which are high-level
languages. High level languages are nearly human languages that are more complex
than the computer understandable language which are called machine language, or
low level language. We have different languages like C, C++, C#, Java, python, etc to
communicate with the computers. The computer only understands binary language (the
language of 0’s and 1’s) also called machine-understandable language or low-level
language but the programs we are going to write are in a high-level language which is
almost similar to human language. Therefore we have a compiler which translates our
code from high or low level language to the machine language. A compiler is the one
that executes a program meanwhile it also translates the code to another.
5. INTRODUCTION TO C PROGRAMMING
C programming is a general-purpose, imperative computer programming language. It was initially
developed by Dennis Ritchie as a system programming language to write operating system called UNIX
C is the most widely used computer language. It is one of the most widely used among modern
software programmers.
WHY TO LEARN C PROGRAMMING LANGUAGE
It is easy to learn
It produces efficient programs
It is a structured language
It can handle low-level activities
It can be compiled on a variety of computer platforms
APPLICATIONS OF C PROGRAMMING
Though C programming was aimed to create an operating system, later C was adopted as a system
development language because it is efficient and executes fast. C programming can be used to:
6. Operating Systems
Text Editors
Game development
Databases. e.t.c
PROGRAM STRUCTURE IN C PROGRAMMING
Basically, a C program consists of the following parts:
• Preprocessor Commands
• Variables
• Statements & Expressions
• Functions
• Comments
Let us look at an example code that would give an output of the words
“Welcome to C programming"
8. Now, let’s explain our basic C program and the various part of the program.
The first line of the program, #include <stdio.h> is a preprocessor1 command,
which tells a C compiler to include stdio.h file before compiling our program.
The next line int main() is the main function where the program execution begins.
The next line printf() is another function available in C which outputs (print) any
word given to it, that’s why the output showed the message “Welcome to C
programming" to be displayed on the screen.
The next line /*..*/ is a comment and will be ignored by the compiler. So
comments are lines that are ignored(not run) in program. They are used for
documenting the code written in order to be understandable by other people
reading our program.
The next line return 0; terminates the main() function and returns the value 0.
Returning 0 means the program ran successfully. If the program returns 1 then
there’s actually an error in the program
1. The preprocessors are the directives, which give instructions to the compiler to preprocess the program. They are
preceded by a hash sign (#). Programs without the preprocessors sometimes throw an error.The preprocessor examines the
code before actual compilation of code begins and resolves all these directives before any code is actually generated by
regular statements.
9. BASIC SYNTAX IN C PROGRAMMING
Basic syntax are the fundamental rules of a programming language. Without them, it is
impossible to write functioning code. Every language has its own set of rules that make up its
basic syntax. Now we will be looking at them in C.
SEMICOLONS: In a C program, the semicolon is a statement terminator. That is, each
individual statement must be ended with a semicolon. A line not ending with a semicolon
gives an error and will not execute successfully.
COMMENTS: As we’ve learnt in the previous chapter, Comments are like helping text in C
program and they are ignored by the compiler. They start with /* and terminate with the
characters */ .
IDENTIFIERS: An identifier in C, is a name used to identify a variable, function, array or any
other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_'
followed by zero or more letters, underscores, and digits (0 to 9). C does not allow
characters like %, @, $ within its identifier.
WHITESPACE: Whitespace are lines without letters and they are totally ignored in C,
example of whitespaces are: A new line, tabs, spaces e.t.c
10. DATA TYPES IN C PROGRAMMING
A data type is a classification of data which tells the compiler how the data is to
be used in the code. Each data type requires different amounts of memory and
has some specific operations which can be performed over it. The following are
the common and most used data types we have in C.
char: The most basic data type in C. It stores character and requires a single
byte (1 byte) of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer and it
requires 4 bytes of memory, although we have “short int”, “long int” and “long
long int” which requires 2 bytes, 4 bytes and 8 bytes respectively.
float: It is used to store decimal numbers (numbers with floating point value)
with single precision. Single precision basically means it can spans to 7 digits
after the decimal. It requires 4 bytes of memory.
double: It is used to store decimal numbers (numbers with floating point value)
with double precision. Double precision basically means it can spans to 14
digits after the decimal. We have “double” and “long double” which requires 8
bytes and 16 bytes respectively.
In general a data type after being declared in a variable cannot be assigned to
another data type, that is after declaring an int as data type it can only store
integer and cannot store a char (string).
11. VARIABLES IN C PROGRAMMING
A variable is nothing but a name given to a storage area (where our data is stored)
that our programs can manipulate. Each variable in C has a specific type, which
determines the size and layout of the variable's memory, the types are the data types
we cover in the last chapter; the range of values that can be stored within that
memory as to int cannot store characters; and the set of operations that can be
applied to the variable.
NAMING A VARIABLE
Here, we are going to make use of the identifiers we’ve talked about, to name a
variable, it must align and follow the naming conventions of the identifiers, that is the
name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is a case-sensitive programming language.
HOW TO DEFINE A VARIABLE (DECLARATION OF A VARIABLE)
A variable definition tells the compiler where and how much storage to create for the
variable. A variable definition specifies a data type and contains a list of one or more
variables of that type. To define a variable, the data type of the intended variable
must be indicated, then the variable name. Example:
12. In the above example, the data types were indicated and the variable name
followed the data type, thus this is the definition of variables in C.
INITIALIZATION OF VARIABLE
Initializing a variable means specifying an initial value to assign to it (i.e. before it is
used at all). Notice that a variable that is not initialized does not have a defined
value, hence it cannot be used until it is assigned such a value. Example:
13. Hint on variable:
Think of variable in programming language as a Post Office, to get the letter sent to
you in the post office, you will need a post office box right, then the box is like a
storage(memory) in programming, after the box you will need a name on the box,
think of the name as an identifier (variable name), getting to the post office to fetch
the letter you will provide the name of your box to the officer, after that he will search
through the boxes and get your box with your name which is the identifier, then the
letter in the box is the same as the data in the variable.
OPERATORS IN C PROGRAMMING
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built-in operators and provides the many types
of operators, in this chapter we will be looking into the most common operators used:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Well, in this chapter, we will be looking into the common operator way each operator
works.
14. ARITHMETIC OPERATORS
The table below shows all the arithmetic operators supported by the C language. Let’s
assume variable A holds 15 and variable B holds 30, then:
Operator Description Example
+ Adds two operands. A + B = 45
− Subtracts second operand from the first. A − B = -15
* Multiplies both operands. A * B = 450
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and remainder of after an integer
division.
B % A = 0
++ Increment operator increases the integer value by one. A++ = 16
-- Decrement operator decreases the integer value by one. A-- = 29
15. Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 15 and variable B holds 30 then −
Operator Description Example
== Checks if the values of two operands are equal or not. If
yes, then the condition becomes true.
(A == B) is not true.
!= Checks if the values of two operands are equal or not. If
the values are not equal, then the condition becomes
true.
(A != B) is true.
> Checks if the value of left operand is greater than the
value of right operand. If yes, then the condition becomes
true.
(A > B) is not true.
< Checks if the value of left operand is less than the value of
right operand. If yes, then the condition becomes true.
(A < B) is true.
>= Checks if the value of left operand is greater than or equal
to the value of right operand. If yes, then the condition
becomes true.
(A >= B) is not true.
<= Checks if the value of left operand is less than or equal to
the value of right operand. If yes, then the condition
becomes true.
(A <= B) is true.
16. Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then −
Operator Description Example
&& Called Logical AND operator. If both the
operands are non-zero, then the condition
becomes true.
(A && B) is false.
|| Called Logical OR Operator. If any of the two
operands is non-zero, then the condition
becomes true.
(A || B) is true.
! Called Logical NOT Operator. It is used to reverse
the logical state of its operand. If a condition is
true, then Logical NOT operator will make it false.
!(A && B) is true.
17. Assignment Operators
The following table lists the assignment operators supported by the C language:
Operator Description Example
= Simple assignment operator. Assigns values from right side
operands to left side operand C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the
left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right
operand from the left operand and assigns the result to the left
operand.
C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand
with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand with
the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two
operands and assigns the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
18. CONDITIONAL STATEMENTS
Conditional statements are those statements where a hypothesis is followed by a
conclusion. Conditional statements require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false. Example of a
common flowchart of conditional statement is as follows:
19. C programming language assumes any non-zero and non-null values as true, and
if it is either zero or null, then it is assumed as false value. The following types of
conditional statements are the statements in C programming language:
S/No. Statement & Description
1 IF statement: An if statement consists of a Boolean expression followed by one or more statements.
2 IF … ELSE statement: An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
3 Nested IF statement: You can use one if or else if statement inside another if or else if statement(s).
4 SWITCH statement: A switch statement allows a variable to be tested for equality against a list of
values.
5 Nested SWITCH statement: You can use one switch statement inside another switch statement(s).
25. LOOPS IN C PROGRAMMING
A Loop is a sequence of instructions that is continually repeated until a certain
condition is reached. In a program, you might need a code of block to run many
times until the conditions you want are met.
S/No. Loop Type & Description
1 While loop: Repeats a statement or group of statements while a given condition is
true. It tests the condition before executing the loop body.
2 For Loop: Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
3 do…while loop: It is more like a while statement, except that it tests the condition
at the end of the loop body.
4 Nested loops: You can use one or more loops inside any other while, for, or
do..while loop.
29. Example of a Nested loop: In this nested loop we will be looking at how to print
prime numbers between 2-100
30. FUNCTIONS IN C PROGRAMMING
Functions are a set of instructions bundled together to achieve a specific outcome. Functions are
a good alternative to having repeating blocks of code in a program.
A function is a group of statements that together perform a task. Every C program has at least one
function, which is main().
Most times, functions are also referred to as methods.
DEFINING A FUNCTION
The general form of defining a basic function most times is as follows:
• Return Type − A function may return a value. The return_type is the data type of the value the
function returns. Some functions perform the desired operations without returning a value. In this
case, void will be the return_type.
• Function Name − This is the actual name of the function. The function name and the parameter
list together constitute the function signature. It must match the convention of naming an
identifier.
• Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to
the parameter. This value is referred to as actual parameter or argument. The parameter
Parameters are optional; that is, a function may contain no parameters.
• Function Body − The function body contains a collection of statements that define what the
function does.
31. DECLARING AND CALLING A VARIABLE
A function declaration tells the compiler about a function name and how to call
the function. However we can define, declare a function at the same time. To use
a function after creating then the function must be called. Example:
32. Arrays are type of data structure that can store collection of elements of the same type.
An array is used in storing a group (or collection) of same data types. For example an int
array holds the elements of int types while a double can only holds its double. Arrays are
just like a library that can store books.
DECLARING ARRAYS
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows.
The arraySize is the number of data to be in the array and must be an integer constant
greater than zero and type can be any of the C data type. For example, to declare
an array of int of 5 elements named myarray; then it will be:
Here, myarray can only hold 5 elemnts(data) that is integer.
33. Arrays are type of data structure that can store collection of elements of the same type.
An array is used in storing a group (or collection) of same data types. For example an int
array holds the elements of int types while a double can only holds its double. Arrays are
just like a library that can store books.
INITIALIZING ARRAYS
There are two ways to initialize an array; You can either initialize all using one single
statement or one by one:
Index of an Array:
Index of an array is like an identifier of a
data in the array, the location of the
data.
Array indexing starts at 0.
Accessing an Array:
An array is accessed using the array
name and the index[identifier]. Example:
ARRAY
34. Yay! Hurray! You’ve learnt the basics of C programming.
Want to learn more?
Yes you can, with you knowing the basics of this, you can learn the intermediary
knowledge of C to build applications and solve real world problems.
Thanks for reading to the end, It’s nice walking you through the basics of C
programming.
Next, we will be focusing on solving basic problem using functions and loops.
References:
1. Tutorials point: https://www.tutorialspoint.com
2. Google: https://www.google.com