Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Wednesday, January 4, 2012

The C Beginnings: part 3



A couple of days back, someone left a comment on the C Language discussion page of this blog stating that a compiler(miracle compiler) was giving issues/errors. So today in this post, I want to focus on the different type of C compiler I have come across and highlight their advantages/features and the major disadvantage or difficultly that may be experienced in using them. I shall keep it very brief, here goes:

Dev C++  This is an open source (GNU) compiler developed by  ”bloodshed”. It can compile both C and C ++ source files, it can be used to write and edit header and resource files in addition to its primary use for C and C ++. It has ready-made template and can be used to develop windows Applications, Console Applications, DLL (digital linkage library) files.
A major difficulty a beginner might have with this compiler (especially if that person doesn’t know anything about the command prompt) is how to run the compiled source file(i.e. the generated  .exe file). 
A snapshot of the Dev C++ compiler
If the run button is clicked on the user interface of the compiler, it often opens the command prompt, runs the code and closes the command prompt, all these happen so fast that the user does not have enough time to see/view solution or the results of the written program that is displayed on the command line.
A simple solution to this is to put the whole code in a perpetual loop (usually a continuous while loop), for example
int main()
{
while(1)
{
                                // your code goes into this place
}
}
When the code is run, the while loop will prevent the command prompt from closing and the display on the command line can be read. If you wish to close the command prompt just close the command prompt window…
Another method is to place this 

getchar(); 

just before the  return 0;   in your code. This will force the command window to remain open until a character on the keyboard is pressed.
This compiler is free and available for download at their website http://www.bloodshed.net   you can check out their forum here http://www.bloodshed.net/forum
 
Borland C ++   is a C and C++ programming environment (that is, an integrated development environment) for MS-DOS and Microsoft Windows. It was the successor to Turbo C++, and included a better debugger, the Turbo Debugger, which was written in protected mode DOS.
A snapshot of the Borland C++ compiler
This compiler solves the problem encountered in the Dev C ++ compiler by running the compiled (.exe) file and displaying the results in an environment(like notepad) made by the compiler developers. This method takes the burden of having to run the file in the command prompt off the user by providing an easy way to see the results of the program/application. It is easy to use and has a simple user interface; most people find this compiler the best for them when they begin their journey into C programming.
 
You can check their website for more details on how to download the commercial one or the free one. Just go to this address http://www.borland.com  



Visual C ++  This compiler is made by Microsoft, it is a commercial  compiler even though a free version is available. It has a robust integrated development environment (IDE) for the C, C++, and C++/CLI programming languages user interface.  
A snapshot of the Microsoft's Visual C++ compiler

  It has tools for developing and debugging C++ code, especially code written for the Microsoft Windows API, the DirectX API, and the Microsoft .NET Framework .I personally think this compiler is too big in terms of size occupied by it on your computer when compared to other C compilers; it also uses a lot of system resources when compared to the other compilers I discussed above.

 
You can check their website(Microsoft visual C++) for more details on how to download the commercial one or the free one. Just go to this address  http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express 




Miracle C Compiler  
 The Miracle C Compiler runs under MS-Windows targeting MSDOS, all the  traditional C syntax are implemented in the compiler, including record (structures/unions) and enumerated data types, integer (int), long and floating point data types, user type definition, bit fields in structures, initializers for all data types.
Miracle C compiler snapshot
It spots a comprehensive library of functions, some example programs demonstrating compiler features and Windows Helpfile documentation is supplied with the installation package. To download this compiler point your browser to this address http://www.c-compiler.com

The above is just an overview of a few of the numerous available C compilers; it is not in any way a detailed list or even represents a large fraction of the available C compilers available today. It just merely shows you that compilers differ sometimes slightly while at other times very greatly from each other. My suggestion for you is to extensively read up materials on the compiler you wish to use before you use it. Some compilers introduce other keywords that are not present in the standard C library, so know your compiler well.


Happy New Year from Giga Frequency

Thursday, December 29, 2011

The C beginings: part 2

In the last part(i.e part 1) i said something about the origin of C, what C language is about and what it is generally used for. In this post i shall go a step further by giving you an example and explaining every line in details....
In most tutorials, they start by showing you how to display " Hello World " , this is always a good place to start but i will try to run through the C language so that you understand it in great details in the shortest possible time after all you are learning at Gigabyte speed.... So here go...
First i shall explain what happens when a C program/code is complied (i am assuming you know what a compiler is, if you do not; click here for a quick run through )
A program in C language is written by first including a library file that should contain all the functions you will be working with. This includes the particular file that the compiler needs to check in order to know what each of the functions you use stands for. Like in a library where each book holds or contains a particular information that you will need to enable you answer/solve a particular problem set, also for programming you have to tell the compiler which file contains the information it will need to solve a particular task. Below is a sample code that displays the words " Hello World " without the quotes on the command line. For a run through on batch programming in MS-DOS click here.

