-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-types-variables.c
146 lines (122 loc) · 3.66 KB
/
data-types-variables.c
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
/**
* @file data-types-variables.c
* @author kunal jain ([email protected])
* @brief Simple functions to test data types,
* constants and ternary operator.
* @version 0.1
* @date 2022-02-17
*
* @copyright Copyright (c) 2022
*
*/
#include<stdio.h>
// Function to show overflow of char datatype
// When you declare a variable in C, by default
// it is a signed data type.
void test_datatype_overflow() {
// signed char - 8 bits
char c = 127;
printf("Value of c -> %d\n", c);
// overflows at 127
// Value goes to negetive extreme
// after overflow
c = 128;
printf("Value of c -> %d\n", c);
// underflows at -128
// Value goes to positive extreme
// after underflow
c = -129;
printf("Value of C -> %d\n", c);
}
// Function to show sizeof()
// sizeof(data-type) -> Function returns size of
// data type in bytes.
// sizeof() Argument -> variable name or data type
void test_datatype_size() {
// sizeof() with explicit data type as argument
printf("Size of char -> %d\n", sizeof(char));
printf("Size of integer -> %d\n", sizeof(int));
// sizeof() with variable name as argument
short int i = 0;
printf("Size of i -> %d\n", sizeof(i));
}
// This function shows how to define octal and
// hexadecimal integer constants.
void test_var_octal_hexa() {
// Leading 0 -> Octal value
int octal = 0123;
// Leading 0x -> hexadecimal value
int hexa = 0x123;
printf("Octal value in decimal -> %d\n", octal);
printf("Hexadecimal value in decimal -> %d\n", hexa);
}
// Enumeration constants
// Value of Sunday -> 0
// Monday -> 1 ... and so on
enum week { Sunday, Monday, Tuesday, Wednesday };
// Values of Enum start from 0, unless explicitly defined.
// Value of JAN -> 1 Explicitly defined
// FEB -> 2
// MAR -> 3 ... and so on
enum months { JAN = 1, FEB, MAR, APR, JUN };
// This function prints enum values.
void test_enum() {
// Access enum value by declaring a variable
// of enum type. Here day is of type enum week
enum week day;
day = Wednesday;
printf("Day -> %d\n", day);
// Access enum value directly using
// name of enum element.
printf("Value of Jan -> %d\n", JAN);
}
// This function prints initial value of local
// and static integer.
void test_var_initialization() {
// Local int initializes with a garbage value
int local_var;
// Static int initializes with zero value
static int static_var;
printf("Value of local variable is -> %d\n", local_var);
printf("Value of static variable is -> %d\n", static_var);
// Remember to explicitly define local variables
// as they initialize with a garbage value.
}
// Function to show how ternary operator works
void test_ternary_operator() {
int z, a = 5, b = 7;
// if (expr1) {
// expr2;
// } else {
// expr3;
// }
// If else way to evaluate max(a,b)
//
// if (a > b) {
// z = a;
// } else {
// z = b;
// }
// Ternary operator -> More concise if else
// expr1 ? expr2 : expr3
// If expr1 is true,
// evaluate expr2
// else evaluate expr3
// Ternary operator way to
// evaluate max(a,b)
z = (a > b) ? a : b;
printf("Value of z -> %d\n", z);
}
int main(void) {
// This is driver function. We call all function
// from this driver function.
// Uncomment the function that you want to run
// and check the output for.
// test_datatype_overflow();
// test_datatype_size();
// test_enum();
// test_var_octal_hexa();
// test_var_initialization();
// test_ternary_operator();
return 0;
}