1. Home
  2. Learn
  3. Introduction to Programming in C

Introduction to Programming in C

C is a powerful and widely-used programming language that forms the foundation for many modern programming languages. It is known for its efficiency, control, and low-level access to memory. Whether you're a beginner or looking to refresh your knowledge, this guide will walk you through the fundamentals of C programming with clear explanations and practical examples.

By the end of this series, you'll be able to write your own C programs, understand how memory works, and build a strong foundation for learning other programming languages.


Table of Contents

  1. What is C?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Operators and Expressions
  5. Conditional Statements
  6. Loops
  7. Functions
  8. Arrays
  9. Pointers
  10. Structures

What is C?

C is a general-purpose, procedural programming language developed in the early 1970s. It is widely used for system programming, embedded systems, and developing operating systems.

1// Example: Your first C program
2#include <stdio.h>
3
4int main() {
5    printf("Hello, C Programming!\n");
6    return 0;
7}

Setting Up Your Environment

To write and run C programs, you need a C compiler like GCC (GNU Compiler Collection) and a text editor or IDE (Integrated Development Environment).

1# Example: Compiling and running a C program
2gcc program.c -o program
3./program

Variables and Data Types

Variables are used to store data in C. You must declare the type of a variable before using it. Common data types include int, float, char, and double.

 1// Example: Declaring variables
 2#include <stdio.h>
 3
 4int main() {
 5    int age = 25;
 6    float height = 5.9;
 7    char grade = 'A';
 8
 9    printf("Age: %d, Height: %.2f, Grade: %c\n", age, height, grade);
10    return 0;
11}

Operators and Expressions

C includes arithmetic, relational, and logical operators to perform operations on data.

 1// Example: Using operators
 2#include <stdio.h>
 3
 4int main() {
 5    int a = 10, b = 5;
 6    int sum = a + b; // 15
 7    int isGreater = a > b; // 1 (true)
 8
 9    printf("Sum: %d, Is Greater: %d\n", sum, isGreater);
10    return 0;
11}

Conditional Statements

Conditional statements like if, else if, and else allow you to execute code based on certain conditions.

 1// Example: Conditional statement
 2#include <stdio.h>
 3
 4int main() {
 5    int age = 18;
 6
 7    if (age >= 18) {
 8        printf("You are an adult.\n");
 9    } else {
10        printf("You are a minor.\n");
11    }
12
13    return 0;
14}

Loops

Loops like for, while, and do-while help you repeat a block of code multiple times.

 1// Example: For loop
 2#include <stdio.h>
 3
 4int main() {
 5    for (int i = 0; i < 5; i++) {
 6        printf("Iteration: %d\n", i);
 7    }
 8
 9    return 0;
10}

Functions

Functions are reusable blocks of code that perform a specific task. You can define functions using the return_type function_name() syntax.

 1// Example: Function
 2#include <stdio.h>
 3
 4// Function to add two numbers
 5int add(int a, int b) {
 6    return a + b;
 7}
 8
 9int main() {
10    int result = add(5, 10);
11    printf("Result: %d\n", result); // 15
12    return 0;
13}

Arrays

Arrays are used to store multiple values of the same type in a single variable. You can access and manipulate array elements using their index.

 1// Example: Array
 2#include <stdio.h>
 3
 4int main() {
 5    int numbers[5] = {1, 2, 3, 4, 5};
 6
 7    for (int i = 0; i < 5; i++) {
 8        printf("Number %d: %d\n", i, numbers[i]);
 9    }
10
11    return 0;
12}

Pointers

Pointers are variables that store memory addresses. They are a powerful feature of C that allows direct memory manipulation.

 1// Example: Pointers
 2#include <stdio.h>
 3
 4int main() {
 5    int num = 10;
 6    int *ptr = &num; // Pointer to num
 7
 8    printf("Value: %d, Address: %p\n", *ptr, ptr);
 9    return 0;
10}

Structures

Structures allow you to group related data of different types under a single name.

 1// Example: Structure
 2#include <stdio.h>
 3
 4// Define a structure
 5struct Person {
 6    char name[50];
 7    int age;
 8    float height;
 9};
10
11int main() {
12    struct Person person1 = {"Alice", 25, 5.9};
13
14    printf("Name: %s, Age: %d, Height: %.2f\n", person1.name, person1.age, person1.height);
15    return 0;
16}