Friday 17 June 2011

What is argc and argv?

C and C++ have a special argument list for main( ), which looks like this:

int main(int argc, char* argv[ ]) { // ...

The first argument is the number of elements in the array, which is the second argument. The second argument is always an array of char*, because the arguments are passed from the command line as character arrays (and remember, an array can be passed only as a pointer). Each whitespace-delimited cluster of characters on the command line is turned into a separate array argument. The following program prints out all its command-line arguments by stepping through the array:

Example:

#include

int main(int argc, char* argv[ ]) {

cout << "argc = " << argc << endl;

for(int i = 0; i < argc; i++)

cout << "argv[" << i << "] = "

<< argv[i] << endl;

}

You’ll notice that argv[0] is the path and name of the program itself. This allows the program to discover information about itself. It also adds one more to the array of program arguments, so a common error when fetching command-line arguments is to grab argv[0] when you want argv[1].

You are not forced to use argc and argv as identifiers in main( ); those identifiers are only conventions (but it will confuse people if you don’t use them). Also, there is an alternate way to declare argv:

int main(int argc, char** argv) { // ...
Both forms are equivalent

What is an Algorithm?

An Algorithm is a generic term for any procedure. We usually mean a procedure that can be implemented as a routine, and which performs some well defined task. In general there are several possible Algorithms to perform the same task. NAO's Algorithm class is an organizational class that does note define any interface, but merely groups classes. Algorithms that perform the same task (or type of task) will share an interface defined in an abstract base class derived from the Algorithm class.

What is Abstract Data Type(ADT)?

In programming, a data set defined by the programmer in terms of the information it can contain and the operations that can be performed with it. An abstract data type is more generalized than a data type constrained by the properties of the objects it contains—for example, the data type "pet" is more generalized than the data types "pet dog," "pet bird," and "pet fish." The standard example used in illustrating an abstract data type is the stack, a small portion of memory used to store information, generally on a temporary basis. As an abstract data type, the stack is simply a structure onto which values can be pushed (added) and from which they can be popped (removed). The type of value, such as integer, is irrelevant to the definition. The way in which the program performs operations on abstract data types is encapsulated, or hidden, from the rest of the program. Encapsulation enables the programmer to change the definition of the data type or its operations without introducing errors to the existing code that uses the abstract data type. Abstract data types represent an intermediate step between traditional programming and object-oriented programming.

What’s the difference between void main and int main?

Void main (): You can't return a value from void main. i.e., you can't return 0; at the end of main (). You can use "return;" but no value is passed back to any program that called it. This may not seem like much of a problem to you at this point; but it *will* become important as you create more complex programs.
int main (): Returns a integer value at the end of execution. The value returned can be a standard 0 or any other int to represent and number of errors/choices/results/etc... int main () is a standard.
Go for int main (), if for no other reason than that it’s a good coding habit. It’s no harder to type "return 0;" than "return;", and it will save you time in the future.

Difference between getch( ) getche( ) and getchar( ) functions.

getch ( ) is used when you want to get a character from the user via keyboard. getch ( ) does not display the character pressed by the user from the keyboard. It returns ASCII Code of the character pressed by the user.
getche ( ) works same like getch ( ) except it displays the character pressed by the user. It returns ASCII Code of the character pressed by the user.
getchar ( ) works like getch ( ) and getche ( ) except it will continue inputting the character until Enter is pressed. It returns the ASCII Code of first character entered by the user