You are on page 1of 16

*****************INTRODUCTION TO C**********************

❖ History of C Language:
o C programming language was developed in 1972 by Dennis Ritchie at bell
laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
o Dennis Ritchie is known as the founder of the c language.
o It was developed to overcome the problems of previous languages such as B,
BCPL, etc.
o Initially, C language was developed to be used in UNIX operating system. It
inherits many features of previous languages such as B and BCPL.
❖ Type of functions:
o Two types of functions in c language:
▪ Built in function or Library function
▪ User defined functions
❖ Features Of c Language:
o C is the widely used language. It provides many features that are given below.
▪ Simple
▪ Machine Independent or Portable
▪ Mid-level programming language
▪ structured programming language
▪ Rich Library
▪ Memory Management
▪ Fast Speed
▪ Pointers
▪ Recursion
▪ Extensible
*****************STRUCTURE OF C**********************

❖ C Editors:

o Turbo C++: Release in 1990


: oldest Editor
o Dev C++
o VS Code, etc…

❖ Shortcut for Turbo C++:


o Full Screen: F5
o Save: F2
o Open: F3
o Undo: Alt+Bksp
o Cut: Shift+Del
o Copy: Ctrl+Ins
o Paste: Shift+Ins
o Windows full screen open: Alt + Enter
o Compile program: Alt+F9 OR Alt + C + Enter
o Run: Ctrl+F9 OR Alt+R+Enter
o Close file: Alt+F3
o Quit Turbo c++: Alt+x
❖ Example:

#include <stdio.h>

#include<conio.h>

void main()

{
clrscr();

printf("Hello, World!");

getch();

o #include <stdio.h> includes the standard input output library functions. The
printf() function is defined in stdio.h .
o #include <conio.h> includes the console input output library functions. The
getch() function is defined in conio.h file.
o void main() The main() function is the entry point of every program in c
language.

o printf() The printf() function is used to print data on the console.


o scanf() is one of the commonly used function to take input from the user.
o The getch() function asks for single character. Until you press any key, it blocks
the screen.

Clrscr(); Console screen clear

\n For new line

\+ For space

*****************Basic Data Types of C**********************

❖ The basic data types are integer-based and floating-point based. C language supports
both signed and unsigned literals.
Data Types Memory size Range Format specifiers
char 1 byte -128 to 127 %c
int 2 byte -32768 to 32767 %d
float 4 byte storing 7 decimal %f
digits
double 8 byte storing 15 decimal %lf
digits

1) Char:
Stores a single character/letter/number, or ASCII values
2) Int:
Stores whole numbers, without decimals
3) Float:
Stores fractional numbers, containing one or more decimals
4) Double:
Stores fractional numbers, containing one or more decimals

❖ Example:

Int mynumber = 10

Data Variable Value


Type Name
*****************Operators of C**********************

❖ An operator is simply a symbol that is used to perform operations.


❖ There are following types of operators to perform different types of operations in C
language.

• Arithmetic Operators
• Relational or Comparision Operators
• Assignment Operator
• Logical Operators
• Increment/Decrement
• Ternary or Conditional Operators
• Bitwise Operators

❖ Arithmetic Operators:

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
❖ Relational or Comparison Operators:

Operator Name Description Example


== Equal to Checks if the values of two operands x == y
are equal or not. If yes, then the
condition becomes true.
!= Not Equal to Checks if the values of two operands x != y
are equal or not. If the values are not
equal, then the condition becomes true.
< Less than Checks if the value of left operand is x<y
less than the value of right operand. If
yes, then the condition becomes true.
> Greater than Checks if the value of left operand is x>y
greater than the value of right operand.
If yes, then the condition becomes true.
<= Less than or equal Checks if the value of left operand is x <= y
to less than or equal to the value of right
operand. If yes, then the condition
becomes true.
>= Greater than or Checks if the value of left operand is x >= y
equal to greater than or equal to the value of
right operand. If yes, then the condition
becomes true.

❖ Assignment Operators:

Operator Same as Example


= X=Y x=y
+= X = X+Y x += y
-= X = X-Y x -= y
*= X = X*Y x *= y
/= X = X/Y x /= y
%= X = X%Y x %= y
❖ Logical or Conditional Operators:

Operator Name Description Example


&& logical AND It returns true when both conditions (x>y)&&(x>z)
are true.
|| logical OR It returns true when at-least one of the (x>=10)||(y>=10)
conditions is true.
! logical NOT It reverses the result – !((x>5)&&(y<5))

If “((x>5)&&(y<5))” is true, logical NOT


operator make it false.

❖ Increment/Decrement Operators:

Operator Name Description Example


++ Increment Increase variable value 1. x++
-- Decrement Decrease variable value 1. x--

❖ Ternary Operators:
o The conditional operator is also known as a ternary operator.
o The conditional statements are the decision-making statements which depends upon the
output of the expression. It is represented by two symbols, i.e., '?' and ':'.
o As conditional operator works on three operands, so it is also known as the ternary operator.
o Syntax:
▪ Test Condition? Expression1: Expression2;
• Expression1 (before the colon) – True
• Expression2 (after the colon) – False
o Ex:

x<y ? Printf(“true”) : printf(“false”);

Test expression 1 expression 2


Condition
❖ Bitwise Operators(Not For Use):
Operator Name
& and

| or

^ XOR

~ Complement

<< X = X/Y

>> X = X%Y

*****************Control Statement in C**********************


• The if-else statement in C is used to perform operation on the basis of condition.
• By using if-else statement, you can perform operation either condition is true or false.
• There are the following ways of if statement in C language.
1. If statement
2. If – else statement
3. If – else-if statement
4. Nested If statement
• Flowchart:
1) If Statement:
• if statement is the most simple decision-making statement.
• The single if statement in c language is used to execute the code if condition is true.
• Syntax:

If (condition)
{
// Statements to execute if
Printf(“”);
}
2) If - else Statement:
• The if-else statement is used to perform two operations for a single condition.
• The if - else statement in c language is used to execute the code if condition is true or false.
• Syntax:

if (condition)
{
// Executes this block if
// condition is true
Printf(“”);
}
else
{
// Executes this block if
// condition is false
Printf(“”);
}

3) If – else-if Ladder Statement:


• Here, a user can decide among multiple options.
• The if else – if statement is used to execute one code from multiple conditions.
• Syntax:

if(condition1)
{
// statement(s);
Printf(“”);
}
else if(condition2)
{
//statement(s);
Printf(“”);
}
.
.
else if (conditionN)
{
//statement(s);
Printf(“”);
}
else
{
//statement(s);
Printf(“”);
}

4) Nested If Statement:
• A nested if in C is an if statement that is the target of another if statement.
• Nested if statements mean an if statement inside another if statement.
• i.e, we can place an if statement inside another if statement.
• Syntax:

if (condition1)
{
// Executes when condition1 is true
Printf(“”);
if (condition2)
{
// Executes when condition2 is true
Printf(“”);
}
}

*****************Switch Case Statement in C**********************


• The switch statement allows us to execute one code block among many alternatives.
• The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple
operations for the different possibles values of a single variable called switch variable.
• Here, We can define various statements in the multiple cases for the different values of a single variable.
• Syntax:

switch(expression) {
case 1 :
Printf(“”);
break; /* optional */

case 2 :
Printf(“”);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
Printf(“”);
}
• Rules:

o The switch expression must be of an integer or character type.


o The case value must be an integer or character constant.
o The case value can be used only inside the switch statement.
o The break statement in switch case is not must. It is optional. If there is no break statement found in
the case, all the cases will be executed present after the matched case. It is known as fall through the
state of C switch statement.
o Let's try to understand it by the examples. We are assuming that there are following variables.
▪ int x,y,z;
▪ char a,b;
▪ float f;

Valid Switch Invalid Switch Valid Case Invalid Case


switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;

*****************LOOP in C**********************

• The looping can be defined as repeating the same process multiple times until a specific condition satisfies.
• There are the following 3 ways of Loop in C language.
o For Loop
o While Loop
o Do While Loop
o Nested Loop

• Syntax Point:
o Initialization
o Condition
o Increment/Decrement

1) For Loop:
o Like while, it iterates the code until condition is false.
o Here, initialization, condition and increment/decrement is given before the code.
So code may be executed 0 or more times.
o Syntax Point:
- Initialization
- Condition
- Increment/Decrement
o Syntax:

for(Initialization; Condition; Increment/Decrement) {


//code to be executed

Printf(“”);

2) While Loop:
o Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
o Syntax Point:
- Initialization
- Condition
- Increment/Decrement

o Syntax:

while(Condition) {
//code to be executed
Printf(“”);

Increment/Decrement;

3) Do While Loop:
o It is more like a while statement, except that it tests the condition at the end of the loop body.
o Syntax Point:
- Initialization
- Increment/Decrement
- Condition
o Syntax:

Do {
//code to be executed
Printf(“”);

Increment/Decrement;

} while(Condition);

4) Nested Loop:
o Like while, it iterates the code until condition is false.
o Here, initialization, condition and increment/decrement is given before the code.
So code may be executed 0 or more times.
o Syntax:
for(Initialization; Condition; Increment/Decrement) {
//code to be executed
Printf(“”);

for(Initialization; Condition; Increment/Decrement) {


//code to be executed
Printf(“”);

}
*********************STATEMENT in c**********************

❖ Break Statement:
o The break is a keyword in C which is used to bring the program control out of the loop.
o The break statement is used inside loops or switch statement.
o The break statement in C can be used in the following two scenarios.
a. With switch case
b. With loop
o Syntax:
//loop or switch case

break;

❖ Continue Statement:
o The continue statement in C language is used to bring the program control to the beginning of the
loop.
o It is mainly used for a condition so that we can skip some code for a particular condition.
o Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped

❖ Go To Statement:
o The goto statement is a jump statement which is sometimes also referred to as unconditional jump
statement.
o The goto statement can be used to jump from anywhere to anywhere within a function.
o Syntax:

goto label;
..
.
label: statement;

***************************Array in C******************************

o Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value.
o To create an array, define the data type (like int) and specify the name of the array followed by square
brackets [].
o The size of the array should be mentioned while declaring it.
o Array elements can be accessed using the position of the element in the array.
o They can be used to store the collection of primitive data types such as int, float, double, char, etc of
any particular type.
o Advantages:
o Code Optimization: we can retrieve or sort the data efficiently.
o Random access: We can get any data located at an index position.
o Easy of sorting: To sort the elements of the array, we need a few lines of code only.
o Disadvantages:
o Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its size at
runtime.

o Declaration of C Array:

- We can declare an array in the c language in the following way.

• data_type array_name[array_size];

You might also like