In the previous article , we talked about how to type a code in C , compile it and run it . In this one I’ll go through Variables and Arithmetic operations .
First , here’s a table of some variable types in C
Type | Number of bytes | Range |
---|---|---|
signed char | 1 | -128 to +127 |
short int | 2 | -32,768 to +32,767 |
int | 4 | -2,147,438,648 to +2,147,438,647 |
long int | 4 | 2,147,438,648 to +2,147,438,647 |
long long int | 8 | 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
float | 4 | ±3.4E38 (6 decimal digits precision) |
double | 8 | ±1.7E308 (15 decimal digits precision) |
long double | 12 | ±1.19E4932 (18 decimal digits precision) |
also there’s an unsigned variable for every variable (eg. unsigned int) , the difference will be in the range (starting from 0) .
there’s also boolean variables which take a value of true or false .
Note : when you try to declare a variable of type boolean it’ll be like this :
- _Bool isTrue = true;
- or you put a header (#include<stdbool.h>) then you declare the variable (bool isTrue= true;)
if we want to know the ranges of the variable types , simply we write this code :
#include<stdio.h>
#include<limits.h>
int main(void){
printf(“the char variable type ranges between %d and %d “,CHAR_MIN , CHAR_MAX);
return 0;
}
let’s examine this code ,
- it included stdio.h , limits.h headers (limits.h has the limits of the variable types)
- it shows the range of char variable type (CHAR_MIN , CHAR_MAX)
the output of running this code will be : the char variable type ranges between -128 and 127
there’s also INT_MIN , INT_MAX , LONG_MIN , LONG_MAX , …
now , I want to make a simple code that print the sum of two numbers , here’s the code :
#include<stdio.h>
int main(void) {
int num1;
int num2;
printf(“enter the first number”);
scanf(“%d”,&num1);
printf(“enter the second number”);
scanf(“%d”,&num2);
printf(“the result is %d \n” , num1+num2);
}
I guess you noticed that there’s a new term “scanf” , this function takes the input from the user and stores it in the variable pointed to “we’ll cover that later” .
there’s also some arithmetic operations , like :
- power “pow(number , power)”
- square roots “sqrt(number)”
- exponential”exp(x)”
- and so on ( just put this header “#include<math.h>” )
Now , with the end of this article I’d like to put a task
make a program to take the input of two numbers and print their sum , difference , multiplication , division , number one power number two and the square root of every one (you can put your code in the comments) .