Variables in C

Variables in C are names that can hold a value of a given type. Once created, the type of the variable cannot be changed.

Variables are placed in the computer memory. When you give that variable a value, its place in the memory saves the value. Later you can change or read that value, using the name of the variable.

Advertise on this site. I promise you will like the rates :)

C - variables in the computer memory

Declaring variables in C

The term declaration means that we tell the compiler that a certain variable exists. To do that, you need to specify its type and name. Let's create one variable for each type from the previous lesson:

char symbol;
int count;
float price;
double amount;

If you want to declare multiple variables of the same type, you can do that in one row. Specify the data type and then list the variables' names, separating them with commas:

int pin1, pin2, pin3, pin4;

Naming

Allowed characters

For the names of your variables you can use letters(both uppercase and lowercase), digits(0..9) and underscore ( _ ):

  • A..Z
  • a..z
  • 0..9
  • _

Restrictions

  • The names of the variables in C cannot start with a digit.
  • The names cannot be the same as a reserved word of the language. Reserved words are special words that have a meaning to the language or are reserved for future expansion of the language. Examples are the keywords that you already know – int, char, void, float, return and others.
  • C is case sensitive, so you can name a variable CHAR or Return

Good naming practices

The name of the variable has to make it obvious what information it holds. Choosing obscure names for the variables is a bad habit that most beginners have. Names like a,b,c,asd and so on are bad names. Reading a well written code should be almost like reading a book and not like deciphering an encrypted message.

Don't use toooooooo long names like

int numberOfProgrammersInTheCurrentCompany;

For longer names use underscores or “camelCase”, where each next word begins with a capital letter:

char first_symbol;
float minPrice;

Assigning a value and printing

To give your variable a value place it on the left of the assignment operator “=” and put the value on the right side.

myVar = 7;

You can do the assignment on immediately when you create the variable

double pi = 3.14;

or later in the code

float minPrice;
...
minPrice = 0.99f;

You can also assign it to the value of another variable:

float newPrice = minPrice;

To output the values of variables in C, we will use the printf function. You already know about it, from the previous lessons. To recall, it uses a format string and arguments. Until now we used constant values as arguments. Now we will use the name of the variables that we want to display.

printf("That computer costs %f", computerPrice);
printf("Your curreny is %c", userCurrency);

Assigning values to variables in C, using scanf

We can use the scanf function to read information. Just like printf, scanf uses a format string and arguments. The format string contains format specifiers that tell what kind of data will be read. The number of specifiers must match the number of arguments. Also, their sequence must be the same – the first argument is for the first specifier and so on.

Finally, the data type of the variable must be compatible with the information that we will read.

int sideA, sideB;
scanf("%d", &sideA);
scanf("%d", &sideB);

Note the & symbol, before the name of the variable. This symbol means that we take the memory address of the variable and not its actual value. Remember – the scanf function needs the address of the variable, not its value!

Let's create a simple echo program. It will read a symbol from the keyboard and then print it back on the screen:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
	char symbol;
	printf("Please, input a symbol and then press Enter:");
	scanf("%c", &symbol);
	printf("You entered the symbol %c", symbol);
	
	return 0;
}

Once you build and execute this, you will be prompted to input a symbol. When the program reaches the scanf function it pauses and waits until you press enter. Then it will read the symbol and the program will continue with the next line.

Remember from the last lesson, that data in C is very flexible. We can interpret the character as integers and this way we will find the ASCII code of a given symbol. Let's modify the above program and make it more useful:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
	char symbol;
	printf("Please, input a symbol and then press Enter:");
	scanf("%c", &symbol);
	printf("You entered the symbol %c. Its code is %d", symbol, symbol);
	
	return 0;
}

See? The second time the argument “symbol” will be interpreted as a number. If you still haven't, compile and test the above program. It could be useful if you need to quickly find the ASCII code of a symbol.

Printing the ASCII code of a char variable in C

Previous: C data types

Next: Operators

   Search this site: