Programming Concept
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>
} ← 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:
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.
• 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
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;
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.
• 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
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);
Example:
float x;
int x1=5;
int x2=2;
x=x1/x2:
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;
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()
{
#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
• 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