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.
- 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.
/* 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 usingprintf() )
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.
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.