🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

C Variables and Data Types — int, float, char, double

C FundamentalsC Variables and Data Types🟢 Free Lesson

C Variables and Data Types — int, float, char, double

Learning Objectives

  • Declare and initialize variables in C
  • Choose the right fundamental data type for a value
  • Understand how much memory common types usually occupy
  • Follow safe naming and initialization habits

What Is a Variable?

A variable is a named storage location in memory that holds a value. You declare a variable by specifying its type and name, then you can assign values and use them in calculations. In C, every variable has a fixed type, and that type determines what kind of data it can store and how it behaves.

int age;
float price;
char grade;
double pi;

These four lines declare four variables. Nothing is stored yet until you assign a value. Declaration tells the compiler how much space to reserve and how to interpret the bits.

Core Data Types

C provides several fundamental types that cover most everyday needs. Integers store whole numbers, floating-point types store decimal numbers, and char stores a single character represented by a small integer code.

TypeTypical UseExample Values
intWhole numbers0, 42, minus 7
floatDecimal numbers3.14, 0.5
doubleHigh precision decimals3.141592653589793
charSingle characterA, 9, question mark

The keyword unsigned can modify integer types to remove negative values and expand the positive range. Most beginners start with plain int for whole numbers.

Declaring and Initializing

You can assign a value at declaration time. This is called initialization, and it is a best practice because using an uninitialized variable leads to unpredictable results.

int score = 100;
float temperature = 98.6;
char initial = 'J';
double balance = 1500.75;

score = score + 5;
printf("%d\n", score);

Notice that character values use single quotes, while strings use double quotes. Integer literals need no special decoration. Floating literals include a decimal point so the compiler treats them as real numbers.

You may declare multiple variables of the same type on one line by separating them with commas, but many style guides prefer one declaration per line for clarity as programs grow.

Format Specifiers for Printing

When you print variables with printf, you match each value with a format specifier. Getting the specifier wrong causes strange output or undefined behavior.

SpecifierType
%dint
%ffloat and double
%cchar
%ldlong integer
%sString, covered in a later lesson
int year = 2026;
float gpa = 3.75;
char level = 'A';

printf("Year: %d\n", year);
printf("GPA: %f\n", gpa);
printf("Level: %c\n", level);

Choosing the Right Size

Exact byte sizes can vary by compiler and platform, but common modern systems use the following pattern. You can check the size of a type on your machine with the sizeof operator, which returns the number of bytes.

printf("int size: %zu bytes\n", sizeof(int));
printf("double size: %zu bytes\n", sizeof(double));

Use int for typical counters and indexes, float when memory is tight and precision is moderate, double for general scientific math, and char for characters or small flags. When you need guaranteed widths, the standard headers provide fixed-size types such as int32_t.

⚠️Watch for Overflow

An int has a largest value. If a calculation exceeds that limit, the number wraps around and silently corrupts your data. Choose a larger type when values can grow beyond normal ranges.

Naming Rules

Variable names must start with a letter or underscore, and they may contain letters, digits, and underscores. Names are case-sensitive, so total and Total are different variables. Keywords such as int and return cannot be used as names.

Good names describe purpose. Prefer count over x, and studentName over tmp. Consistent naming makes debugging dramatically easier for you and anyone who reads your code later.

Key Takeaways

💡

  • Variables are named memory locations with a fixed type
  • int stores whole numbers, float and double store decimals, char stores characters
  • Always initialize variables before reading them
  • Match printf format specifiers to each variable type
  • Use descriptive names and check sizes with sizeof when portability matters

Practice Questions

  1. What is the difference between declaring a variable and initializing it?
  2. Which format specifier would you use to print an int and a char?
  3. Why should variable names describe their purpose instead of using single letters?

Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert C Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement