Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 12 January 2014

Part:2:-Variable and Data Type

Part-2 Variable and Data type
Variable: 
A variable in program is specific piece of memory. Every variable in a program has a name. We use variable to store a data value in memory and retrieve the data that is stored in it.

Rules for giving a Name to variable:- (wrong declaration highlighted by red color)
a) A variable name only contains characters, digits and underscore. Variable names like oh! , Mike-Jack not allowed.

b) The first letter of variable name must be letter or underscore. Variable name must not be started with a digit like 8_oclock, 6_pack. Never try to start variable name with underscore because variable name started with underscore already declared in libraries which can cause quite confusion to understand.

c) No comma or blank space is provided within variable name.

d) Variable names are case-sensitive. If you declare length and Length are distinct.

e) Always try to give Variable name as short as possible and maximum of 31 characters. Giving large name will increase your typing effort.

f) You cannot provide C’s Keyword like main, switch as Variable name.



Data Type
Data type can be defined as type of data of variable. Whenever we declare a variable, we have to mention what kind of data it can hold or store. C offers Standard and minimal set of data type. Sometimes called as “Primitive Data Type”. 

For example:-
int num; //here int is Data Type
char name; //here char is Data Type

C language Data type can be classified into 3 type:-
1) Primary Data Type 2) Derived Data Type (e.g. Array, Pointer, Structure, Union) 3) User defined Data Type

Primary Data Type divided into four basic data types:-




Data Type
Description
Size(in byte)
Range
char
Character
1
-128 to +127
int
Integer
2 or 4
-32768 to +32767
float
Number with decimal point. Precision upto 6 digits
4
-3.4e38 to +3.4e38
Double
Number with decimal point. Precision upto 12 digits
8
-1.7e308 to +1.7e308

Size and Range of Data Type may vary according to the qualifiers. Data Type Qualifier can be divided into two part:-
1) Size Qualifier
2) Sign Qualifier

Size Qualifier: Size Qualifier alter the size of data type. There are 2 types of size qualifiers: short and long.
For Example:
Storage space or size of int data type is 4 byte for 32 bit processor. We can increase the range using long int whose size is 8 byte or decrease the size using short int whose size is 2 byte.

Sign Qualifier: Signed and unsigned keyword used to specify whether variable can store +ve number or +ve as well as –ve number.
Example:- unsigned int range is -32768 to +32767 whereas signed int range is 0 to 65,535

Example 2.1: Finding Size of Data Type using sizeof operator
#include <stdio.h>
#include <conio.h>
void main()
{
       printf("Size of char %d byte\n",sizeof(char));
       printf("Size of int %d byte\n",sizeof(int));
       printf("Size of float %d byte\n",sizeof(float));
       printf("Size of double %d byte\n",sizeof(double));
       getch();
}
On My system I get the following result


Constants:-
A Constant is used just like a variable, though its value never changes. For example: Value of π (pie) in trigonometry is fixed (constant) which is equal to 3.14159265.

We can declare constant in our program in two ways:-
1) Using #define    2) Using const keyword

Using #define:
#include <stdio.h>
#include <conio.h>
#define PI 3.14159f //defining name and value for PI
void main()
{
       float radius=2.2; //radius
       float circum=2*PI*radius; //variable storing circumference
       printf("Circumference of Circle=%f metre",circum); /*print the result on console or terminal*/
       getch();
}


Using const keyword:
#include <stdio.h>
#include <conio.h>
void main()
{
       const float PI=3.14159f; //declare PI using const
       float radius=2.2; //radius
       float circum=2*PI*radius; //variable storing circumference
       printf("Circumference of Circle=%f metre",circum); /*print the result on console or terminal*/
       getch();
}


When we use const keyword, we can specify a Data type for our constant where as in previous example (using # define), PI was just a sequence of characters that replace all the value of PI in your program.

Saturday, 14 December 2013

Friday, 13 December 2013

Part:1:-C Programming

Part-1 C Programming-Introduction

Before going to Start C Programming, Take a Look at Few Definitions:-

What is Program:-
A Program (i.e. Computer Program) is a Sequence of instructions, written to perform a Specified task with Computer.
Task of developing programs is called Programming.

Who is Programmer:-
The Person who is engaged in Programming Activity is known as Programmer. We can also say that Programmer is a person who writes Computer Software.

Introduction to C-Programming language

C is a general-purpose programming language developed by Dennis Ritchie for the Unix Operating System in 1972 at Bell Laboratories. C is successor of B language which was introduced around 1970. C is also Known as Middle level Language because it combines the best elements of low level or Machine language with high level language.
C Language has the ability to communicate directly with hardware makes it powerful Choice for System Programmers for writing system programs like Compiler and Interpreters. Today C is most widely used System Programming language. UNIX and Linux Operating System are written using C Language.

Why you should Learn C?
C is simple and Easy to Learn.
C is structured programming language.
C can compiled on variety of computers and programs written in C runs faster as Compared to other programming language.

C Compiler:-
C programs are written into text files with extension “.c”. Example helloworld.c. After creating a program using C language, if you want to run your program then you need a compiler. Compiler converts your program into a language understandable by a computer i.e.  Machine language (binary format).If you don’t have Compiler than you have to download it. Without Compiler you are not able to run your C Program.

Compiler vendors provides several Integrated Development Environment (IDE) which contain Editor as well as Compiler. There are several IDE available in the market targeting different Operating System. For Example:-Turbo C++, Borland C++, Microsoft C++ etc.


Example1.1: A Simple C Program:-

#include <stdio.h>
#include <conio.h>
void main()
{
       printf("Hello World");
       getch();
}



Explanation of Program:

#include is directive to C preprocessor known as Compiler Directive. Lines starting with # are processed by the preprocessor before the program is compiled by the Compiler. As we know that C is Small Language and it can do almost nothing without including the External Libraries. So, we have to tell the compiler what external code we want to use by including header files. In above example we have used two header files i.e. “stdio.h” and “conio.h”. The stdio library (standard input/output header) contains the code that allow us to read and write data from and to the terminal (Console).

Every program starts Executing from the main function. C Program can contain one or more function out of which one must be main. When printf statement executed, It prints Hello World message to the terminal.

Note:-
C is a Case sensitive Language.
      Always terminate the statement using semi-colon.


Example 1.2 Using Escape Sequence:-

#include <stdio.h>
#include <conio.h>
void main()
{
       printf("Hello World\n");
       printf("Welcome to C Programming Introduction");
       getch();

}

When printf statement executed, all the characters inside double quotes printed exactly same as the written in the code till it encounter the backslash. On encountering the backslash (“\”) in the string, it combines backslash with the next character and form an Escape Sequence. In Example 1.2 it form “\n” which means new line. When “\n” comes, the cursor moves to the next line and starts writing the characters from next line.

Escape Sequence
Description
\n
New Line
\a
Alert Sound
\t
Horizontal tab
\”
Inserting Double quote in string
\\
Inserting backslash in string
Table 1.1 Escape Sequence

Comments: Comments help people to read and understand your Program code. Comments are ignored by the Compiler. We can use Comments in two ways:-

Single line Comment:-
//This is the Example of Single Line Comment

Multiline Comment:-
/* this is the example of Multiline Comment
Program Created by Anoop Kumar Sharma*/



Tuesday, 26 November 2013

Copy Table Schema and Data From One Database to Another Database in SQL Server

Several times we need to copy SQL Server table schemas and data from one database to another database. In this article, I show how to do this using a query as well as graphically in SQL Server.

I have created two databases named databasefrm and databaseto. In database databasefrm, I have a table named Emp containing data like ID, Name, Address and so on. I want to copy the table Emp to databaseto. We can do this using various methods.

Method 1: Using Query
The query required here is:

Query Syntax: 
Select * into DestinationDB.dbo.tableName from SourceDB.dbo.SourceTable
Example
select * into databaseto.dbo.emp from databasefrm.dbo.Emp

This command only copies the table schema and data. If we want to copy objects, indexes, triggers or constraints then we need to generate Scripts (third method) that we will talk about later in this article.

If we want to copy a table in the same database then we need to first use our database then execute this query:
select * into newtable from SourceTable

Example
 
select * into emp1 from emp

We can also select only a few columns into the destination table.

Query Syntax
select col1, col2 into <destination_table> from <source_table>

Example
select Id,Name into databaseto.dbo.emp1 from databasefrm.dbo.Emp

Preview after executing this query:

Here we only copy Id and Name in table emp1 from the Emp table in the databasefrm database.
If we want to copy only the structure or the schema of the table then we need to use this query:
 
select *into <destination_database.dbo.destination table> from <source_database.dbo.source table> where 1 = 2
Preview after executing this query:

Method 2:
1. Open SQL Server Management Studio

2. Right-click on the database name then select "Tasks" > "Export data..." from the object explorer.

3. The SQL Server Import/Export wizard opens; click on "Next"


4. Provide authentication and select the source from which you want to copy the data; click "Next".


5. Specify where to copy the data to; click on "Next".





Method 3: Generate Script
By using the above two methods we are only able to copy the table schema and data but we are not able to copy views, functions, constraints, triggers and so on. We can copy all these things by generating scripts.

Let's see how to generate a script:
  1. Right-click on the database name then select "Tasks" then click on "Generate Scripts". After that the Script Wizard opens. Click on "Next".
  2. Select the Database you want to script.
  3. Choose the object types. Click on "Next".
  4. Select the tables.
  5. Select the Output option for the script.
  6. Change/Edit the database name to the name you want to execute this script for.
Let's see how to do it step-by-step:








Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook