Assume to multiply 10 with 3, and to obtain the result 30.
In order to keep this result "30", variables are very useful. For example, we can use variable "a" as follows.
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.
#include <stdio.h> int main(void) { int a; a = 10 * 3; printf("a = %d\n", a); return (0); }
Compilation
Execution
Executed results
Operator | Meaning |
---|---|
+ | sum |
- | subtraction |
* | multiplication |
/ | division |
% | residual |
Type | Example of declaration |
---|---|
integer | int a; |
real number | double a; |
character | char a; |
string/th> | char a[30]; |
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[20]; | printf("%s",a); | scanf("%s",a); |
#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)
#include <stdio.h> int main(void) { double a; printf("Input decimal\n"); scanf("%lf",&a); printf("Input decimal = %f\n", a); return (0); }
#include <stdio.h> int main(void) { int a; printf("Input decimal =\n"); scanf("%d",&a); printf("Input decimal = %d\n", a); return (0); }
#include <stdio.h> int main(void) { double a; printf("Input integer\n"); scanf("%lf",&a); printf("Input integer = %f\n", a); return (0); }