0% found this document useful (0 votes)
37 views

Programming Concept

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Programming Concept

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Programming Concept

Lesson -4
Overview of C language
• Developed by Dennis Ritchie at Bell Laboratories in 1972.
• Procedural language which follows structure.
• Supports various operating systems and hardware platforms.
• Many compilers are available for executing programs written in “C”.
• Executable creation from a c-program in a two step process
1) Compilation
2) Linking
• The C language is highly efficient programming language and easy to
understand.
• It has both the properties of high level language and low level
language, so it is also term as Middle Level Language or intermediate
language between high level language and low level language.
• It is very powerful programming language because it is used to
prepare system software as well as application software.
• It is a kind of general purposed, structured programming language.
• It has large numbers of vocabularies and simple syntax to write a
program.
Pre-processor and Header Files
• Pre-processor directive is special type of program that processes the
code before it passes through the compiler. The common pre-
processor directive is #include which helps to include the necessary
header files for further execution. The header file is the main file of c
compiler that contains inbuilt library functions. For example:
#include<stdio.h>

Preprocessors and Header files


void main() /* comments*/
{
Declaration part
Executable part
}
void main function ()
• Every c program must contain main () function.
• The instructions are written inside the scope i.e. area of the main
function.
• Mainly it has two part: declaration part and executable part. The
declaration part declares the variables or constant, etc.
• The a program codes are written in the executable part.
Comments
• The comments are not the internal part of program codes. It is
optional but writing comments helps to understand the flow of
programs and good practice for internal documentation of
programmer. It is also useful for further modification and program
testing.
• *// Single Line Comments
• /* Multi-Line …………………………………………………… comments*/
Example Program to print Hello World on
screen.

