Variables and Data Types
In C, a variable is a named storage location in memory that can hold a value. Each variable has a specific data type that determines the kind of data it can store and how much memory it occupies. C provides several built-in data types, which can be categorized into the following groups:
- Basic Data Types:
- Integer number:
charfor characters.intfor integers,shortfor short integers,longfor long integers,long longfor long long integers.
- Floating-point number:
floatfor single-precision floating-point numbers,doublefor double-precision floating-point numbers,long doublefor extended-precision floating-point numbers.
voidfor functions that do not return a value,
- Derived Data Types:
- Arrays, which are collections of elements of the same type,
- Pointers, which store memory addresses,
- Structures, which are collections of variables of different types.
- Enumeration Types:
enumfor defining a set of named integer constants.
Example of declaring variables in C:
#include <stdio.h>
int main() {
int age = 25; // Integer variable
float height = 5.9; // Floating-point variable
char grade = 'A'; // Character variable
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
In this example, we declare three variables: age of type int, height of type float, and grade of type char. We then print their values using the printf function, where %d is used for integers, %.1f for floating-point numbers with one decimal place, and %c for characters.
Variable Naming Rules in C:
- Variable names must begin with a letter (A-Z or a-z) or an underscore (_).
- Variable names can contain letters, digits (0-9), and underscores.
- Variable names are case-sensitive (e.g.,
ageandAgeare different variables). - Variable names cannot be the same as C keywords (e.g.,
int,float,return, etc.).
Data Type Sizes in C:
The size of each data type can vary depending on the system and compiler, but typically:
charis usually 1 byte.shortis usually 2 bytes,intis usually 4 bytes,floatis usually 4 bytes,doubleis usually 8 bytes,longandlong longcan be 4 or 8 bytes depending on the system.
Formate Specifiers in C:
When using printf to display variable values, you need to use the correct format specifier for each data type:
%cforchar%dforint%fforfloat%lffordouble%sfor strings (character arrays)%pfor pointers%ufor unsigned integers%xfor hexadecimal integers%ofor octal integers%efor scientific notation (floating-point numbers)
Range of Values for Integer Types in C:
char: -128 to 127 (signed), 0 to 255 (unsigned)short: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)int: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)long: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)long long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615 (unsigned)
Range of Values for Floating-Point Types in C:
float: approximately 1.5 x 10^-45 to 3.4 x 10^38 (7 decimal digits of precision)double: approximately 5.0 x 10^-324 to 1.7 x 10^308 (15 decimal digits of precision)long double: approximately 3.4 x 10^-4932 to 1.1 x 10^4932 (18 decimal digits of precision)
How to determine the size of the type on your computer?
Write this program to check size:
#include <stdio.h>
int main(void) {
printf("char size: %lu bytes\n", sizeof(char));
printf("int size: %lu bytes\n", sizeof(int));
printf("short size: %lu bytes\n", sizeof(short));
printf("long size: %lu bytes\n", sizeof(long));
printf("float size: %lu bytes\n", sizeof(float));
printf("double size: %lu bytes\n", sizeof(double));
printf("long double size: %lu bytes\n", sizeof(long double));
}
The problem with overflow and underflow in C:
Overflow occurs when a calculation exceeds the maximum limit of a data type, while underflow occurs when a calculation goes below the minimum limit. For example, if you try to store a value larger than 127 in a char variable, it will wrap around and give you an incorrect value due to overflow. Similarly, if you try to store a value smaller than -128 in a char variable, it will also wrap around due to underflow. This can lead to unexpected behavior in your program, so it’s important to be aware of the limits of the data types you are using and to handle potential overflow and underflow situations appropriately, such as by using larger data types or implementing checks before performing calculations.
Understanding variables and data types is fundamental to programming in C, as they allow you to store and manipulate data effectively. Always choose the appropriate data type for your variables to optimize memory usage and ensure that your program behaves as expected.
Variable scope:
Variable scope refers to the region of a program where a variable is defined and can be accessed. In C, there are three main types of variable scope:
- Local Scope: Variables declared inside a function or block are local to that function or block and cannot be accessed outside of it. Example:
#include <stdio.h>
void myFunction() {
int localVar = 10; // local variable
printf("Local variable: %d\n", localVar);
}
int main() {
myFunction();
// printf("%d", localVar); // This will cause an error because localVar is not accessible here
return 0;
}
- Global Scope: Variables declared outside of all functions are global and can be accessed from any function in the program. Example:
#include <stdio.h>
int globalVar = 20; // global variable
void myFunction() {
printf("Global variable: %d\n", globalVar);
}
int main() {
myFunction();
printf("Global variable in main: %d\n", globalVar);
return 0;
}
- Static Scope: Variables declared with the
statickeyword inside a function retain their value between function calls and are only accessible within that function. Example:
#include <stdio.h>
void myFunction() {
static int staticVar = 0; // static variable
staticVar++;
printf("Static variable: %d\n", staticVar);
}
int main() {
myFunction(); // Output: Static variable: 1
myFunction(); // Output: Static variable: 2
return 0;
}