-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatatypes
More file actions
152 lines (118 loc) · 4.31 KB
/
Datatypes
File metadata and controls
152 lines (118 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
1] Variables: A variable is a data name used to store a data value in memory. It must be declared with a specific data type before it can be used.
Declaration syntax: data_type variable_name;
Initialization syntax: data_type variable_name = value;
Rules for Naming Variables:
Names can contain letters, digits, and underscores (_).
Names must start with a letter or an underscore.
No white spaces or reserved keywords are allowed.
Names are case-sensitive (e.g., Total is different from TOTAL).
2] Data Types : Data types specify the type of data a variable can store and the memory size it requires. C supports several fundamental data types:
int: Used to store whole numbers (integers), such as 10 or -42.
float: Used to store single-precision floating-point numbers (numbers with decimals), such as 19.99.
double: Used to store double-precision floating-point numbers, providing greater precision than float.
char: Used to store a single character, such as 'A' or 'b'. Characters are enclosed in single quotes.
void: Used to specify the type of functions that do not return a value.
Type modifiers like short, long, signed, and unsigned can be used with these basic types to alter their size and range.
Constants
Constants (also called literals) are fixed values that the program cannot alter during its execution.
Types of Constants:
Integer Constants: Whole numbers without fractional parts (e.g., 10, -5, 0xFF (hexadecimal)).
Floating-point Constants: Real numbers with fractional parts (e.g., 3.14, 1.23e-3).
Character Constants: A single character enclosed in single quotes (e.g., 'a', '\n' (escape sequence)).
String Constants: A sequence of characters enclosed in double quotes (e.g., "Hello World", "123").
Defining Constants:
Constants can be defined in two primary ways:
Using the const keyword: This is a type-safe method where the value is assigned at declaration and cannot be changed later.
c
const float PI = 3.14159;
const int MAX_VALUE = 100;
Using the #define preprocessor directive: This defines a symbolic constant (macro) that is substituted by its value during the pre-processing stage before compilation. No data type is specified, and no semicolon is used at the end of the #define line.
c
#define PI 3.14159
#define MAX_VALUE 100
___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
1] Long datatype:
#include<stdio.h>
int main()
{
int a;
a = 500;
long b;
b = 10000000;
printf("Balance in millions:%dmillion\n",a);
printf("Total Bank Balance:%ldmillions\n",b);
return 0;
}
2] Integer Data Type:
Stores whole numbers (positive, negative, or zero).
We use int keyword to declare the integer variable:
Size: 4 bytes, Range: -2,147,483,648 to 2,147,483,647.
Format specifier: %d.
Format specifiers are the symbols that are used for printing and scanning values of given data types.
#include <stdio.h>
int main() {
int var = 22;
printf("var = %d", var);
return 0;
}
Output
var = 22
3] Character Data Type:
Stores a single character (like ‘A’, ‘b’, or ‘5’).
Size: 1 byte, Range: -128 to 127 (signed by default).
Format specifier: %c.
#include <stdio.h>
int main() {
char ch = 'A';
printf("ch = %c", ch);
return 0;
}
Output
ch = A
4] Float Data Type:
Stores decimal numbers (numbers with fractional part).
Size: 4 bytes, Approximate range: 3.4e-38 to 3.4e+38.
Format specifier: %f.
#include <stdio.h>
int main() {
float val = 12.45;
printf("val = %f", val);
return 0;
}
Output
val = 12.450000
5] Double Data Type:
Stores decimal numbers with more precision than float.
Size: 8 bytes, Approximate range: 1.7e-308 to 1.7e+308.
Format specifier: %lf.
#include <stdio.h>
int main() {
double val = 1.4521;
printf("val = %lf", val);
return 0;
}
Output
val = 1.452100
6] Void Data Type
Represents no value or empty type.
Used in functions that do not return any value.
Can also be used for generic pointers (void *) in memory operations.
#include <stdio.h>
// Function with void return type
void greet()
{
printf("Hello, welcome!\n");
}
int main()
{
greet();
return 0;
}
Output
Hello, welcome!