You are on page 1of 13

C-C++ Programmer

Unit-1 BASIC KNOWLEDGE OF C


1.1 INTRODUCTION
C is a general-purpose programming language initially developed by Dennis Ritchie between 1969
and 1973 at AT&T Bell Labs. Its design provides constructs that map efficiently to typical machine
instructions, and therefore it found lasting use in applications that had formerly been coded in
assembly language, most notably system software like the Unix computer operating system.
C is one of the most widely used programming languages of all time, and there are very few
computer architectures for which a C compiler does not exist.
Many later languages have borrowed directly or indirectly from C, including C#, D, Go, Java,
JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix's C shell. The most pervasive influence on
these languages has been syntactical, and they tend to combine the recognizable expression and
statement syntax of C with underlying type systems, data models, and semantics that can be
radically different. C++ started as a pre-processor for C and is currently nearly a superset of C.
Before there was an official standard for C, many users and implementers relied on an informal
specification contained in a book by Dennis Ritchie and Brian Kernighan; that version is generally
referred to as "K&R" C. In 1989 the American National Standards Institute published a standard for
C (generally called "ANSI C" or "C89"). The next year, the same specification was approved by the
International Organization for Standardization as an international standard (generally called "C90").
ISO later released an extension to the internationalization support of the standard in 1995, and a
revised standard (known as "C99") in 1999. The current version of the standard (now known as
"C11") was approved in December of 2011.
1.1.1

Design

C is an imperative (procedural) language. It was designed to be compiled using a relatively


straightforward compiler, to provide low-level access to memory, to provide language constructs that
map efficiently to machine instructions, and to require minimal run-time support. C was therefore
useful for many applications that had formerly been coded in assembly language, such as in system
programming.
Despite its low-level capabilities, the language was designed to encourage cross-platform
programming. A standards-compliant and portably written C program can be compiled for a very
wide variety of computer platforms and operating systems with few changes to its source code. The
language has become available on a very wide range of platforms, from embedded microcontrollers
to supercomputers.
1.1.2 First C program
The "hello, world" example, which appeared in the first edition of K&R, has become the model for an
introductory program in most programming textbooks, regardless of programming language. The
program prints "hello, world" to the standard output, which a terminal is usually or screen display.

Unit 1: Basic Knowledge of C

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

Unit 1: Basic Knowledge of C

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.

Unit 1: Basic Knowledge of C

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.

Unit 1: Basic Knowledge of C

C-C++ Programmer
6. Then scroll down by arrow key and Start Installation.

Now you have successfully installed turbo c compiler.


Now to open the turbo c compiler
go to Drive C > TC > Bin > TC .exe

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).

Unit 1: Basic Knowledge of C

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

Windows Command Prompt

Turbo C/C++ Compiler


Why we should run C Program using Command Prompt?

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.

This process is bit time consuming and complex.


Running C Program using Command Prompt :
Create One Directory in D Drive (in my case my directory is C Programs)

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

Unit 1: Basic Knowledge of C

C-C++ Programmer
Now Right Click on My Computer Icon and Click on Properties

Unit 1: Basic Knowledge of C

C-C++ Programmer
Click on Environment Variable Button.

Unit 1: Basic Knowledge of C

C-C++ Programmer
Make entry of bin inside Path Variable

Make Entry of TC>bin inside Path Variable and Click on OK.


C:\TC\BIN
Now Open Command Prompt by typing cmd inside Run Option.
Go to Path Where you have Saved your C Programs.

Unit 1: Basic Knowledge of C

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:\C Programs>tcc hello.c


Turbo C++ Version 3.00 Copyright (c) 1992 Borland
hello.c:
Turbo Link Version 5.0 Copyright (c) 1992 Borland
Available memory 4106832
After typing tcc command we can compile C Program.
Now its time to run C Program after Successful Compilation.Just Type Program name and Hit
Enter you will get output on the console

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

Unit 1: Basic Knowledge of C

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

1 Compile the file by issuing the following command


#: gcc main.c -o HelloWorld
2 Run the program by issuing the following command
3 Unix/GNU/Linux instructions:
#: ./HelloWorld

Unit 1: Basic Knowledge of 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.

1.1.4 Decoding the program structure


1.1.4.1 Header files in C
Header files are included into a program source file by the #include preprocessor directive. The C
language supports two forms of this directive; the first,
#include "header"
is typically used to include a header file header that you write yourself; this would contain definitions
and declarations describing the interfaces between the different parts of your particular application.
By contrast,
#include <file.h>

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>

Unit 1: Basic Knowledge of C

12

C-C++ Programmer
int main()
{
printf("Hello World\n");
return(0);
}

The program have two header files stdio.h and stdlib.h.


C Library provides several header files, each of which contains the type and macro definitions and
variable and function declarations for a group of related facilities. This means that your programs
may need to include several header files, depending on exactly which facilities you are using.
One such facility used in the above example is printf statement. The printf statement outputs the
string which is enclosed in parentheses and double quotes. In the above example(Hello World).
This behavior of the printf statement is defined in the header file stdio.h.
1.1.4.2 main() function
main() is a user-defined function. Normally every C/C++ program should have a main()
function.Compilation of a program begins from main() function. main() function calls all the functions
one by one inside its body. The smallest C program is
void main()
{

}
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.

Unit 1: Basic Knowledge of C

13

You might also like