INTRODUCTION TO C Language New-1
INTRODUCTION TO C Language New-1
❖ 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:
#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.
\+ For space
❖ 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
• Arithmetic Operators
• Relational or Comparision Operators
• Assignment Operator
• Logical Operators
• Increment/Decrement
• Ternary or Conditional Operators
• Bitwise Operators
❖ Arithmetic Operators:
❖ Assignment Operators:
❖ Increment/Decrement Operators:
❖ 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:
| or
^ XOR
~ Complement
<< X = X/Y
>> X = X%Y
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(“”);
}
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(expression) {
case 1 :
Printf(“”);
break; /* optional */
case 2 :
Printf(“”);
break; /* optional */
*****************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:
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(“”);
}
*********************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:
• data_type array_name[array_size];