Home »
C programming language
C language data types and operators
In this tutorial, we are going to learn about the various data types and operators with their usages and examples in C programming language.
Data types
Data types are those keywords (identifies – in case of customizing data type), which are used to define the type of the data. They also tell the compiler how much memory to be required for those variables.
Data Types are categorized in the following categories,
Basic & Primitive Data Types
Basic data types are the built-in data types which are arithmetic types,
Data Type |
Required size in Memory |
Range |
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 bytes | 0 to 4,294,967,295 |
float | 4 byte | 1.2E-38 to 3.4E+38 |
double | 8 byte | 2.3E-308 to 1.7E+308 |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 |
User-Defined / Derived / Non Primitive Data Types
Data Types that are derived from the primitive/basic data types are known as user-defined/ derived data types.
Pointer, Array, Structure, Union
void Data Type
void means nothing, it is used when no data type needed. Generally void is used in function parameters and return types when return type or/and the parameter is nothing.
Operators
Operators are special symbols that are used to perform some specific mathematical or logical operations. Operators are also known as C Tokens, these are very useful in programming even real life, and without operators, no one can do programming.
Classification of Operators:
- Assignment Operator
- Arithmetic operators
- Relational Operators
- Logical Operators
- Increments and Decrement Operators
- Conditional Operators
- Bitwise Operators
Assignment Operator
The assignment operator is one of the most important operators in any programming language, it assigns the value of the right-handed expression, variable, and value (constant value) to the left side variable. Remember variable must be on the left side of the assignment operator.
variableName = expression;
int num;
num = 123; /*Will assign 123 to num*/
num = 10+20; /* Will assign the result of expression 10+20 to num*/
Arithmetic operators
The operators used to perform arithmetic operations are called Arithmetic Operators.
+ Addition To add two operands
- Subtraction To subtract two operands
* Multiplication To multiply two operands
/ Divide To divide two operands (returns quotient)
% Modulus To get remainder
Consider the examplem
#include <stdio.h>
int main()
{
int num1, num2;
int result;
float result1;
num1 = 10;
num2 = 3;
result = num1 + num2;
printf("\nAddition of %d and %d is = %d", num1, num2, result);
result = num1 - num2;
printf("\nSubtraction of %d and %d is = %d", num1, num2, result);
result = num1 * num2;
printf("\nMultiplication of %d and %d is = %d", num1, num2, result);
result1 = (float)num1 / (float)num2;
printf("\nQuotient dividing %d by %d is = %f", num1, num2, result1);
result = num1 % num2;
printf("\nRemainder dividing %d by %d is = %d", num1, num2, result);
return 0;
}
Output:
Addition of 10 and 3 is = 13
Subtraction of 10 and 3 is = 7
Multiplication of 10 and 3 is = 30
Quotient dividing 10 by 3 is = 3.333333
Remainder dividing 10 by 3 is = 1
Here, result1=(float)num1/(float)num2; is a float type expression, so we are converting integer type to float type, it is known as Cast Type/Type Conversion.
Relational Operators
These operators are used to compare two values and returns zero (as FALSE) or non zero (as TRUE) values.
Relational operators are,
== Equal To
< Lest Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal to
!= Not Equal To
Consider the example,
#include <stdio.h>
int main()
{
int num1, num2;
num1 = 10;
num2 = 3;
if (num1 == num2)
printf("\n%d and %d are Equal.", num1, num2);
else
printf("\n%d and %d are not Equal.", num1, num2);
if (num1 != num2)
printf("\n%d is not equal to %d.", num1, num2);
else
printf("\n%d is equal to %d.", num1, num2);
if (num1 < num2)
printf("\n%d is less than %d.", num1, num2);
else
printf("\n%d is not less than %d.", num1, num2);
if (num1 > num2)
printf("\n%d is greater than %d.", num1, num2);
else
printf("\n%d is not greater than %d.", num1, num2);
if (num1 <= num2)
printf("\n%d is less than or equal to %d.", num1, num2);
else
printf("\n%d is not less than or equal to %d.", num1, num2);
if (num1 >= num2)
printf("\n%d is greater than or equal to %d.", num1, num2);
else
printf("\n%d is not greater than or equal to %d.", num1, num2);
return 0;
}
Output:
10 and 3 are not Equal.
10 is not equal to 3.
10 is not less than 3.
10 is greater than 3.
10 is not less than or equal to 3.
10 is greater than or equal to 3.
Logical Operators
The operators are used to check more than one condition whether they are true or not. It is very useful when you have multiple conditions and want to make decisions based on their conditions.
Logical operators are,
&&(Logical AND)
Returns TRUE (1), when all conditions are TRUE.
||(Logical OR)
Returns TRUE (1), when at least one condition is TRUE.
!(Logical NOT)
Return TRUE (1) if condition is FALSE(0)
and returns FALSE (0) if condition is TRUE (1)
Consider the example,
#include <stdio.h>
int main()
{
int val = 10;
if ((val >= 10) && (val <= 20))
printf("\nTRUE-1");
else
printf("\nFALSE-1");
if ((val >= 10) || (val <= 20))
printf("\nTRUE-2");
else
printf("\nFALSE-2");
if (!(val >= 10))
printf("\nTRUE-3");
else
printf("\nFALSE-3");
return 0;
}
Output
TRUE-1
TRUE-2
FALSE-3