Thursday, December 30, 2010

Can you write a program whice invoke another C program ?

#include<dos.h>
void main()
{
system("tcc another.c");
system("another.exe");
}

Can you write a program which produces own source code as output ?

#include <stdio.h>
void main()
 {
   FILE *fd;
   int c;

   fd= fopen("./itself.c","r");
   while ( (c=fgetc(fd)) != EOF)
     {
       printf("%c", c);
     }
  
fclose(fd);
}

Some Good Questions

  1. How can I convert integers to binary or hexadecimal?
  2. How can I call a function, given its name as a string?
  3. How do I access command-line arguments?
  4. How can I return multiple values from a function?
  5. How can I invoke another program from within a C program?
  6. How can I access memory located at a certain address?
  7. How can I allocate arrays or structures bigger than 64K?
  8. How can I find out how much memory is available?
  9. How can I read a directory in a C program?
  10. How can I increase the allowable number of simultaneously open files?
  11. What’s wrong with the call fopen (”c:\newdir\file.dat”, “r”)?
  12. How do you write a program which produces its own source code as its output?
  13. How can I find the day of the week given the date?
  14. Why doesn’t C have nested functions?
  15. What is the most efficient way to count the number of bits which are set in a value?

Tuesday, December 28, 2010

Reading and Writing Serial Port



#include<dos.h>
void main()
  {
    char data;
    int choice;
   
  printf("Enter the choice to send or receive from COM1::");
  scanf("%d",&choice);


  if(choice==1)
    {
      printf("Enter data to send::");
      scanf("%c",data);
      inportb(0x03f8,data);
    }
  else
    {
       data=outport(0x3f8);
       printf("reding from  COM1::%d",data);
    }
getch();
}

Creating CON folder in windows

CON is a reserve word in windows operating system. that's why we can not create the folder named as CON.

But by command prompt we can do this
STEP1: goto command prompt
STEP2: type in prompt e:\> "mkdir \\.\e:\con"
STEP3: verify by typing "dir \\.\e:\con"
STEP4: to delete the file or folder "rmdir \\.\e:\con"

following file names in Windows are reserved because they represent devices:
con, con.* -> the console
prn, prn.* -> the default printer, as a character device
aux, aux.* -> the default serial terminal, as a character device
lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, lpt9 -> the parallel ports, as character devices
com1, com2, com3, com4, com5, com6, com7, com8, com9 -> the COM ports
nul, nul.* -> the NUL or "waste bit bucket" or "black hole for bits" or "/dev/null" device

Write a C program to convert Decimal to Hex


void dec2hex(int num)
{
char hx[16]={"0123456789ABCDEF"};
if(num>15)
{
cout<<(num/16);
dec2hex(num%16);
}
else
cout<<hx[num];
}

Power Of Two

Can you write a program to find, if the number if power of 2 or not..

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter The Number:";
cin>>num;
if(! (num &(num-1)))
cout<<"\nNumber is Power of 2";
else
cout<<"\nNumber is NOT Power of 2";
getch();
}