What is C?

What is C?

Summary of the C programming language and its key elements.


C is a general-purpose, procedural programming language designed and developed by Dennis Ritchie in the early 1970s. It is widely used in system programming, embedded systems, and game development.


Key Elements of C:

  • Variables and Data types: In C, variables are used to store data values. The data types define the type of data that can be stored in a variable. Some of the commonly used data types in C are int, float, char, and double.

  • Operators: C has several operators that are used to perform mathematical, logical, and bitwise operations. Some of the commonly used operators in C are +, -, *, /, %, >, <, >=, <=, ==, and !=.

  • Control Flow: C has several control flow statements that are used to control the flow of execution in a program. The control flow statements include if-else, switch, while, do-while, and for loops.

  • Functions: Functions are used to group a set of statements that perform a specific task. C functions have a return type, a name, and a set of parameters.

  • Pointers: Pointers are variables that store the memory address of another variable. Pointers are used in C to manipulate memory and to create complex data structures.


How to Use C?

To start programming in C, you need to install a C compiler on your computer. There are several free and open-source C compilers available such as GCC, Clang, and TCC.

Once you have a compiler installed, you can start writing C programs using a text editor or an integrated development environment (IDE). You can then compile the program using the compiler and execute the generated executable file.

Here's an example of a simple C program that prints "Hello, World!" to the console:

#include <stdio.h>

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

This program uses the printf function to display the message "Hello, World!" to the console. The return 0 statement indicates that the program has been executed successfully.


The C language standard:

the syntax, semantics, and requirements of a conforming C program. It includes guidelines for the functions, header files, and libraries that can be used in a C program. The standard also specifies the behavior of the compiler and other tools used to build C programs.

The current standard for the C programming language is C18.

In addition to the C programming language standard, there are also various extensions and variations of C, such as C++ and C#, that have their own syntax and features.


C language terminology:

Compiler - A software tool that translates human-readable code written in C into machine code that can be executed by a computer (0-1).


Variable - A named storage location that holds a value, which can be of various types, such as integer, character, or float.


Data types - The type of data that can be stored in a variable. Some examples of data types in C include int, char, and float.


Pointer - A variable that stores the memory address of another variable.


Array - A collection of variables of the same data type, which are stored adjacent to each other in memory.


Struct - A collection of variables of different data types that are grouped together under a single name.


Expression - A combination of operators, constants, and variables that evaluates to a value. For example, 2 + 3 is an expression that evaluates to 5.


Function - A set of instructions that performs a specific task, such as calculating the square root of a number or printing a message to the console.


Statement - A complete line of code that performs a single action or task. Examples of statements in C include declaration statements, assignment statements, and control statements.

A declaration statement introduces a new identifier and specifies its data type:

int x; // Declares a variable named x of type integer

An assignment statement assigns a value to a variable:

x = 5; // Assigns the value 5 to the variable x

Control statements like if-else and loops control the flow of execution in a program based on certain conditions:

if (x > 5) { // If x is greater than 5
    printf("x is greater than 5\n");
} else { // Otherwise
    printf("x is less than or equal to 5\n");
}

Conditional statements - Statements that allow the program to make decisions based on certain conditions. The most common examples in C are if-else statements and switch statements.


Loop - A repetitive statement that executes a block of code multiple times. There are three types of loops in C: for loops, while loops, and do-while loops.


Control flow - A general term used to describe the order in which statements execute. Conditional statements modify control flow by allowing the program to make decisions based on the evaluation of certain conditions.

In other words, control flow refers to the order in which the code is executed, while conditional statements are used to modify the order of execution based on some condition.

Here is an Example to illustrate the difference:

// Example of conditional statements (using if-else statement)

#include <stdio.h>

int main() {
    int age = 18;
    if (age >= 18) {
        printf("You are legally an adult");
    } else {
        printf("You are still a minor");
    }
    return 0;
}
               // Output: "You are legally an adult"


// Example of control flow without a conditional statement (using a for-loop)

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++)
    {
        printf("%d ", i);
    }
 return 0;
}
                // Output: 1 2 3 4 5

In the first example, the program uses a if-else statement to conditionally determine whether the person is an adult or a minor. In the second example, a for loop is used to control the flow of the program by repeatedly executing a block of statements.


C programming is a powerful tool for creating high-performance applications with concise syntax and low-level memory access. C's popularity is evident in its use in UNIX, Linux kernel, and MySQL. By learning C, you gain a deeper understanding of computing and software development, and valuable skills that can be applied in various domains.


Hope that helped!