SlideShare a Scribd company logo
1 of 34
Download to read offline
C PROGRAMMING LANGUAGE
BASICS OF C PROGRAMMING TUTORIAL
BY UNIMAID ENGINEERING STUDENTS - 19/20
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. Diving into C programming.
 Introduction to C programming
 Program structure
 Basic syntax
 Data types
 Variables
 Operators
 Conditional Statements
 Loops
 Functions
 Arrays
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.
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:
 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"
The output of the above code is as shown below:
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.
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
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).
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:
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:
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.
 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
 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.
 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.
 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
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:
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).
Example of IF statement:
Example of IF…ELSE statement:
Example of Nested IF statement:
Example of SWITCH statement:
Example of Nested SWITCH statement:
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.
Example of while loop:
Example of a for loop:
Example of a do...while loop:
Example of a Nested loop: In this nested loop we will be looking at how to print
prime numbers between 2-100
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.
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:
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.
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
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

More Related Content

Similar to C programming language tutorial for beginers.pdf

1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptANISHYAPIT
 
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshuunit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshuGauravRawat830030
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdfAdiseshaK
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdfRajb54
 
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTC programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTBatra Centre
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotesSowri Rajan
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer muniryaseen
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxsaivasu4
 

Similar to C programming language tutorial for beginers.pdf (20)

1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshuunit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
 
C programming notes
C programming notesC programming notes
C programming notes
 
C programme presentation
C programme presentationC programme presentation
C programme presentation
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
 
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTC programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 
Lect '1'
Lect '1'Lect '1'
Lect '1'
 
C Language
C LanguageC Language
C Language
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
 
C language
C language C language
C language
 

Recently uploaded

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Recently uploaded (20)

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

C programming language tutorial for beginers.pdf

  • 1. C PROGRAMMING LANGUAGE BASICS OF C PROGRAMMING TUTORIAL BY UNIMAID ENGINEERING STUDENTS - 19/20
  • 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"
  • 7. The output of the above code is as shown below:
  • 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).
  • 20. Example of IF statement:
  • 21. Example of IF…ELSE statement:
  • 22. Example of Nested IF statement:
  • 23. Example of SWITCH statement:
  • 24. Example of Nested SWITCH statement:
  • 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.
  • 27. Example of a for loop:
  • 28. Example of a 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