C Program to convert IP address to 32-bit long int

C Program to convert IP address to 32-bit long int. Internet Protocol Address is the unique number assigned to the each device, which is connected to the computer network. Previously we are using IPv4 versions, now we are using IPv6. Here we read the ip address using unions, and converted that to the 32-bit long int. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
union iptolint
{
char ip[16];
unsigned long int n;
};
unsigned long int conv(char []);

main()
{
union iptolint ipl;
printf(" Read the IP Address to be convertedn");
scanf("%s",ipl.ip);
ipl.n=conv(ipl.ip);
printf(" Equivalent 32-bit long int is : %lun",ipl.n);
}

unsigned long int conv(char ipadr[])
{
unsigned long int num=0,val;
char *tok,*ptr;
tok=strtok(ipadr,".");
while( tok != NULL)
{
val=strtoul(tok,&ptr,0);
num=(num << 8) + val;
tok=strtok(NULL,".");
}
return(num);
}

Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to do the operations of Sequential file with records.

C Program to do the operations of Sequential file with records. Sequential file, A file which consists of the same record types that is stored on a secondary storage device. The physical sequence of records may be based upon sorting values of one or more data items or fields. Here we are creating, Reading, searching the sequential file, using c file i/o operations. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
typedef struct
{
int usn;
char name[25];
int m1,m2,m3;
}STD;

STD s;

void display(FILE *);
int search(FILE *,int);

