Introduction of C
C is a general-purpose, high-level language that was originally
developed by Dennis M. Ritchie to develop the UNIX operating system at
Bell Labs. C was originally first implemented on the DEC PDP-11 computer in
1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly
available description of C, now known as the K&R standard. The UNIX
operating system, the C compiler, and essentially all UNIX application
programs have been written in C.
C has now become a widely used professional language for various
reasons:
- Easy to learn.
- Structured language.
- It produces efficient programs.
- It can handle low-level activities.
- It can be compiled on a variety of computer platforms.
Facts about C:
- C was invented to write an operating system called UNIX.
- C is a successor of B language which was introduced around the early 1970s.
- The language was formalized in 1988 by the American National Standard Institute (ANSI).
- The UNIX OS was totally written in C.
- Today C is the most widely used and popular System Programming Language.
- Most of the state-of-the-art software have been implemented using C.
- Today's most popular Linux OS and RDBMS MySQL have been written in C.
Why Use C?
C was initially used for system development work, particularly the programs
that make- up the operating system. C was adopted as a system development
language because it produces code that runs nearly as fast as the code written
in assembly language.
Some examples of the use of C might be:
- Operating Systems.
- Language Compilers.
- Assemblers.
- Text Editors.
- Print Spoolers.
- Network Drivers.
- Modern Programs.
- Databases.
- Language Interpreters.
- Utilities
Key Features of C:
- Portability: C was designed with portability in mind, allowing code written in C to be easily adapted and compiled for different platforms. This feature was instrumental in making C a preferred language for system programming, where compatibility across various hardware and operating systems was essential.
- Efficiency: C provides low-level memory manipulation features, giving programmers precise control over memory usage. This level of control allowed developers to write efficient code, making C a popular choice for performance-critical applications.
- Structured Programming: C introduced structured programming concepts like functions and blocks, promoting modular and organized code. This approach made programs easier to read, understand, and maintain.
- Rich Standard Library: C comes with a rich standard library that provides a wide range of functions and utilities for tasks like I/O, string manipulation, memory allocation, and more. This library simplifies common programming tasks and accelerates development.
- Pointer Manipulation: Pointers, a hallmark of C, allow direct memory access and manipulation. While they can be complex to work with, pointers provide powerful tools for tasks like dynamic memory allocation and efficient data structures.
- Legacy and Compatibility: Many existing operating systems and libraries are written in C, making it essential for developers who need to interface with legacy code or work in low-level environments.
- Portability: C was designed with portability in mind, allowing code written in C to be easily adapted and compiled for different platforms. This feature was instrumental in making C a preferred language for system programming, where compatibility across various hardware and operating systems was essential.
- Efficiency: C provides low-level memory manipulation features, giving programmers precise control over memory usage. This level of control allowed developers to write efficient code, making C a popular choice for performance-critical applications.
- Structured Programming: C introduced structured programming concepts like functions and blocks, promoting modular and organized code. This approach made programs easier to read, understand, and maintain.
- Rich Standard Library: C comes with a rich standard library that provides a wide range of functions and utilities for tasks like I/O, string manipulation, memory allocation, and more. This library simplifies common programming tasks and accelerates development.
- Pointer Manipulation: Pointers, a hallmark of C, allow direct memory access and manipulation. While they can be complex to work with, pointers provide powerful tools for tasks like dynamic memory allocation and efficient data structures.
- Legacy and Compatibility: Many existing operating systems and libraries are written in C, making it essential for developers who need to interface with legacy code or work in low-level environments.
The C Compiler:
The source
code written in source file is the human readable source for your program. It
needs to be "compiled" into machine language so that your CPU can actually
execute the program as per the instructions given.
The compiler compiles the source codes into final executable programs. The
most frequently used and free available compiler is the GNU C/C++ compiler,
otherwise you can have compilers either from HP or Solaris if you have the
respective operating systems.
The following
section explains how to install GNU C/C++ compiler on various OS. m We keep
mentioning C/C++ together because GNU gcc compiler works for both C and C++
programming languages.
C Compiler (Software):
- Eclipse.
- NetBeans.
- Code :: Blocks.
- Dev C++.
- Borland C++.
- Turbo C
Among open
source software one best is Eclipse witadvance functionality for C/C++
programmers. It has many new features like syntax highlighting and auto code
completion.It is compatible with all windows operating systems ,Linux and Mac
OS X.
NetBeans
NetBeans is
basically java IDE but it can also be used for C/C++ if you have knowledge to
use it.With this you can create C/C++ application and libraries.It has
exclusive new features like highlighting,automatic formatting braces matching
etc.
Code :: Blocks
Code::blocks
is a free open source utility which fulfill the demands of its users.It has
new feature which other IDE lacks is that you can extend this IDE with the
help of plugins available.
Dev C++
Dev C++ is
free ware IDE.Itsupport C language with many new features like syntax
highlighting,auto code completion,project manager and print support.It is GCC
support compiler.
Borland C++
Borland C++
is a IDE for C and C++ programming languages.It support r MS-DOS and Microsoft
Windows. It has better debugger than turbo c.It is more better than turbo c in
its performance and features.
Turbo C
Turbo C/C++
is the oldest and ancient IDE for C and C++ programming.It was developed by
Borlanb in 2008.Now it has come with new C++ builder which is known as rapid
application development (RAD).
Turbo C++ for Windows 7,8,8.1 and 10. The .NET Framework is required for
Windows 7, Vista and XP, but there is no pre-requirement for more recent
Windows versions.
How to install Turbo C++:
Step 1: Download from Turbo C/C++
Setup.exe From Internet.
Step 2: If any previous version of
"Turbo C++" install in your computer, then first of all uninstall that.
Step 3: Run "setup.exe"
file.
Step 4: Follow the setup
instructions.
How to use Turbo C++ 3.2:
Step 1: Double click on "Turbo
C++" shortcut link on the desktop.
Step 2: If you want run turbo c++
on full screen simply click on button "Run Turbo C++".
Step 3: "OR" If you not want full
screen mode uncheck the "Full screen mode" check box and click on button
"Start Turbo C++".
Structure of C Program:
C program basically consists of the following parts:
- Preprocessor Commands.
- Functions.
- Variables.
- Statements & Expressions.
- Comments.
After the above discussion, we can formally assess the structure of a C
program. By structure, it is meant that any program can be written in
this structure only. Writing a C program in any other structure will
hence lead to a Compilation Error.
1) Header files:
In C programming, header files are
files that contain declarations and prototypes for functions, data
structures, macros, and other items that can be used in your program.
They provide a way to separate interface (declaration) from
implementation, promoting modularity and code organization. Here are
some commonly used header files in C programming, along with their
purposes:
- stdio.h: This header file provides input and output functions like `printf`, `scanf`, and file handling functions (`fopen`, `fclose`, etc.).
- stdlib.h: Contains functions for memory allocation (`malloc`, `calloc`, `realloc`, `free`), conversion functions (`atoi`, `atof`), random number generation (`rand`, `srand`), and other utility functions.
- string.h: Includes functions for string manipulation (`strcpy`, `strcat`, `strlen`, `strcmp`, etc.).
- math.h: Provides mathematical functions like `sqrt`, `sin`, `cos`, `exp`, `log`, and constants like `M_PI`.
- time.h: Contains functions for working with date and time, such as `time`, `ctime`, and `difftime`.
- ctype.h: Offers functions for character classification (`isalpha`, `isdigit`, `islower`, `toupper`, etc.).
- stdbool.h: Introduces the `bool` data type and associated constants `true` and `false`.
- limits.h: Provides constants for various data type sizes and limits.
- float.h: Offers constants related to floating-point types, such as precision and range.
- stddef.h: Defines various standard macros and types, like `NULL` and `size_t`.
- stdarg.h: Used for working with variable argument lists in functions, commonly seen in functions like `printf`.
- assert.h: Used for debugging purposes, with the `assert` macro to check assumptions in your program.
- errno.h: Provides error handling through the `errno` variable and related functions.
- unistd.h: Contains functions related to POSIX operating system services, like `write`, `read`, `close`, etc.
- stdbool.h: Defines the `bool` type, `true`, and `false` constants for boolean values.
2) Main Method Declaration:
The
next part of a C program is to declare the main() function. It is the
entry point of a C program and the execution typically begins with the
first line of the main(). The empty brackets indicate that the main
doesn’t take any parameter (See this for more details). The int that
was written before the main indicates the return type of main(). The
value returned by the main indicates the status of program
termination. See this post for more details on the return type.
3) Body of Main Method
The
body of a function in the C program refers to statements that are a
part of that function. It can be anything like manipulations,
searching, sorting, printing, etc. A pair of curly brackets define the
body of a function. All functions must start and end with curly
brackets.
4) Statement
Statements are the instructions given
to the compiler. In C, a statement is always terminated by a semicolon
(;). In this particular case, we use printf() function to instruct the
compiler to display “Hello World” text on the screen.
5) Return Statement
The
last part of any C function is the return statement. The return
statement refers to the return values from a function. This return
statement and return value depend upon the return type of the
function. The return statement in our program returns the value from
main(). The returned value may be used by an operating system to know
the termination status of your program. The value 0 typically means
successful termination.
Application of C
- Embedded Systems: C's efficiency, portability, and low-level control make it a natural choice for programming embedded systems, which power devices ranging from medical equipment to consumer electronics.
- System Programming: C is still extensively used for developing operating systems, device drivers, and other system-level software due to its ability to interact with hardware and manage system resources efficiently.
- High-Performance Computing: In fields like scientific computing and simulations, where performance is paramount, C is often preferred for its ability to produce optimized and efficient code.
- Game Development: Game engines and performance-critical parts of modern video games are often written in C or C++ due to their performance and control advantages.
- Security and Vulnerability Analysis: C is commonly used for security research and vulnerability analysis due to its direct memory access, which allows for in-depth analysis of software behavior.