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

3/8/2022

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 stored is
Constants & Storage Classes 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 int 2 bytes -32,768 to 32,767(-2^15 to 2^15 - %d
1)
(c) Structure types
(d) Union types signed long int 4 bytes -2,147,483,648 to 2,147,483,647 %ld
(e) Function types. unsigned long int 4 bytes 0 to 4,294,967,295 %lu

Sizeof operator Floating--Point Types


Floating
 The expressions sizeof(type) yields the storage size of Type Storage Value range Precision
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", sizeof(int));
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
 o/p: 4
long double 10 byte 3.4E-4932 to 19 decimal places
1.1E+4932

1
3/8/2022

Enumerated Types Enumerated Types(Cont.)


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

The void Type The void Type(Cont.)


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

Variable Definition Variable initialization


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

d 3

Lvalues and Rvalues


 Static variables have a fixed memory location and 1) lvalue : Expressions that refer to a memory location is
memory is allocated in compile time. called "lvalue" expression. An lvalue may appear as
 Local variables are stored in stack and they do not either the left-hand or right-hand side of an assignment.
have a fixed memory location. 2) rvalue : The term rvalue refers to a data value that is
 Memory is allocated to local variables at runtime but stored at some address in memory. An rvalue is an
not at compile time. hence local variables have their expression that cannot have a value assigned to it
default value as garbage. 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 program  Three types:
may not alter during its execution. These fixed values  Decimal
are also called literals.
 Octal
 Constants:
 Hexadecimal
1)Numeric constants
 Suffix: combination of U and L, for unsigned and long,
(i) Integer constants 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/8/2022

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
 mantissa e exponent
4) An exponent part
5) An optional suffix  mantissa is either a real number expressed in decimal
 You can represent floating point literals either in notation or an integer.
decimal notation or exponential notation.  exponent is an integer number with optional + or –
1) Decimal notation: sign.
 Integral part followed by decimal point and fractional  Eg. 0.65e4
part.  12e-2
 You can omit integral part or fractional part.
 1.5e+5
 eg. 215.
 3.18E3
 -1.2E-1

4
3/8/2022

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 integral part or 0. 1 1.6667E+8 0.006e-3
the fractional part, but not both.
3E 10 2E+10.2 5.0066
 You can omit either the decimal point or the exponent
part, but not both. 50000. 0 827.602 2 E-8
 3.14159 /* Legal */ 12.331 .000743 .12121212e12
 314159E-5L /* Legal */ 1,000.0 0.2
 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, e.g., 'x'  String literals or constants are enclosed in double
and can be stored in a simple variable of char type. quotes "".
 Some escape sequences:  Eg. "hello, dear"
 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/8/2022

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 0144 printf("%3d\n",b);
floats: 3.14 +3e+000 3.141600E+000 printf("%03d\n",b);
Width trick: 10 c = 15.3;
A string d = c / 3;
printf("%3.2f\n",d);
}

6
3/8/2022

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, constants
 First number: 12345 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 Type Conversion(Cont.)


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

7
3/8/2022

Type Conversion(Cont.)
 main()
Operand-1 Operand-2 Result {
char int int int i = 17;
char long int long int char c = 'c'; /* ascii value is 99 */
char float float float sum;
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.)


 O/p: 2) Explicit conversion (Type casting):-
 Value of sum : 116  The type conversion performed by the programmer by
 Value of sum : 116.000000 posing the data type of the expression of specific type is
known as explicit type 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 life- 1) The auto storage class
time of variables and/or functions within a C Program.  The auto storage class is the default storage class for
 Two types of locations where values are stored: all local variables.
1)Memory  Features:
2) CPU registers 1) storage- Memory
 A variable’s storage class tells us: 2) default initial value- garbage value
1)where the variable would be stored. 3) scope- local to the block in which variable is defined
2)what will be the initial value of the variable, if the initial 4) life- till the control remains within the block in which it
value is not specified. 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 would the auto int month;
variable exist.
}

8
3/8/2022

 main() Storage Classes(Cont.)


{
auto int i=1; 2) The register Storage Class
{  The register storage class is used to define local
variables that should be stored in a register instead of
auto int i=2;
RAM.
{
 Features:
auto int i=3;
1) storage- CPU registers
printf(“%d\n”,i);
2) default initial value- garbage value
}
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 that 3) The static Storage Class
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 defined
 CPU registers in a microcomputer are 16 bit registers 4) life- value of the variable persists between different
and therefore cannot hold a float or double value. 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/8/2022

Storage Classes(Cont.)
4) The extern Storage Class #include <stdio.h>
 Features: int count ;
1) storage- Memory extern void write_extern();
2) default initial value- zero main()
3) scope- global {
4) life- as long as program’s execution doesn’t come to count = 5;
an end write_extern();
 When you have multiple files and you define a 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
 extern int count; contained in a variable on the screen.
 void write_extern(void)  General form:
 {  printf(“<format string>”,<list of variables>);
 printf("count is %d\n", count);  <format string> could be:
 }  %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 int %d
short unsigned %u
int
long signed int %ld
long unsigned int %lu
float %f
double %lf
long double %Lf

10

You might also like