void main()
{
int i,n,usn_key,opn;
FILE *fp;
printf(" How many Records ? ");
scanf("%d",&n);
fp=fopen("stud.dat","w");
for(i=0;i<n;i++)
{
printf("Read the Info for Student: %d (usn,name,m1,m2,m3) n",i+1);
scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen("stud.dat","r");
do
{
printf("Press 1- Displayt 2- Searcht 3- Exitt Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("n Student Records in the File n");
display(fp);
break;
case 2: printf(" Read the USN of the student to be searched ?");
scanf("%d",&usn_key);
if(search(fp,usn_key))
{
printf("Success ! Record found in the filen");
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
else
printf(" Failure!! Record with USN %d not foundn",usn_key);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!n");
break;
}
}while(opn != 3);
fclose(fp);
} /* End of main() */

void display(FILE *fp)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
if( s.usn == usn_key) return 1;
return 0;
}





Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to compare two strings

C Strings:
C Program to accepts two strings and compare them, Finally it prints whether both are equal, or first string is greater than the second or the first string is less than the second string. The string comparison starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or until the end of the strings is reached. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/              
#include<stdio.h>
#include<conio.h>

void main()
{
 int count1=0,count2=0,flag=0,i;
 char str1[10],str2[10];

 clrscr();
 puts("Enter a string:");
 gets(str1);

 puts("Enter another string:");
 gets(str2);
 /*Count the number of characters in str1*/
 while (str1[count1]!='')
  count1++;
 /*Count the number of characters in str2*/
 while (str2[count2]!='')
  count2++;
 i=0;

 /*The string comparison starts with the first character in each string and
 continues with subsequent characters until the corresponding characters
 differ or until the end of the strings is reached.*/

 while ( (i < count1) && (i < count2))
 {
  if (str1[i] == str2[i])
  {
   i++;
   continue;
  }
  if (str1[i]<str2[i])
  {
   flag = -1;
   break;
  }
  if (str1[i] > str2[i])
  {
   flag = 1;
   break;
  }
 }

 if (flag==0)
  printf("Both strings are equaln");
 if (flag==1)
  printf("String1 is greater than string2n", str1, str2);
 if (flag == -1)
  printf("String1 is less than string2n", str1, str2);
 getch();
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now!

Share this program with your Facebook friends now! by liking it
Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com

C Program to demonstrate time functions.

C Program to demonstrate time functions. time.h library function is used to get and manipulate date and time functions. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h> /* NULL */
#include <time.h> /* ctime, asctime */

main()
{
time_t now; /* define 'now'. time_t is probably
* a typedef */

/* Calender time is the number of
* seconds since 1/1/1970 */

now = time((time_t *)NULL); /* Get the system time and put it
* into 'now' as 'calender time' */

printf("%s", ctime(&now)); /* Format data in 'now'
* NOTE that 'ctime' inserts a
* 'n' */

/*********************************************************************/

/* Here is another way to extract the time/date information */

time(&now);

printf("%s", ctime(&now)); /* Format data in 'now' */

/*********************************************************************/

{
struct tm *l_time;

l_time = localtime(&now); /* Convert 'calender time' to
* 'local time' - return a pointer
* to the 'tm' structure. localtime
* reserves the storage for us. */
printf("%s", asctime(l_time));
}

/*********************************************************************/

time(&now);
printf("%s", asctime(localtime( &now )));

/*********************************************************************/

{
struct tm *l_time;
char string[20];

time(&now);
l_time = localtime(&now);
strftime(string, sizeof string, "%d-%b-%yn", l_time);
printf("%s", string);
}


}
Read more Similar C Programs
Learn C Programming

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to demonstrate macros.

C Program to demonstrate macros. Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#define SQUARE(x) x*x

main()
{
int value=3;

printf("%d n", SQUARE(value));
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to demonstrate goto statement

C Program to demonstrate goto statement . goto statement jumps the control from one point to another locally(within a function). In this program we perform a division checking for divide by zero by using the goto statement.

The Program

#include <stdio.h>
#include <stdlib.h>
main()
{
char data[100];
double num1, num2;
printf("\nPlease enter a number ==> " );
gets(data);
num1 = atof(data);
printf("\nPlease enter a number ==> " );
gets(data);
num2 = atof(data);
/* Stop a divide by zero with
* the goto statement.
*/
if ( num2 == 0.0 ) goto end_prog;
printf("\n%4.2f divided by %4.2f is %4.2f\n", num1, num2, num1/num2);
end_prog:
printf("\nProgram ended");
}
view raw goto.c hosted with ❤ by GitHub

Sample Output

Similar C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to Demonstrate global and internal variables.

C Program to Demonstrate global and internal variables.When variable declared outside the all the blocks, they are called global variables, and they are initialized by the system. Variables declared within the functions are called as internal variables, the are locale to that function, and user only initialize the variable. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

int counter = 0; /* global because we are outside
all blocks. */
int func(void);

main()
{
counter++; /* global because it has not been
declared within this block */
printf("counter is %2d before the call to funcn", counter);

func(); /* call a function. */

printf("counter is %2d after the call to funcn", counter);
}

int func(void)
{
int counter = 10; /* local. */
printf("counter is %2d within funcn", counter);
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to demonstrate Enumeration.

C Program to demonstrate Enumeration. An enum(Enumeration) is a user-defined type consisting of a set of named constants called enumerators. This Program will return the month in a year. I.E. It returns 9 for September. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include<stdio.h>
main()
{
/*
* Define a list of aliases
*/
enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
/* A A
| |
| |
| ------- list of aliases.
-------------- Enumeration tag. */


enum months month; /* define 'month' variable of type 'months' */

printf("%dn", month=Sep); /* Assign integer value via an alias
* This will return a 9 */
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to demonstrate the ‘do’ statement.

C Program to demonstrate the ‘do’ statement. The C do while statement creates a structured loop that executes as long as a specified condition is true at the end of each pass through the loop. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>

main()
{
int i=1; /* Define an integer variable. */

/*
* The block is executed. Then the i <= 10
* expression is evaluated. If TRUE the block
* is executed again.
*/
do
{
printf ("i is %in", i);
i++;
} while (i <= 10);

}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to Demonstrate the #define pre-processor.

C Program to demonstrate #define pre-processor. Macro is a piece of text that is expanded by the pre-processor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#define EQ ==

main ()
{
/* the EQ is translated to == by
* the C pre-processor. COOL!
*/
if ( 5 EQ 5 ) printf("define works...n");
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com