Lets Learn C Language
Chapter 2 – Data Variables
Data Types in C
Following are the examples of some very common data types used in C:
• char: The most basic data type in C. It stores a single character and
requires a single byte of memory in almost all compilers.
• int: As the name suggests, an int variable is used to store an integer.
• float: It is used to store decimal numbers (numbers with floating
point value) with single precision.
• double: It is used to store decimal numbers (numbers with floating
point value) with double precision.
DigiiMento Education www.DigiiMento.om 2
DigiiMento Education www.DigiiMento.om 3
DigiiMento Education www.DigiiMento.om 4
DigiiMento Education www.DigiiMento.om 5
𝑆𝑖𝑧𝑒𝑜𝑓() Function
DigiiMento Education www.DigiiMento.om 6
Predict the output of following program. Assume that the numbers are
stored in 2's complement form
DigiiMento Education www.DigiiMento.om 7
Predict the output
DigiiMento Education www.DigiiMento.om 8
Predict the output
DigiiMento Education www.DigiiMento.om 9
Assume that the size of char is 1 byte and negatives are stored
in 2's complement form
DigiiMento Education www.DigiiMento.om 10
DigiiMento Education www.DigiiMento.om 11
In C, when an integer value is compared with an unsigned it, the int is promoted to unsigned.
Negative numbers are stored in 2's complement form and unsigned value of the 2's complement
form is much higher than the sizeof int.
DigiiMento Education www.DigiiMento.om 12
DigiiMento Education www.DigiiMento.om 13
DigiiMento Education www.DigiiMento.om 14
Explanation: In C language, float, double and long double
are called real data types. For “float”, “double” and “long
double”, the right format specifiers are %f, %lf and %Lf
from the above options. It should be noted that C standard
has specified other format specifiers as well for real types
which are %g, %e etc.
DigiiMento Education www.DigiiMento.om 15
DigiiMento Education www.DigiiMento.om 16
DigiiMento Education www.DigiiMento.om 17
How are variables scoped in C – Static or
Dynamic?
DigiiMento Education www.DigiiMento.om 18
How are variables scoped in C – Static or
Dynamic?
DigiiMento Education www.DigiiMento.om 19
In C, variables are always statically (or lexically) scoped
i.e., binding of a variable can be determined by
program text and is independent of the run-time
function call stack
DigiiMento Education www.DigiiMento.om 20
DigiiMento Education www.DigiiMento.om 21
Scope rules in C
C scope rules can be covered under following two categories.
• Global Scope: Can be accessed anywhere in a program.
• Block Scope: A Block is a set of statements enclosed within left and
right braces ({ and } respectively)
DigiiMento Education www.DigiiMento.om 22
To restrict access to the current file only, global variables can be marked
as static.
DigiiMento Education www.DigiiMento.om 23
DigiiMento Education www.DigiiMento.om 24
DigiiMento Education www.DigiiMento.om 25
DigiiMento Education www.DigiiMento.om 26
DigiiMento Education www.DigiiMento.om 27
Redeclaration of global variable in C
DigiiMento Education www.DigiiMento.om 28
DigiiMento Education www.DigiiMento.om 29
DigiiMento Education www.DigiiMento.om 30
DigiiMento Education www.DigiiMento.om 31
C allows a global variable to be declared again when first declaration
doesn’t initialize the variable.
DigiiMento Education www.DigiiMento.om 32
DigiiMento Education www.DigiiMento.om 33
DigiiMento Education www.DigiiMento.om 34
Suggested Reading
• https://www.geeksforgeeks.org/internal-linkage-external-linkage-c/
• https://www.geeksforgeeks.org/how-linkers-resolve-multiply-
defined-global-symbols/
DigiiMento Education www.DigiiMento.om 35
Use of bool in C
The C99 standard for C language supports bool variables. Unlike C++, where
no header file is needed to use bool, a header file “stdbool.h” must be
included to use bool in C. If we save the below program as .c, it will not
compile, but if we save it as .cpp, it will work fine
DigiiMento Education www.DigiiMento.om 36
If we include the header file “stdbool.h” in the above program, it will work fine as a C program.
DigiiMento Education www.DigiiMento.om 37
Integer Promotions in C
• Some data types like char , short int take less number of bytes than
int, these data types are automatically promoted to int or unsigned
int when an operation is performed on them. This is called integer
promotion.
• For example no arithmetic calculation happens on smaller types like
char, short and enum. They are first converted to int or unsigned int,
and then arithmetic is done on them.
• If an int can represent all values of the original type, the value is
converted to an int . Otherwise, it is converted to an unsigned int.
DigiiMento Education www.DigiiMento.om 38
Integer Promotions in C
DigiiMento Education www.DigiiMento.om 39
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have
values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200 which is
greater than 128. But integer promotion happens here in arithmetic done on char types and we get the appropriate
result without any overflow.
DigiiMento Education www.DigiiMento.om 40
DigiiMento Education www.DigiiMento.om 41
DigiiMento Education www.DigiiMento.om 42
Comparison of a float with a value in C
DigiiMento Education www.DigiiMento.om 43
DigiiMento Education www.DigiiMento.om 44
Is there any need of “long” data type in C and
C++?
But there is a catch, the size of “long” data type is not fixed unlike
other data types. It varies from architectures, operating system and
even with compiler that we are using. In some of the systems it
behaves like an int data type or a long long data type as follows:
DigiiMento Education www.DigiiMento.om 45
Cross Compiler
• Well it also varies from compiler. But before this, let’s understand
about the concept of cross compiler.
• A cross compiler is a compiler capable of creating executable code for
a platform other than the one on which the compiler is running. For
instance, if I compile the following programs in 64 bit architecture
running a 64 bit Ubuntu, I will get the result like this:
DigiiMento Education www.DigiiMento.om 46
DigiiMento Education www.DigiiMento.om 47
Self Read
• From above we conclude that size of only “long” data type varies from compiler.
Now the question is what exactly is happening here? Let’s discuss it in the way of
how compiler allocates memory internally.
• CPU calls data from RAM by giving the address of the location to MAR (Memory
Address Register). The location is found and the data is transferred to MDR
(Memory Data Register). This data is recorded in one of the Registers in the
Processor for further processing. That’s why size of Data Bus determines the size
of Registers in Processor. Now, a 32 bit register can call data of 4 bytes size only,
at a time. And if the data size exceeds 32 bits, then it would required two cycles
of fetching to have the data in it. This slows down the speed of 32 bit Machine
compared to 64 bit, which would complete the operation in ONE fetch cycle only.
So, obviously for the smaller data, it makes no difference if my processors are
clocked at the same speed. Compilers are designed to generate the most efficient
code for the target machine architecture.
DigiiMento Education www.DigiiMento.om 48
Self Read Contd.
• So, in short the size of a variable is compiler dependent as it
generates the instructions based on the target architecture and
system architecture that only deals with the size of data bus and it’s
transfer.
Note: Interestingly we don’t have any need of “long” data type as
their replacement(int, long long) is already available
from C99 standard.
DigiiMento Education www.DigiiMento.om 49
What if we want 8 Byte int on all compilers/Machines ?
Suggestion: If it is important to you for integer types to have the same
size on all Intel platforms, then consider replacing “long” by
either “int” or “long long”. The size of the “int” integer type is 4 bytes
and the size of the “long long” integer type is 8 bytes for all the above
combinations of operating system, architecture and compiler.
https://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-os
DigiiMento Education www.DigiiMento.om 50
Interesting facts about data-types and
modifiers in C/C++
Here are some logical and interesting facts about data-types and the
modifiers associated with data-types:-
1. If no data type is given to a variable, the compiler automatically
converts it to int data type.
2. Signed is the default modifier for char and int data types.
3. We can’t use any modifiers in float data type. If programmer tries to
use it ,the compiler automatically gives compile time error.
4. Only long modifier is allowed in double data types. We cant use any
other specifier with double data type. If we try any other specifier,
compiler will give compile time error.
DigiiMento Education www.DigiiMento.om 51
1. If no data type is given to a variable, the compiler automatically
converts it to int data type.
DigiiMento Education www.DigiiMento.om 52
Signed is the default modifier for char and int data types.
DigiiMento Education www.DigiiMento.om 53
3. We can’t use any modifiers in float data type. If programmer tries to
use it ,the compiler automatically gives compile time error.
DigiiMento Education www.DigiiMento.om 54
4. Only long modifier is allowed in double data types. We cant
use any other specifier with double data type. If we try any
other specifier, compiler will give compile time error.
DigiiMento Education www.DigiiMento.om 55
Suggested Read (Self)
Difference between float and double in C/C++
• For representing floating point numbers, we
use float, double and long double.
• What’s the difference ?
• double has 2x more precision then float.
• float is a 32 bit IEEE 754 single precision Floating Point Number1 bit
for the sign, (8 bits for the exponent, and 23* for the value), i.e. float
has 7 decimal digits of precision.
• double is a 64 bit IEEE 754 double precision Floating Point Number (1
bit for the sign, 11 bits for the exponent, and 52* bits for the value),
i.e. double has 15 decimal digits of precision.
https://www.geeksforgeeks.org/difference-float-double-c-cpp/
DigiiMento Education www.DigiiMento.om 56
DigiiMento Education www.DigiiMento.om 57
Type Conversion in C
• A type cast is basically a conversion from one type to another. There
are two types of type conversion:
• Implicit Type Conversion
• Explicit Type Conversion
DigiiMento Education www.DigiiMento.om 58
Implicit Type Conversion
Also known as ‘automatic type conversion’
•Done by the compiler on its own, without any
external trigger from the user.
•Generally takes place when in an expression
more than one data type is present. In such
condition type conversion (type promotion)
takes place to avoid lose of data.
•All the data types of the variables are
upgraded to the data type of the variable with
largest data type.
•It is possible for implicit conversions to lose
information, signs can be lost (when signed is
implicitly converted to unsigned), and overflow can
occur (when long long is implicitly converted to
float). DigiiMento Education www.DigiiMento.om 59
DigiiMento Education www.DigiiMento.om 60
Explicit Type Conversion
• This process is also called type casting and it is user defined. Here the
user can type cast the result to make it of a particular data type.
• The syntax in C:
(𝑡𝑦𝑝𝑒) 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛
Advantages of Type Conversion
• This is done to take advantage of certain features of type hierarchies
or type representations.
• It helps us to compute expressions containing variables of different
data types.
DigiiMento Education www.DigiiMento.om 61
DigiiMento Education www.DigiiMento.om 62
Intentionally Left Blank
DigiiMento Education www.DigiiMento.om 63