Introduction to C Programming Language
C is a powerful general-purpose programming language. It is fast, portable and available in all
platforms. If you are new to programming, C is a good choice to start your programming
journey. This is a comprehensive guide on how to get started in C programming language, why
you should learn it and how you can learn it. C is considered to be one of the most powerful
programming languages because of its structure, high-level abstraction and the fact that it is
machine-independent. C was first developed with the UNIX operating system and shares much
in common with UNIX, which still remains a widely used operating system, and forms the core
of the internet data superhighway.
C is procedural programming language. It was initially developed by Dennis Ritchie in the year
1972. It was mainly developed as a system programming language to write an operating system.
The main features of C language include low-level access to memory, a simple set of keywords,
and clean style, these features make C language suitable for system programming like an
operating system or compiler development. Many later languages have borrowed syntax/features
directly or indirectly from C language. Like the syntax of Java, PHP, JavaScript, and many other
languages are mainly based on C language. C++ is nearly a superset of C language (There are
few programs that may compile in C, but not in C++).
Beginning with C programming:
1. Structure of a C program
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.
The structure of a C program is as follows:
The components of the above structure are:
1. Header Files Inclusion: The first and foremost component is the inclusion of the Header
files in a C program. A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source files.
Some of C Header files include the following:
1
, o stddef.h – Defines several useful types and macros.
o stdint.h – Defines exact width integer types.
o stdio.h – Defines core input and output functions
o stdlib.h – Defines numeric conversion functions, pseudo-random network
generator, memory allocation
o string.h – Defines string handling functions
o math.h – Defines common mathematical functions
Syntax to include a header file in C programming Language:
#include
2. Main Method Declaration: The next part of a C program is to declare the main()
function. The syntax to declare the main function is:
Syntax to Declare main method:
int main()
{}
3. Variable Declaration: The next part of any C program is the variable declaration. It
refers to the variables that are to be used in the function. Please note that in the C
program, no variable can be used without being declared. Also in a C program, the
variables are to be declared before any operation in the function.
Example:
int main()
{
int a;
.
.
4. Body: Body of a function in C program, refers to the operations that are performed in the
functions. It can be anything like manipulations, searching, sorting, printing, etc.
Example:
int main()
{
int a;
printf("%d", a);
.
.
5. Return Statement: The last part in any C program is the return statement. The return
statement refers to the returning of the values from a function. This return statement and
return value depend upon the return type of the function. For example, if the return type
is void, then there will be no return statement. In any other case, there will be a return
statement and the return value will be of the type of the specified return type.
2
, Features and Characteristics of C Programming Language
Features of C Programming Language:
C is one of the most popular languages used today and features in operating systems and
embedded systems.
C is a robust programming with an impressive set of built-in functions and a variety of
operators which you can use to write any complex program.
C programs are fast and efficient. This is because C uses a powerful set of data types and
operators.
C combines the power and capability of assembly language with the user friendly
features of a high-level language.
C is the most widely used older programming language. It continues to go strong while
older programming languages such as BASIC and COBOL have been virtually forgotten.
C is very much portable, which means programs written on a machine using C can be
used on other machines as well without any modification.
A C program consists of a number of functions that are supported by C library. In fact,
you can create your own function, which can then be added to the C library.
Characteristics of C Programming Language
1) C is a General Purpose Programming Language This means C can be used to write a variety of
applications. It is often referred to as a “system programming language.”
2) C is a middle level language, which means it combines the features of high level language
with the functionality of an assembly language.
3) C is a structured programming language, which means as a programmer, you are required to
divide a problem into a several different modules or functions.
4) C is renowned for its simplicity and is easy to use because of its structured approach. It has a
vast collection of keywords, operators, built-in functions and data types which make it efficient
and powerful.
5) C is portable, which means a C program runs in different environments. C compilers are
available for all operating systems and hardware platforms. Additionally, you can easily write
code on one system and port it to another.
6) C is popular not just because it can be used as a standalone programming language, but also as
it can be used as an interface to other more visual languages.
7) C is a very flexible language; it is convenient and portable, like a high level language and
flexible like a low level language. It can be interfaced with other programming languages.
8) C is super fast. The compilation and execution of programs is much faster on C than with
most other languages.
9) C is modular, which means C programs can be divided into small modules, which are much
easier to understand.
10) C is easily available. The C software is easy to access and can be easily installed on your
computer. The installation of C hardly takes a few minutes.
11) C is easy to debug. The C compiler detects syntax errors quickly and easily and displays the
errors along with the line numbers of the code and the error message.
12) C makes available a number of in-built memory management functions that save memory
and improve the efficiency of the program such as malloc(), calloc() and alloc().
13) Recursion is one of the common techniques used in C, where in a function calls itself again
and again.
14) Finally, C has a rich set of library functions and supports graphic programming too.
3
, Relationship between C & C++
As more complex software was developed in C, it was recognized that object-oriented concepts
including encapsulation, polymorphism, etc would help manage the complexity. This leads to the
development of C++ as a superset of C.
C++ was built by adding certain features for writing object oriented software, while maintaining
compatibility with C. It was developed with the idea of progressive enhancement — making
compatible changes to C language so C and C++ modules can be combined in the same program
and compiled with the same compiler. This enabled older C-based modules to be re-used with
minimal changes in a larger program written using object-oriented concepts.
A C program can, in theory, be compiled by a C++ compiler without requiring any changes. In
practice, however, stricter type checking enforced by C++ results in errors, requiring some
changes to the code.
Small Number of Keywords (C & C++)
The C language has been a small language in spite of its power. This is because it declares only
32 words as keywords with a specific meaning. By contrast, C++ declares 82 keywords, java has
50 reserved keywords and javascript has 63. And COBOL declares a gigantic 357 keywords.
Imagine the headache of having to remember and sidestep such a large number of keywords!
In the Java programming language, a Keyword is any one of 51 reserved words that have a
predefined meaning in the language; because of this, programmers cannot use keywords as
names for variables, methods, classes, or as any other identifier. Of these 51 keywords, 49 are in
use and 2 are not in use.
C Keywords and Identifiers
Character set
A character set is a set of alphabets, letters and some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C accepts both lowercase and uppercase alphabets as variables and functions.
Digits
0123456789
Special Characters
Special Characters in C Programming
, < > . _
( ) ; $ :
% [ ] # ?
' & { } "
^ ! * / |
- \ ~ +
White space Characters
Blank space, newline, horizontal tab, carriage, return and form feed.
4