Professional Documents
Culture Documents
Data Types Format Type Conversion Variables Constants Storage Classes
Data Types Format Type Conversion Variables Constants Storage Classes
1
3/8/2022
2
3/8/2022
d 3
3
3/8/2022
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 */
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 %
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
8
3/8/2022
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.
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.
10