Data Types in C
*******************************************************
Variables
*******************************************************
Variables
- If you declare a variable in C (later on we talk about how to do this), you ask the operating system for a piece of memory.
- This piece of memory you give a name and you can store something in that piece of memory (for later use).
- There are two basic kinds of variables in C which are numeric and character.
1. Numeric variables
- Numeric variables can either be of the type integer (int) or of the type real (float).
- Integer (int) values are whole numbers (like 10 or -10). Real (float) values can have a decimal point in them. (Like 1.23 or -20.123).
2. Character variables
- Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If you declare a character variable you must always put the character between single quotes (like so ‘A’ ).
- So remember a number without single quotes is not the same as a character with single quotes.
Constants
- The distinction between variables and constants is that variables can at any time change their value, but constants can never change their value. (The value of constants is locked during program duration).
- Constants can be very useful, Pi for instance is a good example to declare as a constant.
Data Types
- So you now know that there are three types of variables: numeric – integer, numeric-real and character.
- A variable has a type-name, a type and a range (minimum / maximum). In the following table you can see the type-name, type and range:
Data Types
Declaring
- So we now know different type-names and types of variables, but how do we declare them. Declaring a variable is very easy.
- First you have to declare the type-name. After the type-name you place the name of the variable.
- The name of a variable can be anything you like as long it includes only letters, underscores or numbers (However you cannot start the name with a number).
- But remember choose the names wisely.
- It is easier if a variable name reflects the use of that variable. (For instance: if you name a float PI, you always know what it means).
Now let’s declare some variables,
- A variable MyIntegerVariable and MyCharacterVariable:
int main()
{
int MyIntegerVariable;
int MyCharacterVariable;
return 0;
}
- It is possible to declare more than one variable at the same time:
int main()
{
int Variable1, Variable2, Variable3;
int abc, def, ghi;
return 0;
}
- To declare a constant is not much different then declaring a variable. The only difference is that you have the word const in front of it:
int main()
{
const float PI = 3.14;
char = 'A';
return 0;
}
- Note: As you can see, you can assign a value with the equal sign during declaration.
Signed and unsigned variables
- The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive.
- By using an unsigned variable you can increase the maximum positive range.
- When you declare a variable in the normal way it is automatically a signed variable.
- You simply put the word unsigned before your variable declaration or signed for a signed variable to declare an unsigned variable, although there is no reason to declare a variable as already signed.
int main()
{
unsigned int MyOnlyPositiveVar;
signed int MyNegativeAndPositiveVar;
int MyNegativeAndPositiveVar;
}
Calculations and variables
- There are different operators that can be used for calculations which are listed in the following table:
int main()
{
int a, b;
a = 1;
b = a + 1;
a = b - 1;
return 0;
}
Reading and printing
- Calculating something without reading input or printing something on the screen is not much fun. To read input from the keyboard we will use the command scanf. (How to print something to the screen we all ready know).
- So let’s make a program that can do all these things:
#include<stdio.h>
int main()
{
int inputvalue;
scanf("%d", &inputvalue);
inputvalue = inputvalue * 10;
printf("Ten times the input equals %d\n",inputvalue);
return 0;
}
1. Numeric variables
2. Character variables
- Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If you declare a character variable you must always put the character between single quotes (like so ‘A’ ).
- So remember a number without single quotes is not the same as a character with single quotes.
Constants
- The distinction between variables and constants is that variables can at any time change their value, but constants can never change their value. (The value of constants is locked during program duration).
- Constants can be very useful, Pi for instance is a good example to declare as a constant.
Data Types
- So you now know that there are three types of variables: numeric – integer, numeric-real and character.
- A variable has a type-name, a type and a range (minimum / maximum). In the following table you can see the type-name, type and range:
Data Types |
- So we now know different type-names and types of variables, but how do we declare them. Declaring a variable is very easy.
- First you have to declare the type-name. After the type-name you place the name of the variable.
- The name of a variable can be anything you like as long it includes only letters, underscores or numbers (However you cannot start the name with a number).
- But remember choose the names wisely.
- It is easier if a variable name reflects the use of that variable. (For instance: if you name a float PI, you always know what it means).
Now let’s declare some variables,
- A variable MyIntegerVariable and MyCharacterVariable:
int main()
{
int MyIntegerVariable;
int MyCharacterVariable;
return 0;
}
- It is possible to declare more than one variable at the same time:
int main()
{
int Variable1, Variable2, Variable3;
int abc, def, ghi;
return 0;
}
- To declare a constant is not much different then declaring a variable. The only difference is that you have the word const in front of it:
int main()
{
const float PI = 3.14;
char = 'A';
return 0;
}
- Note: As you can see, you can assign a value with the equal sign during declaration.
Signed and unsigned variables
- The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive.
- By using an unsigned variable you can increase the maximum positive range.
- When you declare a variable in the normal way it is automatically a signed variable.
- You simply put the word unsigned before your variable declaration or signed for a signed variable to declare an unsigned variable, although there is no reason to declare a variable as already signed.
int main()
{
unsigned int MyOnlyPositiveVar;
signed int MyNegativeAndPositiveVar;
int MyNegativeAndPositiveVar;
}
Calculations and variables
- There are different operators that can be used for calculations which are listed in the following table:
int main()
{
{
int a, b;
a = 1;
b = a + 1;
a = b - 1;
return 0;
}
Reading and printing
- Calculating something without reading input or printing something on the screen is not much fun. To read input from the keyboard we will use the command scanf. (How to print something to the screen we all ready know).
- So let’s make a program that can do all these things:
#include<stdio.h>
int main()
{
int inputvalue;
scanf("%d", &inputvalue);
inputvalue = inputvalue * 10;
printf("Ten times the input equals %d\n",inputvalue);
return 0;
}
OR
- A data type specifies the type of data that a variable can store such as integer, floating, character etc.
There are 4 types of data types in C language.
- The basic data types are integer-based and floating-point based. C language
supports both signed and unsigned literals.
Basic Data Types
- The memory size of basic data types may change according to 32 or 64 bit operating system.
- Let's see the basic data types. It size is given according to 32 bit OS.
Tags:
best c programming
c by mskuthar
C Programming Language
Data Types in C
janta polytechnic jahangirabad bulandshahr
manncsemanjeet
upbte c programming syllabus