Data Type, Variable, and String
AAP, RCH, AMS
Data Type
• Each data type requires different amounts of memory and has some specific
operations which can be performed over it.
Following are the examples of some very common data types used in C:
• char: 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.
Data Type
• Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler.
• Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc
compiler.
Data Type Memory (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
2 0 to 65,535
unsigned short int %hu
unsigned int 4 0 to 4,294,967,295 %u
-2,147,483,648 to
int 4 %d
2,147,483,647
-2,147,483,648 to
long int 4 %ld
2,147,483,647
Data Type
• Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler.
• Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc
compiler.
Format
Data Type Memory (bytes) Range
Specifier
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
0 to
unsigned long long
8 18,446,744,073,709,551,615 %llu
int
char 1 0-255 %c
Data Type
• Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler.
• Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc
compiler.
Format
Data Type Memory (bytes) Range
Specifier
float 4 %f
double 8 %lf
long double 16
%Lf
string %s
Variables
• A variable in simple terms is a storage place that has some memory
allocated to it.
• Basically, a variable is used to store some form of data. Different
types of variables require different amounts of memory, different
type of memory locations, and some specific set of operations that
can be applied to them.
Variables
Variable Declaration: A typical variable declaration is of the form:
type variable_name;
or for multiple variables:
type variable1_name, variable2_name, variable3_name;
Variables
• A variable name can consist of alphabets (both upper and lower
case), numbers, and the underscore ‘_’ character. However, the
name must not start with a number.
Variables
Difference b/w variable declaration and definition:
• Variable declaration refers to the part where a variable is first declared or
introduced before its first use.
• Declaration of variable type is also done in the part.
• A variable definition is a part where the variable is assigned a memory
location and a value.
• Most of the time, variable declaration and definition are done together.
Variables
• Rules for defining variables
1. A variable can have alphabets, digits, and underscore.
2. A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword, e.g. int, goto, etc.
Variables
• Types of Variables in C
1. Local Variable: A variable that is declared and used inside the function or block is called a local variable. It
is scope is limited to function or block. It cannot be used outside the block. Local variables need to be
initialized before use.
Variables
• Types of Variables in C
2. Global Variable: A variable that is declared outside the
function or block is called a global variable. It is declared at
the start of the program. It is available for all functions.
Variables
• Types of Variables in C
3. Static Variable: A variable that retains its value between
multiple function calls is known as a static variable. It is
declared with the static keyword.
Result:
Variables
• Types of Variables in C
3. Automatic Variable: All variables in C that are declared inside the block, are automatic variables by default.
We can explicitly declare an automatic variable using the auto keyword. Automatic variables are similar to
local variables
Variables
• Types of Variables in C
5. External Variable: External variables can be shared between multiple C files. We can declare an external
variable using extern keyword
myfile.h Prak-2.c
externfile.c
OUTPUT:
Variables
Extern: extern simply tells us that the variable is defined elsewhere and not within the same block
where it is used.
Basically, the value is assigned to it in a different block and this can be overwritten/changed in a
different block as well.
So an extern variable is nothing but a global variable initialized with a legal value where it is
declared in order to be used elsewhere.
It can be accessed within any function/block. Also, a normal global variable can be made extern as
well by placing the ‘extern’ keyword before its declaration/definition in any function/block.
This basically signifies that we are not initializing a new variable but instead we are using/accessing
the global variable only.
The main purpose of using extern variables is that they can be accessed between two different
files which are part of a large program.
Keywords
• These are reserved words whose meaning is already known to the compiler. There are 32
keywords available in c
Keywords
• static: static keyword is used to declare static variables
• Static variables have the property of preserving their value even after they are out of their
scope! Hence, static variables preserve the value of their last use in their scope. So we can say
that they are initialized only once and exist till the termination of the program. Thus, no new
memory is allocated because they are not re-declared.
• Their scope is local to the function to which they were defined. Global static variables can be
accessed anywhere within that file as their scope is local to the file. By default, they are
assigned the value 0 by the compiler.
Strings
• Strings are defined as an array of characters.
• Declaration of strings: Declaring a string is as simple as declaring a one-
dimensional array. Below is the basic syntax for declaring a string.
char str_name[size];
1. char str[] = “rudychandra";
2. char str[15] = “rudychandra";
3. char str[] = {‘r',’u',’d',’y',’c',’h',’a',’n',’d',’r',’a’,’\0'};
4. char str[11] = {‘r',’u',’d',’y',’c',’h',’a',’n',’d',’r',’a’,’\0'};
Strings
1. char str[] = “rudychandra";
r u d y c h a n d r a \0
Memory Diagram
2. char str[15] = “rudychandra";
r u d y c h a n d r a \0
Memory Diagram
Strings
1. char str[] = {‘r',’u',’d',’y',’c',’h',’a',’n',’d',’r',’a’,’\0'};
r u d y c h a n d r a \0
Memory Diagram
2. char str[11] = {‘r',’u',’d',’y',’c',’h',’a',’n',’d',’r',’a’,’\0’};
r u d y c h a n d r a \0
Memory Diagram
Strings
• Read String
• You can use the scanf() function to read a string.
• The scanf() function reads the sequence of characters until it encounters
whitespace (space, newline, tab, etc.).
Strings
• Code
• Output
Strings
• Even though rudy chandra was entered in the above program, only
“rudy" was stored in the name string.
• It's because there was a space after rudy.
• How to read a line of text?
Strings
• You can use the fgets() function to read a line of string. And, you can
use puts() to display the string.
• Code
• Output
String
• Access Strings
• Since strings are actually arrays in C, you can access a string by referring to
its index number inside square brackets [].
• This example prints the first character (0) in greetings:
String
• Modify Strings
• To change the value of a specific character in a string, refer to the index
number, and use single quotes:
String
• Commonly Used String Functions
• strlen() - calculates the length of a string
• strcpy() - copies a string to another
• strcmp() - compares two strings
• strcat() - concatenates two strings
• strlwr() - converts string to lowercase
• strupr() - converts string to uppercase
Strings
• Assigning Values to Strings
• Use the strcpy() function to copy the string instead