/*Program to Print Hello World"/ ← Comments


#include<stdio.h> ← Preprocessor and Header File
void main() ← Main Function
{ ← Scope Start

printf("Hello World"); ← Output Function to Print

} ← Scope End
Completion Process
• The completion process is the process of converting source file (program codes) into
executable file (machine codes or object codes). This is automatically performed by
the compiler. The compilation process is explained in the following figure.
• Step 1 Use text editor like to write your source code and save the file with extension
d c. Generally we prefer to use Turbo c or C++.
• Step2 Compile the program using a compiler. It converts into machine codes called
object file.
• Step 3 Link the program using a linker. If no errors occur, the linker produces an
executable program located in a disk file with .exe extension Turbo C or C++
automatically performs this task.
• Step 4 Execute the executable file as program.
Header Files
• Header files are standard library files that must be included before
program compilation and pre-processor directives help to include the
header files.
• Header files are standard library file of C-programming language
which contents the detail information about variables, constant,
operators, expressions, library functions, etc. These necessary header
files must be included in our program codes before program
compilation otherwise the compiler does not execute our program
codes. The pre-processor directives help to include or link these
header files to our program codes. Example of pre-processor
directives are: #include, #define, etc. Some of the header files are
listed below.
• #include<stdio.h>: It is the main standard input output header file. It
contains the
function definition of input output functions such as scanf(), printf()
etc.
• #include<conio.h>: It is a header file which is included in the program
this is used to use, clrscr(), getch(), etc. We do not need to include this
file in C program. It is necessary for C programs. conio stands for
console Input Output header file. The clrscr() helps to clear the screen.
• #include<math.h>: This header file is used for mathematical functions
such as pow(), sqrt(), sin(), tan(), cos() etc.
• #include<string.h>: It is string header file. It contains the function
definition of string processing functions such as strlen(), strcat(),
strcpy() etc.
Character Set used in C
• The character set defines a group of letters, words, numbers, symbols
and express which is used in C language. The character set in C
language can be grouped into following three categories.
• Letters upper case A to Z and lowercase a to z
• Digits: 0 to 9
• Special Characters
 Tokens
• The smallest part of C programming language is called C tokens.
Keyword:
• There are certain words which are reserved by the C compiler . These
words are known as keywords. Mainly , there are 32 keywords used in
C language .
Identifiers
• Identifiers are the names given to program unit such as variable,
structure, function etc. They are not defined in programming
language but are used to define by the programmer. Some basic rules
to define identifiers are given below.
• First character must be an alphabet or underscore than digits or
alphabets It must consist of only letters, digits and underscore.
• Any standard C language keyword cannot be used as identifier name.
• It should not contain a space.
• It allows both upper case and lower case characters.
Basic Data types in C
•  A data might be in the form of number, character, string or record
and they are called data types.
• Mainly C language provides two types of data: primary data types and
secondary data types.
Primary Data Types
• The fundamental data types are called primary data types. 
• singed/unsigned and long/short prefix are used before primary data types. Singed da type defines both
positive and negative values. For example:

signed int x; //it holds both positive and negative numbers

unsigned int x; // it holds positive numbers

The long data type helps us to maximize the range of values and the short data types minimizes the
range of values for basic data types.

short int x; //it minimizes the range of values than ordinary integer value.

long int x; // it maximizes the range of vales than ordinary integer value.
Variable
• A variable is a value that can change any time. It is a memory location
used to store a data value. Any variable declared in a program should
confirm to the following:
• They must always begin with a letter, although some systems permit
underscore as first character.
• The White space is not allowed
• A variable should not be a keyword.
• It should not contain any special characters.
• It does not allow keyword.
• Numeric variable: The variable that stores numeric data only is called numeric variable.
The numeric data may be whole number or fractional number. Examples of numeric
variables are integer, floating point and double.

• String variable: The variable that stores character data only is called string variable. The
string data may be single character or string. Examples of string variable are: character,
array of character (string), table of strings.

The purpose of variable declaration is to allocate memory space inside a memory of


computer.
• Declaration does two things:
It tells the compiler what the variable name is.
It specifies what type of data the variable will hold.
Constant
• A constant value is the one which does not change during the
execution of program.
• C supports several types of constants. They are:-
• 1) Integer Constant: An integer constant is a sequence of digits. There
are 3 types of integers integer, octal integers and hexadecimal integer.
These constants are given below
• A) Decimal Integers: 123 -31 0 +78
• B) Octal Integers: O26 O O347 O676
• C) Hexadecimal integer:  OX2 0X8C OXbcd
• Character Constant
A character constant stores only single character. It is enclosed by
single quotation mark.
• Examples of character constants are:
'A’ 'B‘ ‘2’ '6‘

• String Constant:
A string constant is a set of characters enclosed in double quotation
marks. The characters in a string constant sequence may be a
alphabet, number, special character and blank space.
• Example of string constants are

"VISHAL“ "1234“ "God Bless"


Symbolic Constant
• A symbolic constant is a type of constant that substitutes the value or
expression to the constant name. It is defined by using the pre-
processor #define.
Syntax:
#define constant_name constant_value

Example
#define PI 3.14
#define name "HSEB"
#define area 100
Escape Sequence Constant
• The non-printing characters started with slash(\) are called escape
sequence constants. They are special characters used in output
functions.
Escape Sequence Meaning
\a Audible Alert(bell)
\b Backspace
\n New Line
\r Carriage Return
\t Horizontal tab
\v Vertical tab
DIFFERENCES BETWEEN VARIABLE
AND CONSTANT
VARIABLE CONSTANT
• It is a name whose value can be • It is a name whose value cannot be
changed during the execution of a changed during the execution of a
program program.
• The purpose of variable declaration is • The purpose of constant declaration is
to allocate to allocate memory space inside a
memory space inside a memory of memory of computer and assign value
computer. in it.
• Syntax • Syntax:
data type variablename; data _type constantname = value;
• For example • For example
int a; e.g. int a = 5; float b= 10.9;
float b;
Type of Specifier
• it is also known as format specifier. Type of specifier controls the type
and format of the value to be printed or entered. Each conversion
specification begins with % sign and ends with a conversion
character. Specifier character Meaning
C Single character
d Decimal integer
f Floating point without exponent form
i Signed decimal integer
o Octal integer without leading zero
s String
x Hexadecimal integer, without leading zero
ld Long integer
Simple and Compound Statements
• Simple Statement: A simple statement consists of an expression followed by a semicolon.
• Examples

b=5;

c=d+e;
printf("Enter a number");

• Compound Statement
A compound statement consists of several individual statements enclosed in a pair of braces { }. It provides capability for
embedding statements within other statements.

Example
{

r-5;
area=3.14*r*r;

It shows a compound statement to calculate area of a circle


OPERATOR:
• An operator is defined as a meaningful symbol which helps to carry
out specific calculations or logical comparison. The different types of
operator are:
1) Arithmetic Operators: The types of operator which perform
arithmetic operation such as addition, subtraction, multiplication etc
called as arithmetic operator.
Relational Operators:
• The types of operator which is required to compare the relationship
between operands and bring out a decision accordingly is called
Relational Operator. It is also called comparison operator because it is
used to compare any two expressions.
Logical Operators
• Logical operators are those operator which are used to give logical
value either true or false.
Assignment Operator
• The assignment operator is used to assign the value of expression into
variable. It is written in the form:
variable = expression
• For example:
• X= A+B;
• Unary Operators
• Unary operator are those operator which are used in C programming for increment and decrement of
the value . They extensively used in loops. The syntax of the operators is given below:
• 1. ++ variable name (increment prefix)
• 2. variable name++ (increment postfix)
• 3 --variable name (decrement prefix)
• 4.variable name-- (decrement postfix)

The increment operator ++ adds the value 1 to the current value of operand and the decrement
operator - - subtracts the value 1 from the current value of operand.

Consider the following


m = 5;
y = ++m; //prefix operation (first increment then assignment)
In this case the value of y and m would be 6.

Suppose if we rewrite the above statement as,


m = 5;

• y = m++; //postfix operation (first assignment then increments) Then the value of y will be 5 and that of
m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable
on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and
then increments the operand.
Ternary Operator (?: operator)
• Ternary operator is also known as conditional operator as it checks condition to make decision. It uses two symbol: the
question mark (?) and the colon (:) but not together as in >=, <= etc. This is the only operator used in C that takes three
operands, so it is named as ternary operator.
• Syntax

(condition)? statement1: statement2

In this operator, at first condition is checked and if it is found true then statement will be executed otherwise statement2
will be executed which is similar to the working style of if else statement

Example

a = 10;

b=15;
• x=(a> b) ?a:b;

Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
The Comma Operator
• The comma operator can be used to link related expressions together.
Comma-linked lists of expressions are evaluated left to right and value
of right most expression is the value of the combined expression.
Example
• value = (x = 10, y = 5, x+y);

First assigns 10 to x and 5 to y and finally assigns 15 to value. Since


comma has the lowest precedence in operators the parenthesis is
necessary.
Expressions
• An expression is a combination of variables, constants and operators
written according to the syntax of C language.
Type Casting and Conversions:
•  Implicit Type Conversion:
• C permits mixing of constants and variables of different types in an expression. C automatically converts any
intermediate values to the proper type so that the expression can be evaluated without losing any significance. This
automatic type conversion is known as implicit type conversion

Example:

float x;

int x1=5;

int x2=2;

x=x1/x2:

printf("Output is %f", x);

Output is 2.000000
Explicit type Conversion:
• There may arise a situation where we want to force a type conversion in a way that is different
from automatic conversion. The process of such conversion is known as explicit conversion or
casting a value. The syntax is: (type_name) expression Example:

float x;

int x1=5;

int x2=2;

x=(float)x1/x2;

printf("Output is %f",x);

Output is 2.500000
Library Functions
• The functions that are pre-defined in programming language are
called library functions.
Control Structure :
• It is defined as the group of statement or function that
helps to control the flow of instruction .
• It allows programmer to control the flow of program
statement execution in a program.
Types :
• There are mainly three types of control statement :
a.Branching statement (selective )
b.Looping statement ( Iterative or Repetitive)
c.Jumping statement (unconditional )
1. Branching Statement
if statement
• It is the simplest form of conditional statement in which
statement are executed if the test expression is true.
• Syntax :
If ( condition )
{
statement ;
}
Example program to demonstrate the use of if
statement .
If else statement
• It is selective control structure which can handle both excepted as well as
unexpected situation.
• In this control structure, statements written in body part of if are executed if
the condition is true otherwise statements written in body part of else are
executed.
• Syntax:
• If(condition)
statement 1;
else
statement 2;
Example program to demonstrate the use of if
statement .
• #include<stdio.h>
• #include<conio.h>
• Void main()
•{
• Int a,b;
• Printf(“\n enter two number”);
• Scanf(“%d %d”, &a, &b);
• If(a>b)
• Printf(“\n%d is larger”, a);
• Else
• Printf(“\n%d is larger”,b);
•}
If else if statement
• When we have two or more conditions to be checked in a series we can use if-else if statement.
syntax:
if (condition1)
statement1;
Else if(condition 2)
statement2;

Else if (condition n-1)


Statement n-1;
Else
Statement n;
Example program to demonstrate the use of if
statement .
#include <stdio.h>
Void main()
{
Int a,b,c;
Printf(“\n enter any three numbers:”);
Scanf(“%d%d%d”, &a, &b, &c);
If (a>b && a>c)
Printf(“\n%d is the largest number”,a);
Else if (b>c)
Printf(“\n%d is the largest number”, b);
Else
Printf(“\n%d is the largest number”, c);
}
Nested if else statement:
• An entire if statement written within the body of if part or else part of another if else statement is
called nested if else statement.
Syntax:
If (condition1)
{
IF (condition 2)
statement 1;
Else
Statement 2;
}
ELSE
Statement 3;
Example program to demonstrate the use of if
statement
#include<stdio.h>
#include <conio.h>
Void main()
{
Int eng, math, comp, che, phy, total;
Float per;
Printf(“\n enter the marks of English”);
Scanf(“%d”, &eng);
Printf(“\n enter the marks of Math”);
Scanf(“%d”, &math);
Printf(“\n enter the marks of computer”);
Scanf(“%d”, &comp);
Printf(“\n enter the marks of chemistry”);
continue
• Scanf(“%d”, &che);
• Printf(“\n enter the marks of physics”);
• Scanf(“%d”, &phy);
Total= eng+math+comp+che+phy
Per= (float) total/5
Printf(“\n total obtained marks=%d\n percentage=%f”,total, per);
If(eng>=35 && math>=35 && comp>=35 && phy>=35 && che>=35)
{
If(per>=75)
Printf(“\n Distinction”);
Else if(per>= 60)
Printf(“\n First Division”);
Else if(per>=45)
Printf(“\n second division”);
continue
Else
Printf(“\n Third Division”);
}
Else
Printf(“\n you are failed. Word hard”);
}
Switch case
• Switch statement is a multipath decision making statement that allows selection and execution of a
block of statements from several blocks of statements based upon the value of expression which is
included within switch statement and branches accordingly.
• The expression may consist of integral value or character constant.
• Syntax:
• Switch(expression)
{ case constant1:
Statement1;
Break;
Case constant2:
Staement2;
Break;
--------------
Case constant n:
Statement n;
Break;
Default:
Statement;
}
Write a program which reads any two integer
values from user and calculates sum,
difference and product using switch case?
void main()
{
Int a,b, c, ch;
Printf(“\n enter two numbers”);
Scanf(“%d%d”, &a, &b);
Printf(“\n 1.sum”);
Printf(“\n 2. difference”);
Printf(“\n 3. product”);
Printf(“\n enter your choice(1-3)”:);
Scanf(“%d”, &ch);
Switch(ch)
{
Case1: c=a+b;
continue
Printf(“\n sum of two numbers=%d”,c);
Break;
Case2: C=a-b;
Printf(“\n difference of two numbers= %d”,c);
Break;
Case3: C=a*b;
Printf(“\n product of two number =%d”,c);
Break;
Default:
Printf(“\n wrong choice!!! Please enter 1/2/3 only”);
}
Getch();
}
Looping:

• Looping is the process of executing the same program statement or


block of program statements repeatedly for specified number of times
or until the given condition is satisfied.
• Mainly there three types of loop used in c language:
• For , while and do while loop.
While loop

It executes the program statements repeatedly until the given condition is
true. It check condition at first if it is found true then it executes the
statements written in its body par otherwise it just gets out from the loop
structure. It is also known as entry control or pre-test loop.
• Syntax:
Initialization;

while(condition)
{

statements:
increment/decrement:
}
Write a program to display "C is the best"
10 times using while loop.
#include<stdio.h>

void main()
{
int i=1;

while(i<=10)
{
Printf(“\n c is the best “);
i++;
}
Getch();
}
Write a program to display numbers from 1 to
10.
#include<stdio.h>

void main()
{

int i=1;

while(i<=10)
{

printf("%d\t“,i);

i++;
}
}
Write a program to calculate and display sum
of the numbers from 1 to 10
#include<stdio.h>
void main()
{

int i=1, sum=0;


While(i<=10)
{
Sum = sum + I;
i++;
}
Printf(“\n sum of numbers from 1 to 10 =%d”, sum);
}
Assignment
Write program using while loop to

1. display your name 10 times on screen

2 display the series 10 9 8 --------- to 1.

3. calculate and display sum of natural numbers up to n.


do while loop
• It also executes program statements repeatedly until the given condition is true. It executes
the program statements once at first then only condition is checked. If condition is found true
then it executes the program statements again, otherwise it gets out from the loop structure.
As it checks condition at last it is also known post-test loop or exit control loop.
• Syntax:
Initialization
Do
{
Statements;
Increment/decrement;
}
While(condition);
Write a program to display the series 1 6 11
16 ………………… 101.
#include<stdio.h>
Void main
{
Int i=1;
Do
{
Printf(“%d\t”, i);
i= i+5;
} while( i<=101);
}
Differences between while loop and do while
loop
While loop Do while loop
• In this loop, condition is checked in • In this loop, condition is checked at
the the end.
• Also known as post-test or exit
beginning.
control loop.
• Also known as pre-test or entry
• It is terminated with semicolon.
control loop.
• it is not terminated with semicolon. • In do while loop, statements are
executed even the condition is false.
• In while loop, statements are not
executed if the condition is false. • It uses two keywords 'do' and
'while.
• It uses keyword 'while‘
• The syntax of do while loop is as
• The syntax of while loop is as follows:
follows:
do your self • Do your self
Write a program to display the series 5 9
13 ………….. Up to tenth term.
#include<stdio.h>
Void main()
{
Int i=1, n=5;
Do
{
Printf(“%d\t”, n);
N=n+4;
i++;
}
while(i<=10);
Getch();
}
Write a program to display multiplication table of 6.

#include<stdio.h>
Void main()
{
Int i= 1, p;
Do
{
P= 6*i;
Printf(“\n6 * %d= %d”, i,p);
i++;
}
while(i<=10);
Getch();
}
Assignment
• Write programs using do while loop to

1 calculate and display the series 1 8 27 …………………..to 10th term.


• 2. display the series: 1 5 9... up to 20 term.

3. display multiplication table of any number given by the user.


Jumping statement
• Jumping statements are particularly used to jump
execution of program statements from one place to
another place inside a program .
Types of Jumping statement

1
• break

2
• continue

3
• goto statement
Break statement :
• When a break statement is encountered inside a
loop, the loop is immediately terminated and the
program control resumes at the next statement
following the loop.
• Example :
• Output : 1 2 3
Continue Statement :
• When a continue statement is encountered inside a loop,
then that particular iteration is skipped and the loop will
continue with the next iteration .
• Example :
• Output : 1 2 3 5
goto statement
Write a program to display numbers from 1 to
10 using go to statement.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int i=1;
Label1:
Printf(“%d\n”,i);
i++;
If(i<=10)
Goto label1;
Getch()
}
1. Write a program to display the following Fibonacci series: 1 1 2 3 5...to nth term.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a=0, b=1,c=1,n;
printf("\nEnter the number of terms to be displayed:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",c);
c=a+b;
a=b;
b=c;
getch();
}
2.Write a program to calculate and display sum of the digits present in the given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r, sum=0;
Printf ("\n Enter a number:");
Scanf (“%d“ , &n);
do
{
r=n%10;
sum= sum+r ;
n=n/10;
} while(n!=0);
printf("\nSum of the digits %d",sum);
getch();
}
3. Write a c program to check the
given number is palindrome or not .
Void main()
{
Int n, temp,sum=0,d;
Printf(“\n enter a number”);
Scanf(“%d”,&n);
Temp=n;
While(n!=0)
{
d= n%10;
Sum=sum*10+d;
n=n/10;
}
If(temp==sum)
Printf(“\n the number is palindrome”);
Else
Printf(“\n the number is not palindrome”);
}
Write a c program to check the given
number
Void main()
is prime or composite.
{
Int n, i;
Printf(“\n enter number to check prime”);
Scanf(“%d”, &n);
For (i=2;i<=n;i++)
{
If(n%i==0)
Break;
}
If(i==n)
Printf(“\n the given number is prime”);
Else
Printf(“\n the given number is composite”);
}
Array :
• Introduction to Array :
An array is defined as the collection of similar type of data items
treated as a single unit.

Characteristics of Array :
All the array elements share the common name.
The elements of array are stored in contiguous location.
Programs becomes short and simple which handles large volume
of similar kinds of data items.
Disadvantages of array
• Not possible to hold dissimilar type of data.
• It is static in nature . So, it is difficult to define the
size of array during running time.
Types of array :
• There are two types of array :
Array
One Dimensional Array

Multi-Dimensional Array
1.One Dimensional Array :
Array Initialization

• We can initialize array at the time of declaration.


• Syntax : data_type array_name[size]={var1, var2,……..,var n};
• Var1 is the first element , Var2 is the second element of array .
Example :
int marks[5]={50,60,70,80,90};
Here only one subscript is present which determines the dimension of the array.
Write a program to input 5 numbers in an array
and display them .
Void main()
{
Int i, num[5];
For(i=0;i<5;i++)
{
Printf(“\n enter array elements”);
Scanf(“%d”,&num[i]);
}
Printf(“\n array elements are”);
For(i=0;i<5;i++)
Printf(“%d\t”,num[i]);
}

You might also like