Learn the basics of C, a widely used programming language

C is one of the most popular programming languages and is widely used in computer science courses around the world. It is also commonly learned alongside Python and Java in schools. While C is not the easiest language to learn, it is a fundamental language that provides a deep understanding of programming principles.

Today, C is heavily used in embedded devices and powers many Internet servers running on Linux. The Linux kernel itself is built using C, making it a critical part of the infrastructure for Android devices. With C, you have the power to create software that runs on a global scale.

When C was created, it was considered a high-level language because it was portable across different computer architectures. Unlike languages such as Python, Ruby, or JavaScript, C is a compiled language, meaning it generates a binary file that can be directly executed and distributed. One of the unique aspects of C is its ability to manually manage memory, making it ideal for writing programs for devices like Arduino.

Let’s dive into a basic C program called “Hello, World!”

#include <stdio.h>

int main(void) {
    printf("Hello, World!");
}

In this program, we first import the stdio library, which provides input/output functions. C is a small language, and anything beyond its core functionality is provided by libraries. The stdio library, for example, provides the printf() function.

In C, code is organized into functions. Every C program has a main() function, which serves as the entry point for the program. Functions in C can have arguments and return a value. In the case of main(), it doesn’t accept any arguments and returns an integer value (int).

The printf() function is an example of a function invocation. Inside the stdio library, printf() is defined as int printf(const char *format, ...);. Here, we pass a string wrapped in double quotes as an argument to the function.

To compile and run a C program, you need a C compiler. Most Linux and macOS systems come with a C compiler pre-installed. For Windows, you can use the Windows Subsystem for Linux (WSL) to get a C compiler.

To compile the hello.c program, run the following command in your terminal:

gcc hello.c -o hello

This will generate an executable file named hello. To run the program, type:

./hello

And you should see the output: “Hello, World!”

C programs are highly optimized and result in small executable files. This optimization makes C ideal for embedded devices with limited resources.

Explore more C tutorials:

  • Variables and types
  • Constants
  • Operators
  • Conditionals
  • Loops
  • Pointers
  • Functions
  • Arrays
  • Strings
  • Input/output
  • Type definitions
  • Enumerated types
  • Structs
  • Header files
  • The C preprocessor

Tags: C programming, embedded devices, Linux, compiled language, memory management