Posts

Benefits of C language over other programming languages

C  is a middle level programming language developed by Dennis Ritchie during the early 1970s while working at AT&T Bell Labs in USA. The objective of its development was in the context of the re-design of the UNIX operating system to enable it to be used on multiple computers. Earlier the language B was now used for improving the UNIX system. Being a high level language, B allowed much faster production of code than in assembly language. Still, B suffered from drawbacks as it did not understand data-types and did not provide the use of “structures”. These drawbacks became the driving force for Ritchie for development of a new programming language called C. He kept most of language B’s syntax and added data-types and many other required changes. Eventually C was developed during 1971-73, containing both high-level functionality and the detailed features required to program an operating system. Hence, many of the UNIX components including UNIX kernel itself were eventually r...

Compiling a C program:- Behind the Scenes

Image
C is a high level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine. How do we compile and run a C program? Below are the steps we use on an Ubuntu machine with gcc compiler. We first create a C program using an editor and save the file as filename.c $ vi filename.c The diagram on right shows a simple program to add two numbers. Then compile it using below command. $ gcc –Wall filename.c –o filename The option -Wall enables all compiler’s warning messages. This option is recommended to generate better code. The option -o is used to specify output file name. If we do not use this option, then an output file with name a.out is generated. After compilation executable is generated and we run the generated executable using below command. $ ./filename What goes inside the compilation process? Compiler converts a C program into an executable. There are four phases for a C program to become an executa...

Is it fine to write “void main()” or “main()” in C/C++?

The definition void main() { /* ... */ } is not and never has been C++, nor has it even been C.  See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts int main() { /* ... */ } Run on IDE and int main( int argc, char * argv[]) { /* ... */ } Run on IDE A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to “the system” that invokes it. On systems that doesn’t provide such a facility the return value is ignored, but that doesn’t make “void main()” legal C++ or legal C.  Even if your compiler accepts “void main()” avoid it, or risk being considered ignorant by C and C++ programmers. In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution.  For example: #include <iostream> in...