Académique Documents
Professionnel Documents
Culture Documents
• itsmohitonly@gmail.com
• Contact for software development and
web site development work,projects
C is a general-purpose,
Block structured,
structured
Procedural,
Procedural
Computer programming language
Developed in 1972
by Dennis Ritchie
at the Bell Telephone Laboratories for use with the Unix
Operating System.
Operating System
Language Compilers
Assemblers
Text Editors
Modern Programs
Data Bases
Language Interpreters
Why C?
C is simple.
• C is fast.
• C offers better interaction with hardware.
hardware
ENGLISH C
Alphabets, digits Alphabets, digits, special
symbols
Words, numbers Constants, variables, keywords
INTEGER int
FLOAT float
CHARACTER char
void void
• ………………………………………………………………………………………………………………..
USER DEFINED DATA TYPES
TYPEDEF
ENUM
• ………………………………………………………………………………………………………………
16 bit short
32bit long
char 1byte
By clrscr( );
we can clear the screen,
it can used after data type declaration.
we use clrscr(); after the data type declaration and before
the printf( ).
…………………………………………………………
Full Forms
.h = Extension
Each symbol except (decision box) used in the flow chart should have
one entry point and one exit point.
Input /output
/read /write
Processing
Condition
connector
Flow line
Example of Flow Chart
Q. Draw a flow chart to calculate 2 number ?
Start
Read a,b
Sum=a+b
Print sum
Stop
Algorithm
Syntax error
when rules or syntax of programming language are not followed
Logical error
error in planning the programming logic.
System Software=
the system software contains those programs that control and support the computer system
and its data processing applications
it includes=
• the operating system
• the language translators
Operating System
• operating system is a organized collection of integrated set of specialized programs that
controls the overall operations of a computer.
• it helps the computer to supervise and manage its resources.
• operating system calls the application program whenever needed.
• translate the special service programs and manages data needed to produced the output as
desired by the users.
Application Software
Application software is a set of program designed
for a specific uses or application such as word
processing ,graphics ,or spreadsheet analysis.
The operating system acts as a communication link
between the hardware and the application program.
It is responsible for loading the application
software into memory and then staring the program.
When we have finished using application software
we return to the operating system.
PROGRAMMING LANGUAGE
A language is a system of communication.
A PROGRAMMING LANGUAGE consists of all
symbols ,characters and usage rules that permit
people to communicate with computers
A program is a set of logically related instructions
that are arranged in a sequence that guides the
computer to solve a problem.
The process of writing a program is called
programming.
Machine Language
this is a sequence of instructions written in the form of binary
numbers consisting of 1's and 0's to which is the computer
responds directly.
A machine language has 2parts =
the first part is the command or an operation that tells the
computer what functions are to be performed.
the second part of the instruction is the operand that tells
the computer where to find or store the data on which the
desired operation is to be performed.
Readability
Portability
Easy Debugging
Easy Software Development
TYPES =
Assembler
Compiler
Interpreter
Interpreter
simple and easy to write.
does not require much memory space to store itself
translate the program line by line into machine language.
check for the syntax errors line by line and execute each line or
statement immediately when statement become error free.
Testing
• Uses of Documentation=
easier maintenance
avoid duplication
CHARACTER SET
C character set includes the
LETTERS=
UPPER CASE (A-Z)
LOWER CASE (a-z)
digits (0 - 9)
special character
White spaces=
• BLANK SPACES
HORIZONTAL TAB
NEW LINE
FORM FEED
bell \a
back space \b
horizontal tab \t
vertical tab \v
form feed \f
carrier return \r
back slash \\
null \0
question mark \?
C TOKENS
KEYWORD (EX. FLOAT ,WHILE)
( RESERVED WORD )
CONSTANTS (15.5,100)
STRINGS ("ABC","YEAR")
SPECIAL SYMBOL ([ ],{ })
OPERATORS ( + - * , )
IDENTIFIER /
VARIABLE NAME
CONSTANTS IN C=
Constant=quantity that does not change
ARITHMATIC OPERATOR
RELATIONAL OPERATOR
LOGICAL OPERATOR
ASSIGNMENT
INCREMENT (++)
DECREMENT (--)
CONDITIONAL (IF,ELSE)
BITWISE
COMMA ( ,)
SIZEOF( ) C COMPILE TIME
POINTER OPERATOR (*.&)
COMMA OPERATOR
• Sum=(x=20,y=30,x+y)
• Expressions are evaluated left to right and the value of rightmost
expressions is the value of the combined expression.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
• Used to determine the length of array and structure when their sizes are not known
to the programmer
• Example:
If (first>second)
Max=first;
Else
Max=second;
The above statement can be written as
Max=(first>second) first: second;
The ? : operator
• C language has a special operator, useful for making two way
decisions .
• This operator is a combinational of ? And : takes three
operands.
• This operator is popularly known as conditional operator or
ternary operator
• The general form of conditional operator is as follows:
• Conditional expression ?<expression1>:<expression2>
• The conditional expression is evaluated first.
• If the result is true, expression 1 is evaluated and returned as
the value of the conditional expression.
• Otherwise expression 2 is evaluated and its value is returned .
• In general conditional operator is used as substitute for simple
if…else Statement
• Consider the following code:
• int a=10;
• Int b=20;
• Int c;
If (a>b)
c=a;
else
c=b;
• This code segment can be written as follows using ternary
operator
C=(a>b) ? a: b
Unary Operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Module division
Relational Operator
OPERATOR MEANING
&& AND
|| OR
! NOT
Assignment Operators
• The assignment operator = assigns a value to a variable.
• For example,
• x = 1 sets x to 1,
• and a = b sets a to whatever b's value is.
• We've called the = sign the ``assignment operator'' and
referred to ``assignment expressions'' because, in fact, = is
an operator just like + or -.
• C does not have ``assignment statements''; instead, an
assignment like a = b is an expression and can be used
wherever any expression can appear.
• Since it's an expression, the assignment a = b has
a value, namely, the same value that's assigned
to a. This value can then be used in a larger
expression;
• for example, we might write
• c = a = b which is equivalent to c = (a = b) and
assigns b's value to both a and c.
• (The assignment operator, therefore, groups from
right to left.).
BITWISE OPERATOR
OPERATOR MEANING
& BITWISE AND
| BITWISE OR
^ BITWISE EXCLUSIVE
OR
• -- subtract 1
• + + i or I + +
• - - i or I - -
----------------------------------------------------------------------------------------------------------
• ++i
• I=I+1
• I+=1 are equivalent
--------------------------------------------------------------------------------------------------------
• -- i
• I=I-1
• I -=1 are equivalent
---------------------------------------------------------------------------------------------------------
Control Structure in C
Conditional statement
» If-else
» Else-if
» switch case
UNCONDITIONAL STATEMENT
• While STATEMENT
• Do while
• for STATEMENT
• jump
• continue
• break
• goto
if -else
• The first statement or block of statements
is executed if the condition is true,
• and the second statement or block of
statements (following the keyword else) is
executed if the condition is not true.
• While loop
• The while loop continues to loop until the conditional expression
becomes false. The condition is tested upon entering the loop.
• Loops generally consist of one or more control expressions which
control the execution of the loop, and the body, which is the statement
or set of statements
• The most basic loop in C is the while loop. A while loop has one
control expression, and executes as long as that expression is true.
• The general syntax of a while loop is
• while( expression ) statement A while loop starts out like an if
statement: if the condition expressed by the expression is true, the
statement is executed.
• However, after executing the statement, the condition is tested again,
and if it's still true, the statement is executed again. (As long as the
condition remains true, the body of the loop is executed over and over
again. (If the condition is false right at the start, the body of the loop
is not executed at all.)
do.. while STATEMENT
do while is a exit control looping structure.
that is loop body will be executed first before evaluating loop control statement.
do
{
body of the loop;
.............
.............
}
While (test condition)
statement x;
do while
the loop get terminated once the test condition returns false
and the program control is passed to the statement that is next to the
loop control.
for statement
the for statement is the most popular and more concise loop control
structure supported by almost all the programming languages.
for is an entry control looping structure.
for is quite efficient looping structure when compared while or do while
because it takes cares of initialization of loop control variable evaluating
loop control statement,
increasing /decreasing the value of loop control variable.
the execution of the loop control variable is done first ,
using assignment statement such as i=1, or count =10 etc,
here i and count are referred as loop control variables,
the value of the loop control variable is tested using test
condition.
if the test condition n is true the body of the loop will be get
executed otherwise the loop will be get terminated and the
execution follows to the next statement that immediately
follows the loop.
when the loop body completes the execution the control is
passed back to the for statement
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("my name is MOHIT SHARMA \n3 k 21");
getch();
}
// (print positive –negative )
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr();
printf("enter n");
scanf("%d",&n);
if(n>0)
{printf("positive value");
}
else
{
printf("negative value");
}
getch();
}
// (voting program)
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("enter age");
scanf("%d",&age);
if(age>18)
else
{
printf(“ u r child ");
}
getch();
}
// A AND B WHICH IS MAX
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter a,b");
scanf("%d%d",&a,&b);
if(a>b)
{printf("a is max");
}
else
{
printf("b is max");
}
getch();
}
• FEATUES OF POINTER
4. pointers use for manipulating dynamic data structure such as link list, queue,
stack, graph.
• When 2 pointer point the same array one pointer variable can be subtracted from the
another variable.
DOCUMENTATION SECTION
LINK SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
MAIN FUNCTION SECTION
SUB PROGRAM SECTION
• Documentation section
consist of a set of comment lines giving the name of the program
the author or programmer and other details
• Link section
provides instructions to the compiler to link functions from the system
library
• Definition section
defines all symbolic constants.
• Global declaration
global variables and user define function
int marks[100] ;
like any other variables arrays must be declared before they are used
for ex.
int arr[5];
now we have created an array that can store 5 integer values.
Memory Representation of Array
for ex.
int arr[5];
now we have created an array that can store 5 integer values.
all the elements of the array gets stored in consecutive memory locations.
in the above ex. we have declared an array that can stored 5 integer values
suppose the first element of the array is stored in 80th address location then the
second element gets stored in 82th ,
third in 84th ,
fourth in 86th and fifth in 88th address location
(remember each integer occupies 2bytes of storage in memory ,that is why address
locations are 80,82,84,86,88)
the above statement int arr [5] creates an array in the following manner in
computer memory
Size of an array:
U can find the size of an array ,by performing following
computation.
No of elements in an array *size of an array
Hence the size of the array int arr [5] would be 5 * sizeof (int)
That is 5*2=10 bytes (size of an int type on a 16bit machine is 2
bytes.)
Initialize an array
We can initialize the element of array either at the time of declaration or any where else in the
program .
Some of the general forms of the initializing an array are as follows
Initializing an array elements at the time of declaration
Int arr[5]={42, 13 ,87 ,76 ,54};
Int arr[5];
Int i;
For (i=0; i<5; i++)
{
Scanf (“ %d ”, & arr [i] );
}
Array in memory after initialization
42 13 87 76 54
Addresses of an individual
element in an array
Referring to the elements of an array
The main thing after declaring and initializing an array is how to
access the individual element of an array.
If u observe, while initializing elements of an array we have used
arr[0],arr[1]…. and so on.
From that we got a little bit of idea on how to refer to individual
element of an array.
42 13 87 76 54
Storage class in C
Automatic storage class, auto
local variable known to only to the function in
which it is declared .default is auto.
Units batch1,batch2;
marks name1[50],name2[50] ;
• Batch1 and batch2 are declared as int variable and name[50] and
name2[50] are declared as 50 element floating point array variable.
• The main advantage of typedef is that we can create meaningful data type
names for increasing readability of the program.
• Another user defined data type is enumerated data type provided by ANSI
standard .
• It is defined as follows.
FUNCTION
User Define Function
function definition=
formal parameter
actual parameter
function name
calling a function
call by reference
primitive function
math.h
•abs()
•log()
sqr()
•exp()
cos()
sin()
• Some points Regarding to the Recursion
• The length of the source program can be reduced by using functions at appropriate
places.
• This factor is particularly important when functions are having limited memory.
• A function once written ,compiled and packaged into a library can be reused in
many other program without rewriting the code.
C functions can be classified into two categories :
• Library functions
• User defined functions
• The basic difference between the two is =
• Library are prewritten, compiled packaged into
Header files and are made as a part of c standard
library.
• The functions like printf (), scanf () etc. are a part of
C Standard Library,
• Where as an user defined functions needs to be
written by the programmer according to the
requirements of the application he is developing.
User Defined Functions
for example:
• Function prototype must appear before the statement that call the
respective functions and are usually placed at the beginning of the
program
• In the above program the line int square (int) ; is the function
prototype.
• A part of the function prototype is the function signature
• A function signature is the consist of the name of the function and
order and type of the arguments
• In the above ex..=
int square (int) ; //function signature
Function Header
return type function name ( Argument list )
argument declaration ;
( or )
(or )
Formal Arguments
Formal Arguments are valid variables that are used in Function
Header of the function definition in order to store the values of the
actual arguments.
Formal Arguments are must be surrounded by parenthesis.
must not be terminated by using a semicolon.
Actual Arguments
Actual Arguments are the values that are passed to a
Function during a Function Call
An actual argument can be either a variable or constant or
valid expression .
one more thing is to be remember is the type and order of
the actual arguments must match the formal arguments.
Function call
• A function is invoked using a function call.
A function call requires the programmer =
• To specify the specific function by name
• “Pass “data to the function that it needs: (arguments)
• Functions optionally return a single “value” to the
caller ,which needs to be trapped for further use in
the program.
The function that is invoking another function is
refereed as calling function , and the function that
has been invoked is referred as called function.
ex. boss to worker analogy
• a boss (the calling function or caller ) tells a worker
(the called function) to perform a task and return
(i.e. report back) the results when the task is done.
• one more thing we need to remember here is once function
call is made (calling function invokes Called Function),
or
return (expression) ;
• Paramerters
• information flow to the function
• Local variables
• variables and witin the function (private to the function)created when
function is called, ”destroyed”when functions returns.
• Function Body
• contains the software that implements the function
• Return Staement
• specifies the value that is returned to the caller,returns flow of
execution to the caller
Without Return Value without Argument
• Rule :
• When the value is not return then it is essential to write void
in definition.
• When the value is return then we can not use void in the
definition part.
• when the value is return then we write
int function name;
then we also write
int definition;
and
then we also write
int declaration;
Calling a function:
• a function can be called by simplifying using the function
name in an statement .
• example:
void main()
{
int p;
p=mul(10 , 4);
printf (“ %d ”,p);
}
• when the compiler encounters a function call, the control is
transferred to the function mul (x , y).
• this function is then executed line by line as described and a
value is returned when a return statement is encountered
.this value is assigned to p.
No arguments and No return values
void main()
{
void sum( ); //declaration
sum(); //calling the function
sum(); //calling the function
sum(); //calling the function
getch();
}
void sum() //definition
{
int x,y,z;
printf( “enter the value of x , y ”);
scanf (“%d %d”,&x,&y);
z=x+y;
printf (“ the z is % d ”,z”);
}
# header file //With return value Without argument
void main()
{
int a,b; //data type declare
int sum(); // function declaration
a= sum( ); //calling the function
b=sum( ); //calling the function
printf(“%d”,a);
printf(“%d”,b);
getch();
}
int sum() // function definition
{
int x,y,z;
printf( “enter the value of x , y ”);
scanf (“%d %d”,&x,&y);
z=x+y;
return(z);
}
# header file // Without return value With Argument
void main()
{
int square( int x ) ; //declaration
int z,a;
scanf(“%d”,&a);
z=square(a);
printf(“%d”,z);
getch();
}
int square (int x) //definition
{
int y ;
y = x * x;
printf (“%d”,y);
}
# header file //with return value with argument
void main()
{
int square( int x); // function declaration
int z, a; //datatype declare
scanf(“%d”,&a);
z=square(a);
printf(“%d”,b);
getch();
}
int square (int x) // function definition
{
int y ;
y =x *x ;
return (y);
}
VARIABLE DECLARATION=
LOCAL
(ONLY FOR SUB PROGRAM)
GLOBAL
(FOR WHOLE PROGRAM)
FOR EX.
main()
{
subprogram-->function
int p
......
return 0;
}
Unformatted Console I/O
• main( )
• int main()
• void main()
• main(void)
Return type
Function Name
Semicolon
Standard library files=
conio.h
math.h
ctype.h
string.h
time.h
• Defining structures
• a structure is a collection of variables under a single
name.
• these variables can be of different types, and each
has a name which is used to select it from the
structure.
• a structure is a convenient way of grouping several
pieces of related information together.
Declaring structure
• the general form of declaring a structure is as follows:
};
example:
struct student
{
char name[20];
int age;
float height,weight;
};
#
#include<stdio.h>
struct student
{
char name[20];
int age;
float height, weight;
};
main()
{
struct students s[5];
int i;
clrscr( );
for( i=0 ; i<5; i++)
{
printf(“enter student name\n”);
scanf(“%s”,&s[i].name);
printf(“enter student age”);
scanf(“%d”,&s[i] ,age);
}
getch();
}
• structures and pointers
#include<stdio.h>
strucrt student
{ char name[20];
int age;
float height,weight;
};
main()
{
struct student s,*ptr=&s;
printf(“enter name\n”);
scanf(“%s”,&ptr->age);
printf(“enter age \n”);
scanf(“%d”,&ptr->age);
printf(“enter height and weight of the student \n”);
scanf(“%f%f”,&ptr->height,&ptr->weight);
printf(“name\t%s\n”ptr->name);
printf(“age\t%d\n”,ptr->age);
printf(“height\t%5.5f \t weight \t %5.2f\n”,ptr->height,ptr->weight)”
getch();
}
File System
File function in C =
• fopen
• fclose
• fscanf
• fprintf
• putc
• putw
• getc
• getw
• wind
• ftail
• fsick
• File Operation
• Creation
• updation
• Insertion
• deletion
• modification
• Maintenance
• retrieval
• Some question of c language
• who developed c language?
• Dennis Ritchie
……………………………………
• give 2 ex. of OOP?
• c++, java, c sharp
……………………………………..
• bell \a
• back space \b
• horizontal tap \t
• vertical tab \v
• form feed \f
• carrier return \r
• back slash \\
• null \0
• question mark \?
…………………………………………………..
• what are constants?
• int ,char, float
Q
• what are the fundamental data types in c?
……………………………………
• what is the range of integer, character, float?
…………………………
• name 4 data types modifier?
………………………………..
• name the operators in c?
…………………………..
• name dynamic data structure?
• stack, queue, linked list.
……………………………………
Q
• write the ways to write main()?