Monday, March 27, 2023

 C PROGRAMMING 

INTRODUCTION  : 

         C is a general-purpose programming language developed at AT & T's Bell Telephone Laboratories (USA) in 1972 by Dennis Ritchie. Since it was originally designed for and implemented on the UNIX operating system, therefore, it is strongly associated with UNIX though nowadays it is running on almost all operating systems. Here are most common reasons why C is still so popular:

 

- C is one of the most popular programming languages of all time to create system software as well as application software.

- C is a standardized programming language with international standards.

- C is the base for almost all popular programming languages.

- C is one of the foundations for modern computer science and information technology.

- The language of choice for serious programming.

- Ease of understanding and learning, and one of the most commonly used programming languages in colleges and universities.

- Once students have learned C, then it will be easy for them to pick up any other languages.

simple application in C:

/*welcome.c (program name)*/
/*The following program displays the message "Welcome to C Programming"*
#include <stdio.h>
main()
{
printf("Welcome to C Programming");
}

Structure of the above C Program

Comments

Comments are a good way to better understanding of the program statements. In C programming language the comment entries start with a "/*" and terminate with "*/" at the end of the line. The compiler (a compiler is a computer program which translates source code written in a programming language into object code.) ignores the comment statements.

In the above program, the lines

/*welcome.c ( program name )*/
/*This program displays the message "Welcome to C Programming"*/

are comment lines.

#include directive

The #include directive instructs the compiler to include the contents of the file "stdio.h" (standard input-output header file) enclosed within angular brackets.

main() function

In C programming language the main() function serves a special purpose, it tells the compiler where the program start. Every C program must have one and only one main() function otherwise compiler can not understand the starting position of a program. The body of the main function surrounded by curly braces ( { } ). In the above program there is only one statement [ printf("Welcome to C Programming"); ] between the curly braces. Each statement in a C program terminated with a semicolon( ; ).

format of main function in c

Compile and execute C program in Linux and Windows

Introduction

In this document, we will see how we can compile and execute C program in Linux and Windows. The document also contains a list of C compilers available.

We will see how to compile using Visual C++ 2008 Express Edition, Turbo C++, wxDev-C++ in Windows. For Linux and Unix operating systems, we will be using GNU Gcc to compile C program. Both of these are free.

Contents:

Install, compile and execute C program with Visual C++ 2008 Express Edition with SP1 in Windows

Download and install

To download, point your browser to http://www.microsoft.com/visualstudio/en-us/products/2008-editions/express and select Language and click on Free Download. After you complete download, run the executable file, it will ask you to select whether you want to install some other optional products, uncheck those options so that you can install only the product required. The installation process will start downloading other files from the Microsoft site and depending upon your connection speed it will take a while to complete the installation.

visual c++ 208 express edition

Once the installation is complete, in Windows 7, start the Visual Studio 2008 Command Prompt from All Programs > Microsoft Visual C++ 2008 Express Edition > Visual Studio Tools > Visual Studio 2008 Command Prompt.

visual studio command prompt

Write, compile and execute your first program

Step 1 : type notepad demo.c (you can replace the filename with your own) and press Enter, when asked if you want to create a new file, say yes.

visual studio command prompt prompt

Step 2 : type the following code and click File > Save . When asked where to save the file, select the location suitable.

Step 3 : Type cl sample.c and press enter now. This will compile the program to create an executable file (.exe).

compile C program Windows 7

Step 4 : Now simply type the name of the file sample and enter. That will show you the output.

Output C program Windows 7

Install, compile and execute C program with Turbo C++ in Windows

Obtain and install

Turbo C++ is very popular C compiler and IDE. The problem is, Borland, the creator organization of the compiler and IDE does not provide any option to download the compiler and IDE. But if you google with "turbo c++ download", you will get plenty of sites to download the compiler and IDE. There are three files to download - TC.exe, tc.r00 and tc.r01. After downloading, run TC.exe file it will create a folder TC at the end the installation process. To run turbo C++, enter bin directory within TC and look for tc.exe. Double click the tc.exe file and turbo C++ IDE will open.

Run Turbo C++ in full screen in Windows 7

Those who are running Turbo C++ is Windows 7, when the IDE is opened, click on "Ignore" in the popup window saying that it does not support full-screen mode. Now, click on the top left corner of the Turbo C++ window and select "Properties" . Select "Font" tab and change the font to "Lucidia Console" from default "Raster fonts". Also, change the font size to "28". Now select "Layout" tab and set "width" as 80 and "Height" as 25 under "Screen Buffer Size". Under "Window Size", change "Width" as 60 and "Height" as 25. Under "Window Position", set the value of "Left" and "Top" both as -4. Make sure "Left System Position Window" is unchecked. Click "Ok" to close the properties window. Now you can see the Turbo C++ IDE in full screen.

turbo c++ window

Write, Compile and Run C program

Open a new file from File > New in the Turbo C++ IDE. Write a small program in the IDE.

Now go to File > Save As and save the program with the filename of your choice (make sure extension of the filename is .c).

Click on Options and go to Directories. Click on Directories and set Output Directory as you want and Source Directory as where you have saved the C program file.

Now go to compile and click on Compile. And then Click on Run. You will see the output of your C program.

Write, Compile and Run C program using wxDev-C++ in windows

wxDev-C++ is easy to use IDE which you may opt for to write C Program. You may download the installer from wxdsgn.sourceforge.net. We found it working perfectly on Windows 7 and Windows XP. It also installs MinGW along with and you don't need to set any environment variables. The default compiler for this IDE is gcc.

After downloading the installer, run the exe file by double clicking on it and an installation wizard will guide you to install. Once you finish the installation, you start it from Programs and the first time when you run it, it takes a while for parsing header files. The IDe window looks like following:

wxDev-C++ IDE Window

You may start programming by clicking on File > New > Source File in the window. Note that while saving the file, you must select file type as C as this IDE supports C++ also.

wxDev-C++ IDE Window

You may use F9 or as shown below to Compile and Run program.

wxDev-C++ IDE Window

When compilation is done, it opens a new window to show you output.

wxDev-C++ IDE Window
If you have errors, it shows you in the pane below like following.

wxDev-C++ IDE Window

Though slightly dated, we find wxDev-C++ an excellent IDE for programming C. You may try it if you are using Windows.

Install, compile and execute C program in Linux

Most of the time, when you are installing Linux, GNU Gcc compiler is already installed. If not, run the following command (our system is Ubuntu Linux):

c install linux

If C compiler is already installed, it will show you a message like above. If not, it will install all the necessary packages.

Now open a text editor and write a small C program like following and save it as demo.c :

 #include <stdio.h>
main()
{
printf("Welcome to C Programming");
} 

Now run the command as shown below to compile and execute the file :

c-program run linux

This how you can install GNU Gcc compiler, write a C program and run it under Linux.

List of the C Compilers

The following table is a list of the C compilers available. This is not a complete list but this will give you a fair idea about various C compilers, which OS / Platform they support and whether it has a programming environment.

CompilerAuthorOperating SystemIDE?License type
AMPCAxiomatic Solutions Sdn BhdWindows,Unix & OtherYesProprietary
Amsterdam Compiler KitAndrew Tanenbaum and Ceriel JacobsUnix & OtherNoBSD
CCS C CompilerCCS, Inc.Windows,Unix & OtherYesProprietary
ClangLow-Level Virtual MachineWindows,Unix & OtherNoBSD
DMS Software Reengineering ToolkitSemantic DesignsWindows,Unix & OtherNoProprietary
GCC CGNU ProjectWindows,Unix,IBM mainframe, AmigaOS, VAX/VMS, RTEMSNoGPL
RCC (RCOR C Compiler)Rodrigo Caetano (rcor)Windows,UnixNoGPL
Interactive CKISS Institute for Practical RoboticsWindows, Unix, Mac, Linux, IRIX, Solaris, SunOSNoFreeware
LabWindows/CVINational InstrumentsWindows,Unix & OtherYesProprietary
lccChris Fraser and David HansonWindows,Unix & OtherWindows onlyFreeware (source code available for non-commercial use)
Mark Williams CMark Williams CompanyWindows,OtherYesProprietary
Microsoft CMicrosoftWindowsYesProprietary
NwccNils WellerWindows,Unix,OtherNoBSD
Open64SGI Google HP Intel Nvidia PathScale Tsinghua University and othersUnix,OtherNoGPL
Pelles CPelle OriniusWindowsYesFreeware
PGCCThe Portland GroupWindows,Unix,Other— Mac OS XYes — Visual Studio on WindowsProprietary
Portable C CompilerAnders Magnusson and othersWindows,Unix,OtherNoBSD
Power CMix SoftwareOtherNoProprietary
QuickCMicrosoftWindowsYesProprietary
SAS/CSAS InstituteWindows,Unix,Other-IBM mainframe, AmigaOS, 68K, 88K, UnixYesProprietary
Tiny C CompilerFabrice BellardWindows,UnixNoLGPL
Turbo CEmbarcaderoOtherNoProprietary - V 2.01 freely available
CCS C CompilerCCS, Inc.Windows,Unix,OtherYesProprietary
MikroC CompilerMikroElektronika, Inc.Windows,Unix,OtherYesProprietary
ups debugger[1] (includes C interpreter)Tom Hughes, Ian Edwards, and othersUnix,Other-Solaris, SunOSYesGPL
VBCCDr. Volker BarthelmannWindows,Unix,OtherNoFreeware (source code available, modification not allowed)
XL CIBMUnix-AIXEclipseProprietary

P

No comments:

Post a Comment