C Variables

What Is a Variable?

A variable in C stores a value in memory.

It must be declared with a data type before it's used.

Loading...
Output:

Declaring and Initializing Variables

You can declare and assign a variable in one line or separately:

Loading...
Output:

Common Data Types

C supports different types of variables. Here are three common ones:

  • int: integers
  • float: decimal numbers
  • char: characters
Loading...
Output:

Multiple Declarations

You can declare multiple variables of the same type on a single line:

Loading...
Output:

Constants

Use const to create a variable whose value cannot change:

Loading...
Output:

Naming Rules

Variable names:

  • Must start with a letter or underscore
  • Are case-sensitive (totalTotal)
  • Cannot be a C keyword like int or return
Loading...
Output:

Format Specifiers

Format specifiers tell printf() how to display the variable:

  • %d – integer
  • %f – float
  • %.2f – float with 2 decimal places
  • %c – character
Loading...
Output:

Practice Question

Which of the following is invalid? Ask the AI chatbot to see if your answer is correct.

A. int speed = 100;
B. const float pi = 3.14;
C. int 1stPlace = 5;

Hint: Variable names can’t begin with a number.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic