Professional Documents
Culture Documents
Design
C-C++ Programmer
The original version was:
main()
{
printf("hello, world\n");
}
A standard-conforming "hello, world" program is:[nb 1]
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
The first line of the program contains a pre-processing directive, indicated by #include. This causes
the compiler to replace that line with the entire text of the stdio.h standard header, which contains
declarations for standard input and output functions such as printf. The angle brackets surrounding
stdio.h indicate that stdio.h is located using a search strategy that prefers standard headers to other
headers having the same name. (Double quotes are used to include local or project-specific header
files.)
The next line indicates that a function named main is being defined. The main function serves a
special purpose in C programs; the run-time environment calls the main function to begin program
execution. The type specifier int indicates that the value that is returned to the invoker (in this case
the run-time environment) as a result of evaluating the main function is an integer. The keyword
void as a parameter list indicates that this function takes no arguments.
The opening curly brace indicates the beginning of the definition of the main function. The next line
calls (diverts execution to) a function named printf, which is supplied from a system library. In this
call, the printf function is passed (provided with) a single argument, the address of the first character
in the string literal "hello, world\n". The string literal is an unnamed array with elements of type char,
set up automatically by the compiler with a final 0-valued character to mark the end of the array
(printf needs to know this). The \n is an escape sequence that C translates to a newline character,
which on output signifies the end of the current line. The return value of the printf function is of type
int, but it is silently discarded since it is not used. (A more careful program might test the return
value to determine whether or not the printf function succeeded.) The semicolon; terminates the
statement.
The closing curly brace indicates the end of the code for the main function. The main function is
implicitly understood to return the integer value 0 upon completion, which is interpreted by the runtime system as an exit code indicating successful execution.
1.1.3 Compilers* to execute a C program
*A compiler is a computer program (or set of programs) that transforms source code written in a
programming language (the source language) into another computer language (the target language,
often having a binary form known as object code). The most common reason for wanting to
transform source code is to create an executable program. An executable program is in a form
C-C++ Programmer
understandable by the underlying machine, the processor can only then perform the expected
function.
A C program can be compiled and executed across various operating systems like Windows, Linux,
and Mac OS. Various compilers are written for the purpose.
Following are two instances of the compilers which can be used to compile and execute C
programs.
1.1.3.1 Running a C program in Windows environment using Turbo C compiler
1.1.3.1.1 Installing a Turbo C compiler
Turbo C was an Integrated Development Environment and compiler for the C programming
language from Borland. It was first introduced in 1987 and was noted for its integrated development
environment, small size, extremely fast compile speed and comprehensive manuals.
In May 1990, Borland replaced Turbo C with Turbo C++.
Here we are going to talk about Turbo C++ 3.0 version. it is best of all version and widely used by
students.
Turbo C++ 3.0 was released in 1991. Initially released as an MS-DOS compiler, 3.0 supported C++
templates, Borlands inline assembler, and generation of MS-DOS mode.
To install turbo c compiler
1. First you need to download the compiler, which is available on internet.
2. After finished download unzip the turbo c to folder & open the folder.
Note: For the time of installation the folder should be present anywhere in Drive C. Like desktop,
documents, downloads..
3. Run install.exe and press ENTER to continue.
C-C++ Programmer
4. Enter the SOURCE drive to use: C
5. Enter the SOURCE Path: source path is where you extracted the turbo c.zip file we dont need to
give the source it will find and take automatically. you just press ENTER at this step.
C-C++ Programmer
6. Then scroll down by arrow key and Start Installation.
You can have shortcut on desktop so that no need to follow long path to open TC.
Just open TC folder in drive C select TC.exe and right click on it, Click on send to> Desktop (create
shortcut).
C-C++ Programmer
1.1.3.1.2 Run a C program in Windows environment using command prompt
Introduction: C Program can be run using Command Prompt .We can use MS-DOS to run c
Program. Every window OS comes with inbuilt Command Prompt. So we are going to use this
command prompt to run our c program.
Pre-requisite:
1
We have Stored our C programs inside directories other than (C:/TC/bin) then we are unable to run
c program.
In order to run C Program we must copy C Program and paste it inside bin folder.
Now we must have Turbo C/C++ IDE installed in your system.( You may download it from here)
After installing Turbo C/C++ IDE we have this directory Structure
C-C++ Programmer
Now Right Click on My Computer Icon and Click on Properties
C-C++ Programmer
Click on Environment Variable Button.
C-C++ Programmer
Make entry of bin inside Path Variable
C-C++ Programmer
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights
reserved.
C:\Users\Santoshi>D:
D:\>cd C Programs
D:\C Programs>
Suppose we have Saved hello.c program inside C Programs folder. then type this command to
Compile hello.c
D:\CPROGR~1>hello
Hello
Note :
Actually tcc.exe Command is located inside C:/TC/bin So we have make this entry inside path
variable so that Command Prompt can find this command from that specified path.
1.1.3.2 Running C programs on Linux
When you write a program, it doesn't do anything until you compile it. Many beginner programmers
will use graphical IDEs such as Microsoft Visual Studio to write and compile their code, but if you
are told to use a Unix machine, you won't be able to use this software package. GCC is a compiler
for C, C++, Java, Fortan and other program code that can be used in Unix, GNU/Linux machines. It
is distributed as Free Software under the GNU General Public License (GNU GPL). It is useful to
know how to do at least simple compiling with this compiler. This guide assumes the reader has a
basic knowledge of using Unix, GNU/Linux from the command line.
1. Open up a terminal on Unix or GNU/Linux or a command prompt on Microsoft
Windows
In order to see if you have the GNU C/C++ compiler installed on your system
#: gcc --version
10
C-C++ Programmer
It should inform you about the version number of the compiler. If the command is not found,
it is likely that gcc/g++ isn't installed.
2. Make sure you have gcc/g++ installed on your system, consult your Unix or GNU/Linux
distribution documentation for the appropriate installation method.
3. Create a working directory using the following command:
#: mkdir CCPP
4. Change into the directory
#: cd CCPP
5. Use a text editor such as nano, gedit, vi, notepad,(on Microsoft Windows ) for example,
6.
Unix/GNU/Linux Instructions:
#: nano main.c
#: gedit main.c
#: vi main.c
7. Windows Instructions:
#: notepad main.c
You can use notepad as a text editor on Microsoft Windows
8. Enter the following source code below:
#:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
return(0);
}
Once this task is complete save the source code as a text file in your editor as main.c
11
C-C++ Programmer
4 Microsoft Windows Instructions:
#: HelloWorld.exe
On Windows when the HelloWorld.c file is compiled it will usually have an .exe extention.
5 To compile multiple files:
#: gcc -o outputfile file1.c file2.c file3.c
6 To compile with more error checking using the -Wall tag:
#: gcc -Wall -o outputfile file1.c
7 To compile files without linking them:
#: gcc -c file1.c file2.c file3.c
After the last step, if you want to link them, type: gcc -o outputfile file1.o file2.o file3.o
8 Fix any errors/warnings the compiler reports.
Recompile the code if you had to fix errors.
Execute the program.
is typically used to include a header file file.h that contains definitions and declarations for a
standard library.
Including such a header file gives the current program access to the functions defined in the header
file. Like in the following example including the header file stdio.h gives access to the functions printf
() and scanf() which are defined in the stdio.h header file. Functions are discussed in detail in
following units
Consider the following C program:
#include <stdio.h>
#include <stdlib.h>
12
C-C++ Programmer
int main()
{
printf("Hello World\n");
return(0);
}
}
Consider the following example
#include<stdio.h>
#include<stdlib.h>
intmain()
{
printf("HelloWorld\n");
return(0);
}
There are three functions in the program main(), printf() and return(). The function of printf() and
return() is defined in the header files, which are included in the program(stdio.h and stdlib.h).
The space between the parentheses ({....}) of the main() function is the space where the
programmer can write the instructions which define the behavior of the program.
13