History of C Language:
- 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.
- Dennis Ritchie is known as the founder of the c language.
- It was developed to overcome the problems of previous languages
such as B, BCPL, etc.
- Initially, the C language was developed to be used in the UNIX
operating system. It inherits many features of previous languages
such as B and BCPL.
Type of functions:
- Two types of functions in c language:
1. Built in function or Library function
2. User defined functions
Editors of C Language:
- Turbo C++: Release in 1990
: oldest Editor
- Dev C++
- VS Code, etc…
Shortcut for Turbo C++:
- Full Screen: F5
- Save: F2
- Open: F3
- Undo: Alt+Bksp
- Cut: Shift+Del
- Copy: Ctrl+Ins
- Paste: Shift+Ins
- Windows full screen open: Alt + Enter
- Compile program: Alt+F9 OR Alt + C + Enter
- Run: Ctrl+F9 OR Alt+R+Enter
- Close file: Alt+F3
- Quit Turbo c++: Alt+x
\n For new line
\t For space
Structure of C Language:
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello, World!");
getch();
}
● #include <stdio.h> includes the standard input output library
functions. The printf() function is defined in stdio.h .
● #include <conio.h> includes the console input output library
functions. The getch() function is defined in conio.h file.
● void main() The main() function is the entry point of every program
in c language.
● clrscr() This function is used to clear the previous output from the
console.
● printf() The printf() function is used to print data on the console.
● scanf() is one of the commonly used function to take input from the
user.
● The getch() function asks for single character. Until you press any key,
it blocks the screen.
Basic Data Types of C:
- The basic data types are integer-based and floating-point based.
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 digits %f
double 8 byte storing 15 decimal digits %lf
● Char: Stores a single ;character/letter/number, or ASCII values.
● Int: Stores whole numbers, without decimals.
● Float: Stores fractional numbers, containing one or more decimals.
● 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:
1. Arithmetic Operators
2. Relational or Comparison Operators
3. Assignment Operator
4. Logical Operators
5. Increment/Decrement
6. Ternary or Conditional Operators
7. Bitwise Operators
1. 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
% Modulo Returns the division remainder x%y
2. Relational or Comparison Operators:
Operator Name Description Example
== Checks if the values of two operands x == y
Equal to are equal or not. If yes, then the
condition becomes true.
!= Checks if the values of two operands x != y
Not Equal to 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.
3. 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
4. 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 (x>=10)||(y>=10)
the 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.
5. Increment/Decrement Operators:
Operator Name Description Example
++ Increment Increase variable value 1. x++
-- Decrement Decrease variable value 1. x--
6. Ternary Operators:
- The conditional operator is also known as a ternary operator.
- The conditional statements are the decision-making statements
which depend upon the output of the expression. It is represented by
two symbols, i.e., '?' and ':'.
Syntax:
Test Condition? Expression1: Expression2;
- Expression1 (before the colon) – True
- Expression2 (after the colon) – False
Example:
x<y ? Printf(“true”) : printf(“false”);
Test expression1 expression 2
Condition
7. 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 an if-else statement, you can perform an operation whether the
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
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
the 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 the
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.
- 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 alternative to if-else-if ladder
statement which allows us to execute multiple operations for the
different possible values of a single variable called switch 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:
- The case value must be either int or char type.
- There can be any number of cases.
- No duplicate case values are allowed.
- Each statement of the case can have a break statement. It is optional.
- The default Statement is also optional.
Valid switch expression:
- switch(a)
- switch(a + b + c)
- switch(ch1 + a)
- switch(a < b)
- switch(my_func(12))
- switch('a')
Invalid switch expression:
- switch(a + 12.2) // expression must yield an integer value not
double or float.
- switch(f1)
- switch("string") // string is not allowed.
Valid case constant:
- case 1
- case 1 + 2
- case 'a'
- case 'a' < 'b'
Invalid case constant:
- case "string" // string constants are not allowed
- case 1.2 // floating point constants are not allowed
- case a // variables are not allowed
- case a + b // variables are not allowed
- case 1,2,3 // each case must contain only one constant
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.
1. For Loop
2. While Loop
3. Do While Loop
4. Nested Loop
Syntax Point:
- Initialization
- Condition
- Increment/Decrement
1. For Loop:
- Here, initialization, condition and increment/decrement is
given before the code. So code may be executed 0 or more
times.
Syntax Point:
- Initialization
- Condition
- Increment/Decrement
Syntax:
for(Initialization; Condition; Increment/Decrement) {
//code to be executed
Printf(“”);
}
2. While Loop:
- Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
Syntax Point:
- Initialization
- Condition
- Increment/Decrement
Syntax:
while(Condition) {
//code to be executed
Printf(“”);
Increment/Decrement;
}
3. Do While Loop:
- It is more like a while statement, except that it tests the
condition at the end of the loop body.
Syntax Point:
- Initialization
- Increment/Decrement
- Condition
Syntax:
Do {
//code to be executed
Printf(“”);
Increment/Decrement;
} while(Condition);
4. Nested Loop:
- Like while, it iterates the code until the condition is false.
- Here, initialization, condition and increment/decrement is
given before the code. So code may be executed 0 or more
times.
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:
- The break is a keyword in C which is used to bring the program
control out of the loop.
- The break statement is used inside loops or switch statement.
Syntax:
//loop or switch case
break;
● Continue Statement:
- The continue statement in C language is used to bring the program
control to the beginning of the loop.
- It is mainly used for a condition so that we can skip some code for a
particular condition.
Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped
● Go To Statement:
- The goto statement is a jump statement which is sometimes also
referred to as an unconditional jump statement.
- The goto statement can be used to jump from anywhere to anywhere
within a function.
Syntax:
label:
statement;
goto label;
Array in C:
- Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
- To create an array, define the data type (like int) and specify the name of
the array followed by square brackets [].
- The size of the array should be mentioned while declaring it.
- Array elements can be accessed using the position of the element in the
array.
- They can be used to store the collection of primitive data types such as int,
float, double, char, etc of any particular type.
One Dimensional Array in C:
Declaration of C Array:
- We can declare an array in the c language in the following way.
Syntax:
data_type array_name[array_size];
Example:
int subject[5];
Initialization of C Array:
- The simplest way to initialize an array is by using the index of each
element.
- We can initialize each element of the array by using the index.
Example:
subject [0]=80;
subject [1]=60;
subject [2]=70;
subject [3]=85;
subject [4]=75;
Declaration with Initialization:
- We can initialize the c array at the time of declaration.
Example:
int subject[5]={20,30,40,50,60};
Two Dimensional Array in C:
- An array of arrays is known as 2D array. The two dimensional (2D)
array in C programming is also known as matrix.
- A matrix can be represented as a table of rows and columns.
Declaration of two dimensional Array:
Syntax:
data_type array_name[rows][columns];
Example:
int arr[4][3];
Initialization of 2D Array:
Example:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
String in C:
- Strings are used for storing text/characters. Use double quotes.
- To output the string, you can use the printf() function together with the
format specifier %s.
Syntax:
data_type name[size] = value;
Example 1:
char academy[12] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘S’, ‘k’, ‘y’, ‘w’, ‘I’, ‘n’, ‘!’};
printf(“%s”, academy);
Example2:
char academy[] = "Hello Skywin!";
printf(“%s”, academy);
Access Strings:
- Since strings are actually arrays in C, you can access a string by referring to
its index number inside square brackets [].
- we have to use the %c format specifier to print a single character.
Example 1:
char academy[] = "Hello Skywin!";
printf(“%c”, academy[0]);
Output: H
Example 2:
char academy[] = "Hello Skywin!";
academy[0]=’A’;
printf(“%s”, academy);
Output: Aello Skywin!