#include <stdio.h>

int main( )
{
       printf(" Hello World " );
       return 0;
}

I will start explaining the above code line by line. Note the C compiler is not sensitive to spaces in between the wordings, i have spaced the above code in this manner for clarity and it is also a good programming technique to make your code readable by other programmers or by yourself when you go through the code later.
The first line contains the #include <stdio.h>   first notice the word in-between the < > brackets. It is stdio.h and it means standard (std) - input(i) - output(o) - .header file(.h)  . This is the default file in the C standard library that contains functions and methods of interacting with the input and output devices of the machine(could be a computer-PC, micro controller, etc.) you run the code on. The # tells the pre-compiler that this line of code is meant for it, so the code is telling the pre-compiler to go and fetch the standard input output file and include it in the code. The < > bracket mean the the pre-compiler should search all the memory of the computer for that file, in order to specify to the pre-compiler that it should search only the current directory where the file containing your code is stored, you use double quotes(" ") .
The second line starts with int main( )  this tells the compiler that this is the main block where the critical or important code to run is. The int in front of the main( ) is not really necessary because by default all C main methods return an integer value (int for short).
The next line of code tells the compiler that starting from the {  to the } contains a block of code grouped under int main( )
Within the { } contain the code printf(" Hello World ");  meaning print formatted data Hello World to the default output device(in this case the command line screen). The semi colon at the end tells the compiler that it has gotten to the end of a statement. The return 0 on the next line just returns an integer of zero to indicate to the compiler that the code executed properly.
By this stage, i know you have  a lot of questions, feel free to leave them as comments below this post and i will answer it as soon as possible. If you wish to contact me for any reason use the form on the contact page of this blog.



Season's Greetings from Giga Frequency

Wednesday, December 21, 2011

What is a compiler?


A compiler is a special type of computer program that translates source code from a high-level programming language( human readable text file) into a lower level language (e.g., assembly language or machine code) that is a form that the computer can more easily understand. At its most basic level, a computer can only understand binary(  1 and  0). If a human being was to write an instruction for a computer using the language it understands(binary), that person will write very slowly and find the information contained in the long string of 1s and 0s incomprehensible, the person will also be prone to make a lot of mistakes and introduce errors in the intended instruction. A compiler is a computer program that was created to bridges this gap and make writing computer programs(instruction) more human friendly.
In the beginning when compilers were created, they were very simple programs that could only translate symbols into the bits, the 1s and 0s, the computer understood. Programs were also very simple, composed of a series of steps that were originally translated by hand into data the computer could understand. 

This method of programming was very time consuming and prone to mistakes by the writer of the computer instruction, so portions of this task were automated or programmed, and that was how the first compiler was written. This program assembled, or compiled, the steps required to execute the step by step program.
These simple compilers were used to write a more sophisticated compiler. With the newer version, more rules could be added to the compiler program to allow a more natural language structure for the human programmer to operate with. This made writing programs easier and allowed more people to begin writing programs. As more people started writing programs, more ideas about writing programs were offered and used to make more sophisticated compilers. In this way, compiler programs continue to evolve, improve and become easier to use.

If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler. On the other hand a decompiler is a program that translates from a low level language to a higher level one. A program that translates between high-level languages (e.g from python to JAVA for example) is usually called a language translator, language converter, or source to source translator. A language rewriter is usually a program that translates the form of expressions without a change of language.
A compiler's function include but may not be limited to the following: 
  • lexical analysis
  • pre-processing ( usually a pre-compiler built in with the compiler run through the code first)
  • parsing of the code for syntax errors
  • semantic analysis (Syntax-directed translation)
  • generation of the code
  • also to optimize the generated code
Hope you have gained some knowledge about compilers(at least the basic understanding). For a more detailed explanation you can check Wikipedia here.

Note:
The faults or errors introduced into the compiled program by an incorrect behavior of a compiler in compiling the source code can be very difficult to track down. 

Season Greetings from Giga Frequency

Friday, December 9, 2011

The C beginings...

Hello everyone, i have been posting stuffs about IT news and others.. Well i want to start the day off by discussing a very cool Language, the C language.
C is a general-purpose programming language which spots features such as
  • economy of expression
  • modern flow control and data structures 
  • and a rich set of operators. 
C is not a " very high level '' language, it is small(i.e. it is compact and tiny) , and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages.

C was originally designed for and implemented on the UNIX operating system on the DEC
PDP-11, by Dennis Ritchie. The operating system, the C compiler - a compiler is a piece of software that converts human readable high language into machine readable language(code) , and essentially all UNIX
applications programs are written in C.
Production compilers also exist for several other machines, including the IBM System/370, the Honeywell 6000, and the Interdata 8/32. C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C.

...i will continue this c stuff later posts....stay tuned...
....to search all the categories on this blog click here