# Variable

## Fundamental manipulation of variables

Assume to multiply 10 with 3, and to obtain the result 30.

10 x 3 = 30

In order to keep this result "30", variables are very useful. For example, we can use variable "a" as follows.

a = 10 * 3;

This means that result of multiplication of 10 and 3 is input to variable "a". After this procedure, if you refer to variable "a", you can obtain its numerical value, "30". Variable can be regarded as a container which can keep numerical values and characters.

Type below "variable1.c", and save it as "variable1.c". After saving, complie and execute it.

##### variable1.c
```#include <stdio.h>

int main(void) {

int  a;

a = 10 * 3;

printf("a = %d\n", a);

return (0);

}
```

Compilation

\$ gcc variable1.c

Execution

% ./a.out

Executed results

% ./a.out
a = 30

Operator Meaning
+ sum
- subtraction
* multiplication
/ division
% residual

## Type declaration

Type Example of declaration
integer int  a;
real number double  a;
character char  a;
string/th> char  a;

## printf & scanf

Type Example of declaration Output to screen Input from keyboard
integer int  a; printf("%d",a); scanf("%d",&a);
real number double  a; printf("%f",a); scanf("%lf",&a);
character char  a; printf("%c",a); scanf("%c",&a);
string char  a; printf("%s",a); scanf("%s",a);
Exercise: Type and save variable2.c, variable3.c, variable4.c, variable5.c. And then, compile, execute and compare with each result.
##### variable2.c
```#include <stdio.h>

int main(void) {

int a;

printf("Input integer\n");
scanf("%d",&a);

printf("Input integer = %d\n", a);

return (0);

}
```

Execution example (Input (number and string) are expressed in red)

% ./a.out
Input integer
3
Input integer = 3
##### variable3.c
```#include <stdio.h>

int main(void) {

double a;

printf("Input decimal\n");
scanf("%lf",&a);

printf("Input decimal = %f\n", a);

return (0);

}
```
##### variable4.c
```#include <stdio.h>

int main(void) {

int a;

printf("Input decimal =\n");
scanf("%d",&a);

printf("Input decimal = %d\n", a);

return (0);

}
```
##### variable5.c
```#include <stdio.h>

int main(void) {

double a;

printf("Input integer\n");
scanf("%lf",&a);

printf("Input integer = %f\n", a);

return (0);

}
```

Lessons for C programming
Junichi Susaki, Kyoto University