C Programming Basics

Learn the fundamental concepts of C programming with practical examples and hands-on exercises.

Introduction to C Programming

C is a general-purpose programming language that was developed by Dennis Ritchie at Bell Labs in the early 1970s. It's one of the most widely used programming languages and serves as the foundation for many other languages like C++, Java, and C#.

Key characteristics of C:
- Procedural programming language
- Low-level access to memory
- Simple and efficient
- Portable across different platforms
- Rich set of built-in operators and functions

Your First C Program

Let's start with the classic "Hello, World!" program:

```c
#include <stdio.h>

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

Breaking down this program:
- `#include <stdio.h>` - Includes the standard input/output library
- `int main()` - The main function where program execution begins
- `printf()` - Function to print text to the console
- `return 0;` - Indicates successful program termination

Variables and Data Types

Variables are containers for storing data. In C, you must declare variables before using them.

Basic data types in C:
- `int` - Integer numbers (e.g., 42, -10)
- `float` - Floating-point numbers (e.g., 3.14, -2.5)
- `double` - Double-precision floating-point numbers
- `char` - Single characters (e.g., 'A', 'z')
- `void` - Represents absence of type

Example:
```c
int age = 25;
float height = 5.9;
char grade = 'A';
```

Input and Output

C provides several functions for input and output operations:

`printf()` - Print formatted output
`scanf()` - Read formatted input

Example:
```c
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}
```

Format specifiers:
- `%d` - Integer
- `%f` - Float
- `%c` - Character
- `%s` - String

Operators

C provides various operators for performing operations:

Arithmetic operators: +, -, *, /, %
Assignment operators: =, +=, -=, *=, /=
Comparison operators: ==, !=, <, >, <=, >=
Logical operators: &&, ||, !

Example:
```c
int a = 10, b = 3;
int sum = a + b;        // 13
int remainder = a % b;  // 1
int isEqual = (a == b); // 0 (false)
```

Practice Examples

Try these examples in our online C compiler to reinforce your learning:

Simple Calculator

A basic calculator that performs arithmetic operations based on user input.

#include <stdio.h>

int main() {
    float num1, num2, result;
    char operator;
    
    printf("Enter first number: ");
    scanf("%f", &num1);
    
    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);
    
    printf("Enter second number: ");
    scanf("%f", &num2);
    
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if(num2 != 0)
                result = num1 / num2;
            else {
                printf("Error: Division by zero!\n");
                return 1;
            }
            break;
        default:
            printf("Invalid operator!\n");
            return 1;
    }
    
    printf("Result: %.2f\n", result);
    return 0;
}

Temperature Converter

Convert temperatures between Celsius and Fahrenheit scales.

#include <stdio.h>

int main() {
    float celsius, fahrenheit;
    int choice;
    
    printf("Temperature Converter\n");
    printf("1. Celsius to Fahrenheit\n");
    printf("2. Fahrenheit to Celsius\n");
    printf("Enter your choice (1 or 2): ");
    scanf("%d", &choice);
    
    if(choice == 1) {
        printf("Enter temperature in Celsius: ");
        scanf("%f", &celsius);
        fahrenheit = (celsius * 9/5) + 32;
        printf("%.2f°C = %.2f°F\n", celsius, fahrenheit);
    } else if(choice == 2) {
        printf("Enter temperature in Fahrenheit: ");
        scanf("%f", &fahrenheit);
        celsius = (fahrenheit - 32) * 5/9;
        printf("%.2f°F = %.2f°C\n", fahrenheit, celsius);
    } else {
        printf("Invalid choice!\n");
    }
    
    return 0;
}

Ready for the Next Step?

Continue your C programming journey with our next tutorial.

Next: C Functions →