Podcast Title

Author Name

0:00
0:00
Album Art

C Programming Fundamentals: A Quick Start Guide

By 10xdev team August 03, 2025

The Origins of C

The C language was invented by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s. Many of the ideas for the C language were borrowed from the earlier B language and its predecessors. C is considered a middle-level programming language; because of this, we can use C for system-level programming, like writing operating systems, and also for developing games and applications.

It is a structured programming language, meaning we can write our programs using a set of functions and modules. For object-oriented programming, we can use C++, the extension of the C language.

Getting Started with C

To get started with the C language, you need to have a compiler installed. Several compilers like GCC and Clang are widely used. A C file ends with a .c extension, and a mandatory main function is required in every C file.

On top of your C file, you can import other C files whose code can be used in your C program. These files are called header files, and they end with a .h extension. To import a header file, use #include and then the file name in angle brackets if it is from the standard C library, or in double quotes if you have your own header file.

For instance, here I have included the stdio.h header file from the C library, which includes functions like scanf and printf used for input and output in C programming.

#include <stdio.h>

int main() {
  // Your code here
  return 0;
}

Core Building Blocks: Keywords and Data Types

C language has over 30 keywords and around five primary data types: - char: Used to refer to characters. - int: For integers. - float: For decimal values up to 6 digits after the decimal point. - double: Also for decimal values, but up to 15 digits after the decimal point. - void: For empty or no type. void is usually used in functions to specify the return value.

Working with Variables

To declare a variable, specify the data type and the variable name. There are several rules for declaring a variable: - A variable name can only have alphabets (both uppercase and lowercase), digits, and underscore. - The first letter of a variable should be either an alphabet or an underscore. - No commas or blanks are allowed within a variable name. - No special symbol other than an underscore can be used in a variable name.

After that, if we want to assign the value to the variable, you can do it at declaration or set it later. Make sure to enter a semicolon after the end of every statement.

Input and Output

To print something, you can type printf and then, in brackets, use double quotes. In those double quotes, write anything that you want to print. You can print the value of variables with the help of format specifiers. %d is used for the integer data type. After closing the double quotes, add a comma and then the variable name.

int myNumber = 42;
printf("My favorite number is %d\n", myNumber);

Here are other format specifiers for printing and scanning different types of data: - %c: char - %f: float - %lf: double - %s: string (character array)

To take input from the user, we can use the scanf function. Type scanf and then, in double quotes, the format specifier for the data that will be entered by the user. Then, after a comma, use the ampersand symbol (&), also known as the address symbol in the C language, and the variable name to store the data given by the user to the address of the variable.

Essential Operators

We can use numerous operators in the C language for various operations.

Arithmetic Operators: - + (addition), - (subtraction), * (multiplication), / (division), % (modulus)

Relational Operators: - == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)

Logical Operators: - && (logical AND), || (logical OR), ! (logical NOT)

These logical operators are for combining two or more expressions. For example: if (a == 6 && b == 9). Here, if the value of a is 6 AND the value of b is 9, then the code in the if statement will be executed.

Controlling Program Flow

To write an if statement in a C program, type if and, in brackets, your condition, like if (a == b). Then, add the code that you want to run in curly brackets if there are multiple statements. If there is only one, you can write it without the curly brackets.

After if, you can also add else statements, so that if the if condition is false, the statements in the else section will be executed. We can also nest if-else statements or use an if-else if ladder.

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if condition1 is false and condition2 is true
} else {
  // block of code to be executed if both conditions are false
}

A switch statement in the C language helps select the statement to execute from a group of statements. A simple example of a switch statement can be like this:

int cars = 2;

switch (cars) {
  case 1:
    printf("You have 1 car.\n");
    break;
  case 2:
    printf("You have 2 cars.\n");
    break;
  case 3:
    printf("You have 3 cars.\n");
    break;
  default:
    printf("You have many cars!\n");
}

Here, the value of cars will be compared to these cases. You can also have a default case so that if none of the cases match, the default case will be executed.

Mastering Loops in C

There are three types of loops in the C language: the while loop, the do-while loop, and the for loop.

To write a while loop, type while and then in brackets the condition, like a != b, and then in curly brackets the code you want to run.

For a do-while loop, type do, then the code in curly brackets, and then while and the condition in brackets. In a do-while loop, the code is executed once, no matter whether the condition is true or false, and then the condition is checked.

After these two comes the for loop, which gives us more control. To write a for loop, we can type for and then in brackets, we can initialize the control variable (int i = 1), have our condition (i <= 3), and then increment or decrement the value of i.

For example, this code will print 1 2 3 in the terminal: c for (int i = 1; i <= 3; i++) { printf("%d ", i); } First, i = 1 will be initialized. Then the condition is checked. Since i is less than or equal to 3, the value of i will be printed. Now, i will be incremented by 1. The condition will be rechecked, and this continues until the condition becomes false.

Understanding Functions

A function is a block of code that performs a specific task, like a function to add two numbers. To declare a function, first, we need to enter the data type that the function returns. Then we can name the function, and in brackets, the data that will be given to the function.

int addNumbers(int a, int b) {
    int sum = a + b;
    return sum;
}

To call this function and save the result, we can declare a variable and assign the value returned by the function to that variable.

int result = addNumbers(5, 10); // result will be 15

Working with Arrays

An array is a collection of data items of the same data type. You can declare an array the same as a variable, but after the name, in square brackets, specify the size of the array.

int numbers[10]; // This array can store 10 integer items

You can have empty square brackets if you assign a value to array elements at the time of array initialization. Remember that counting starts from 0, so the index of the first item in the array is 0. To access any item from the array, you can write the array name and its index in square brackets. Not only a simple array, but you can also declare and work on two-dimensional, three-dimensional, or any multi-dimensional array.

Pointers Explained

When we declare a variable, the variable name is assigned to a memory location. To get the memory address of that location, we can use the ampersand (&). A variable that stores another variable's address is called a pointer.

Declaring a pointer is almost the same as declaring a variable; you just need to add a star sign (*) before the variable name.

int myAge = 30;     // An int variable
int* ptr = &myAge;  // A pointer variable, with the name ptr, that stores the address of myAge

You can use a star before the pointer name to get the value at the address stored in the pointer variable. The star is also known as the dereferencing pointer. A pointer that stores the address of another pointer variable is called a double pointer.

Handling Strings

A string is an array of characters. To declare a string in a C program, we need to use the data type char, then the string name, and in brackets, the string size.

char greeting[20] = "Hello World";

To print or scan a string, you must use %s as a format specifier. There is a string.h header file in the C library which includes functions that you can use to compare strings, copy strings, and a lot more.

Structs and Unions

In the C language, a structure is a collection of different data items. You can use the struct keyword and then the structure name to declare a structure.

struct Person {
  char name[50];
  int age;
};

struct Person p1 = {"Ahmed", 25};

To access or change the elements of your structure variables, type the structure variable name, then a dot (.), and then the element name (p1.age).

A Union is a special data type in C that allows storing different data types in the same memory location. We can access and declare the union the same as a structure, but instead of the struct keyword, we have to use the union keyword. In a Union, you can only have one member with a value at a time. Union is used for efficient memory management.

Comments and Compilation

You can use a double forward slash (//) for single-line comments in the C language. Multi-line comments start with /* and end with */.

To compile your C program using GCC, you can type gcc and then the file name, followed by -o and the desired name for the executable file.

gcc my_program.c -o my_program

And that's how you compile and run an excellent C program.

Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Recommended For You

Up Next