Bharat Banate's Work Profile

View Bharat Banate's profile on LinkedIn
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, September 20, 2007

Info: Ada Lovelace - First programmer

Lovelace was the daughter of Lord Byron and assistant to the mathematician, Charles Babbage, the inventor of the computer, and became the first computer programmer.
Augusta Ada Byron was born on 10th December 1815, in London, the daughter of the poet George Gordon Byron and Annabella Milbanke Byron. On 21st April 1816, Byron separated from his wife and left Britain for ever, leaving his wife and daughter, never to see them again.
Ada was educated privately by tutors and by her mother, who had an abiding interest in mathematics. Lord Byron once called her ‘the princess of parallelograms’. Her mother gave Ada regular lessons in maths, in the hope that the logical discipline would inhibit the onset of madness, which Annabella thought existed in the Byron family.
In 1835, Ada married William King, 8th Baron King and, when he was created an earl in 1838, she became countess of Lovelace. She became acquainted with Mary Somerville, a noted scientific author, who introduced her in turn to Charles Babbage, the inventor of a calculating machine, later to become known as the computer. She told Babbage that she was well acquainted with mathematics and offered to help with the construction of his machine. Babbage doubted that a woman would have sufficient knowledge of mathematics, to be of any value to him, but when Ada added that she had some knowledge of languages, Babbage hired her as a translator.
In 1843, Ada translated and annotated an article written by the Italian mathematician and engineer, Luigi Federico Menabrea, who had proposed new functions for Babbage’s Analytical Machine. Ada not only translated the article, but added her own details and annotations. Her elaborate annotations, especially her description of how the Analytical Engine could be programmed to compute and make calculations beyond the power of the human brain, earned her the title of first computer programmer. She wrote in her notes ‘the Analytical Engine weaves algebraic patterns, just as the Jacquard-loom weaves flowers and leaves.’ She added ‘the Engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.’
In 1852, at the age of 36, Ada contracted cancer and was put in the care of physicians who recommended bloodletting. Unfortunately they went too far, as was often the case in the Nineteenth Century, and she bled to death. She died on 29th November 1852 and was buried with her father at the Byron family church in Hucknall.
The programming language Ada is named after her.

Tuesday, September 18, 2007

D: Why D?


Continued from previous post...


Why, indeed. Who needs another programming language?
The software industry has come a long way since the C language was invented. Many new concepts were added to the language with C++, but backwards compatibility with C was maintained, including compatibility with nearly all the weaknesses of the original design. There have been many attempts to fix those weaknesses, but the compatibility issue frustrates it. Meanwhile, both C and C++ undergo a constant accretion of new features. These new features must be carefully fitted into the existing structure without requiring rewriting old code. The end result is very complicated - the C standard is nearly 500 pages, and the C++ standard is about 750 pages! C++ is a difficult and costly language to implement, resulting in implementation variations that make it frustrating to write fully portable C++ code.

C++ programmers tend to program in particular islands of the language, i.e. getting very proficient using certain features while avoiding other feature sets. While the code is usually portable from compiler to compiler, it can be hard to port it from programmer to programmer. A great strength of C++ is that it can support many radically different styles of programming - but in long term use, the overlapping and contradictory styles are a hindrance.
C++ implements things like resizable arrays and string concatenation as part of the standard library, not as part of the core language. Not being part of the core language has several
suboptimal consequences.

Can the power and capability of C++ be extracted, redesigned, and recast into a language that is simple, orthogonal, and practical? Can it all be put into a package that is easy for compiler writers to correctly implement, and which enables compilers to efficiently generate aggressively optimized code?

Modern compiler technology has progressed to the point where language features for the purpose of compensating for primitive compiler technology can be omitted. (An example of this would be the 'register' keyword in C, a more subtle example is the macro preprocessor in C.) We can rely on modern compiler optimization technology to not need language features necessary to get acceptable code quality out of primitive compilers.

Sunday, September 16, 2007

D: What is D?

D is a general purpose systems and applications programming language. It is a higher level language than C++, but retains the ability to write high performance code and interface directly with the operating system API's and with hardware. D is well suited to writing medium to large scale million line programs with teams of developers. D is easy to learn, provides many capabilities to aid the programmer, and is well suited to aggressive compiler optimization technology.

D is not a scripting language, nor an interpreted language. It doesn't come with a VM, a religion, or an overriding philosophy. It's a practical language for practical programmers who need to get the job done quickly, reliably, and leave behind maintainable, easy to understand code.

D is the culmination of decades of experience implementing compilers for many diverse languages, and attempting to construct large projects using those languages. D draws inspiration from those other languages (most especially C++) and tempers it with experience and real world practicality.

Friday, September 14, 2007

C: How to use memcpy function in C

The memcpy function in C++ copies the specified number of bytes of data from the specified source to the specified destination. This is a binary copy so the underlying data type is irrelevant. The following steps will help you use the memcpy function.

Step 0
Learn the syntax of memcpy in C++. The complete syntax is void *memcpy (void *destination, const void *source, size_t num);. Note that this function always copies num bytes and does not look for a terminating character in order to be as efficient as possible. Memcpy returns the destination array.

