Professional Documents
Culture Documents
Data Types, Formats, Type Data Types, Formats, Type
Data Types, Formats, Type Data Types, Formats, Type
1
3/1/2023
2
3/1/2023
3
3/1/2023
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 */
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
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.
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.
10