About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program to Create Your Own Header File in C Programming

Make Your Own Header File ?
Step1 : Type this Code 
int add(int a,int b)
{
return(a+b);
}
  • In this Code write only function definition as you write in General C Program
Step 2 : Save Code
  • Save Above Code with [.h ] Extension .
  • Let name of our header file be myhead [ myhead.h ]
  • Compile Code if required.
Step 3 : Write Main Program
#include
#include"myhead.h"
void main()
{
int number1=10,number2=10,number3;
number3 = add(number1,number2);
printf("Addition of Two numbers : %d",number3);
}
  1. Include Our New Header File .
  2. Instead of writing < myhead.h> use this terminology “myhead.h”
  3. All the Functions defined in the myhead.h header file are now ready for use .
  4. Directly  call function add(); [ Provide proper parameter and take care of return type ]
Note
  1. While running your program precaution to be taken : Both files [ myhead.h and sample.c ] should be in same folder.

C Program to Swap two numbers using XOR Operator

Generally Swaping two number requires three variables , Let’s Take look atProcedure of swaping two Number
For Swaping Two numbers following procedure is used -
x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)
Now we will Explaining above three statements using example ….
Let x = 12 and y = 9 [ For our sake and simplicity consider number is of 4 bits ]
x = 1100
y = 1001

X-OR Table :
  A   B  A X-OR B
110
101
011
000
Step 1 : After : x = x ^ y
x   = 1100
y   = 1001
----------
x^y = 0101
----------
x   = 0101    ..... New Value of x
Step 2 : After y = y ^ x
x   = 0101    ..... New Value is taken
y   = 1001    ..... Old Value of Y
----------
y^x = 1100
----------
y   = 1100    ..... New Value of y = Initial x
Step 3 : After x = x ^ y
x   = 0101    ..... New Value from step 1
y   = 1100    ..... New Value of y from Step 2
----------
y^x = 1001
----------
x   = 1001    ..... New Value of x = Initial y
Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]
#include
#include
void main()
{
int num1,num2;

printf("\nEnter First Number : ");
scanf("%d",&num1);

printf("\nEnter Second Number : ");
scanf("%d",&num2);

num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;

printf("\nNumbers after Exchange : ");
printf("num1 = %d and num2 = %d",num1,num2);

getch();
}
Output :
Enter First Number : 20
Enter Second Number : 40

Numbers after Exchange : num1 = 40 and num2 = 20

C Program to Demonstrate Nested Printf Statements

nested Printf statements : Example 1

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",printf("abcdefghijklmnopqrstuvwxyz"));
getch();
}

Output :
abcdefghijklmnopqrstuvwxyz26

How ?
  1. “abcdefghijklmnopqrstuvwxyz” will be first Printed while executing inner printf
  2. Total Length of the “abcdefghijklmnopqrstuvwxyz” is 26
  3. So printf will return total length of string
  4. It returns 26 to outer printf
  5. This outer printf will print 26

C Program to Demonstrate Printf inside Another Printf Statement

Printf inside printf in C : Example 1

#include
#include
void main()
{
int num=1342;
clrscr();
printf("%d",printf("%d",printf("%d",num)));
getch();
}

Output :
134241

How ?
  1. Firstly Inner printf is executed which results in printing 1324 
  2. This Printf Returns total number of Digits i.e  4 and second inner printf will looks like
  3. printf("%d",printf("%d",4));
  4. It prints 4 and Returns the total number of digits i.e 1 (4 is single digit number )
  5. printf("%d",1);
  6. It prints simply 1 and output will looks like 132441

Rule :
Inner printf returns Length of string printed on screen to the outer printf

C Program to Print Hello word without using semicolon

Part 1 : Printf Hello word in C without using semicolon [only ones ]

#include
void main()
{
   if(printf("Hello"))
   {
   }
}

Output :
Hello

Part 2 : Printf Hello word in C without using semicolon [infinite times]

#include
void main()
{
   while(printf("Hello"))
   {
   }
}

Part 3 : Printf Hello [Using Switch]

#include
void main()
{
   switch(printf("Hello"))
   {
   }
}

Part 4 : Using Else-if Ladder

#include
void main()
{
   if(printf(""))
      {
      }
   else if (printf("Hello"))
      {
      }
   else
      {
      }
}

Part 5 : Printf Hello [Using While and Not]

#include
void main()
{
    while(!printf("Hello"))
    {
    }
}

Part 6 : Using #define

#include
#define PRINT printf("Hello")
void main()
{
    if(PRINT)
    {
    }
}

C Program to Accept Paragraph using scanf

Accept Paragraph using scanf in C
#include
void main()
{
char para[100];
printf("Enter Paragraph : ");
scanf("%[^t]",para);
printf("%s",para);
}

Output :[Press Tab to Stop Accepting Characters ]
Enter Paragraph : C Programming is very easy to understand
C
Language
is backbone of
C++
Language

How ?
scanf("%[^t]",para);
  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. [^t]  represent that all characters are accepted except tab(t) ,whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
//------------------------------------
// Accepts only 100 Characters
//------------------------------------
scanf("%100[^t]",para);

C Program to Input Password for Validation of User name

How to Input Password in C ?
#include< stdio.h>
#include< conio.h>

void main()
{
char password[25],ch;
int i;

clrscr();
puts("Enter password : ");

while(1)
    {
    if(i<0)
         i=0;
    ch=getch();

    if(ch==13)
        break;

    if(ch==8) /*ASCII value of BACKSPACE*/
        {
        putch('b');
        putch(NULL);
        putch('b');
        i--;
        continue;
        }

   password[i++]=ch;
   ch='*';
   putch(ch);
   }

password[i]='';
printf("\nPassword Entered : %s",password);
getch();
}
Output :
Enter password : ******
Password Entered : rakesh

Explain ?
ch=getch();
  • Accept Character without Echo [ without displaying on Screen ]
  • getch will accept character and store it in “ch”
if(ch==13)
        break;
  • ASCII Value of “Enter Key” is 13
  • Stop Accepting Password Characters after “Enter” Key.
if(ch==8) /*ASCII value of BACKSPACE*/
        {
        putch('b');
        putch(NULL);
        putch('b');
        i--;
        continue;
        }
  • ASCII Value of “BACKSPACE” is 8
  • After hitting “backspace”following actions should be carried out -
    • Cursor Should be moved 1 character back.
    • Overwrite that character by “NULL”.
    • After Writing NULL again cursor is moved 1 character ahead so again move cursor 1 character back .
    • Decrement Current Track of Character. [i]
password[i++]=ch;
   ch='*';
  • Store Accepted Character in String array .
  • Instead of Displaying Character , display Asterisk (*)