C

C Programming

Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises

Variables and Constants in C

Variables are storage locations with an associated name that can contain data. Constants are fixed values that cannot be changed during program execution.

Variables

A variable is a name given to a memory location where data is stored. Variables must be declared before use.

Variable Declaration and Initialization
#include <stdio.h>

int main() {
    // Variable declaration
    int age;
    float salary;
    char grade;
    
    // Variable initialization
    age = 25;
    salary = 50000.50;
    grade = 'A';
    
    // Declaration and initialization together
    int count = 10;
    double pi = 3.14159;
    
    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    
    return 0;
}

Variable Naming Rules

Good Practice
  • Use descriptive names: studentAge instead of a
  • Use camelCase: firstName
  • Use underscores: first_name
Avoid
  • Single letter names (except for loops)
  • Starting with numbers: 2name
  • Using keywords: int, float

Constants

Constants are fixed values that cannot be modified during program execution. There are several ways to define constants in C:

Different Types of Constants
#include <stdio.h>
#define PI 3.14159  // Preprocessor constant

int main() {
    // const keyword
    const int MAX_SIZE = 100;
    const float GRAVITY = 9.8;
    
    // Literal constants
    int number = 42;        // Integer literal
    float price = 19.99;    // Float literal
    char letter = 'X';      // Character literal
    char name[] = "John";   // String literal
    
    printf("PI: %.5f\n", PI);
    printf("Max Size: %d\n", MAX_SIZE);
    printf("Gravity: %.1f\n", GRAVITY);
    
    return 0;
}
Tip

Use #define for compile-time constants and const for runtime constants. Constants are typically written in UPPERCASE.

Explanation

  • Variables: Named memory locations with a specific type; declare before use and initialize to avoid undefined values.
  • Constants: Values that do not change; use const for typed, scoped constants and #define for macros.
  • Why it matters: Clear variable names and appropriate constants make code safer and easier to maintain.
  • Common pitfalls: Modifying a const value, mixing #define macros with typed constants, or using vague names.
Keywords
C variables, constants, const, #define, initialization, naming
Quick Tips
  • Prefer const for type safety and scope; reserve #define for simple symbolic values.
  • Use UPPERCASE names for macros (e.g., MAX_SIZE) to distinguish from variables.
  • Initialize variables at declaration when possible to avoid undefined behavior.

Frequently Asked Questions

C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.

No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.

You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.

The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.

C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.

Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.