0% found this document useful (0 votes)
27 views

Data Types and Variables in C

Data types define the format for storing and manipulating different types of data in C programming. The main data types are integer (int, short, long), floating point (float, double), character (char), and void. Variables are names given to reserved memory locations used to store and operate on data. Variables must be declared with a data type before use and can be initialized with a starting value. Common types of variables include local, global, static, automatic, and external variables.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Data Types and Variables in C

Data types define the format for storing and manipulating different types of data in C programming. The main data types are integer (int, short, long), floating point (float, double), character (char), and void. Variables are names given to reserved memory locations used to store and operate on data. Variables must be declared with a data type before use and can be initialized with a starting value. Common types of variables include local, global, static, automatic, and external variables.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

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

Valid variable names int a;


int _ab;
int a30;
Invalid variable names:
int 2; // being with a number
int a b; // involve white/blank spaces/
int long; // involve a reserved word long.
Declaring, Defining and Initializing a
variable
• Declaration of variables must be done before they are used
in the program.
• Declaration does the following things.
i. It tells the compiler what the variable name is.
ii. It specifies what type of data the variable will hold.
• Declaration is more like informing the compiler that there
exist a variable with following datatype which is used in the
program.
Defining a variable
• Defining a variable means the compiler has to now assign a
storage to the variable because it will be used in the program.
• To define a variable we must provide the datatype and the
variable name.
• We can even define multiple variables of same datatype in a
single line by using comma (,) to separate them.
• Examples
int a; // one variable
float b, c; // two variables
char a, b, c; // three variables
Initializing a variable
• Initializing a variable means to provide it with a value.
• A variable can be initialized and defined in a single
statement, like:
int a = 10;
• #include <stdio.h>
• // Variable declaration(optional)
• extern int a, b;
• extern int c;
• int main () {
• /* variable definition: */
• int a, b;
• /* actual initialization */
• a = 7;
• b = 14;
• /* using addition operator */
• c = a + b;
• /* display the result */
• printf("Sum is : %d \n", c);
• return 0;
• }
Types of Variables in C
• There are many types of variables in c:
i. local variable
ii. global variable
iii. static variable
iv. automatic variable
v. external variable
Local variable
• A variable that is declared inside the function or block is
called a local variable.
• It must be declared at the start of the block. ( immediately
after the function)
void function1() // a function
{
int x=10; //local variable
}
• You must have to initialize the local variable before it is
used.
Global Variable
• A variable that is declared outside the function or block is called a
global variable.
• Any function can change the value of the global variable.
• It is available to all the functions.
• It must be declared at the start of the block or before the function.
e.g
int value=20; //global variable
void function1() // the function
{
int x=10; //local variable
}
Static Variable
• A variable that is declared with the static keyword is called static
variable.
• It retains its value between multiple function calls.
void function1(){
int x=10; //local variable
static int y=10; //static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
• If you call this function many times, the local variable will print
the same value for each function call, e.g, 11,11,11 and so on.
• But the static variable will print the incremented value in each
function call, e.g. 11, 12, 13 and so on.
#include<stdio.h>
main()
{
void dfe()
{ int x=10;//local variable static int y=10;//static variable x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
}
Automatic Variable
• All variables in C that are declared inside the block, are
automatic variables by default.
• We can explicitly declare an automatic variable
using auto keyword.
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
/* C program to Find Total, Average, and Percentage of Five Subjects */
#include <stdio.h>
int main()
{
int english, chemistry, computers, physics, maths;
float Total, Average, Percentage;
printf("Please Enter the marks of five subjects: \n");
scanf("%d%d%d%d%d", &english, &chemistry, &computers, &physics, &maths);
Total = english + chemistry + computers + physics + maths;
Average = Total / 5;
Percentage= (Total / 500) * 100;
printf("Total Marks = %.2f\n", Total);
printf("Average Marks = %.2f\n", Average);
printf("Percentage Pass= %.2f", Percentage);
return 0;
}

You might also like