Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

C is a 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 UNIX operating system. 

Question for Introduction: C Language
Try yourself: Who is the father of C language?
View Solution


Main Features of C language

The main features of the C language include low-level memory access, a simple set of keywords, and a clean style. These features make C language suitable for system programming like an operating system or compiler development.
Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE)

Let's discuss the features of C language in detail:
1. Simple and Efficient - The basic syntax style of implementing C language is very simple and easy to learn. This makes the language easily comprehensible and enables a programmer to redesign or create a new application. 

2. Fast - It is a well-known fact that statically typed programming languages are faster than dynamic ones. C is a statically typed programming language, which gives it an edge over other dynamic languages. Also, unlike Java and Python, which are interpreter-based, C is a compiler-based program. This makes the compilation and execution of codes faster.

3. Portability - Another feature of the C language is portability. To put it simply, C programs are machine-independent which means that you can run the fraction of a code created in C on various machines with none or some machine-specific changes. 

4. Extensibility - You can easily (and quickly) extend a C program. This means that if a code is already written, you can add new features to it with a few alterations. 

5. Function-Rich Libraries - C comes with an extensive set of libraries with several built-in functions that make the life of a programmer easy. You can also create your user-defined functions and add them to C libraries.
6. Dynamic Memory Management - One of the most significant features of C language is its support for dynamic memory management (DMA). It means that you can utilize and manage the size of the data structure in C during runtime. For instance, you can use the free() function to free up the allocated memory at any time.

7. Modularity With Structured Language - C is a general-purpose structured language. This feature of C language allows you to break code into different parts using functions which can be stored in the form of libraries for future use and reusability.

8. Mid-Level Programming Language - Although C was initially developed to do only low-level programming, it now also supports the features and functionalities of high-level programming, making it a mid-level language. And as a mid-level programming language, it provides the best of both worlds. For instance, C allows direct manipulation of hardware, which high-level programming languages do not offer.

9. Pointers - With the use of pointers in C, you can directly interact with memory. As the name suggests, pointers point to a specific location in the memory and interact directly with it. Using the C pointers, you can operate with memory, arrays, functions, and structures.

10. Recursion - It means that you can create a function that can call itself multiple times until a given condition is true, just like the loops. Recursion in C programming provides the functionality of code reusability and backtracking.

Note - Many later languages have borrowed syntax/features directly or indirectly from the C language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on the C language. C++ is nearly a superset of C language (Few programs may compile in C, but not in C++).


Beginning with C programming

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:

Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE)Structure of a C program

1. The components of the above structure are:
(i) 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: 

  • stddef.h: Defines several useful types and macros.
  • stdint.h: Defines exact width integer types.
  • stdio.h: Defines core input and output functions
  • stdlib.h: Defines numeric conversion functions, pseudo-random network generator, memory allocation
  • string.h: Defines string handling functions
  • math.h: Defines common mathematical functions

Question for Introduction: C Language
Try yourself:What is the extension of header file?
View Solution

(ii) 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 the main method:

#include<stdio.h>

int main()
{}

(iii) 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:

#include<stdio.h>

int main()
{
    int a;
.
.

(iv) Body: The body of a function in the C program, refers to the operations that are performed in the functions. It can be anything like manipulations, searching, sorting, printing, etc.
Example:

#include<stdio.h>

int main()
{
    int a;
    printf("%d", a);
.
.

(v) Return Statement: The last part of 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.
Example:

#include<stdio.h>

int main()
{
    int a;
    printf("%d", a);
    return 0;
}

(vi) Writing first program: 

Following is first program in C:

#include <stdio.h>
int main(void)
{
    printf("GeeksQuiz");
    return 0;
}

(vii) Let us analyze the program line by line. 

  • Line 1: [ #include <stdio.h> ] 
    1. In a C program, all lines that start with # are processed by a preprocessor which is a program invoked by the compiler.
    2. In a very basic term, the preprocessor takes a C program and produces another C program. 
    3. The produced program has no lines starting with #, all such lines are processed by the preprocessor.
    4. In the above example, the preprocessor copies the preprocessed code of stdio.h to our file.
    5. The .h files are called header files in C. These header files generally contain declarations of functions. We need stdio.h for the function printf() used in the program.
    Question for Introduction: C Language
    Try yourself:The C-preprocessors are specified with _________ symbol.
    View Solution
  • Line 2 [ int main(void) ] 
    1. There must be a starting point from where the execution of compiled C program begins.
    2. In C, the execution typically begins with the first line of main().
    3. The void written in brackets indicates that the main doesn’t take any parameter (See this for more details). main() can be written to take parameters also. We will be covering that in future posts.
    4. The int that was written before main indicates the return type of main(). The value returned by main indicates the status of program termination. 
  • Line 3 and 6: [ { and } ] 
    In C language, a pair of curly brackets defines the scope and are mainly used in functions and control statements like if, else, and loops. All functions must start and end with curly brackets.
  • Line 4 [ printf(“GeeksQuiz”); ] 
    printf() is a standard library function to print something on standard output. The semicolon at the end of printf indicates line termination. In C, a semicolon is always used to indicate the end of a statement.
  • Line 5 [ return 0; ] The return statement 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.

2. How to execute the above program: 
In order to execute the above program, we need a compiler to compile and run our programs. There are certain online compilers that can be used to start C without installing a compiler.

  • Windows: There are many compilers available freely for the compilation of C programs like Code Blocks and Dev-CPP. We strongly recommend Code Blocks.
  • Linux: For Linux, gcc comes bundled with Linux, Code Blocks can also be used with Linux.
The document Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Introduction: C Language - Programming and Data Structures - Computer Science Engineering (CSE)

1. What are the main features of the C programming language?
Ans. The main features of the C programming language are: 1. Portability: C programs can be written on one computer system and compiled to run on another, as long as the target system has a compatible C compiler. 2. Efficiency: C is a low-level language that allows direct memory manipulation and efficient execution of code. It provides features like pointers and bitwise operators that allow developers to optimize their programs. 3. Modularity: C supports the modular programming paradigm, allowing developers to break their code into smaller, manageable modules. This promotes code reusability and maintainability. 4. Extensibility: C allows the use of external libraries, which can be written in other languages like assembly or C++. This makes it easy to incorporate existing code or take advantage of specialized functionalities. 5. Wide Usage: C is one of the most widely used programming languages in the world. It is used for developing operating systems, embedded systems, game engines, and various other applications.
2. What is the significance of portability in the C programming language?
Ans. Portability is a significant feature of the C programming language. It means that a C program can be written on one computer system and compiled to run on another, as long as the target system has a compatible C compiler. The significance of portability in C includes: 1. Cross-platform compatibility: C programs can be executed on different computer systems, including Windows, macOS, Linux, and embedded systems, without the need for significant modifications. 2. Code reusability: Portable C code can be reused across different projects and platforms, saving development time and effort. 3. Easy migration: Portability makes it easier to migrate C programs from one system to another, allowing for scalability and adaptability. 4. Reduced maintenance: As C programs can be executed on various systems, it reduces the need for maintaining multiple versions of the same codebase for different platforms. 5. Accessibility: The portability of C allows developers to reach a wider audience and target different devices and operating systems.
3. How does C programming support modularity?
Ans. The C programming language supports modularity through various features: 1. Functions: C allows developers to break their code into smaller, reusable functions. Functions help in organizing code and promoting code reusability. 2. Header files: C provides header files that contain function prototypes and declarations. By including these header files in different parts of the program, developers can easily use functions defined in other modules. 3. Libraries: C supports the creation and usage of libraries, which are collections of precompiled code modules. Libraries allow developers to separate their code into reusable components, enhancing modularity. 4. Separation of interface and implementation: C allows developers to define interfaces in header files and implement them in separate source files. This separation allows for abstraction and encapsulation, promoting modularity. 5. Makefiles: C programming often involves the use of Makefiles, which automate the build process and manage dependencies between different modules. Makefiles further enhance modularity by providing a structured approach to building and linking code modules.
4. How does C programming achieve efficiency?
Ans. C programming achieves efficiency through various features: 1. Low-level programming: C is a low-level programming language, which allows direct memory manipulation and efficient execution of code. It provides features like pointers and bitwise operators that enable developers to optimize their programs. 2. Control over memory allocation: C provides manual memory management through functions like malloc() and free(). This allows developers to allocate and deallocate memory as needed, reducing memory wastage and improving efficiency. 3. Efficient data structures: C allows developers to define custom data structures, such as arrays and structures, which can be efficiently manipulated and accessed. This enables efficient storage and retrieval of data. 4. Inline assembly: C programming allows the use of inline assembly, which gives developers direct control over the CPU instructions executed by the program. This can result in highly optimized and efficient code. 5. Compiler optimizations: C compilers often include optimizations that automatically optimize the generated machine code for better performance. These optimizations can include loop unrolling, function inlining, and constant propagation, among others.
5. Why is C programming widely used in various applications?
Ans. C programming is widely used in various applications due to the following reasons: 1. Efficiency: C is a low-level language that allows direct memory manipulation and efficient execution of code. It provides features like pointers and bitwise operators that enable developers to optimize their programs for performance. 2. Portability: C programs can be written on one computer system and compiled to run on another, as long as the target system has a compatible C compiler. This makes C suitable for developing cross-platform applications. 3. Wide range of applications: C is used in a wide range of applications, including operating systems, embedded systems, game development, scientific simulations, and high-performance computing. 4. Extensibility: C allows the use of external libraries, which can be written in other languages like assembly or C++. This makes it easy to incorporate existing code or take advantage of specialized functionalities. 5. Legacy codebase: Many existing software systems and libraries are written in C. This legacy codebase ensures the continued relevance and usage of C in various applications.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Objective type Questions

,

Free

,

ppt

,

Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE)

,

Exam

,

pdf

,

Semester Notes

,

Viva Questions

,

Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

,

MCQs

,

past year papers

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Sample Paper

,

Extra Questions

,

Introduction: C Language | Programming and Data Structures - Computer Science Engineering (CSE)

,

mock tests for examination

,

study material

,

video lectures

,

Summary

;