CSE NotesCSE Notes
Simplifying Complexity

C as a Middle-Level Language

  1. High-Level Features:
    • Abstraction: C provides constructs like functions, structures, and arrays, allowing programmers to write code that is easier to read and maintain.
    • Portability: Programs written in C can be compiled and run on various platforms with minimal modifications, thanks to its standardized syntax.
  2. Low-Level Features:
    • Memory Management: C allows direct manipulation of hardware and memory through pointers, enabling fine control over system resources.
    • Efficiency: C programs can be very efficient and close to machine code, making it suitable for system-level programming (like operating systems and embedded systems).

Advantages of C as a Middle-Level Language

  • Performance: Since C provides low-level access to memory and system resources, programs can be highly optimized for performance.
  • Flexibility: C can be used for a wide range of programming tasks, from system software to application development.
  • Access to Hardware: C’s ability to interact directly with hardware makes it ideal for writing system software, drivers, and real-time applications.

Applications of C

  • Operating Systems: Many operating systems (like UNIX) are written in C due to its efficiency and control.
  • Embedded Systems: C is commonly used in programming microcontrollers and other embedded devices.
  • Compilers and Interpreters: Many language compilers and interpreters are written in C.

A C program has a specific structure that consists of various components. Here’s a breakdown of the typical structure of a C program:

Structure of a C Program

  • Preprocessor Directives:
    • These are lines that begin with #, which tell the compiler to include certain files or define constants before the actual compilation starts.

Example

#include <stdio.h>
#define PI 3.14

  • Global Declarations:

This section includes variable declarations or function prototypes that are accessible throughout the program.

  • Main Function:
    • Every C program must have a main() function. This is the entry point where execution begins.

int main() {
// Code goes here
return 0;
}

  • Local Declarations:

Variables that are declared within a function (including main()) are local to that function.

Example

printf(“Hello, World!\n”);

  • Return Statement:

The main() function usually ends with a return 0; statement, indicating successful completion of the program.

If the function returns an integer, return provides a status code to the operating system.

Example of a Simple C Program

#include <stdio.h> // Preprocessor directive

// Function prototype
void greet();

int main() { // Main function
int number; // Local variable declaration

printf(“Enter a number: “); // Output
scanf(“%d”, &number); // Input

if (number % 2 == 0) { // Conditional statement
printf(“Even\n”);
} else {
printf(“Odd\n”);
}

greet(); // Function call
return 0; // Return statement
}

// Function definition
void greet() {
printf(“Hello, World!\n”);
}

Summary

  • Preprocessor Directives: Include libraries or define constants.
  • Global Declarations: Variables or function prototypes available throughout the program.
  • Main Function: Entry point of the program where execution starts.
  • Local Declarations: Variables declared within functions.
  • Statements and Expressions: The logic of the program.
  • Return Statement: Indicates the end of the main() function.