Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C Variables, Data Types, and Constants – A Practical Guide

What is a Variable?

A variable is an identifier that holds a value. Unlike constants, a variable’s value can change during program execution.

Variables can be referenced from multiple places in a program. A descriptive name helps readers understand the variable’s purpose.

int height = 170; // height in centimeters
int age = 29; // age in years

Before using a variable, it must be declared. Variable names may contain letters, digits, and underscores, but must start with a letter or underscore.

Variable Naming Rules

  1. Only letters (a–z, A–Z), digits (0–9), and underscore (_).
  2. Cannot start with a digit.
  3. No whitespace.
  4. Cannot be a reserved keyword.
  5. C is case‑sensitive: age and AGE are distinct identifiers.

Valid and Invalid Names

Valid:

height
_HEIGHT
_height1
My_name

Invalid:

1height
Hei$ght
My name

Declaring and Initializing Variables

Declare first, then assign:

int my_variable;
my_variable = 48;

Or combine declaration and initialization:

int my_variable = 48;

Data Types Overview

C offers three categories of data types:

  1. Primitive (int, char, float, double, void)
  2. Derived (arrays, functions, pointers, structures)
  3. User‑defined (typedef, structs, enums, unions)

Primitive Data Types

Five fundamental types:

  1. int – integer values
  2. char – single characters
  3. float – single‑precision floating point
  4. double – double‑precision floating point
  5. void – no value (used in function signatures)

Standard Data Type Sizes and Ranges

Data typeSize (bytes)Range
char / signed char1-128 to 127
unsigned char10 to 255
int / signed int2-32768 to 32767
unsigned int20 to 65535
short int / unsigned short int20 to 65535
signed short int2-32768 to 32767
long int / signed long int4-2147483648 to 2147483647
unsigned long int40 to 4294967295
float4±3.4E-38 to ±3.4E+38
double8±1.7E-308 to ±1.7E+308
long double10±3.4E-4932 to ±1.1E+4932

Note: C does not have a built‑in Boolean type; use int with 0 (false) or 1 (true) or the stdbool.h header for bool.

Integer Data Types

Integers are whole numbers. The standard 16‑bit int ranges from –32768 to 32767. Shorter and longer variants are available to suit the required range.

Typical declarations:

int age;
short int smallCount;
long int largeCount;

Floating‑Point Data Types

Use float for values with fractional parts up to about 6‑digit precision. double offers about 14‑digit precision, and long double provides the most precision.

Examples:

float ratio = 0.75f;
double balance = 12345.6789;
long double pi = 3.141592653589793238462643383279502884L;

Character Data Type

Stores a single character in a one‑byte value. Characters are specified in single quotes.

Example:

char letter = 'K';

Void Data Type

Indicates that a function does not return a value.

Example:

void displayData(void) {
    // ...
}

Declaring Multiple Variables

Variables of the same type can be declared on a single line, separated by commas:

int x, y, z;
float salary = 13.48f;
char letter = 'K';

Print examples use format specifiers:

printf("%d\n", z);
printf("%f\n", salary);
printf("%c\n", letter);

Constants

A constant value never changes during program execution. C provides several forms:

Integer Constants

Decimal, octal (prefixed with 0), and hexadecimal (prefixed with 0x) literals.

Examples:

111          // decimal
012          // octal (10 decimal)
0x2BCD       // hexadecimal (19293 decimal)

Character Constants

Single character surrounded by single quotes or by its ASCII code.

Examples:

'A'
'9'

String Constants

Sequence of characters enclosed in double quotes.

Examples:

"Hello"
"Programming"

Real (Floating‑Point) Constants

Numbers with a decimal point or in scientific notation.

Examples:

202.15
300.00
1.23e-4   // 0.000123

Defining Constants with const and #define

Using const reserves storage and enforces immutability:

#include <stdio.h>
int main(void) {
    const double PI = 3.141592653589793;
    printf("%f\n", PI);
    return 0;
}

Using #define is a pre‑processor macro that does not consume storage:

#include <stdio.h>
#define PI 3.141592653589793
int main(void) {
    printf("%f\n", PI);
    return 0;
}

Summary

C Language

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Understanding Variable Scope in C#: Class, Method, and Block Levels
  3. Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
  4. C Variables, Constants, and Literals: A Complete Guide
  5. Java Primitive Data Types: A Clear Guide
  6. Understanding C Data Types: A Comprehensive Guide
  7. Understanding Variables in C: Types, Naming Rules, and Memory Management
  8. Understanding C Constants and Literals: Types, Usage, and Best Practices
  9. Mastering C Pointers: Practical Steps to Advanced Programming
  10. Unlocking Sinumerik 840D: Master $TC_DP Tool Data Variables for Precise CNC Programming