Académique Documents
Professionnel Documents
Culture Documents
What Is C ?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It designed and
written by Dennis Ritchie. C began to replace the more familiar languages like PL/I, ALGOL, BCPL, B
etc.
Future of C language..
C language runs all platforms. Because c using compiler for compile the program.
C language most popular system programming language.
C language is a structure oriented programming language.
C language also called high-level language but it can handle low-level activities.
C language was invented for implementing UNIX operating system.
C language use many predefined library function. And user also create own library
function.
An interpreter checks the errors of a program statement by statement. After checking one statement,
it converts that statement into machine code and then executes that statement. The process
continues until the last statement of program occurs.
What is C STRUCTURED ?
This section use to written a comment lines. Comment line display information
for user or other person can understood the program. Comment line not
compile if run program.
Document Section Ex… Single line //--------------------
Multiple line /*-----------------*/
Link Section This section use to link predefine function or system libraries.
Ex… #include<stdio.h>
Definition Section This section use to declare all symbolic constant.
Ex… #define PI 3.4128
Global Declaration Section
This section use to declare variable outside the main function and they variable
use one or more times in main function.
Main Function Ex… int x, y, z;
All C programs must one main function. And main function have a two parts.
{ Ex… Void main()
Declaration Part This section use to declare variable inside the main function and they variable
use in executable part.
Ex… int x =5, y =8;
Executable Part
This section use to written a set of instruction in sequence. And executed
variables those declare in declaration part.
} Ex… printf(“Hello World\n”);
Sub Program This section use to written a function by user. Sub program section always write
after declare main function.
How To Executing a ‘C’ Program If Writing a C Programs, We Face
Some Error, Error Also Known As
Enter Program Bugs. Basically There Are Three
Types Of Errors In C Programming.
Edit Source Program
Syntax errors: These errors occur of wrongly typed
Compile Source Program statements, which are not according to the syntax
or grammatical rules of the language.
printf(“Hello, world”) - semicolon missing.
Syntax printf(“Hello, world”); - correct.
Errors ?
Logical errors: These errors occur of logically
incorrect instructions in the program. if there
should be an instruction, which multiplies two
Link With System Library numbers and is wrongly written to perform
addition. This logically incorrect instruction may
Execute Object Code produce wrong results.
int average(int a, int b)
{
Logic & Data return a + b / 2; /* should be (a + b) / 2;*/
Errors ? }
Runtime errors: These errors occur during the
execution of the programs, though the program is
Correct Output free from syntax and logical errors. Runtime error
also known as a compile error.
How To Learn a ‘C’ Program
Letters…. 0-9
Digits……
~ Tiled ] Right Bracket
# Hash . Dot
Symbol... ^ Caret ; Semicolon
( Left Parentheses “ Double Quotation
% Percent : Colon
= Assignment < Less Than
- Minus Sign > Greater Than
) Right Parentheses , Comma
_ Underscore \ Back Slash
+ Plus Sign ? Question Mark
{ Lift Brace
} Right Brace White Space
| Vertical Bar \T Tab
/ Division \N New Line
‘ Single Quote \R Carriage Return
* Asterisk
[ Left Bracket
What Is C Tokens ?
Keyword Identifiers
Constant Operators
Special Symbols
What Is Statement of C ?
#include<stdio.h>
void main()
{
printf(“Hello World”);
}
What Is Keyword ?
The numeric quantities having fractional part are called floating point
constants. Floating point constant can be represented using two forms.
Fractional part >> 0.5, 0.99, 2.58
Exponent notation >> 7.e82, 1.23e1
Character enclosed within a pair of single quote is called single character
constant. Each character associated unique value.
‘9’ ‘a’ ‘$’ //valid
“9” ‘99’ //invalid
Backslash constant use for effects. They are also called escape sequence
character constant.
\a \t \n \v ….etc.
Backslash constant use for effects. They are also called escape sequence
character constant.
Bell \a beep sound
Backspace \b move one position left side
Horizontal tab \t move 8 position right side
Vertical tab \v this is not support
Newline \n move next line
Form feed \f move next page
Carriage return \r start of current line
Quotation mark \” “
Question mark \? ?
Backslash \\ \
NULL \0 NULL character
What Is Operators ?
A operator is a symbol that perform arithmetic or logical operation.
+ - / * //operators
2, 5, 8 //operands
The operators that are used to compare two operand and find the
relationship between operand.. The two operand may be variables, constants or
expressions.
Description Symbols Description Symbols
Less than < Greater/equal >=
Equal == Not equal !=
Lesser/equal <= Greater >
The operators that are used to store value of statement into the variable.
Syntax:- =
They operator that are used to increment or decrement value of variable n times
( if conditions true ). These operator are also called unary operator and they are four
types…
Types Example
Post - increment i++
Pre - increment ++i
Post - decrement i--
Pre - decrement --i
The operator that are use to join two or more than relational operators for
compare the operand.
Description Example
AND &&
OR !!
NOT !
The operator that are use to process data in bit level. These operators use only int
or char data type not in float or double data type … Description
Symbol
Bitwise AND &
Bitwise OR |
Bitwise Exclusive OR ^
Shift left <<
Shift right >>
One’s complement ~
The operator that are use to process special data.
Description Symbol
Comma ,
Size of sizeof(variable name)
Pointer operators &, *
Selection operator . and ->
[] () {} , ; : * = #
Character : This data type allows a variable to store only one character.
“char” keyword is used to refer character data type.
C variable is a named location in a memory where a program can manipulate the data. This
location is used to hold the value of the variable.
• The value of the C variable may get change in the program.
• C variable might be belonging to any of the data type like int, float, char etc.
Rules for naming C variable:
• Variable name must begin with letter or underscore.
• Variables are case sensitive
• They can be constructed with digits, letters.
• No special symbols are allowed other than underscore.
Declaring & initializing C variable:
• Variables should be declared in the C program before to use.
• Variable initialization means assigning a value to the variable.
There are three types of variables in C program They are,
Local variable
The scope of local variables will be within the function only.
These variables are declared within the function and can’t be accessed outside the function
Global variable
The scope of global variables will be throughout the program. These variables can be accessed
from anywhere in the program.
This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
Control Statements
Simple-if
If-else
Nested-if
Sequential Statements
Conditional Statements Else-if-ladder
switch
Branching Statements
goto
break
Un-Conditional Statements
continue
return
for
Do-while
Sequential Statements The programmer writes a sequence of statements to do a
specific activity. All these statements in a program are executed in the
order in which they appear in the program. These programming
statements that are executed sequentially, that is one after the other,
are called sequential control statements.
Branching Statements In sequential control, we know that all the statements are
executed in the order. However, computer can skip execution of some
statements. This skipping facility is provided by the instruction called
skip instructions. these skip instruction are also called branching
statement. The branching statement are two types.
We know that all the statements are executed in the order. The
computer can skip the execution of some statements based on some
Conditional condition. These condition statement are also called conditional
branching statement.
Syntax:-
if(Condition )
{
Statement;
}
Next
statement;
if-else
Syntax:-
if(Condition )
{
Statement;
}
else
{
statement;
}
Nested if- statement
Syntax:-
if(Condition )
{
if(Condition )
{
Statement;
}
}
else
{
if(Condition )
{
Statement;
}
}
if- else-if ladder
}
Else if
{
Statement;
}
Else
{ }
Switch
Switch case statements are a substitute for long if statements that compare
a variable to several "integral" values ("integral" values are simply values
that can be expressed as an integer, such as the value of a char).
Syntax:-
goto label; Label:
{ {
skip(statement) statement
} }
label: goto Label;
Syntax:- break;
continue
This is an unconditional branch statement or jump statement. this jumping
statement, we can terminate the further execution of the program and
transfer the control to the beginning of any immediate loop.
Syntax:- continue;
While loop
A while loop in C programming language repeatedly executes a target
statement as long as a given condition is true. It also called entry controlled
loop.
Syntax:- while(condition)
{
statement ;
increment/ decrement;
}
do while loop
for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming language checks its condition at the
bottom of the loop. It also called exit control loop.
Syntax:- do
{
statement ;
increment/ decrement;
}
while(condition)
for loop
A set of statement may have to be repeatedly executed for a specified
number of times. In this situations we use for loop. The for loop is also an
entry-controlled loop. for loop hold three expression.
Statement;
}
What difference between a string and an array ?
- String can hold only char data. Where as an array can hold any data type.
- An array size can not be changed. Where as a string size can be changed if
it is a char pointer
- The last element of an array is an element of the specific type. The last
character of a string is a null – ‘\0’ character.
- The length of an array is to specified in [] at the time of declaration
(except char[]). The length of the string is the number of characters + one
(null character).
- Array use index number for store data in array variable. Index number
start number of 0 to n numbers.
- String use to store series of char in string variable. Start number 1 to \0
(-1 length of string )
String
String.h header file supports all the string functions in C language. All the
string functions are given below.
S.no String Description
functions
1 strcat ( ) Concatenates str2 at the end of str1.
2 strncat ( ) appends a portion of string to another
3 strcpy ( ) Copies str2 into str1
4 strncpy ( ) copies given number of characters of one string to another
5 strlen ( ) gives the length of str1.
6 strcmp ( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2.
7 strcmpi_(.) Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as same.
8 strchr ( ) Returns pointer to first occurrence of char in str1.
9 strrchr ( ) last occurrence of given character in a string is found
10 strstr ( ) Returns pointer to first occurrence of str2 in str1.
11 strrstr ( ) Returns pointer to last occurrence of str2 in str1.
12 strdup ( ) duplicates the string
13 strlwr ( ) converts string to lowercase
14 strupr ( ) converts string to uppercase
15 strrev ( ) reverses the given string
16 strset ( ) sets all character in a string to given character
17 strnset ( ) It sets the portion of characters in a string to given character
strcat ( )
#include <stdio.h>
Example:-
#include <string.h>
#include <conio.h>
void main( )
{
char source[ ] = " india" ;
char target[ ]= " welcome" ;
clrscr();
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
getch();
}
Syntax :- char * strncat ( char * destination, const char * source, size_t num );
#include <stdio.h>
Example:-
#include <string.h>
#include <conio.h>
void main( )
{
char source[ ] = “ india " ;
char target[ ]= " welcome " ;
clrscr();
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strncat ( target, source, 4 ) ;
printf ( "\nTarget string after strncat( ) = %s", target ) ;
getch();
}
strcpy( ) function copies contents of one string into another string. Syntax for strcpy
function is given below.
#include <stdio.h>
Example:-
#include <string.h>
#include <conio.h>
void main( )
{
char source[ ] = " welcome india" ;
char target[ 20]= " " ;
clrscr();
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\nTarget string after strcpy( ) = %s", target ) ;
getch();
}
strncpy( ) function copies portion of contents of one string into another string.
Syntax :- char * strncpy ( char * destination, const char * source, size_t num );
#include <stdio.h>
Example:-
#include <string.h>
#include <conio.h>
void main( )
{
char source[ ] = " welcome india" ;
char target[ 20]= " " ;
clrscr();
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strncpy ( target, source, 5 ) ;
printf ( "\nTarget string after strncpy( ) = %s", target ) ;
getch();
}
Output: welcom
strlen( )
Output: 7
strcmp( )
strcmp( ) function in C compares two given strings and returns zero if they are
same.
strchr( ) function returns pointer to the first occurrence of the character in a given
string.
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char s [55] =“i am student in samarpit";
char *p;
clrscr();
p = strchr (s,‘u');
printf(“character ‘u’ find position at: %d ”,p-s+1);
getch();
}
Output: 8
strrchr( )
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char s [55] =“i am student in samarpit";
char *p;
clrscr();
p = strrchr (string,'i');
printf(“character ‘u’ find position at: %d ”,p-s+1);
getch();
}
Output: 23
strstr( )
strstr( ) function returns pointer to the first occurrence of the string in a given
string.
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char *p1= “samarpit”;
char *p2;
clrscr();
p2 = strdup (p1);
printf("Duplicated string is : %s", p2);
getch();
}
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char str[ ] = "MODIFY This String To LOwer";
clrscr();
printf("%s\n",strlwr (str));
getch();
}
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char str[ ] = "Modify This String To Upper";
clrscr();
printf("%s\n",strupr(str));
getch();
}
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char name[30] = "Hello";
clrscr();
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
getch();
}
Output: Hello
olleH
strset( )
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char name[20] = "Hello";
clrscr();
printf("Original string is : %s", name);
strset(name,’#’);
printf("After string set: %s",name);
getch();
}
Output: #####
strnset( )
Example:-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{
char name[20] = "Hello india";
clrscr();
printf("Original string is : %s", name);
strnset(name,'#',4));
printf("After string set: %s",name);
getch();
}
Output: ####o india
Array
#include<stdio.h>
int main( )
{
int i, arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; output
for (i=0;i<5;i++)
{ value of arr[0] is 10
printf(“value of arr[%d] is %d \n”, i, arr[i]); value of arr[1] is 20
} value of arr[2] is 30
} value of arr[3] is 40
value of arr[4] is 50
search
More times we working with large amount of data. It may be necessary to
determine a particular item is present in the large amount of data or not. This process of
finding a particular item in the large amount of data is called searching.
Linear search
linear search is a simple search technique in which we search for a given key item in
the list in linear order. one after the other. The liner search is also called sequential search.
Therefore it is considered to be too slow to be used when searching elements in large lists. But
this is very simple and easier to implement.
Binary search
binary search is a also search technique in which we search for a given key item in
the list in sort order. This method starts by comparing the searched element to the elements in
the middle of the list.
Selection Sort
Selection sort is to repetitively pick up the smallest element and put it into
the right position:
• Find the smallest element, and put it to the first position.
• Find the next smallest element, and put it to the second position.
• Repeat until all elements are in the right positions.
Bubble Sort
Bubble sort repetitively compares adjacent pairs of elements and swaps if
necessary.
• Scan the array, swapping adjacent pair of elements if they are not in relative
order. This bubbles up the largest element to the end.
• Scan the array again, bubbling up the second largest element.
• Repeat until all elements are in order.
Two-dimensional array
The term of dimension represents number of index used to access a particular item
in an array.
The element of the arrays that explain or represent two characteristics are called
two-dimensional array. Arrays with one set of square brackets ‘[ ]’ are single dimensional
arrays . Arrays with two sets of square brackets ‘[ ] [ ]’ are called two-dimensional array. Arrays
with two or more dimensional are called multi-dimensional arrays.
the-dimensional array is used when data item are arranged row-wise and column-
wise in a tabular form.
#include<stdio.h>
int main( )
{ RC
int i, j, arr[2][2];
arr[0][0] = 10;
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40;
for (i=0;i<2;i++) output
{
for (j=0;j<2;j++) Array value is: 10
{ printf(“\n array value is: %d”, arr[i][j]); Array value is: 20
} Array value is: 30
} Array value is: 40
}
Function
A large program can be divided into a series of individual related programs called
modules. These modules are called function. Thus function (also called sub-program) is a
program segment that carries out some specific and well-defined task.
function can be classified into two categories .
(1) library function ( Built-in function)
(2) User defined function (UDF)
Advantage :
the programmer’s job is made easier because the function are already available.
The library function are simple to use and easy to reminder.
Disadvantage :
the library function are limited, programmer cannot completely depend only on
library function.
(2) User defined function (UDF)
The programmers need other than library function to achieve some specific tasks.
These types of function which are written by the programmer to do some specific task are
called user defined functions (UDF).
Advantage :
o Reusability
o Reduction of code size
o Increase in readability of the program
o Modular programming approach
o Easier debugging
o Extendibility
o Function sharing
There are 3 aspects in each C function.
• Function declaration or prototype - This informs compiler about the function name,
function parameters and return value’s data type.
• Function call – This calls the actual function
• Function definition – This contains all the statements to be executed.
.no C function aspects syntax
1 function definition return_type function_name ( arguments list )
{ Body of function; }
2 function call function_name ( arguments list );
3 function declaration return_type function_name ( argument list );
There are two ways that a C function can be called from a program. They are,
• Call by value
• Call by reference
Call by value:
The value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal
parameter.
Call by reference:
The address of the variable is passed to the function as parameter.
The value of the actual parameter can be modified by formal parameter.
C – Argument, return value
All C functions can be called either with arguments or without arguments
in a C program.
} Notes.
• & is known as address of operator. It is an unary operator.
• Operand must be the name of variable.
• & operator gives address number of variable(Operand).
• & is also knows as referencing operator.
• * is indirection operator. It is an unary operator
• It`s takes address as an argument. It is also know as dereferencing.
• A normal C variable can hold only one data of one data type at a time.
• An array can hold group of data of same data type.
• A structure can hold group of data of different data types
• Data types can be int, char, float, double and long double etc.