An Introduction to C

Share this article

Key Takeaways

  • C is a general-purpose, structured programming language that bridges the gap between machine and high-level languages, making it a middle-level language suitable for systems and applications programming.
  • C language is portable due to its reliance on library functions for most computer dependent features, allowing most C programs to be processed on different computers with minimal alterations.
  • C was developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories and is an evolution of earlier languages BCPL and B, combining their best features.
  • A C program consists of one or more modules called functions, with one function always being called ‘main’. The program begins by executing the main function, which may access other functions.
  • C language has five main components: the character set, data types, constants, variables, and keywords. These components form the basic elements of a C program.
Introduction C is a general purpose, structured programming language. Its instructions consist of terms that resemble algebraic expressions, augmented by certain English keywords such as if, else, for, do and while. In this respect it resembles high level structured programming languages such as Pascal and Fortran. C also contains additional features, that allow it to be used at a lower level, thus bridging the gap between machine language and high level language. This flexibility allows C to be used for systems programming as well as for applications programming. Therefore C is called a middle level language.   C is characterized by the ability to write very concise source programs, due in part to the large number of operators included within the language. It has a relatively small instruction set, though actual implementations include extensive library functions which enhance the basic instructions. C encourages users to create their own library fuctions. An important characteristic of C is that its programs are highly portable. The reason for this is that C relegates most computer dependent features to its library functions. Thus, every version of C is accompanied by its own set of library functions which are relatively standardized. Therefore most C programs can be processed on many different computers with little or no alteration. History of C: C was developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories,Inc. (now a part of AT&T). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell Laboratories. The Combined Programming Language(CPL) was developed at Cambridge University in 1963 with the goal of developing a common programming language which can be used to solve different types of problems on various hardware platforms. However it turned out to be too complex, hard to learn and difficult to implement. Subsequently in 1967, a subset of CPL, Basic CPL(BCPL) was developed by Martin Richards incorporating only the essential features. However it was not found to be sufficiently powerful. Around the same time another subset of CPL, a language called B was developed by Ken Thompson at Bell Labs. However it also turned out to be insufficient . Then, in 1972, Dennis Ritchie at Bell Labs developed the C language incorporating the best features of both BCPL and B. C was largely confined to use within Bell Labs until 1978, when Brian Kernighan and Ritchie published a definitive description of the language . The Kerninghan and Ritchie description of C is commonly referred to as ‘K &R C’. Following the publication of ‘K&R C’,computer professionals, impressed with C’s many desirable features, began to promote the use of C. By the mid 1980’s the popularity of C had become widespread-many c compilers and interpreters had been written for computers of all sizes and many commercial application programs had been developed. Moreover, many commercial software products that had originally been written in other languages were rewritten in C in order to take advantage of its efficiency and portability. Early commercial implementations of C differed a little from Kerninghan and Ritchie’s original description, resulting in minor incompatibilities between different implementations. As a result, the American National Standards Institute(ANSI committee X3J11)  developed a standardized definition of C. Virtually all commercial compilers and interpreters adhere to the ANSI standard. Many provide additional features of their own. C and Systems Programming: There are several features of C, which make it suitable for systems programming. They are as follows:
  • C is a machine independent and highly portable language.
  • It is easy to learn; it has only 28 keywords.
  • It has a comprehensive set of operators to tackle business as well as scientific applications with ease.
  • Users can create their own functions and add to the C library to perform a variety of tasks.
  • C language allows the manipulation of bits, bytes and addresses.
  • It has a large library of functions.
  • C operates on the same data types as the computer, so the codes generated are fast and efficient.
Structure of a C Program: Every C program consists of one or more modules called functions. One of the functions must be called main. The program will always begin by executing the main function, which may access other functions. The main function is normally,but not necessarily located at the beginning of the program. The group of statements within main( ) are executed sequentially. When the closing brace of main( ) is encountered, program execution stops and control is returned to the operating system. Any other function defintions must be defined separately, either ahead or after main( ). Each function must contain: 1. A function heading, which consists of the function name, followed by an optional list of arguments, enclosed in parantheses. 2. A return type written before the function name. It denotes the type of data that the function will return to the program. 3. A list of argument declarations, if arguments are included in the heading. 4. A compound statement
, which comprises the remainder of the function. The arguments(also called parameters) are symbols that represent information being passed between the function and other parts of the program. Each compound statement is enclosed between a pair of braces{ }. The braces may contain one or more elementary statements (called expression statements) and other compound statements. Thus compound statements may be nested one within another. Each expression statement must end with a semicolon(;). Comments (remarks) may appear anywhere within a program as long as they are enclosed within the delimiters /* and */. Comments are used for documentation and are useful in identifying the program’s principal features or in explaining the underlying logic of various program features. Components of C Language: There are five main components of the C Language:- 1. The character set: C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9 and certain special characters as building blocks to form basic program elements(e. g. constants, variables, expressions, statements etc. ). 2. Data Types: The C language is designed to handle five primary data types, namely, character, integer, float, double and void; and secondary data types like array, pointer, structure, union and enum. 3. Constants: A constant is a fixed value entity that does not change its value throughout program execution. 4. Variables: A variable is an entity whose value can change during program execution. They are used for storing input data or to store values generated as a result of processing. 5. Keywords: Keywords are reserved words which have been assigned specific meanings in the C language. Keywords cannot be used as variable names. The components of C language will be discussed in greater detail in the following articles. This section gives only a brief introduction to the components of C. Example 1: The following program reads in the radius of a circle, calculates the area and then prints the result.
/* program to calculate the area of a circle*/

#include<stdio.h> /*Library file access*/

#include<conio.h> /*Library file access*/

void main( )            /* Function Heading*/

    {

       float radius, area; /*Variable declarations*/

       /*Output Statement(prompt)*/

       printf("Enter the radius :");

      /*Input Statement*/

      scanf("%f", &radius);

      /*Assignment Statement*/

      area = 3.14159*radius*radius;

      /*Output Statement*/

      printf("Area of the circle :%f", area);

       getch( );

     }

Program output:-

Enter the radius: 3

Area of the circle: 28. 27431

The following points must be considered to understand the above program:- 1. The program is typed in lowercase. C is case sensitive i. e. uppercase and lowercase characters are not equivalent in C. It is customary to type C instructions in lowercase. Comments and messages(such as those printed using printf() ) can be typed in anycase. 2. The first line is a comment that identifies the purpose of the program. 3. The instruction #include <stdio.h> contains a reference to a special file called stdio. h . This file contains the definition of certain functions required to read and print data such as printf() and scanf() . It is a header file and hence the extension . h. 4. Similarly #include <conio.h> links the file conio. h which is another header file that contains the definitions of functions used for reading and printing data at the console. The function getch()
is defined in conio. h. # denotes a preprocessor directive. More about this in a later article. 5. The instruction void main() is a heading for the function main( ). The keyword void denotes the return type of main and indicates that the function does not return any value to the program after the program has finished executing. The empty parantheses ( ) after main indicates that this function does not include any arguments. Program execution always begins from main( ). 6. The remaining five lines of the program are indented and enclosed in a pair of braces { }. These five lines comprise the compound statement within the function main( ). 7. The instruction float radius, area;  is a variable declaration. It establishes the symbolic names ‘radius’ and ‘area’ as floating point variables. These variables can accept values of type ‘float ‘ i. e numbers containing a decimal point or an exponent. 8. The next four instructions are  expression statements. The instruction printf(“Enter the radius :”); generates a request for information namely,the value for the radius. This statement generates a prompt where the user enters the value . 9. The value of the radius is read into (or stored in) the variable radius with the help of the scanf ( ) function. The instruction scanf(“%f”, &radius); is used for reading data.   “%f” is a conversion character which is used to accept a floating point value. 10. The next instruction, area = 3.14159*radius*radius; is called an  assignment statement. This instruction calculates the area by using the value of radius entered by the user and assigns the value to the variable area. 11. The next printf( ) statement prints the message Area of the circle followed by the calculated area. 12. The statement getch(); is used to pause the screen so that you can read the output. If getch( ) is not used the screen will just flash and go away. This function waits for the user to input some character(as it accepts a character as input), after the program has finished executing. Any key present on the keyboard pressed by the user is accepted by the getch function as input and its ASCII value is returned to main( ). Example2: Below is a variation of the above program:
/*program to calculate the area of a circle using a user defined function*/

#include <stdio.h>

#include <conio.h>

#define PI 3.14159

float process(float radius);/*function prototype*/

void main()

    {

      float area,radius;

      printf("n Enter the radius:");

      scanf("%f", &radius);

      area= process(radius);

      printf("Area =%f", area);

      getch();

     }

float process( float r)

     {

     float a; /*local variable declaration*/

     a= PI*r*r;
     return(a);

     }
This version utilizes a separate programmer defined function called process, to calculate the area. Within this function, r is an argument (also called a parameter) that accepts the value of radius supplied to  process from main, and a is the calculated result returned to main. A reference to the function appears in main( ), within the statement area= process(radius); In this statement, the value of area being returned from the function process is stored in the variable area. The main function is preceeded by a function prototype, which indicates that there is a user defined function called process which is defined after main and that it accepts a floating point argument and returns a floating point value. If the user defined function process, was defined before main( ), the function prototype would,generally, have not been required. More explanation about this when I write about functions in a later article. This program also contains a symbolic constant, PI, which represents the numeric value 3. 14159. This is a form of shorthand that exists for the programmers convenience. When the program is actually compiled, the symbolic constant will automatically be replaced by its numerical value. The output of this program is the same as that of the previous program. This article was a brief introduction, it gives an idea of C programming. The next article will talk about the fundamental concepts of C which include the C character set, Identifiers and keywords, data types in detail, constants,variables, variable declarations, expressions, statements and symbolic constants .

Frequently Asked Questions about C Programming Language

Why is C considered a powerful programming language?

C is considered a powerful programming language due to its versatility and efficiency. It allows low-level access to memory, offers a simple set of keywords, and has low-level support for complex mathematical functions and graphics. This makes it ideal for system programming like operating system or compiler development. Moreover, C is a structured programming language which allows complex programs to be broken down into simpler ones called functions. It also allows data to be free or encapsulated in structures, providing both flexibility and control over data handling and manipulation.

Is C an object-oriented programming language?

No, C is not an object-oriented programming language. It is a procedural language, which means that programs in C are a list of instructions that tell the computer what to do step by step. However, it is possible to write object-oriented code in C, but it does not support the four main principles of object-oriented programming (OOP) – encapsulation, inheritance, polymorphism, and abstraction – directly.

What are the main uses of C language?

C language is used in a variety of applications. It is commonly used in system programming, game development, embedded systems, and developing other programming languages. C is also used in scripting for kernels and operating systems. Due to its speed and efficiency, it is a popular choice for high-performance software applications.

How does C compare to other programming languages?

C is often compared to other programming languages due to its wide usage and powerful features. Compared to languages like Python or Java, C gives the programmer more control over the system resources and internal workings of the program. However, this also means that C can be more complex and harder to manage for larger applications. C does not have the built-in safety features of some higher-level languages, which can lead to common programming errors like memory leaks.

Is C a good language for beginners?

C can be a good language for beginners due to its fundamental place in programming. Learning C can provide a solid foundation for understanding more complex programming concepts and languages. However, it can also be challenging due to its low-level nature and lack of modern conveniences found in higher-level languages. It’s recommended for beginners to have a good understanding of programming concepts before diving into C.

What are the key features of C language?

C language has several key features that make it a powerful and flexible language. These include low-level access to memory, simple set of keywords, and the ability to create functions within a program. C also supports the use of pointers, which are used for dynamic memory allocation and structures, which allow for complex data structures.

How is memory management handled in C?

In C, memory management is handled manually by the programmer. This means that the programmer is responsible for allocating and deallocating memory using the malloc() and free() functions. This allows for efficient use of memory, but it also means that the programmer must be careful to avoid memory leaks and other common programming errors.

What is the role of libraries in C?

Libraries in C are collections of pre-compiled code that can be reused in different programs. They provide a way to share common code and functionality, reducing the amount of code that needs to be written and making the code easier to manage. The C standard library provides a set of functions for performing basic operations like input/output processing, string manipulation, mathematical computations, and more.

What are the data types available in C?

C supports several different data types, including integer types (int, short, long), floating-point types (float, double), character types (char), and void. C also supports derived data types like arrays, structures, unions, and pointers.

How does error handling work in C?

Error handling in C is done using a few different methods. One common method is to return an error code from a function that can be checked by the calling function. Another method is to set a global error condition that can be checked after a function call. C also provides the setjmp() and longjmp() functions for implementing a form of exception handling. However, C does not have built-in support for try/catch blocks like some other languages.

Surabhi SaxenaSurabhi Saxena
View Author

My name is Surabhi Saxena, I am a graduate in computer applications. I have been a college topper, and have been awarded during college by the education minister for excellence in academics. I love programming and Linux. I have a blog on Linux at www.myblogonunix.wordpress.com.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week