Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

3/1/2023

Data Types, Formats, Type Data Types


Conversions, Variables, The type of a variable determines how much space
it occupies in storage and how the bit pattern
Constants & Storage Classes stored is interpreted.
Classification:
1) Basic Types: They are arithmetic types and
consists of the two types:
(a) integer types
(b) floating-point types.
2) Enumerated types: They are again arithmetic
types and they are used to define variables that
can only be assigned certain discrete integer
values throughout the program.

Data Types Integer Types


3) The type void: Type Storage Value range Format
size
void indicates that no value is available
unsigned char 1 byte 0 to 255(2^8 -1) %c
signed char 1 byte -128 to 127(-2^7 to 2^7 -1) %c
4) Derived types: unsigned short 2 bytes 0 to 65,535(2^16 -1) %u
(a) Pointer types int
(b) Array types signed short 2 bytes -32,768 to 32,767(-2^15 to %d
int 2^15 -1)
(c) Structure types
(d) Union types signed long int 4 bytes -2,147,483,648 to %ld
2,147,483,647
(e) Function types.
unsigned long 4 bytes 0 to 4,294,967,295 %lu
int

Sizeof operator Floating--Point Types


Floating
The expressions sizeof(type) yields the storage Type Storage Value range Precision
size of the object or type in bytes. size
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
printf("Storage size for int : %d \n",
double 8 byte 2.3E-308 to 1.7E+308 15 decimal
sizeof(int));
places
o/p: 4 long double 10 byte 3.4E-4932 to 19 decimal
1.1E+4932 places

1
3/1/2023

Enumerated Types Enumerated Types(Cont.)


They are user defined types. #include< stdio.h>
Syntax: enum identifier {value1, value2,.... void main()
Value n}; {
Eg. enum month {JAN,FEB,MAR,APR}; int i;
enum month rmonth; enum month {JAN,FEB,MAR,APR};
rmonth=FEB; clrscr();
printf("%d“,rmonth); for(i=JAN;i<=APR;i++)
o/p: 1 printf("\n%d“,i);
Default Numeric value assigned to first enum }
value is “0”.

The void Type The void Type(Cont.)


The void type specifies that no value is 3)Pointers to void:
available. It is used in three kinds of It is General Purpose Pointer.
situations: It does not have any data type associated with
it.
1) Function returns as void: A function with no
It can store address of any type of variable
return value has the return type as void.
Declaration of Void Pointer :
eg. void exit (int status); void * pointer_name;
2) Function arguments as void: A function with Example:
no parameter can accept as a void. void *ptr; // ptr is declared as Void pointer
eg. int rand(void); char c;
int i;
ptr = &c; // ptr has address of character data
ptr = &i; // ptr has address of integer data

The void Type(Cont.) Variables


char *cptr; // cptr is declared as char pointer A variable is nothing but a name given to a
char c; storage area that our programs can manipulate.
int i; Each variable in C has a specific type, which
determines the size and layout of the variable's
cptr = &c; // cptr has address of character data
memory; the range of values that can be stored
cptr = &i; // error within that memory; and the set of operations
that can be applied to the variable.
The name of a variable can be composed of
letters, digits, and the underscore character.
It must begin with either a letter or an
underscore.
Upper and lowercase letters are distinct because
C is case-sensitive.

2
3/1/2023

Variable Definition Variable initialization


Syntax: type variable_list; int d = 3, f = 5; // definition and initializing
Eg. int i, j, k; d and f.
char c, ch; char x = 'x'; // the variable x has the value
'x'.
float f, salary;
variables with static storage duration are
double d;
implicitly initialized with NULL (all bytes have
the value 0); the initial value of all other
variables is undefined.
d 3

Lvalues and Rvalues


Static variables have a fixed memory location and 1) lvalue : Expressions that refer to a memory
memory is allocated in compile time. location is called "lvalue" expression. An lvalue
Local variables are stored in stack and they do may appear as either the left-hand or right-hand
not have a fixed memory location. side of an assignment.
Memory is allocated to local variables 2) rvalue : The term rvalue refers to a data value
at runtime but not at compile time. hence local that is stored at some address in memory. An
variables have their default value as garbage. rvalue is an expression that cannot have a value
assigned to it which means an rvalue may appear
on the right- but not left-hand side of an
assignment.
int g = 20;//valid statement
int a=g;
10 = 20;//invalid statement

Constants and Literals Integer Constants


