Data Types and Variables in C
Data Types and Variables in C
DATA TYPES
What is data type?
• C data types are defined as the data storage format that a
variable can store a data to perform a specific operation.
• Data types are used to define a variable before to use in a
program.
• Size of variable, constant and array are determined by data
types.
• Creating a variable for storing single and multiple values in a program
as part of performing one operation.
• In programming, to perform any operation first we must store values.
• To store values we must allocate memory. Here we need to provide
the information of the number of bytes and type of memory that
must be allocated to store a value.
• To specify the number of bytes and memory types to the compiler
and operating system.
• To do this, we must use some set of keywords.
• These sets of keywords are collectively called data types.
• Hence we can say data type is used for allocating memory for storing
the value in a program by specifying the required numbers of bytes
and memory type.
• In C language, data types can be classified into three parts:-
i. Primitive/Basic data types
ii. Derived data types
iii. User-defined data types
Primitive Data Types
Integer data type
• The keyword int is used to declare integer data type.
• If integer data is small then we can use keyword short or short int
and to store long integer number we can use long or long int
keyword and to store very long number we can use long long or long
long int keyword.
#include<stdio.h>
int main ()
{
short a = 10; //short data type
short int a1 = 20;
int a2 = 1000; //int data type
long a3 = 100000000; //long data type
long int a4 = 200000000;
long long a5 = 500000000000;//long long data type
long long int a6 = 900000000000;
printf("%d\n",a);
printf("%d\n",a1);
printf("%d\n",a2);
printf("%ld\n",a3);
printf("%ld\n",a4);
printf("%lld\n",a5);
printf("%lld\n",a6);
return 0;
}
Data Type Specifiers
• In C programming we need lots of format specifier to work with
various data types.
• Format specifiers defines the type of data to be printed on standard
output.
• Whether to print formatted output or to take formatted input we
need format specifiers.
• Format specifiers are also called as format string.
Character Data Type
• In C language, to store character data types keyword char is used.
• For character type variables and single-character constants, 1 byte (8
bits) of memory space is allocated.
• It may be observed that small int value may be stored in char
variables and char values may be stored in int variables.
• The range of char data type is 0 to 255 for unsigned char and -128 to
127 for signed char.
• Example of char data types:- ‘m’, ‘A’, ‘5’, ‘@’, ‘?’ e.t.c. Note that all
are inside the single quotation.
• “a” is not a char data type because it is inside double quotation not in
the single quotation.
• ‘abc’ is not a char data type because inside a single quotation only
one character must be present.
• ‘’ is not a char data type, a blank single quotation is not a valid
character.
• ‘ ’ is a char data type, space is inside a single quotation and space is a
valid character.
• m is not a char data type, it is a variable because it is not inside a
single quotation.
#include<stdio.h>
int main ()
{
char ch = 'A';
char gender='M';
char number='5';
printf("ch = %c\n",ch);
printf("Gender = %c\n",gender);
printf("Number = %c\n",number);
// display these character value into decimal use %d
printf("Decimal value of Gender = %d\n",gender);
printf("Decimal value of Number = %d\n",number);
return 0;}
• It is classified under the integral data type as the storage occurs in the
form of ASCII values which are used to represent every character.
• Thus ‘a’ has the decimal value 97.
• There are two variations of char data type: unsigned char data type
and signed char data type.
Examples of Signed and Unsigned Characters
// signed characters
#include<stdio.h>
main ()
{
int char = -1;
unsigned char i = char;
printf(“Unsigned char: %c\n”, i);
return 0;
}
Examples of Signed and Unsigned Characters
// unsigned characters
#include<stdio.h>
main ()
{
int char =97;
unsigned char i = char;
printf(“Unsigned char: %c\n”, i);
return 0;
}
Floating point data type
• By default, every floating-point number is treated as a double data
type.
• Float and long double data type are also used for floating-point.
• Sometimes every real-world value can’t be stored using integer,
floating-point values are required.
• For example to calculate the weight of person floating-point values
are required.
Void data type
• As the name indicates this type has no values.
• Most of the time it is used to indicate that a function does not return
any value.
• All other primitive data types short, int, long int, float, double and
long double can be used for both calculation (like storing values to a
variable) and returning from a function but void can only be used for
returning from a function.
• Void can’t be used for storing and calculation in a program.
Bool Data type
• With the release of C99, the C language incorporated a Boolean type.
Boolean types represent only two values: true or false.
• Prior to C99, C used integers to represent the Boolean values: a
nonzero number (both positive or negative) was used to represent
true, and zero was used to represent false.
• The boolean type, which is referred to by the keyword bool, is stored
in memory as 0 (false) and 1(true)
• Don’t forget to add the header file “stdbool.h” otherwise you will get
a compile-time error.
• The keyword bool, true and false are defined inside header file
“stdbool.h”
#include<stdio.h>
#include<stdbool.h>
int main()
{
bool x = true;
bool y = false;
printf("%d\t", x);
printf("%d\n", y);
return 0;
}
VARIABLES
Introduction
• When we want to store any information(data) on our
computer/laptop, we store it in the computer's memory
space.
• Similarly, in C language, when we want to use some
data value in our program, we can store it in a memory
space and name the memory space so that it becomes
easier to access it.
• The naming of an address in a computer memory is
known as variable.
What is a variable?
• A variable is a name of the memory location used to
store data.
• Unlike constant, variables are changeable, we can
change value of a variable during execution of a
program.
• A programmer can choose a meaningful variable name.
• Example : average, height, age, total etc.
• It is a way to represent memory location through
symbols so that it can be easily identified.
Variables in C
Syntax: data type variable list;
A variable in C language must be given a type, which
defines what type of data the variable will hold and the
variable.
Example
int a;
float b;
char c;
•Here, a, b, c are variables(variable names).
•The int, float, char are the data types.
Variables in C
• We can also provide values while declaring the variables
as given below:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8; ;//declaring 1 variable of float type
char c='A’; ;//declaring 1 variable of character type
• We call this initialization.
• The declaration of variables is ended with a ; sign while
declaring a character involves ‘ ’signs and a string
includes “ “. i.e char ("Hello,World");
Rules for defining variables
i. A variable can have alphabets, digits, and underscore.
ii. A variable name can start with the alphabet, and
underscore only. It can't start with a digit.
iii. No blank space or whitespace is allowed within the
variable name.
iv. A variable name must not be any reserved word or
keyword, e.g. int, float, etc.
v. Upper and lower case names are treated as
different, as C is case-sensitive, so it is suggested to
keep the variable names in lower case.
Valid and invalid variable names