Step 1
Know that the pointers to the source and destination arrays are type-cast to a type of void. The size of the destination and source arrays should be at least num bytes to avoid overflows, although this is not required. Memmove should be considered as a safer approach if the source and destination overlap.

Step 2
Understand that the C++ memcpy function is kept in the cstring library. You may need to include the string.h header file to use memcpy.

Step 3
Look at the following complete program for some simple examples of how to use memcpy:


#include_<_stdio.h_>
#include_<_string.h_>
int main ()
{
__char string1[]="test string";
__char string2[80];
__memcpy (string2,string1,strlen(string1)+1);
__printf ("string1: %s\nstring2: %s\n",string1,string2);
__memcpy (string1,"",1);
__printf ("string1: %s\n",string1);
__return 0;
}

Observe the following output for this program:
string1: test string
string2: test string
string1:
The first use of memcpy copies the contents of string1 to the contents of string2. The second use of memcpy clears the contents of string1 by moving the null terminator character to the first position of string1.

Note: Please DO NOT copy-paste the code given in this post.

Saturday, September 8, 2007

C: Is C under DOS the same as C under UNIX ?

Well, the topic seems to be weird... people can spend hours tyring to decide on this. There will be many people on either side, and none willing to budge.However, most of them will agree that in the end C remains a single entity no matter what the OS under which it is run.Why?The basic structure of C, its power, its data types, its control structures, its syntax all remain the same no matter which OS it runs under.So, where is the difference?
To prove a point to the non-believers lets write a program and run it under DOS and UNIX.


main (int argc, char* argv)
{
_int i;
_for(i=0; i < argc; i++)
_{
__printf("Argument is %s\n", argv[i]);
_}
}

Lets run this program with the parameter 'Hello World'.Under both DOS and UNIX two statements each will be displayed: the program name and 'Hello World'.
Now doesn't this prove that C under DOS and UNIX is the same?

No !!!
Lets run the program again, but with different parameter. Instead of 'Hello World', pass a * .
And what we get then?
Under DOS we get the program name and the star ' * '.
But under UNIX there is a difference. Instead of the star we get a directory listing.

There is the proof that C under these two OS's is different? Not so fast.
Lets first see what happened.Under DOS, C took the star as an argument and printed it. That's because the DOS shell, COMMAND.COM, took it for what it was: a star. The UNIX shell, however, is intelligent. It sometimes proceeds to deduce. In this case it interpreted the star to mean a directory listing. This directory listing was then passed to the program as an argument which proceeded to display the files on screen.

On the face of it, yes, the program worked differently and therefore we can say C is different under DOS and UNIX. But is it really the program that give us this difference in output? Not at all. It was the shell. If the shell had not been built to interpret the star, the directory listing would never have appeared. And that is hardly the fault of the program.

Wednesday, September 5, 2007

C/C++: Variable-length argument lists

Let's move back to basics! The C programming...We know C programming very well. Do we? Well, we have learnt almost everything about C programming. But not everything.
C supports variable-length argument lists for functions. Best and well-known examples are printf and scanf functions and all their variants. But had we written any variable-argument function?

This post contains an implementation of a minimal version of printf, to show how to write a function that processes a var-arg list in a portable way. As we are mainly interested in the argument processing, min_printf will process the format string and arguments but will call original printf to do the format conversions.
The proper declaration for printf is:
int printf(char *fmt,...)
where, the declaration ... means that number and types of these arguments may vary.
The declaration ... can only appear at the end of an argument list.
Our
min_printf is declared as:
void min_printf(char *fmt,...)

Since we will not return the character count that printf does, return type is void. The real crux is how min_printf walks along the argument list when the list even doesn't have a name. The standard header stdarg.h contains a set of macro definitions that define how to step through an argument list. The implementation of this header will vary from machine to machine, but the interface it presents is uniform.
The type va_list is used to declare a variable that will refer to each argument in turn. In our function min_printf, this variable is ap, for "argument pointer". The macro va_start initializes ap to point to the first unnamed argument. It must be called once before ap is used. There must be at least one named argument. The final named argument is used by va_start to get started. Each call to va_arg returns one argument and steps ap to the next one. va_arg uses a type name to determine what type to return and how big a step to take. Finally, va_end does whatever cleanup is necessary. It must be called before the function returns. These properties form the basis for our simple min_printf. Here is the code for min_printf:
/* min_printf: minimal printf with variable argument list */
void min_printf(char *fmt, ...)
{
_va_list ap; /* points to each unnamed arg */
_char *p, *sval;
_int ival;
_double dval;
_/* make ap point to 1st unnamed arg */
_va_start(ap,fmt);
_for (p=fmt; *p; p++)
_{
__if (*p != '%')
__{
___putchar(*p);
___continue;
__}//if
__switch(*++p)
__{
___case 'd':
____ival = va_arg(ap,int);
____printf("%d", ival);
____break;
___case 'f':
____dval = va_arg(ap,double);
____printf("%f", dval);
____break;
___case 's':
____for (sval=va_arg(ap,char*); *sval; sval++)
_____putchar(*sval);
___break;
___default:
____putchar(*p);
____break;
__}//switch
_}//for
_/* clean up when done */
_va_end(ap);
}//min_printf
Note:
If you copy-paste this code, kindly remove the underscores( _ ) at the beginning of each line. Underscores are added for indentation purpose.