Data Types & Arithmetic Operation
DATA TYPES
Definition and Purpose: Data types are fundamental in programming, designating the type, size, and range of values that variables can store in memory. They dictate how data is interpreted and the operations that can be performed on it.
Categories:
Primitive Data Types: These are the basic, fundamental data types provided by the programming language. They represent single values directly. Examples in C include
int(for integers),floatanddouble(for real numbers), andchar(for characters).Derived Data Types: These are constructed from primitive data types or other derived types. They provide more complex ways to store data. Examples include
arrays(collections of similar data types),functions(blocks of code designed to perform a specific task), andpointers(variables that store memory addresses).User Defined Data Types: These types are custom-defined by programmers to suit specific application needs, often combining different primitive and derived data types into a single structure. Examples in C are
structures(collections of variables of different data types under a single name),unions(similar to structures but allocate memory for only one member at a time), andenumerations(sets of named integer constants).
VARIABLES AND CONSTANTS
Variables: Variables are symbolic names given to memory locations used to store data. They allow programs to access and manipulate data. For example,
volumemight store the result of a calculation, andradiuscould hold user input. Variables can be declared and initialized:Declaration:
int age;(reserves space for an integer namedage).Initialization:
int age = 30;(declaresageand assigns it an initial value).
Constants: Constants are fixed values that do not change during program execution. Using constants instead of "magic numbers" (unnamed numerical constants) improves code readability and maintainability.
Literal Constants: Direct values like
10,3.14,'A'. They have no name.Symbolic Constants: Constants assigned a name. They can be defined using:
#define PI 3.14159(preprocessor directive)const float PI = 3.14159;(keyword for type-safe constants)
MEMORY ALLOCATION
Memory Segments: When a program executes, its memory is typically divided into several segments:
Text Segment (Code Segment): Stores the compiled machine code (instructions) of the program. This segment is usually read-only to prevent accidental modification.
Data Segment: Stores global and static variables that are initialized at compile time. These variables reserve memory throughout the program's lifetime.
BSS Segment (Block Started by Symbol): Stores global and static variables that are uninitialized. These variables are initialized to zero by the system before the program starts execution.
Heap Segment: Used for dynamic memory allocation. Memory is allocated and deallocated at runtime by functions like
malloc(),calloc(),realloc(), andfree()for C. Its size can grow or shrink.Stack Segment: Used for local variables, function parameters, and return addresses. It operates on a LIFO (Last-In, First-Out) principle, growing downwards from higher memory addresses.
NAMING CONVENTION
Valid Names: In C, identifiers (names for variables, functions, etc.) must adhere to specific rules:
Must begin with a letter (a-z, A-Z) or an underscore (
_).Can be followed by letters, digits (0-9), or underscores.
Case-sensitive (e.g.,
myVarandmyvarare different).Cannot be a reserved keyword (e.g.,
int,float,return).
Significance of Characters: While modern compilers often support longer names, traditionally, only the first 31 characters of an identifier were considered significant by some C standards. It's good practice to keep names reasonably concise and descriptive.
Best Practices: Use meaningful names (e.g.,
employeeSalaryinstead ofs), follow a consistent style (e.g., camelCase for variables, SCREAMINGSNAKECASE for constants), and avoid single-letter variable names unless their scope is very small (like loop countersi,j,k).
ILLEGAL NAMING
Examples of Illegal Names: Understanding invalid names helps avoid common errors:
123variable: Illegal because it starts with a digit.my-variable: Illegal because it contains a special character (-). Hyphens are arithmetic operators, not allowed in identifiers.float: Illegal becausefloatis a reserved keyword in C._my variable: Illegal because it contains a space.if,while,for: All are reserved keywords and cannot be used as identifiers.
SYMBOLIC CONSTANTS
Definition: Symbolic constants are user-defined names that represent a constant value. They enhance code clarity and make updates easier.
Two Primary Methods in C:
#definePreprocessor Directive:esempio: #define PI 3.14159The preprocessor replaces all occurrences of
PIwith3.14159before compilation. It does not occupy memory space as a variable.No type checking is performed.
constKeyword:esempio: const float PI = 3.14159f;Declares a variable whose value cannot be changed after initialization. It is type-checked by the compiler.
Occupies memory like a regular variable.
Generally preferred over
#definefor type safety and scope control.
PRIMITIVE DATA TYPES
Fundamental Building Blocks: Primitive data types are the most basic data types available, serving as the foundation for storing simple data values. They are directly supported by the hardware and represent fundamental categories of information.
Types Include:
Integer Types: Used to store whole numbers (positive, negative, or zero) without fractional components. These include
int,long int,short int,unsigned int,unsigned long int,unsigned short int.Real Numbers (Floating-Point Types): Used to store numbers with fractional parts. These include
float(single-precision floating-point) anddouble(double-precision floating-point).Characters: Used to store single characters (letters, digits, symbols). These include
char,signed char,unsigned char.
INTEGER TYPES
C Integer Ranges and Sizes: The exact ranges and sizes of integer types can vary slightly between compilers and systems, but standard minimums are guaranteed. The
limits.hheader file provides macros with specific system limits.int: Typically 4 bytes (32 bits), storing values from to (approx. -2,147,483,648 to 2,147,483,647).short int: Typically 2 bytes (16 bits), storing values from to (approx. -32,768 to 32,767).long int: Typically 4 or 8 bytes, often 8 bytes (64 bits) on modern systems, storing values from to .unsigned int: Typically 4 bytes, storing values from to (approx. 0 to 4,294,967,295).unsignedtypes only store non-negative values, effectively doubling their positive range.long long int: Introduced in C99, typically 8 bytes, always guaranteed to be at least 64 bits.
REAL NUMBERS
Real Data Types for Floating-Point Values: These types store numbers with decimal points, representing approximations of real numbers. The trade-off is precision and range.
float: Stores single-precision floating-point values. Typically 4 bytes, offering approximately 6-7 decimal digits of precision. Range: to .double: Stores double-precision floating-point values. Typically 8 bytes, offering approximately 15-17 decimal digits of precision, making it suitable for most scientific and engineering computations. Range: to .long double: Provides even greater precision, often 10 or 16 bytes depending on the system.
Precision:
doubleoffers greater precision and range thanfloat, making it the default choice for most floating-point calculations unless memory or performance constraints specifically requirefloat.
CHARACTERS
Character Storage: The
chardata type is used to store single alphanumeric characters. Internally, characters are stored as integer values corresponding to their ASCII or Unicode representation.Char Size: Always 1 byte. This allows it to store 256 distinct values.
signed char: Range from -128 to 127. Default forcharon some systems.unsigned char: Range from 0 to 255. Default forcharon other systems.
ASCII Representation: Each character (e.g., 'A', 'a', '5', '$') has a corresponding decimal (and hexadecimal) value defined by the ASCII (American Standard Code for Information Interchange) table. For example, 'A' is 65, 'a' is 97, '0' is 48.
Escape Sequences: Special characters are represented using escape sequences starting with a backslash
\:\n: Newline\t: Tab\b: Backspace\\: Backslash\': Single quote\": Double quote
FORMAT SPECIFIERS
Purpose: Format specifiers are used in functions like
printf()andscanf()to indicate the type of data being input or output. They tell the compiler how to interpret and display the variable's value.Common Specifiers:
%dor%i: For signed decimal integers (int).%u: For unsigned decimal integers (unsigned int).%ld: For long signed decimal integers (long int).%lldforlong long int.%hu: For short unsigned integers (unsigned short int).%f: For single-precision floating-point numbers (floatanddoublewhen printing,floatwhen scanning).%lf: For double-precision floating-point numbers (doublewhen scanning withscanf()).%c: For a single character (char).%s: For a string of characters (character array).%p: For printing memory addresses (pointers).%xor%X: For hexadecimal integers (lowercase/uppercase).%.nf: To specifyndigits after the decimal point for floating-point numbers (e.g.,%.2ffor two decimal places).
ARITHMETIC OPERATORS
Basic Operations: These operators perform common mathematical calculations. They are binary operators, meaning they operate on two operands.
+: Addition (e.g.,5 + 3results in8).-: Subtraction (e.g.,10 - 4results in6).*: Multiplication (e.g.,6 * 2results in12)./: Division (e.g.,10 / 2results in5).Integer Division: If both operands are integers, the result is an integer (truncating any fractional part).
7 / 3results in2.Floating-Point Division: If at least one operand is a floating-point type, the result is a floating-point number.
7.0 / 3results in2.333....
%: Modulus (remainder of integer division). Works only with integer operands. (e.g.,7 % 3results in1).
ARITHMETIC ASSIGNMENT
Compound Assignment Operators: These operators combine an arithmetic operation with an assignment operation, providing a shorthand for modifying a variable's value. They improve conciseness and often readability.
Examples (assume
x = 10initially):x += 5;is equivalent tox = x + 5;(x becomes 15)x -= 3;is equivalent tox = x - 3;(x becomes 7)x *= 2;is equivalent tox = x * 2;(x becomes 20)x /= 4;is equivalent tox = x / 4;(x becomes 2)x %= 3;is equivalent tox = x % 3;(x becomes 1)
INCREMENT AND DECREMENT OPERATORS
Usage: These unary operators increase or decrease the value of a variable by one. They have both prefix and postfix forms, which differ in when the value is updated in the context of an expression.
Increment Operator (
++):Postfix (x++): The value of
xis used in the expression first, and thenxis incremented.int a = 5; int b = a++; // b is 5, a is 6Prefix (++x): The value of
xis incremented first, and then the new value is used in the expression.int a = 5; int b = ++a; // b is 6, a is 6
Decrement Operator (
--):Postfix (x--): The value of
xis used in the expression first, and thenxis decremented.int a = 5; int b = a--; // b is 5, a is 4Prefix (--x): The value of
xis decremented first, and then the new value is used in the expression.int a = 5; int b = --a; // b is 4, a is 4
OPERATOR PRECEDENCE
Order of Evaluation: Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. Operators of the same precedence are evaluated according to their associativity (usually left-to-right).
General Order (Highest to Lowest):
Parentheses
(): Expressions inside parentheses are evaluated first.Unary Operators: Increment/Decrement (
++,--), Unary+and-, Logical NOT!.Multiplication/Division/Modulus (
*,/,%): Evaluated next, from left to right.Addition/Subtraction (
+,-): Evaluated next, from left to right.Assignment Operators (
=,+=,-=, etc.): Evaluated last, generally right-to-left.
Example: In
, multiplication (*) has higher precedence than addition (+), sob * cis evaluated first.
TYPE CONVERSION
Details: Type conversion (also known as type coercion or implicit conversion) occurs automatically when operands of different data types are involved in an expression. The compiler converts the "smaller" or less precise type to the "larger" or more precise type to avoid loss of data.
Promotion: When converting from a smaller type to a larger type (e.g.,
inttofloat,floattodouble), data is generally preserved.Demotion (Data Loss): When converting from a larger type to a smaller type (e.g.,
doubletoint,inttochar), data loss or unpredictable results can occur. For example, converting afloatto anintwill truncate the decimal part.
Type Promotion Rules in C:
charandshort intare promoted tointin expressions.If one operand is
long double, the other is converted tolong double.Else, if one operand is
double, the other is converted todouble.Else, if one operand is
float, the other is converted tofloat.Else, if one operand is
unsigned long int, the other is converted tounsigned long int.(Further rules for
long,unsigned int, etc.)
TYPE CASTING
Syntax: Type casting (also known as explicit conversion) allows programmers to manually specify a type conversion using the syntax
(type) expression. This forces an expression to be treated as a different data type.Purpose:
To override implicit type conversions.
To perform specific calculations where temporary type changes are needed (e.g., to force floating-point division when both operands are integers:
(float)numerator / denominator).To ensure correct function arguments or return types.
Example: If
int x = 7, y = 3;, thenx / yresults in2. But(float)x / yresults in2.333..., becausexis temporarily converted tofloat, which then forcesyto also be converted tofloatfor division.
MATHEMATICAL FUNCTIONS
Defined in Libraries: C provides a rich set of mathematical functions, primarily defined in the
<math.h>header for floating-point operations and<stdlib.h>for some integer-related functions. Most functions in<math.h>operate on and returndoubletypes.Key Functions from
<math.h>(operates in radians for angles):sqrt(x): Square root ofx().pow(base, exp):baseraised to the power ofexp().sin(angle),cos(angle),tan(angle): Trigonometric functions.log(x): Natural logarithm ().log10(x): Base-10 logarithm ().ceil(x): Smallest integer greater than or equal tox(rounds up).floor(x): Largest integer less than or equal tox(rounds down).fabs(x): Absolute value of a floating-point number.
Key Functions from
<stdlib.h>:abs(x): Absolute value of an integer.rand(),srand(): For random number generation.
SUMMARY
This detailed note covers essential programming concepts including Data Types and their categories (primitive, derived, user-defined), how Variables & Constants are used to store data, the organization of Memory Allocation in a program, proper Naming Conventions and rules for identifiers, and the use of Symbolic Constants for readability. It also delves into specific Primitive Data Types like integers, real numbers, and characters, explaining their ranges and sizes. Finally, it details Format Specifiers for I/O, various Arithmetic Operators (including compound assignment, increment, and decrement), clear rules for Operator Precedence, methods of Type Conversion and explicit Type Casting, and common Mathematical Functions available in C libraries. Understanding these concepts is crucial for writing efficient, correct, and readable C programs.