Thursday, December 8, 2022

Variables

Introduction to Variables

You can think of variables as containers to store data values. For example, you store food in containers to keep it condensed and save it for later.



There are different variable types in C. The following are a few examples of commonly used variable keywords:

int - storing integers, such as 4 or -57

float - stores floating point numbers, with decimals, such as 4.5 or -57.2

char - stores single characters, such as 'x' or 'y'
char values are always in single quotes

Declaring Variables

In order to create a variable, you have to specify the type of variable and assign the type a value:

Syntax:

type variableName = value;

  • You can name your variable whatever you would like!

Example 1.1

char myChar = 'C'; //my character is C

Example 1.2

int myInt = 32; //my integer is 32

Example 1.3

float myFloat = 32.5; //my floating point number is 32.5

Declaring a variable without assigning the value straightaway is another useful practice.

Example 1.4

int myInt; //myInt is the name of my variable
myInt = 20; //my integer is 20

You can also overwrite previous values with this function.

Example 1.5

int myInt = 32; //my integer is 32
myInt = 20; //my integer is now 20

Output Variables and Format Specifiers

C is different from other coding languages like Python because you cannot use a print function to display the valuable of a variable.

Example 2.1

int myInt = 32;
printf(myInt); //no output

C has a workaround for this called "format specifiers".

Format Specifiers are used with the print function, printf(),
and they act as a placeholder for variable values.

A format specifier starts with a % sign, followed by a character.

For the value of an int variable in C, you must use "%d" or "%i" including the double quotes inside the parentheses of the printf() function.

Example 2.2

int myInt = 32;
printf("%d", myInt); //Prints 32

For the value of a char variable in C, you must use "%c" including the double quotes inside the parentheses of the printf() function.

Example 2.3

char myChar = 'A'; //my character is A
printf("%c", myChar); //Prints A

For the value of a float variable in C, you must use "%f" including the double quotes inside the parentheses of the printf() function.

Example 2.4

float myFloat = 32.5; //my floating point number is 32.5
printf("%f", myFloat); //Prints 32.5

You can use the print feature alongside format specifiers to combine both variables and text.

Example 2.5

int myInt = 5;
printf("The lucky number is: %d", myInt); //Prints "The lucky number is: 5"

Another useful function is printing different types in a single output.

Example 2.6

int myInt = 8;
char myChar = 'Z';
printf("My favorite number is %d and my favorite letter is %c", myInt); //Prints "My favorite number is 8 and my favorite letter is Z"

Adding Multiple Variables

Adding variables is a good first look into the potential of advanced mathematics in coding languages. To add variables in C you can use the + operator.

Example 3.1

int x = 8;
int y = 2;
int sum = x + y;
printf("The sum of x and y is %d", sum); //Prints "The Sum of x and y is 10"


Declaring Multiple Variables is a more streamlined approach to achieve the same result. You can do this my using a comma-seperated list.

Example 3.2

int x = 8, y = 2, z = 10;
printf("The sum of x, y, and z is %d", x + y + z); //Prints "The Sum of x, y, and z is 20"


You are also able to assign the same value to multiple variables in C. This can be helpful to multiply an integer or float.

Example 3.3

int x, y, z;
x = y = z = 420;
printf("420 three times is %d", x + y + z); //Prints "420 three times is 1260)

Test Your Variable Knowledge

Create a Variable called x with a value of 60 and add it with another variable y with a value of 60.

Possible Answers

int x = 60;
int y = 60;
int sum = x + y;
printf("The sum of x and y is %d", sum); //Prints "The Sum of x and y is 120"

int x = 60, y = 60;
printf("The sum of x, y, and z is %d", x + y + z); //Prints "The Sum of x, y, and z is 120"

Most Streamlined Answer

int x, y;
x = y = 60;
printf("60 two times is %d", x + y); //Prints "60 two times is 120)


Hope you enjoyed learning about C and the applications of variables :)






No comments:

Post a Comment