The constants refer to fixed values that the Three types:
program may not alter during its execution. These Decimal
fixed values are also called literals.
Octal
Constants:
Hexadecimal
1)Numeric constants
Suffix: combination of U and L, for unsigned and
(i) Integer constants long, respectively.
(ii) Real constants Prefix: 0x or 0X for hexadecimal
2)Character constants 0 for octal
(i) Single character constants nothing for decimal.
(ii) String constants
Eg. const int a=25;

3
3/1/2023

Integer Constants(Cont.) Integer Constants(Cont.)


212 212 /* Legal */
215u 215u /* Legal */
078 078 /* Illegal: 8 is not an octal digit */
032UU 032UU /* Illegal: cannot repeat a suffix */
85 85 /* decimal */
0213 0213 /* octal */
0x4b 0x4b /* hexadecimal */
30 30 /* int */
30u 30u /* unsigned int */
30l 30l /* long */
30ul 30ul /* unsigned long */

Integer Constants(Cont.) Integer Constants(Cont.)


Illegal decimal literals: Illegal hexadecimal literals:
12,245 OX12.34
36.0 OBE38
10 20 30 Ox. 4bf f
123-45-6789 OXDEFG
0900

Illegal octal literals:


743
05280
0777.777

Floating--point Constants
Floating Floating--point Constants(Cont.)
Floating
A floating-point literal consists of the following: .95
1) An integral part -.71
2) A decimal point
2)Exponential notation:
3) A fractional part
4) An exponent part mantissa e exponent
5) An optional suffix mantissa is either a real number expressed in
You can represent floating point literals either decimal notation or an integer.
in decimal notation or exponential notation. exponent is an integer number with optional + or
1) Decimal notation: – sign.
Integral part followed by decimal point and Eg. 0.65e4
fractional part. 12e-2
You can omit integral part or fractional part.
1.5e+5
eg. 215.
3.18E3
-1.2E-1

4
3/1/2023

Floating--point Constants(Cont.)
Floating Floating--point Constants(Cont.)
Floating
Both the integral and fractional parts are made
up of decimal digits. You can omit either the 0. 1 1.6667E+8 0.006e-3
integral part or the fractional part, but not
both. 3E 10 2E+10.2 5.0066
You can omit either the decimal point or the 50000. 0 827.602 2 E-8
exponent part, but not both. 12.331 .000743 .12121212e12
3.14159 /* Legal */
1,000.0 0.2
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */

Character constants String Constants


Character literals are enclosed in single quotes, String literals or constants are enclosed in
e.g., 'x' and can be stored in a simple variable double quotes "".
of char type. Eg. "hello, dear"
Some escape sequences:
newline (‘\n’)
tab (‘\t’)
Backspace (‘\b’)
Null(‘\0’)
printf("Hello\tWorld\nWelcome");
o/p: Hello World
Welcome

Defining Constants Format Specifier


1)Using #define preprocessor. prototype:
#define identifier value %[flags][width][.precision][length]specifier
#define LENGTH 10
2)Using const keyword.
const type variable = value;
const int LENGTH = 10;

5
3/1/2023

Example--1
Example
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o
\n", 100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416,
3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}

Example--2
Example
Output: main()
{ int a,b;
Characters: a A float c,d;
Decimals: 1977 650000 a = 15;
Preceding with blanks: 1977 b = a / 2;
Preceding with zeros: 0000001977 printf("%d\n",b);
Some different radices: 100 64 144 0x64 printf("%3d\n",b);
0144
printf("%03d\n",b);
floats: 3.14 +3e+000 3.141600E+000
c = 15.3;
Width trick: 10
d = c / 3;
A string
printf("%3.2f\n",d);
}

6
3/1/2023

Example--3
Example
• 7 main()
• 7 { printf("The color: %s\n", "blue");
• 007 printf("First number: %d\n", 12345);
• 5.10 printf("Second number: %04d\n", 25);
printf("Third number: %i\n", 1234);
printf("Float number: %3.2f\n", 3.14159);
printf("Hexadecimal: %x\n", 255);
printf("Octal: %o\n", 255);
printf("Unsigned value: %u\n", 150);
printf("Just print the percentage sign %%\n",
10);
}

Expression
The color: blue An expression is a combination of variables,
First number: 12345 constants and operators arranged as per the
syntax of the language.
Second number: 0025
eg. a*b-c*d
Third number: 1234
Float number: 3.14
Hexadecimal: ff
Octal: 377
Unsigned value: 150
Just print the percentage sign %

Type Conversion(Cont.)
Type Conversion
When variables and constants of different types 1) Implicit type conversion:
are combined in an expression then they are When the type conversion is performed
converted to same data type. The process of automatically by the compiler without programmers
converting one predefined type into another is intervention, such type of conversion is known
called type conversion. as implicit type conversion or type promotion.
eg. int a=10; The compiler converts all operands into the data
float b=12.5; type of the largest operand.
float c=a+b; It should be noted that the final result of
Two types: expression is converted to type of variable on
left side of assignment operator before assigning
1) Implicit Conversion
value to it.
2) Explicit Conversion
Eg. int i=3;
float f=i;
printf(“%f”,f); // 3.000000

7
3/1/2023

Type Conversion(Cont.)

main()
Operand-1 Operand-2 Result {
char int int int i = 17;
char long int long char c = 'c'; /* ascii value is 99 */
int float sum;
char float float sum = i + c;
long int float float printf("Value of sum : %d\n", sum );
long int double double printf("Value of sum : %f\n", sum );
}
double long int double
float double double

Type Conversion(Cont.) Type Conversion(Cont.)


2) Explicit conversion (Type casting):-
O/p: The type conversion performed by the programmer
by posing the data type of the expression of
Value of sum : 116
specific type is known as explicit type
Value of sum : 116.000000 conversion.
(data_type)expression;
x=(int)a+b*d;
Rules to be followed to avoid the loss of
information:
1) All integer types to be converted to float.
2) All float types to be converted to double.
3) All character types to be converted to integer.
eg. float a=6.35;
printf(“%d”,(int)a);
printf(“%f”,a);

Storage Classes Storage Classes(Cont.)


A storage class defines the scope (visibility) and
1) The auto storage class
life-time of variables and/or functions within a C
Program. The auto storage class is the default storage
class for all local variables.
Two types of locations where values are stored:
Features:
1)Memory
1) storage- Memory
2) CPU registers
2) default initial value- garbage value
A variable’s storage class tells us:
3) scope- local to the block in which variable is
1)where the variable would be stored.
defined
2)what will be the initial value of the variable,
4) life- till the control remains within the
if the initial value is not specified. block in which it is defined
3)what is the scope of the variable i.e. in which
{
function the value of the variable would be
available. int mount;
4)what is the life of the variable i.e. how long auto int month;
would the variable exist. }

8
3/1/2023

Storage Classes(Cont.)
main()
2) The register Storage Class
{
The register storage class is used to define
auto int i=1; local variables that should be stored in a
{ register instead of RAM.
auto int i=2; Features:
{ 1) storage- CPU registers
auto int i=3; 2) default initial value- garbage value
printf(“%d\n”,i); 3) scope- local to the block in which variable is
} defined
printf(“%d\n”,i); 4) life- till the control remains within the
} block in which it is defined
printf(“%d\n”,i); {
} register int miles;
}

Storage Classes(Cont.)

The register should only be used for variables 3) The static Storage Class
that require quick access such as counters. Features:
We can not use it for all types of variables. 1) storage- Memory
eg. register float a; 2) default initial value- zero
register double b; 3) scope- local to the block in which variable is
CPU registers in a microcomputer are 16 bit defined
registers and therefore cannot hold a float or 4) life- value of the variable persists between
double value. different function calls
The static modifier may also be applied to global
variables.

Storage Classes(Cont.) Storage Classes(Cont.)

main() O/p:
{ 1
incr(); 2
incr(); 3
incr();
}
incr()
{
static int i=1;
printf(“%d”,i);
i=i+1;
}

9
3/1/2023

Storage Classes(Cont.)
4) The extern Storage Class
Features: #include <stdio.h>
1) storage- Memory int count ;
2) default initial value- zero extern void write_extern();
3) scope- global main()
4) life- as long as program’s execution doesn’t {
come to an end count = 5;
When you have multiple files and you define a write_extern();
global variable or function, which will be used
in other files also, then extern will be used in }
another file to give reference of defined
variable or function.
The extern modifier is most commonly used when
there are two or more files sharing the same
global variables or functions.

printf()
printf () & scanf()
scanf()
#include <stdio.h> printf() is a function which is used to print the
value contained in a variable on the screen.
extern int count;
General form:
void write_extern(void)
printf(“<format string>”,<list of variables>);
{
<format string> could be:
printf("count is %d\n", count);
%c for char
} %d for int so on..
scanf() is used to input data through the keyboard.
General form:
scanf(“control string”,&variable1,&variable2,…);
&(ampersand) symbol before each variable name is an
operator that specifies the variable name’s address.

Data Type Format


signed char %c
unsigned char %c
short signed %d
int
short unsigned %u
int
long signed int %ld
long unsigned %lu
int
float %f
double %lf
long double %Lf

10

You might also like