C Program to Find out the size of the different data types


#include <stdio.h>
main()
{
 
 printf("    short int is %2d bytes \n", sizeof(short int));
 printf("          int is %2d bytes \n", sizeof(int));
 printf("        int * is %2d bytes \n", sizeof(int *));
 printf("     long int is %2d bytes \n", sizeof(long int));
 printf("   long int * is %2d bytes \n", sizeof(long int *));
 printf("   signed int is %2d bytes \n", sizeof(signed int));
 printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
 printf("\n");
 printf("        float is %2d bytes \n", sizeof(float));
 printf("      float * is %2d bytes \n", sizeof(float *));
 printf("       double is %2d bytes \n", sizeof(double));
 printf("     double * is %2d bytes \n", sizeof(double *));
 printf("  long double is %2d bytes \n", sizeof(long double));
 printf("\n");
 printf("  signed char is %2d bytes \n", sizeof(signed char));
 printf("         char is %2d bytes \n", sizeof(char));
 printf("       char * is %2d bytes \n", sizeof(char *));
 printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
}

C Program to Demonstrate the increment and decrement operators.


#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{

 /*
  * ++i -> i incremented before i is used.
  * –i -> i decremented before i is used.
  * j++ -> j is incremented AFTER j has been used.
  * j– -> j is decremented AFTER j has been used.
  */

 int i=1,j=1;

 puts(“\tDemo 1”);
 printf(“\t%d %d\n”,++i, j++);   /* O/P  2 1 */
 printf(“\t%d %d\n”,i, j);    /* O/P  2 2 */

 i=1;j=1;

 puts(“\n\tDemo 2”);
 printf(“\t%d \n”,i=j++);    /* O/P  1 */
 printf(“\t%d \n”,i=++j);    /* O/P  3 */

 i = 0; j = 0;

 puts(“\n\tDemo 3”);
 if ( (i++ == 1) && (j++ == 1)) puts(“Some text”);

 /* Will i and j get incremented? The answer is NO! Because
  * the expression in the left of ‘&&’ resolves to false the
  * compiler does NOT execute the expression on the right and
  * so ‘j’ does not get executed!!!!! */

 printf(“\t%d %d\n”,i, j);    /* O/P 1 0 */
getch();
}

C Program to implement topological sort.


#include<stdio.h>
#define MAX 200
int n,adj[MAX][MAX];
int front = -1,rear = -1,queue[MAX];
void main()
{
 int i,j = 0,k;
 int topsort[MAX],indeg[MAX];
 create_graph();
 printf(“The adjacency matrix is:\n”);
 display();
 for(i=1;i<+n;i++)
 {
  indeg[i]=indegree(i);
  if(indeg[i]==0)
   insert_queue(i);
 }
 while(front<=rear)
 {
  k=delete_queue();
  topsort[j++]=k;
  for(i=1;i<=n;i++)
  {
   if(adj[k][i]==1)
   {
    adj[k][i]=0;
    indeg[i]=indeg[i]-1;
    if(indeg[i]==0)
     insert_queue(i);
   }
  }
 }
 printf("Nodes after topological sorting are:\n");
 for(i=0;i<=n;i++)
  printf("%d",topsort[i]);
 printf("\n");
}
create_graph()
{
 int i,max_edges,origin,destin;
 printf("\n Enter number of vertices:");
 scamf("%d",&n);
 max_edges = n * (n - 1);
 for(i = 1;i <= max_edges;i++)
 {
  printf("\n Enter edge %d (00 to quit):",i);
  scanf("%d%d",&origin,&destin);
  if((origin == 0) && (destin == 0))
  {
   printf("Invalid edge!!\n");
   i–;
  }
  else
   adj[origin][destin] = 1;
 }return;
}
display()
{
 int i,j;
 for(i = 0;i <= n;i++)
 {
  for(j = 1;jrear)
  {
   printf(“Queue Underflow”);
   return;
  }
  else
  {
   del_item = queue[front];
   front = front + 1;
   return del_item;
  }
 }
 int indegree(int node)
 {
  int i,in_deg = 0;
  for(i = 1;i <= n;i++)
   if(adj[i][node] == 1)
    in_deg++;
  returnin_deg;
 }

C Program to mask password text with *


#include <stdio.h>
#include <conio.h>
 
void main()
{
 char pasword[10],usrname[10], ch;
 int i;
 
 clrscr();
 
 printf("Enter User name: ");
 gets(usrname);
 printf("Enter the password <any 8 characters>: ");
 
 for(i=0;i<8;i++)
 {
  ch = getch();
  pasword[i] = ch;
  ch = '*' ;
  printf("%c",ch);
 }
 
 pasword[i] = '';
 
 /*If you want to know what you have entered as password, you can print it*/
 printf("\nYour password is :");
 
 for(i=0;i<8;i++)
 {
  printf("%c",pasword[i]);
 }
}

C Program to demonstrate time functions.


#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-%y\n", l_time);
  printf("%s", string);
 }
 
 
}

C Program to convert days to years, weeks and days


#include <stdio.h>
#define DAYSINWEEK 7
 
void main()
{
 int ndays, year, week, days;
 
 printf("Enter the number of days\n");
 scanf("%d",&ndays);
 
 year = ndays/365;
 week = (ndays % 365)/DAYSINWEEK;
 days = (ndays%365) % DAYSINWEEK;
 
 printf ("%d is equivalent to %d years, %d weeks and %d days\n",
   ndays, year, week, days);
}