DEV Community

Cover image for INTRODUCTION TO C - PROGRAMMING LANGUAGE
Frank
Frank

Posted on

INTRODUCTION TO C - PROGRAMMING LANGUAGE

C is a high-level, general-purpose programming language developed by Dennis Ritchie in 1972 at Bell Laboratories, New Jersey, USA.


What is C?

Basic Instructions in C

Libraries in C

Why Do We Write Programs?

Writing and Running a C Program

Quiz


What is C?

C is one of the most widely taught programming languages in schools, alongside Python and Java. It plays a crucial role in embedded systems and serves as the backbone of most internet servers, particularly those running on Linux. The Linux kernel and the core of all Android devices are both written in C. In essence, C code powers much of the world, making its impact both significant and widespread.

Image description
C is a language, just like English or French, but it is designed for computers to understand. Programmers use C to instruct computers to perform various tasks.

At the time of its inception, C was revolutionary because of its portability across different machines. Today, we take cross-platform compatibility for granted, but C’s simplicity and compiler portability made it groundbreaking. C is a compiled language, like Go, Java, Swift, and Rust, which means that a directly executable binary file is generated. Unlike interpreted languages such as Python, Ruby, or JavaScript, C does not have garbage collection, meaning programmers must manually manage memory. This makes C particularly suited for programming embedded systems like Arduino, giving it power and control over hardware.


Basic Instructions in C

Image description

Imagine your computer as a highly capable but obedient friend that only understands a specific set of instructions. C allows you to communicate those instructions and organize tasks efficiently.

Here’s a simple example to demonstrate how C works:

#include <stdio.h>  // Standard input/output header file

int main(void) {    // Starting point of the program
    printf("Hello, World!\n");  // Prints "Hello, World!" on the screen
    return 0;  // Indicates successful program execution
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code:

  • int: Represents the return type of the main() function. It’s a datatype of 4 bytes (size can vary in local machines).

  • main(): The entry point of the program. In this case, it returns an integer (int) and takes void as an argument, meaning it doesn’t receive any input.

  • void: A data type indicating no arguments.

  • { }: These curly braces define the scope of the function, enclosing all the statements within the function.

  • printf: A function from the stdio.h library used to print formatted text to the console.

  • "Hello, World!": A string, which is a sequence of characters enclosed in double quotes.

  • \n: An escape character that creates a newline after the output.

  • return 0: Indicates that the program ended successfully. Returning a non-zero value typically signals an error.


Libraries in C

C is a minimal language at its core, and most additional functionality comes from libraries. These libraries are written by other programmers or built into the compiler. For example, the stdio.h library provides the printf() function we used in the example.

We will dive deeper into this concept as we continue our journey through C programming.


Why Do We Write Programs?

A computer is a general-purpose machine capable of performing any computational task. However, it doesn’t understand human languages like English or French. To communicate with computers, we write programs—a sequence of instructions that the computer follows to complete specific tasks.

In simple terms, a program is a set of instructions given to a computer to perform a specific task. Computers understand only binary code (zeros and ones), and programming languages like C allow us to translate our instructions into a form the computer can process.

Note: Software is a collection of programs.

Before C, there was a programming language called B, developed by Ken Thompson. C was developed as its successor and was later used to write the Unix/Linux operating system. C is also called a system programming language. It was standardized by ANSI in 1989.


Writing and Running a C Program

Here’s a simple guide to writing and running a C program:

Set Up Your Development Environment

Create a New C File

  • Use a text editor (e.g., Notepad, Visual Studio Code, Sublime Text) or an IDE to create a new file with a .c extension (e.g., my_program.c).

Write Your Code

  • Include necessary header files (e.g., #include <stdio.h>) and write your main() function.

  • Write the main function, the starting point of C programs.
    Example “Hello World” Program:

#include <stdio.h>  

int main(void) {    
    printf("Hello, World!\n");  
    return 0;  
}
Enter fullscreen mode Exit fullscreen mode

Save Your File

  • Save your file with a .c extension.

Compile Your Program

  • Open a terminal or command prompt and navigate to the directory where your C file is located.
  • Compile your code using a compiler, such as GCC:

     gcc my_program.c -o my_program
    

    This command compiles my_program.c into an executable named my_program.

Run Your Program

  • After compilation, run the program:

     ./my_program
    
  • You should see the output on the screen. For the “Hello, World!” program, the output will be:

     Hello, World!
    

Debugging

After you must’ve been done with writing your program in C-language or any language. The program has to be converted into MACHINE LANGUAGE (binary) and computer will execute your program i.e. the C.P.U because it is the central processing unit. The C.P.U. is responsible for giving you an output.


Quiz

  1. What is the purpose of the printf function in C, and how is it used to display output on the screen? Provide a simple example.
  2. Explain the role of the main() function in a C program. Why is it considered the starting point, and what does its return value signify?
  3. Why is the #include <stdio.h> statement commonly used in C programs? What essential functionality does it bring to your code?

Feel free to drop you answers in the comment section

Top comments (0)