Introduction to c-language
History of c language
Dennis Ritchie - founder of C language
Here we are going to discuss a brief history of the c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
C Is a Middle Level Language
C is often called a middle-level computer language. This does not mean that C is less powerful,
harder to use, or less developed than a high-level language such as BASIC or Pascal, nor does it
imply that C has the cumbersome nature of assembly language (and its associated troubles). Rather,
C is thought of as a middle-level language because it combines the best elements of high-level
languages with the control and flexibility of assembly language. Table 1-1 shows how C fits into the
spectrum of computer languages.
As a middle-level language, C allows the manipulation of bits, bytes, and addresses— the basic
elements with which the computer functions. Despite this fact, C code is also very portable.
Portability means that it is easy to adapt software written for one type of computer or operating
system to another type. For example, if you can easily convert a program written for DOS so that it
runs under Windows 2000, that program is portable.
High level
Ada
Modula-2
Pascal
COBOL
FORTRAN
BASIC
Middle level
Java
C++
C
FORTH
Macro-assembler
Low level
Assembler
C Is a Structured Language
In your previous programming experience, you may have heard the term block-structured applied to a computer language. Although the term block-structured language does not strictly apply to C, C is commonly referred to simply as a structured language. It has many similarities to other structured languages, such as ALGOL, Pascal, and Modula2.
The distinguishing feature of a structured language is compartmentalization of code and data. This is the ability of a language to section off and hide from the rest of the program all information and instructions necessary to perform a specific task.
A structured language offers a variety of programming possibilities. For example, structured languages typically support several loop constructs, such as while, do-while, and for . In a structured language, the use of goto is either prohibited or discouraged and is not the common form of program control (as is the case in standard BASIC and traditional FORTRAN, for example). A structured language allows you to place statements anywhere on a line and does not require a strict field concept (as some older FORTRANs do).
Here are some examples of structured and nonstructured languages:
Nonstructured Structured
FORTRAN Pascal
BASIC Ada
COBOL C++
C
Java
Modula2
A way to structure and compartmentalize code in C is through the use of blocks of code. A code block is a logically connected group of program statements that is treated as a unit. In C, you create a code block by placing a sequence of statements between opening and closing curly braces. In this example,
the two statements after the if and between the curly braces are both executed if x is less than 10. These two statements together with the braces represent a code block. They are a logical unit: One of the statements cannot execute without the other executing also. Code blocks allow many algorithms to be implemented with clarity, elegance, and efficiency. Moreover, they help the programmer better conceptualize the true nature of the algorithm being implemented.
COMPILER vs INTERPRETER
Tap on image for better quality |
32 keywords defined by the C89 standard
auto break case.
char const continue
default do double
else enum extern
float for goto
if int. long
return short signed
sizeof static struct
switch typedef union
unsigned void. volatile
while
In C, uppercase and lowercase characters are different: else is a keyword; ELSE is not. You may not
use a keyword for any purpose other than as a keyword in a C program— that is, you may not use it
as a variable or function name.
THE MAIN FUNCTION
All C programs consist of one or more functions. As a general rule, the only function that must be
present is called main( ), which is the first function called when program execution begins. In wellwritten C code, main( ) contains what is, in essence, an outline of what the program does. The
outline is composed of function calls. Although main( ) is not a keyword, treat it as if it were. For
example, don't try to use main as the name of a variable because you will probably confuse the
compiler.
The Library and Linking
Most programs include calls to various functions
contained in C's standard library.
All C compilers come with a standard library of functions that perform most commonly needed
tasks. Standard C specifies a minimal set of functions that will be supported by all compilers.
However, your compiler will probably contain many other functions. For example, the standard
library does not define any graphics functions, but your compiler will probably include some.
When you call a library function, the C compiler ''remembers" its name. Later, the linker combines
the code you wrote with the object code already found in the standard library. This process is called
linking
Separate Compilation
Most short C programs are completely contained within one source file. However, as a program's
length grows, so does its compile time (and long compile times make for short tempers). Thus, C
allows a program to be spread across two or more files, and it
Page 13
lets you compile each file separately. Once you have compiled all files, they are linked, along with
any library routines, to form the complete object code. The advantage of separate compilation is that
if you change the code of one file, you do not need to recompile the entire program. On all but the
most simple projects, this saves a substantial amount of time. Separate compilation also allows
multiple programmers to more easily work together on a single project, and it provides a means of
organizing the code for a large project.
Compiling a C Program
Creating an executable form of your C program consists of these three steps:
1. Creating your program
2. Compiling your program
3. Linking your program with whatever functions are needed from the library
Compile is the creation of an executable program from code written in a programming language Compiling allows the computer to run and understand the program without the need of the programming software used to create it.
C's Memory Map
A compiled C program creates and uses four logically distinct regions of memory. The first region is
the memory that actually holds the program's executable code. The next region is memory where
global variables are stored. The remaining two regions are the stack and the heap. The stack is used
for a great many things while your program executes. It holds the return addresses of function calls,
arguments to functions, and local variables. It will also save the current state of the CPU. The heap
is a region of free memory that your program can use via C's dynamic memory allocation functions.
Although the exact physical layout of each of the four regions of memory differs among CPU types
and C implementations, the diagram in Figure 1-2 shows conceptually how your C programs appear
in memory.
Figure 1-2
Conceptualized memory
map of a C program
C vs. C++
C++ is an object-oriented
programming language that was built upon the foundation of C. In general terms, C is a subset of
C++, or conversely, C++ is a superset of C.
In general, you can use a C++ compiler to compile a C program. In fact, today most compilers
handle both C and C++ programs. Thus, most programmers will use a C++ compiler to compile
their C code!
Extensions
There is one thing that you must be careful about when using a C++ compiler to compile a C
program: the file extension. By convention, C programs use the .C extension. C++ programs
use .CPP .
Important terms
• Source code The text of a program that a user can read, commonly thought of as the program. The
source code is input into the C compiler.
• Object code Translation of the source code of a program into machine code, which the computer
can read and execute directly. Object code is the input to the linker.
• Linker A program that links separately compiled modules into one program. It also combines the
functions in the Standard C library with the code that you wrote. The output of the linker is an
executable program.
• Library The file containing the standard functions that your program can use. These functions
include all I/O operations as well as other useful routines.
• Compile time The time during which your program is being compiled.
• Run time The time during which your program is executing.
Comments
Post a Comment