Tuesday, January 10, 2006

Breaking Myths about C and C++ - Part 1

Breaking Myths about C and C++ - Part 1

Myth C++ is a high level Language

Reality The Answer is NO. Both C and C++ is middle level language. Though, C++ tends toward high –level languages but still it has some middle-level languages feature like pointers. If there are pointers, we can interact with address; we can do that in C++ too.


Myth High level languages are modern and middle-level languages are their ancestors.
Reality This is not at all true. FORTRAN is a high-level language and provides a higher degree of abstraction from C but it was developed earlier and has less features and less flexibility. FORTRAN, BASIC and PASCAL are high-level languages developed much earlier than C or C++. They have one thing common in them, they are non-structured. All the ancient languages are non-structured while modern languages are highly structured. Structured languages are Pascal, Ada, Java, C#, C++, C, Modula-2


Myth we cannot declare a variable with the name main

Reality Yes we can as main is not a keyword, but it will confuse compiler, therefore it is suggested that we should avoid using it.


Myth
#include<stdio.h>
int i;
void func1(void);
void func2(void);
int main(void)
{
i =100;
printf(“%d\n”,i);
func1();
func2();
}
void func1(void)
{
int i =0;
printf(“%d\n”,i);
}

void func2(void)
{
printf(“%d\n”,i);
}
The output of this program will be
100
0
0

Reality
The output will be:
100
0
100

If global variable is declared again locally, the value of global variable in that scope changes locally according to changes in that scope but the changes are not reflected globally.


Myth There are two qualifiers that control how variables may be accessed or modified: const and volatile. A variable cannot be both const and volatile at the same time.

Reality const prevents the variable’s value to be changed if explicitly specified by the program whereas volatile allows the variable’s value to be changed in ways not explicitly specified by the program. For example, a global address may be passed to the operating system’s clock routine and used to hold the real time of the system. In this situation, the value is not changed explicitly changed by the program. Therefore, system clock cannot be changed by any naïve user as well as it gets changed automatically.


Myth extern is another way to declare global variables and extern is used to declare the global variables and function from within the main function.

Reality It is true that when a variable is declared preceding with the extern specifier, it becomes global variable but it is used for different purpose. Suppose, a variable is declared extern, then it is not required to define the variable in the same file and there can be many declarations but only one definition. It is used to make compiler understand that the given variable has external linkages. Thus, compiler will not generate errors if the variable is not defined. extern can be used to declare functions and main functions from outside any function. It is usually used in header files. In C++, While providing linkage specification of function for other languages, extern definition should be written outside any function.

extern “Java”
{
void MyName( )
void ResourceAllocator( )
}

Comments:
hmm... the C program... The output would be just '100', since you are calling neither of the functions.

--
Prashanth Mohan -- http://prashblog.be
 
thanx for findin errors... we really need people like u...
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?