Beruflich Dokumente
Kultur Dokumente
C language Tutorial with programming approach for beginners and professionals, helps you
to understand the C language tutorial easily. Our C tutorial explains each topic with programs.
The C Language is developed for creating system applications that direct interacts to the
hardware devices such as drivers, kernals etc.
C programming is considered as the base for other programming languages, that is why it is
known as mother language.
1. Mother language
1) C as a mother language
C language is considered as the mother language of all the modern languages because most
of the compilers, JVMs, Kernals etc. are written in C language and most of languages
follows c syntax e.g. C++, Java etc.
It provides the core concepts like array, functions, file handling etc. that is being used in many
languages like C++, java, C# etc.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
3) C as a procedural language
A procedure is known as function, method, routine, subroutine etc. A procedural
language specifies a series of steps or procedures for the program to solve the
problem.
A procedural language breaks the program into functions, data structures etc.
In C language, we break the program into parts using functions. It makes the program easier
to understand and modify.
Low level language is specific to one machine i.e. machine dependent. It is machine
dependent, fast to run. But it is not easy to understand.
High Level language is not specific to one machine i.e. machine independent. It is easy to
understand.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
History of C Language
It was developed to overcome the problems of previous languages such as B, BCPL etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Features of C Language
C is the widely used language. It provides a lot of features that are given below.
1. Simple
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
1) Simple
C is a simple language in the sense that it provides structured approach (to break the
problem into parts), rich set of library functions, data typesetc.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
5) Rich Library
C provides a lot of inbuilt functions that makes the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the
allocated memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast.
8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the
pointers. We can use pointers for memory, structures, functions, array etc.
9) Recursion
In c, we can call the function within the function. It provides code reusability for every
function.
10) Extensible
C language is extensible because it can easily adopt new features.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
How to install C
There are many compilers available for c and c++. You need to download any one. Here, we
are going to use Turbo C++. It will work for both C and C++. To install the Turbo C software,
you need to follow following steps.
2. Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
4. Click on the tc application file located inside c:\TC\BIN to write the c program
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Press enter, it will look inside the c:\turboc directory for the required files.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Select Start installation by the down arrow key then press enter.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
In windows 7 or window 8, it will show a dialog block to ignore and close the application
because fullscreen mode is not supported. Click on Ignore button.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
First C Program
Before starting the abcd of C language, you need to learn how to write, compile and run the
first c program.
To write the first c program, open the C console and write the following code:
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
int main() The main() function is the entry point of every program in c language.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for
successful execution and 1 for unsuccessful execution.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
You can view the user screen any time by pressing the alt+f5 keys.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Flow of C Program
The C program follows many steps in execution. To understand the flow of C program well,
let us see a simple program first.
File: simple.c
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
Execution Flow
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Let's try to understand the flow of above program by the figure given below.
2) Expanded source code is sent to compiler which compiles the code and converts it into
assembly code.
3) The assembly code is sent to assembler which assembles the code and converts it into
object code. Now a simple.obj file is generated.
4) The object code is sent to linker which links it to the library such as header files. Then it is
converted into executable code. A simple.exe file is generated.
5) The executable code is sent to loader which loads it into memory and then it is executed.
After execution, output is sent to console.
printf() function
The printf() function is used for output. It prints the given statement to the console.
1. printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
1. scanf("format string",argument_list);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Variables in C
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
1. type variable_list;
1. int a;
2. float b;
3. char c;
We can also provide values while declaring the variables as given below:
o A variable name can start with alphabet and underscore only. It can't start with digit.
o A variable name must not be any reserved word or keyword e.g. int, float etc.
1. int a;
2. int _ab;
3. int a30;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. int 2;
2. int a b;
3. int long;
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable
A variable that is declared inside the function or block is called local variable.
1. void function1(){
2. int x=10;//local variable
3. }
Global Variable
A variable that is declared outside the function or block is called global variable. Any function
can change the value of the global variable. It is available to all the functions.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Static Variable
A variable that is declared with static keyword is called static variable.
#include<stdio.h>
int fun()
count++;
return count;
int fun2()
int x=0;
x++;
return x;
int main()
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
return 0;
If you call this function many times, local variable will print the same value for each
function call e.g, 1,1,1 and so on. But static variable will print the incremented value in
each function call e.g. 1, 2, 3 and so on.
Automatic Variable
All variables in C that is declared inside the block, are automatic variables by default. By we
can explicitly declare automatic variable using auto keyword.
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
External Variable
We can share a variable in multiple C source files by using external variable. To declare a
external variable, you need to use extern keyword.
myfile.h
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
The memory size of basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. Its size is given according to 32 bit architecture.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Float 4 byte
Double 8 byte
Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant name etc.
There are only 32 reserved words (keywords) in C language.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Misc Operator
Precedence of Operators in C
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right
to left.
1. int value=10+20*10;
The value variable will contain 210 because * (multiplicative operator) is evaluated before +
(additive operator).
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Comments in C
Comments in C language are used to provide information about lines of code. It is widely used
for documenting code. There are 2 types of comments in C language.
1. #include<stdio.h>
2. int main(){
3. //printing information
4. printf("Hello C");
5. return 0;
6. }
Output:
Hello C
1. /*
2. code
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
3. to be commented
4. */
1. #include<stdio.h>
2. int main(){
3. /*printing information
4. Multi Line Comment*/
5. printf("Hello C");
6. return 0;
7. }
Output:
Hello C
Constants in C
A constant is a value or variable that can't be changed in the program, for example: 10, 20,
'a', 3.4, "c programming" etc.
List of Constants in C
Constant Example
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C const keyword
The const keyword is used to define constant in C programming.
1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. printf("The value of PI is: %f",PI);
5. return 0;
6. }
Output:
If you try to change the the value of PI, it will render compile time error.
1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. PI=4.5;
5. printf("The value of PI is: %f",PI);
6. return 0;
7. }
Output:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C if else Statement
The if statement in C language is used to perform operation on the basis of condition. By
using if-else statement, you can perform operation either condition is true or false.
o If statement
o If-else statement
o If else-if ladder
o Nested if
If Statement
The single if statement in C language is used to execute the code if condition is true. The
syntax of if statement is given below:
1. if(expression){
2. //code to be executed
3. }
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Flowchart of if statement in C
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. return 0;
10. }
Output
enter a number:4
4 is even number
enter a number:5
If-else Statement
The if-else statement in C language is used to execute the code if condition is true or false.
The syntax of if-else statement is given below:
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
5. }
Flowchart of if-else statement in C
Let's see the simple example of even and odd number using if-else statement in C language.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. else{
10. printf("%d is odd number",number);
11. }
12. return 0;
13. }
Output
enter a number:4
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
4 is even number
enter a number:5
5 is odd number
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
7. printf("number is equals to 10");
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
12. else if(number==100){
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Programming Functions
A function is a block of code that performs a specific task. Dividing complex problem into small
components makes program easy to understand and use.
These functions are defined in the header file. When you include the header file, these
functions are available for use. For example:
The printf() is a standard library function to send formatted output to the screen
(display output on the screen). This function is defined in "stdio.h" header file.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
There are other numerous library functions defined under "stdio.h", such
as scanf(), fprintf(), getchar() etc. Once you include "stdio.h" in your program, all
these functions are available for use.
User-defined functions
As mentioned earlier, C allow programmers to define functions. Such functions created
by the user are called user-defined functions.
Depending upon the complexity and requirement of the program, you can create as
many user-defined functions as you want.
void functionName()
... .. ...
... .. ...
int main()
... .. ...
... .. ...
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
functionName();
... .. ...
... .. ...
When the compiler encounters functionName(); inside the main function, control of the
program jumps to
void functionName()
And, the compiler starts executing the codes inside the user-defined function.
The control of the program jumps to statement next to functionName(); once all the
codes inside the function definition are executed.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Programming User-defined
functions
C allows you to define functions according to your need. These functions are known as
user-defined functions. For example:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Suppose, you need to create a circle and color it depending upon the radius and color.
You can create two functions to solve this problem:
createCircle() function
color() function
#include <stdio.h>
int main()
{
int n1,n2,sum;
printf("sum = %d",sum);
return 0;
}
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
int result;
result = a+b;
return result; // return statement
}
Function prototype
A function prototype is simply the declaration of a function that specifies function's
name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be
used in the program.
In the above example, int addNumbers(int a, int b); is the function prototype which
provides following information to the compiler:
The function prototype is not needed if the user-defined function is defined before
the main() function.
Calling a function
Control of the program is transferred to the user-defined function by calling it.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
In the above example, function call is made using addNumbers(n1,n2); statement inside
the main().
Function definition
Function definition contains the block of code to perform a specific task i.e. in this case,
adding two numbers and returning it.
When a function is called, the control of the program is transferred to the function
definition. And, the compiler starts executing the codes inside the body of a function.
The parameters a and b accepts the passed arguments in the function definition. These
arguments are called formal parameters of the function.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
The type of arguments passed to a function and the formal parameters must match,
otherwise the compiler throws error.
If n1 is of char type, a also should be of char type. If n2 is of float type, variable b also
should be of float type.
Return Statement
The return statement terminates the execution of a function and returns a value to the
calling function. The program control is transferred to the calling function after return
statement.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
In the above example, the value of variable result is returned to the variable sum in
the main() function.
For example,
return a;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
return (a+b);
The type of value returned from the function and the return type specified in function
prototype and function definition must match.
For better understanding of arguments and return value from the function, user-defined
functions can be categorized as:
The 4 programs below check whether an integer entered by the user is a prime number
or not. And, all these programs generate the same output.
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
The checkPrimeNumber() function takes input from the user, checks whether it is a prime
number or not and displays it on the screen.
The return type of the function is void. Hence, no value is returned from the function.
int main()
{
int n, i, flag = 0;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
return 0;
}
return n;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Here, the getInteger() function takes input from the user and returns it. The code to
check whether a number is prime or not is inside the main() function.
int main()
{
int n;
return 0;
}
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
{
if(n%i == 0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n);
}
int main()
{
int n, flag;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
flag = checkPrimeNumber(n);
if(flag==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
return 0;
}
The checkPrimeNumber() function checks whether the passed argument is prime or not.
If the passed argument is a prime number, the function returns 0. If the passed
argument is a non-prime number, the function returns 1. The return value is assigned
to flag variable.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Switch Statement
The switch statement in C language is used to execute the code from multiple conditions. It
is like if else-if ladder statement.
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement
found in switch case, all the cases will be executed after matching the case value. It is known
as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. int x,y,z;
2. char a,b;
3. float f;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
/************************************************************************
******
Online C Compiler.
Write your code in this editor and press "Run" button to compile and execute it.
*************************************************************************
******/
#include<stdio.h>
#include<conio.h>
int main()
int i=1,num1,num2;
int choice;
do
printf("*****select your
option*****\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");
scanf("%d",&choice);
switch(choice)
case 1:
printf("\nenter a number");
scanf("%d",&num1);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
printf("\nenter a number");
scanf("%d",&num2);
printf("addition is %d",num1+num2);
break;
case 2:
printf("\nenter a number");
scanf("%d",&num1);
printf("\nenter a number");
scanf("%d",&num2);
printf("addition is %d",num1-num2);
break;
case 3:
printf("\nenter a number");
scanf("%d",&num1);
printf("\nenter a number");
scanf("%d",&num2);
printf("addition is %d",num1*num2);
break;
case 4:
printf("\nenter a number");
scanf("%d",&num1);
printf("\nenter a number");
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
scanf("%d",&num2);
printf("addition is %d",num1/num2);
break;
default:
printf("wrong choice");
scanf("%d",&i);
}while(i>=1);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Loops
The loops in C language are used to execute a block of code or a part of the program several
times.
Advantage of loops in C
1) It saves code.
Types of C Loops
There are three types of loops in C language that is given below:
1. do while
2. while
3. for
do-while loop in C
It iterates the code until condition is false. Here, condition is given after the code. So at least
once, code is executed whether condition is true or false.
1. do{
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
2. //code to be executed
3. }while(condition);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
while loop in C
Like do while, it iterates the code until condition is false. Here, condition is given before the
code. So code may be executed 0 or more times.
1. while(condition){
2. //code to be executed
3. }
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
for loop in C
Like while, it iterates the code until condition is false. Here, initialization, condition and
increment/decrement is given before the code. So code may be executed 0 or more times.
1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
do while example
There is given the simple program of c language do while loop where we are printing the table
of 1.
1. #include<stdio.h>
2. int main(){
3. int i=1;
4. do{
5. printf("%d \n",i);
6. i++;
7. }while(i<=10);
8. return 0;
9. }
Output
1
2
3
4
5
6
7
8
9
10
Program to print table for the given number using do while loop
1. #include<stdio.h>
2. int main(){
3. int i=1,number=0;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6. do{
7. printf("%d \n",(number*i));
8. i++;
9. }while(i<=10);
10. return 0;
11. }
Output
Enter a number: 5
5
10
15
20
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
25
30
35
40
45
50
Enter a number: 10
10
20
30
40
50
60
70
80
90
100
1. #include<stdio.h>
2. int main(){
3. int i=1;
4. while(i<=10){
5. printf("%d \n",i);
6. i++;
7. }
8. return 0;
9. }
Output
1
2
3
4
5
6
7
8
9
10
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C continue statement
The continue statement in C language is used to continue the execution of loop (while, do
while and for). It is used with if condition within the loop.
Syntax:
1. jump-statement;
2. continue;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
As you can see, 5 is not printed on the console because loop is continued at i==5.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C goto statement
The goto statement is known as jump statement in C language. It is used to unconditionally
jump to other label. It transfers control to other parts of the program.
It is rarely used today because it makes program less readable and complex.
Syntax:
1. goto label;
goto example
Let's see a simple example to use goto statement in C language.
1. #include <stdio.h>
2. int main() {
3. int age;
4. ineligible: //as a lable
5. printf("You are not eligible to vote!\n");
6.
7. printf("Enter you age:\n");
8. scanf("%d", &age);
9. if(age<18)
10. goto ineligible;
11. else
12. printf("You are eligible to vote!\n");
13.
14. return 0;
15. }
Output:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Type Casting in C
Type casting allows us to convert one data type into other. In C language, we use cast
operator for type casting which is denoted by (type).
Syntax:
1. (type)value;
Note: It is always recommended to convert lower value to higher for avoiding data loss.
1. int f= 9/4;
2. printf("f : %d\n", f );//Output: 2
1. #include<stdio.h>
2. int main(){
3. float f= (float)9/4;
4. printf("f : %f\n", f );
5. return 0;
6. }
Output:
f : 2.250000
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Recursion in C
When function is called within the same function, it is known as recursion in C. The function
which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is know as tail
recursion. In tail recursion, we generally call the same function with return statement. An
example of tail recursion is given below.
1. recursionfunction(){
2. recursionfunction();//calling self function
3. }
1. #include<stdio.h>
2. int factorial (int n)
3. {
4. if ( n < 0)
5. return -1; /*Wrong value*/
6. if (n == 0)
7. return 1; /*Terminating condition*/
8. return (n * factorial (n -1));
9. }
10. int main(){
11. int fact=0;
12. fact=factorial(5);
13. printf("\n factorial of 5 is %d",fact);
14. return 0;
15. }
Output
factorial of 5 is 120
We can understand the above program of recursive method call by the figure given below:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Storage Classes in C
Storage classes are used to define scope and life time of a variable. There are four storage
classes in C programming.
o auto
o extern
o static
o register
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
extern RAM Zero Global Till the end of main program, May
be declared anywhere in the
program
1) auto
The auto keyword is applied to all local variables automatically. It is the default storage class
that is why it is known as automatic variable.
1. #include<stdio.h>
2. int main(){
3. int a=10;
4. auto int b=10;//same like above
5. printf("%d %d",a,b);
6. return 0;
7. }
Output:
10 10
2) register
The register variable allocates memory in register than RAM. Its size is same of register size.
It has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
3) static
The static variable is initialized only once and exists till the end of the program. It retains its
value between multiple functions call.
The static variable has the default value 0 which is provided by compiler.
1. #include<stdio.h>
2. int func(){
3.
4. static int i=0;//static variable
5. int j=0;//local variable
6. i++;
7. j++;
8. printf("i= %d and j= %d\n", i, j);
9. }
10. int main() {
11. func();
12. func();
13. func();
14. return 0;
15. }
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1
4) extern
The extern variable is visible to all the programs. It is used if two or more files are sharing
same variable or function.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Array
Array in C language is a collection or group of elements (data). All the elements of c array
are homogeneous (similar). It has contiguous memory location.
C array is beneficial if you have to store similar elements. Suppose you have to store marks
of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to
manage. For example we can not access the value of these variables with only 1 or 2 lines of
code.
Another way to do this is array. By using array, we can access the elements easily. Only few
lines of code is required to access the elements of array.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array
easily.
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed
the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.
Declaration of C Array
We can declare an array in the c language in the following way.
1. data_type array_name[array_size];
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. int marks[5];
Here, int is the data_type, marks is the array_name and 5 is the array_size.
Initialization of C Array
A simple way to initialize array is by index. Notice that array index starts from 0 and ends
with [SIZE - 1].
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;
C array example
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5];//declaration of array
5. marks[0]=80;//initialization of array
6. marks[1]=60;
7. marks[2]=70;
8. marks[3]=85;
9. marks[4]=75;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define size. So it can also be written as the
following code.
1. int marks[]={20,30,40,50,60};
Let's see the full program to declare and initialize the array in C.
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5]={20,30,40,50,60};//declaration and initialization of array
5. //traversal of array
6. for(i=0;i<5;i++){
7. printf("%d \n",marks[i]);
8. }
9. return 0;
10. }
Output
20
30
40
50
60
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
The two dimensional, three dimensional or other dimensional arrays are also known
as multidimensional arrays.
1. data_type array_name[size1][size2];
1. int twodimen[4][3];
Initialization of 2D Array in C
A way to initialize the two dimensional array at the time of declaration is given below.
1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
C Pointers
The pointer in C language is a variable, it is also known as locator or indicator that points
to an address of a value.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees etc. and used with arrays, structures and functions.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many usage of pointers in c language.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Address Of Operator
The address of operator '&' returns the address of a variable. But, we need to use %u to
display the address of a variable.
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. printf("value of number is %d, address of number is %u",number,&number);
5. return 0;
6. }
Output
value of number is 50, address of number is fff4
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol).
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Pointer example
An example of using pointers printing the address and value is given below.
As you can see in the above figure, pointer variable stores the address of number variable
i.e. fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Output
Address of number variable is fff4
Address of p variable is fff4
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Value of p variable is 50
NULL Pointer
A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have
any address to be specified in the pointer at the time of declaration, you can assign NULL
value. It will a better approach.
int *p=NULL;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Pointer to Pointer
In C pointer to pointer concept, a pointer refers to the address of another pointer.
In c language, a pointer can point to the address of another pointer which points to the
address of a value. Let's understand it by the diagram given below:
1. int **p2;
As you can see in the above figure, p2 contains the address of p (fff2) and p contains the
address of number variable (fff4).
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. int *p;//pointer to int
5. int **p2;//pointer to pointer
6. p=&number;//stores the address of number variable
7. p2=&p;
8. printf("Address of number variable is %x \n",&number);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Strings
String in C language is an array of characters that is terminated by \0 (null character).
1. By char array
2. By string literal
As you know well, array index starts from 0, so it will be represented as in the figure given
below.
While declaring string, size is not mandatory. So you can write the above code as given below:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
You can also define string by string literal in C language. For example:
1. char ch[]="swati";
In such case, '\0' will be appended at the end of string by the compiler.
String Example in C
Let's see a simple example to declare and print string. The '%s' is used to print string in c
language.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'s', 'w', 'a', 't', 'i', '\0'};
5. char ch2[11]="swati";
6.
7. printf("Char Array Value is: %s\n", ch);
8. printf("String Literal Value is: %s\n", ch2);
9. return 0;
10. }
Output:
Let's see a simple program to read and write string using gets() and puts() functions.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char name[50];
5. printf("Enter your name: ");
6. gets(name); //reads string from user
7. printf("Your name is: ");
8. puts(name); //displays string
9. return 0;
10. }
Output:
C String Functions
There are many important string functions defined in "string.h" library.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[20]= {‘s’, ’w’, ’a’, 't', ’I’, '\0'}
5. printf("Length of string is: %d",strlen(ch));
6. return 0;
7. }
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
8. char ch[20]= {‘s’, ’w’, ’a’, 't', ’I’, '\0'};
4. char ch2[20];
5. strcpy(ch2,ch);
6. printf("Value of second string is: %s",ch2);
7. return 0;
8. }
Output:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
The strcat(first_string, second_string) function concatenates two strings and result is returned
to first_string.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
5. char ch2[10]={'c', '\0'};
6. strcat(ch,ch2);
7. printf("Value of first string is: %s",ch);
8. return 0;
9. }
Output:
Here, we are using gets() function which reads string from the console.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str1[20],str2[20];
5. printf("Enter 1st string: ");
6. gets(str1);//reads string from console
7. printf("Enter 2nd string: ");
8. gets(str2);
9. if(strcmp(str1,str2)==0)
10. printf("Strings are equal");
11. else
12. printf("Strings are not equal");
13. return 0;
14. }
Output:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nReverse String is: %s",strrev(str));
9. return 0;
10. }
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nUpper String is: %s",strupr(str));
9. return 0;
10. }
Output:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Math
C Programming allows us to perform mathematical operations through the functions defined
in <math.h> header file. The <math.h> header file contains various methods for performing
mathematical operations such as sqrt(), pow(), ceil(), floor() etc.
C Math Functions
There are various methods in math.h header file. The commonly used functions of math.h
header file are given below.
1) ceil(number) rounds up the given number. It returns the integer value which
is greater than or equal to given number.
2) floor(number) rounds down the given number. It returns the integer value
which is less than or equal to given number.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
C Math Example
Let's see a simple example of math functions found in math.h header file.
1. #include<stdio.h>
2. #include <math.h>
3. int main(){
4. printf("\n%f",ceil(3.6));
5. printf("\n%f",ceil(3.3));
6. printf("\n%f",floor(3.6));
7. printf("\n%f",floor(3.2));
8. printf("\n%f",sqrt(16));
9. printf("\n%f",sqrt(7));
10. printf("\n%f",pow(2,4));
11. printf("\n%f",pow(3,3));
12. printf("\n%d",abs(-12));
13. return 0;
14. }
Output:
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Structure in C
Structure in c language is a user defined datatype that allows you to hold different type of
elements.
It works like a template in C++ and class in Java. You can have different type of elements in
it.
Defining structure
The struct keyword is used to define structure. Let's see the syntax to define structure in c.
1. struct structure_name
2. {
3. data_type member1;
4. data_type member2;
5. .
6. .
7. data_type memeberN;
8. };
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. };
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Example
1st way:
Let's see the example to declare structure variable by struct keyword. It should be declared
within the main function.
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. };
2nd way:
Let's see another way to declare variable at the time of defining structure.
1. struct employee
2. { int id;
3. char name[50];
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
4. float salary;
5. }e1,e2;
Which approach is good
But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the
structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main()
fuction.
Let's see the code to access the id member of p1 variable by . (member) operator.
1. p1.id
C Structure example
Example of Structure in C
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Unions are quite similar to structures in C. Like structures, unions are also derived
types.
union car
char name[50];
int price;
};
Defining a union is as easy as replacing the keyword struct with the keyword union.
union car
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
char name[50];
int price;
OR
union car
char name[50];
int price;
};
int main()
return 0;
In both cases, union variables car1, car2 and union pointer variable car3 of type union
car is created.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
In the above example, suppose you want to access price for union variable car1, it can
be accessed as:
car1.price
Likewise, if you want to access price for the union pointer variable car3, it can be
accessed as:
(*car3).price
or;
car3->price
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d", sizeof(uJob));
printf("\nsize of structure = %d", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40
The amount of memory required to store a structure variable is the sum of memory size
of all members.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
But, the memory required to store a union variable is the memory required for the
largest element of an union.
But, in the case of union, only one of its members can be accessed at a time and all
other members will contain garbage values.
#include <stdio.h>
union job
{
char name[32];
float salary;
int workerNo;
} job1;
int main()
{
printf("Enter name:\n");
scanf("%s", &job1.name);
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
return 0;
}
Output
Enter name
Hillary
Enter salary
1234.23
Displaying
Name: f%Bary
Salary: 1234.2
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
SQL
What is SQL
o SQL stands for Structured Query Language.
o SQL is a database language, it is used for database creation, deletion, fetching rows
and modifying rows etc.
All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, Postgres and SQL Server use SQL
as standard database language.
o With SQL, user can access data from relational database management system.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
o It allows user to define the data in database and manipulate it when needed.
What is Database
A database is an organized collection of data.
Database handlers create database in such a way that only one set of software program
provide access of data to all the users.
There are many dynamic websites on the world wide web now a days which are handled
through databases. For example, a model to checks the availability of rooms in a hotel. It is
an example of dynamic website that uses database.
There are many database available like MySQL, Sybase, Oracle, Mango DB, Informix,
Postgre, SQL Server etc.
SQL or Structured Query Language is used to perform operation on the data stored in a
database. SQL depends on relational algebra and tuple relational calculus.
What is RDBMS
RDBMS stands for Relational Database Management Systems..
All modern database management systems like SQL, MS SQL Server, IBM DB2, ORACLE, My-
SQL and Microsoft Access are based on RDBMS.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
How it works
Data is represented in terms of tuples (rows) in RDBMS.
Relational database is most commonly used database. It contains number of tables and each
table has its own primary key.
Due to a collection of organized set of tables, data can be accessed easily in RDBMS.
What is table
The RDBMS database uses tables to store data. A table is a collection of related data entries
and contains rows and columns to store data.
ID Name AGE C
1 Ajeet 24
2 aryan 20
3 Mahesh 21
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
4 Ratan 22
5 Vimal 26
What is field
Field is a smaller entity of the table which contains specific information about every record in
the table. In the above example, the field in the student table consist of id, name, age, course.
1 Ajeet 24 B
What is column
A column is a vertical entity in the table which contains all information associated with a
specific field in a table. For example: "name" is a column in the above table which contains
all information about student's name.
Ajeet
Aryan
Mahesh
Ratan
Vimal
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
NULL Values
The NULL value of the table specifies that the field has been left blank during record creation.
It is totally different from the value filled with zero or a field that contains space.
Data Integrity
There are the following categories of data integrity exist with each RDBMS:
Domain integrity: It enforces valid entries for a given column by restricting the type, the
format, or the range of values.
Referential integrity: It specifies that rows cannot be deleted, which are used by other
records.
User-defined integrity: It enforces some specific business rules that are defined by users.
These rules are different from entity, domain or referential integrity.
SQL Syntax
SQL follows some unique set of rules and guidelines called syntax. Here, we are providing all
the basic SQL syntax.
o SQL is not case sensitive. Generally SQL keywords are written in uppercase.
o SQL statements are dependent on text lines. We can place a single SQL statement on
one or multiple text lines.
o You can perform most of the action in a database with SQL statements.
SQL statement
SQL statements are started with any of the SQL commands/keywords like SELECT, INSERT,
UPDATE, DELETE, ALTER, DROP etc. and the statement ends with a semicolon (;).
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
In this tutorial, we will use semicolon at the end of each SQL statement.
SQL Commands
These are the some important SQL command:
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Bit varying BIT VARYING(X) Here, 'x' is the number of bits to store
(length can vary up to x).
Time with time TIME WITH TIME It is exactly same as time but also store
zone ZONE an offset from UTC of the time specified.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
If you want to add tables in that database, you can use CREATE TABLE statement.
If you want to delete or drop an existing database in a SQL schema, you can use SQL DROP
DATABASE
If you delete or drop the database, all the tables and views will also be deleted. So be careful while
using this command
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
If you are using SQL server 2000, you can also use this command to rename the database.
But, Microsoft phased out it.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
SQL Table
Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is
known as relation and row as tuple.
Note: A table has a specified number of columns, but can have any number of rows.
Table is the simple form of data storage. A table is also considered as a convenient
representation of relations.
Employee
In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS" and "SALARY"
are the column names. The combination of data of multiple columns forms a row e.g. "Ankit",
"Lucknow" and 15000 are the data of one row.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
It is a variable where we temporary store records and results. This is same like temp table
but in the case of temp table we need to explicitly drop it.
Table variables are used to store a set of records. So declaration syntax generally looks like
CREATE TABLE syntax.
When a transaction rolled back the data associated with table variable is not rolled back.
If you want to create a table, you should name the table and define its column and each
column's data type.
The data type of the columns may vary from one database to another. For example, NUMBER
is supported in Oracle database for integer value whereas INT is supported in MySQL.
Let us take an example to create a STUDENTS table with ID as primary key and NOT NULL
are the constraint showing that these fields cannot be NULL while creating records in the
table.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
You can verify it, if you have created the table successfully by looking at the message
displayed by the SQL Server, else you can use DESC command as follows:
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
Now you have the STUDENTS table available in your database and you can use to store
required information related to students.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
6. Email varchar(255),
7. AddressLine varchar(255),
8. City varchar(255)
9. );
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
A SQL DROP TABLE statement is used to delete a table definition and all data from a table.
This is very important to know that once a table is deleted all the information available in the
table is lost forever, so we have to be very careful when using this command.
Let's see the syntax to drop the table from the database.
First we verify STUDENTS table and then we would delete it from the database.
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
This shows that STUDENTS table is available in the database, so we can drop it as follows:
Now, use the following command to check whether table exists or not.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
But if you do not specify the WHERE condition it will remove all the rows from the table.
There are some more terms similar to DELETE statement like as DROP statement and
TRUNCATE statement but they are not exactly same there are some differences between
them.
The TRUNCATE statement: it is used to delete all the rows from the table and free the
containing space.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1 Aryan Allahabad
2 Shurabhi Varanasi
3 Pappu Delhi
On the other hand when we TRUNCATE a table, the table structure remains the same, so
you will not face any of the above problems.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
2. RENAME TO new_table_name;
Let us take an example of a table named "STUDENTS", now due to some reason we want to
change it into table name "ARTISTS".
Table1: students
You should use any one of the following syntax to RENAME the table name:
Or
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
Let's see the syntax to truncate the table from the database.
For example, you can write following command to truncate the data of employee table
Note: The rollback process is not possible after truncate table statement. Once you truncate
a table you cannot use a flashback table statement to retrieve the content of the table.
For example, you can write following command to copy the records of hr_employee table into
employee table.
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
You can also use SQL ALTER TABLE command to add and drop various constraints on an
existing table.
If you want to add multiple columns in table, the SQL table will be
If you want to modify multiple columns in table, the SQL table will be
www.cryolitesoftwares.com contact:0522-4962150
Cryolite India Softwares Pvt. Ltd. C Programming Language Notes
1300000
-250000
10,50,000
-2,50,000*5%=12,500
800000
-500000*20%=100000
300000*30%=90000
www.cryolitesoftwares.com contact:0522-4962150