Sample


[br]3 [br]Complex programs can't be made only of actions, but they also need a way to <strong>memorise data</strong>. <strong>Variables</strong> are the elements that perform this task. <strong>A variable is a box of a certain type that can contain only that type of information.</strong> An integral variable can only contain integers. <strong>Creating a variable</strong> is simple: [crayon-5daf6e082d354767300629/] Example [crayon-5daf6e082d36a698529898/] <ul> <li><strong>int</strong> is the type of the variable named number and it tells the computer that this variable can only contain whole numbers.</li> <li>the variable's name is used to get or set the variable's value.</li> </ul> You can also give a variable an <strong>initial value</strong>: [crayon-5daf6e082d36e556655707/] <strong>To get the value of a variable</strong> - <em>e.g. to print it or to use it in an operation</em> - you can simply use its name: [crayon-5daf6e082d373028552967/] You can <strong>set the value</strong> of a variable with an assignment (<i>variable = value</i>): [crayon-5daf6e082d379365447298/] Using variables, you can <strong>print and read values</strong> to/from the terminal. <code>printf</code> -for printing - and <code>scanf</code> -for reading- <strong>replace some special characters</strong> between quotes <strong>with variables</strong> specified outside the quotes. The <code>%d</code> character is replaced with a variable of type <strong>int</strong>, so that <code>printf</code> prints the variable's value and <code>scanf</code> assign the value read to that variable. In <code>scanf</code> the name of the variable must come after the <code>&</code> symbol, while in <code>printf</code> it mustn't. [br]https://itszephyro.com/blog/learn-c/variables/[br]#include <stdio.h> int main() { int var = 10; printf("Initial value of var: %d\n", var); printf("Enter a new value: "); scanf("%d", &var); printf("New value of var: %d", var); } [br]

[br]3
[br]Complex programs can’t be made only of actions, but they also need a way to memorise data.

Variables are the elements that perform this task.

A variable is a box of a certain type that can contain only that type of information.

An integral variable can only contain integers.

Creating a variable is simple:

Example

  • int is the type of the variable named number and it tells the computer that this variable can only contain whole numbers.
  • the variable’s name is used to get or set the variable’s value.

You can also give a variable an initial value:

To get the value of a variablee.g. to print it or to use it in an operation – you can simply use its name:

You can set the value of a variable with an assignment (variable = value):

Using variables, you can print and read values to/from the terminal.

printf -for printing – and scanf -for reading- replace some special characters between quotes with variables specified outside the quotes.

The %d character is replaced with a variable of type int, so that printf prints the variable’s value and scanf assign the value read to that variable.

In scanf the name of the variable must come after the & symbol, while in printf it mustn’t.
[br]https://itszephyro.com/blog/learn-c/variables/[br]#include

int main() {
int var = 10;
printf(“Initial value of var: %d\n”, var);
printf(“Enter a new value: “);
scanf(“%d”, &var);
printf(“New value of var: %d”, var);
}
[br]

To learn more, visit: