Back to C Programming
2026-07-125 min read

C - Features

Learn C - Features step by step with clear examples and exercises.

Why This Matters

C is a high-level programming language that was developed by Dennis Ritchie in 1972. It's widely used for system software, application software, device drivers, embedded systems, and more. This lesson will explore the unique features of C that make it such a powerful and versatile language.

Why This Matters

Understanding the features of C is essential for any programmer who wants to develop efficient, reliable, and high-performance software. C's flexibility, portability, and low-level control over hardware resources have made it a popular choice for system programming, embedded systems, and even game development.

Prerequisites

Before diving into the features of C, you should be familiar with basic programming concepts such as variables, loops, functions, and data structures. A basic understanding of computer organization, including memory management, CPU architecture, and file I/O, will also be helpful in appreciating some of C's unique features.

Core Concept

Data Types

C provides several built-in data types to store different kinds of values:

  1. char: used for storing single characters or integers within the range -128 to 127.
  2. int: used for storing integer values, which can be either positive or negative.
  3. float: used for storing floating-point numbers with a decimal point.
  4. double: similar to float but provides more precision and memory.
  5. void: used as the return type of functions that do not return any value.
  6. bool (not a built-in type in C, but can be defined using an enumeration): used for storing Boolean values (true or false).

Variables and Constants

Variables are storage locations in memory where you can store data. In C, variables must be declared before they can be used, and their data type determines the size and range of possible values. Constants are values that cannot be changed during runtime.

int a = 10; // Variable declaration and initialization
const int b = 20; // Constant declaration and initialization

Arrays

An array is a collection of variables of the same data type, stored in contiguous memory locations. In C, arrays are zero-indexed, meaning that the first element has an index of 0.

int arr[5] = {1, 2, 3, 4, 5}; // Array declaration and initialization

Pointers

Pointers in C are variables that store the memory addresses of other variables or data structures. They provide low-level control over memory allocation and deallocation, making C a powerful tool for system programming.

int x = 10;
int *ptr = &x; // ptr is a pointer variable that stores the address of x

Functions

Functions in C are blocks of code that perform specific tasks. The main() function is the entry point for every C program, and it's where execution begins.

void greet(char *name) {
printf("Hello, %s!\n", name);
}

int main() {
char name[] = "Alice";
greet(name);
return 0;
}

Preprocessor Directives

C's preprocessor allows you to include header files, conditional compilation, and macro definitions. This makes C more flexible and customizable than many other programming languages.

#include <stdio.h> // Include the standard input/output library

#define PI 3.141592653589793 // Define a constant using a macro

int main() {
printf("The value of PI is %f\n", PI);
return 0;
}

Worked Example

Let's create a simple C program that calculates the factorial of a number using recursion.

#include <stdio.h>

int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num = 5;
printf("The factorial of %d is %d\n", num, factorial(num));
return 0;
}

Common Mistakes

  1. Forgetting semicolons: Semicolons are required at the end of every statement in C. Failing to include them can cause syntax errors.
  2. Incorrect variable declarations: Variables must be declared before they can be used, and their data type must match the type of values you plan to store.
  3. Misusing pointers: Pointers are powerful but can also lead to memory leaks or segmentation faults if not handled correctly.
  4. Ignoring function prototypes: Function prototypes help the compiler understand what parameters a function expects and what type of value it returns.
  5. Not handling edge cases: Always consider and handle edge cases, such as invalid input or special values, to ensure your program behaves correctly in all situations.

Practice Questions

  1. Write a C program that calculates the sum of the first n numbers using a loop.
  2. Create a function in C that swaps the values of two variables without using a temporary variable.
  3. Write a C program that finds the largest number in an array using recursion.
  4. Implement a binary search algorithm in C for sorted arrays.
  5. Write a C program that calculates the average of an array of numbers using a function.

FAQ

  1. Why is C considered a low-level language?

C provides direct access to hardware resources and memory, making it a low-level language compared to higher-level languages like Python or Java.

  1. What are some common libraries used in C programming?

Some popular C libraries include the Standard Template Library (STL), OpenGL for graphics programming, and POSIX for portable operating system interfaces.

  1. Why is it important to declare variables before using them in C?

Declaring variables before using them helps the compiler understand their data type and size, which can improve performance and prevent errors.

  1. What are some best practices for using pointers in C?

Some best practices include initializing pointer variables, checking for null pointers before dereferencing, and properly managing memory allocation and deallocation to avoid leaks or segmentation faults.

  1. Why is recursion useful in C programming?

Recursion can make certain algorithms easier to understand and implement, especially when dealing with problems that have a natural recursive structure, such as tree traversals or factorial calculations.