C Programming Data Types, Storage Classes, and Input Output Functions

C Data Types and Classification

  • Data types indicate the specific type of data that a variable can hold in memory, which can be numeric or non-numeric in nature.

  • The basic, built-in, primary, or fundamental data types in C are:

    • int

    • float

    • double

    • char

    • void


Basic C Data Types
  • Data types in C are broadly categorized into three distinct groups:

    • Built-in Datatypes: int, float, char, double, void

    • Derived Datatypes: Array, functions

    • User-Defined Datatypes: Enum, Struct, union


C Datatypes Classification Hierarchy
  • Overview of basic data types, keywords, and memory allocations:

    • Integer: Keyword int, Memory size: 2 bytes2\,bytes

    • Real (floating-point): Keyword float, Memory size: 4 bytes4\,bytes

    • Double precision: Keyword double, Memory size: 8 bytes8\,bytes

    • Character: Keyword char, Memory size: 1 byte1\,byte

    • Non-specific: Keyword void, Memory size: --


Basic Data Types Keywords and Sizes

Detailed Characteristics of Basic Data Types

  • Integer (int) Data Type:

    • Keyword used to represent standard whole numbers without decimal components.

    • An integer is defined as a sequence of digits without a decimal point.

    • The numeric range of an integer depends on the word length of the computer system.

    • Word length refers to the number of bits that can be accessed simultaneously by the processor.

    • Standard allocation: 16 bits16\,bits (2 bytes2\,bytes) of memory.

    • Declaration example: int num;

    • Valid Examples: -248, 14042, 27246, 0, +1996

    • Invalid Examples: 3,333 (contains illegal comma), -34.0 (contains decimal point), +3,468.3 (contains comma and decimal point), 99999999 (exceeds standard 16-bit integer boundary)


Valid and Invalid Examples of Integer Constants
  • Floating-Point (float) Data Type:

    • Keyword used to indicate real numbers containing fractional components.

    • Provides precision up to 6 digits6\,digits

    • Standard allocation: 32 bits32\,bits (4 bytes4\,bytes) of memory.

    • Declaration example: float num;

    • Basic examples: 23.05, 56.5, 32

    • Valid Examples: -248.45, 1.4042 E + 02, 27246, 0

    • Invalid Examples: 3,333 (contains comma), -34.0 (listed as invalid integer), +3,468.3 (contains comma), 99999999


Valid and Invalid Examples of Floating-Point Constants
  • Character (char) Data Type:

    • Keyword used to specify individual character values.

    • Standard allocation: 8 bits8\,bits (1 byte1\,byte) of memory.

    • Character constants must be enclosed within a pair of single quotes (apostrophes).

    • Valid character examples: 'a', 'm', '$', '%', '1', '5'

    • Declaration and assignment example: char gender; gender=\'M\';

    • String Constant: A sequence of characters enclosed within a pair of double quotes, automatically terminated by a null character ('\0'). Examples include "Program" and "2k8".

  • Double Precision (double) Data Type:

    • Keyword used to denote double-precision floating-point numbers.

    • Standard allocation: 64 bits64\,bits (8 bytes8\,bytes) of memory.

    • Precision directly correlates with data accuracy: while float stores up to 6 digits6\,digits after the decimal point, double stores up to 16 digits16\,digits after the decimal point.

    • Value example: 123.00000000000000000

    • Declaration example: double num;

  • Void (void) Data Type:

    • Represents a non-specific or empty type that stores nothing.

    • Expressly informs the compiler that no value is to be returned or expected.

Memory Sizes and Ranges of Data Types

  • Detailed memory allocation and minimal ranges across standard data types and modifiers:

    • char: Size: 8 bits8\,bits, Minimal Range: −128 to 127-128\text{ to }127

    • unsigned / unsigned char: Size: 8 bits8\,bits, Minimal Range: 0 to 2550\text{ to }255

    • signed char: Size: 8 bits8\,bits, Minimal Range: −128 to 127-128\text{ to }127

    • int: Size: 16 bits16\,bits, Minimal Range: −32,768 to 32,767-32,768\text{ to }32,767

    • unsigned int: Size: 16 bits16\,bits, Minimal Range: 0 to 65,5350\text{ to }65,535

    • signed int: Size: 16 bits16\,bits, Minimal Range: −32,768 to 32,767-32,768\text{ to }32,767 (Same as int)

    • short int: Size: 16 bits16\,bits, Minimal Range: Same as int

    • unsigned short int: Size: 8 bits8\,bits, Minimal Range: 0 to 65,5350\text{ to }65,535

    • signed short int: Size: 8 bits8\,bits, Minimal Range: Same as short int

    • long int: Size: 32 bits32\,bits, Minimal Range: −2,147,483,647 to 2,147,483,647-2,147,483,647\text{ to }2,147,483,647

    • signed long int: Size: 32 bits32\,bits, Minimal Range: 0 to 4,294,967,2950\text{ to }4,294,967,295

    • unsigned long int: Size: 32 bits32\,bits, Minimal Range: 0 to 4,294,967,2950\text{ to }4,294,967,295

    • float: Size: 32 bits32\,bits, Precision: 6 to 7 digits6\text{ to }7\text{ digits}

    • double: Size: 64 bits64\,bits, Precision: 15 to 16 decimal digits total15\text{ to }16\text{ decimal digits total}

    • long double: Size: 128 bits128\,bits, Precision: 18\text{ to }21\text{ decimal digits on most compilers

Backslash Constants and Escape Sequences

  • Backslash character constants (escape sequences) are special character combinations used primarily within output functions.

  • Although written as two characters starting with a backslash (\), they are interpreted as a single character entity.

  • Summary of backslash constants and their functions:

    • '\a': Audible Alert (Bell)

    • '\b': Backspace

    • '\f': Form feed

    • '\n': New Line

    • '\r': Carriage Return

    • '\t': Horizontal tab

    • '\v': Vertical Tab

    • '\'': Single Quote

    • '\"': Double Quote

    • '\?': Question Mark

    • '\\': Back Slash

    • '\0': Null


Backslash Constants and Escape Sequences

Data Type Modifiers, Qualifiers, and Constant Declarations

  • Data Type Modifiers / Qualifiers:

    • Modifiers are keywords used to alter the storage size or signedness behavior of standard primitive data types.

    • The primary modifiers in C are signed, unsigned, short, and long.

    • signed:

    • Applied predominantly to integer and character types. Default integer declarations assume signed values.

    • For signed character types, values range from −128-128 to 127127.

    • unsigned:

    • Applied to int and char, as well as in combination with long or short.

    • Restricts values to non-negative integers; for character variables, unsigned values range from 00 to 255255.

    • long:

    • Applied to int and double data types.

    • Doubles the bit-length of the underlying storage (e.g., transforming a 16-bit int into a 32-bit long int).

    • short:

    • Reduces or constrains the storage requirement of an integer type.

  • Declaring Variables as Constant (const):

    • Ensures that a variable's assigned value remains fixed throughout the entire execution of the program.

    • Established by prepending the variable declaration with the const qualifier during initialization.

    • Example: const int class_size = 69;

  • Declaring Variables as Volatile (volatile):

    • Applied to variables whose values can be altered externally at any point without direct action from code execution.

    • Signals to the compiler that the variable's value must always be re-read from memory directly because it may change unexpectedly.

    • Example: volatile int num=125;

  • Symbolic Constants (#define):

    • Symbolic constants are established via preprocessor directives and replace declared symbolic names with fixed constant values throughout the program prior to compilation.

    • Preprocessor statements begin with # and must not end with a semicolon.

    • Syntax format: #define symbolic_name value_of_constant

    • Valid Preprocessor Examples:

    • #define X 25

    • #define PASS_MARK 50

    • #define PI 3.14159

    • #define NAME "NTTF"

    • Invalid Preprocessor Examples:

    • #define X=25 (illegal use of assignment operator =)

    • # define X 25 (illegal whitespace between # and define)

    • #define X 25; (illegal trailing semicolon ;)

    • #define X 25,Y 35 (illegal comma separator for multiple assignments)

    • #Define X 25 (illegal capitalization of keyword Define)

    • #define $ 25 (illegal character $ used as identifier)


Valid and Invalid Examples of Symbolic Constants
  • Delimiters (Separators):

    • Symbols that delimit boundaries between syntactic elements within a C program.

    • Function to separate constants, variables, expressions, and statements.

    • Frequently used delimiters include: Comma (,), Semicolon (;), Blankspace, Single quote ('), Double quote (").

Storage Classes in C

  • A variable's storage class defines four fundamental properties:

    1. Storage Location: The physical region of memory where the variable resides.

    2. Initial Value: The default value assigned prior to explicit initialization.

    3. Scope: The visibility and accessibility region of the variable within the source code.

    4. Life (Lifetime): The duration over which the variable retains allocated memory during program run-time.

  • The Four Storage Classes:

    • Automatic (auto):

    • Default storage class for all local variables declared inside functions or code blocks.

    • Storage Place: RAM (Stack)

    • Initial Value: Garbage value

    • Scope: Local within the block { } where declared

    • Life: Created upon block execution entry; destroyed upon block execution exit

    • Static (static):

    • Used to preserve variable state across repeated block or function invocations.

    • Storage Place: RAM (Data segment / Heap)

    • Initial Value: Zero (00)

    • Scope: Local within the declared block

    • Life: Retained continuously until the end of program execution

    • Special Property: Retains its last modified value even after exiting its block/function scope

    • Register (register):

    • Requests local storage within CPU registers for ultra-fast access speed.

    • Storage Place: CPU Register (internal CPU memory, not RAM)

    • Initial Value: Garbage value

    • Scope: Local within the declared block { }

    • Life: Active until execution exits the containing block

    • External (extern):

    • References global variables declared outside the current block or in separate source files.

    • Storage Place: RAM (Data segment)

    • Initial Value: Zero (00)

    • Scope: Global across multiple files in the entire application

    • Life: Retained throughout the entire program lifetime

    • Special Property: Acts as a reference declaration only; does not allocate new memory space


Storage Classes in C Summary Table

Input and Output (I/O) Functions

  • Input and Output Concepts:

    • Input denotes receiving or accepting external data into a program (via keyboard, command line, or files).

    • Output denotes transmitting or presenting processed data (to display screen, printer, or storage files).

    • C standard I/O library functions are housed inside the <stdio.h> header file (Standard Input Output Header File).

  • Categorization of Standard I/O Functions:

    • Console I/O Functions: Perform data operations strictly using the system keyboard for input and the display monitor for output.

    • Disk I/O Functions: Perform data transfer operations on secondary storage devices such as hard disks or floppy drives.

    • Port I/O Functions: Handle direct hardware data transfer operations across hardware interfaces such as printer or mouse ports.

  • Sub-Classification of Console I/O:

    • Formatted Console I/O

    • Unformatted (Non-Formatted) Console I/O

Formatted Console I/O Functions

  • Formatted I/O functions allow users to read or display data formatted according to specific conversion specifications (format specifiers).

  • They support all primitive and complex data types (int, float, char, strings, etc.) and provide user-friendly layout control.

  • printf() Function:

    • Pre-defined output function declared in <stdio.h> used to print formatted output to the console monitor.

    • Syntax: printf("format string", arg1, arg2, ...);

    • Example Implementation:

#include <stdio.h>
int main() {
    // Creating an int type variable
    int a = 20;

    // Printing the value of a variable
    printf("%d\n", a);

    // Printing a string
    printf("This is a string");
    return 0;
}
  • Execution Output:

20
This is a string
  • scanf() Function:

    • Pre-defined input function declared in <stdio.h> used to read formatted data from the keyboard.

    • Incorporates the address-of operator & to directly assign input values to the target variable's memory address.

    • Syntax: scanf("Format Specifier", &var1, &var2, ...);

  • sprintf() Function:

    • Stands for "string print". Formats and writes data into a target character array (string buffer) instead of printing to the console screen.

    • Syntax: sprintf(array_name, "format specifier", variable_name);

    • Example Usage:

char note[50];
sprintf(note, "My age is %d", 21);
printf("%s", note); // Output: My age is 21
  • Multi-variable formatting example: sprintf(str, "Age = %d and Marks = %.1f", age, marks);

    • sscanf() Function:

  • Stands for "string scanf". Reads formatted data from an existing character array/string buffer rather than waiting for keyboard input.

  • Syntax: sscanf(array_name, "format specifier", &variable_name);

  • Example Usage:

char data[] = "Arjun 21"; // existing string text
char name[20];
int age;
sscanf(data, "%s %d", name, &age);
printf("%s is %d years old", name, age); // Output: Arjun is 21 years old
  • Multi-variable reading example: sscanf(data, "%d %f", &age, &marks);

Unformatted Console I/O Functions

  • Unformatted I/O functions operate strictly on single character values or character arrays/strings (char).

  • They do not support format specifiers or custom output formatting layout controls.

  • getch() Function:

    • Declared in <conio.h>.

    • Reads a single character input from the keyboard.

    • Does not display (echo) the typed character on the console screen.

    • Returns instantly without requiring the user to press the Enter key.

    • Commonly utilized to hold or pause the execution screen.

    • Syntax: getch();

    • Code Example:

#include <conio.h>
#include <stdio.h>
int main() {
    printf("Enter any character: ");
    // Reads a character but does not display it
    getch();
    return 0;
}
  • getche() Function:

    • Declared in <conio.h>.

    • Reads a single character from the keyboard and immediately displays (echoes) the character on the console screen.

    • Returns immediately without waiting for an Enter key press.

    • Syntax: getche();

    • Code Example:

#include <conio.h>
#include <stdio.h>
int main() {
    printf("Enter any character: ");
    // Reads a character and displays it immediately
    getche();
    return 0;
}
  • getchar() Function:

    • Declared in <stdio.h>.

    • Reads only the first single character from the input buffer, regardless of how many characters were typed.

    • Waits for and requires the Enter key to be pressed before proceeding.

    • Syntax: ch = getchar();

    • Code Example:

#include <stdio.h>
int main() {
    char ch;
    printf("Enter any character: ");
    ch = getchar();
    printf("You entered: %c\n", ch);
    return 0;
}
  • putchar() Function:

    • Declared in <stdio.h>.

    • Outputs a single character at a time to the console display by accepting a direct character constant or character variable argument.

    • Syntax: putchar(variable_name);

    • Code Example:

#include <conio.h>
#include <stdio.h>
int main() {
    char ch;
    printf("Enter any character: ");
    ch = getchar();
    putchar(ch);
    return 0;
}
  • putch() Function:

    • Declared in <conio.h>.

    • Displays a single specified character at the current active cursor location on the console screen.

    • Syntax: putch(variable_name);

    • Code Example:

#include <conio.h>
#include <stdio.h>
int main() {
    char ch;
    printf("Enter any character:\n ");
    ch = getch();
    printf("\nEntered character is: ");
    putch(ch);
    return 0;
}
  • Additional string-based unformatted I/O functions include gets() (for reading character arrays/strings) and puts() (for writing character arrays/strings).

Formatted vs. Unformatted I/O Functions

  • Comparative analysis of Formatted and Unformatted Console I/O Functions:

    • Formatting Flexibility:

    • Formatted I/O: Allows users to input or display data in custom formats.

    • Unformatted I/O: Does not allow custom user-defined formatting.

    • Format Specifier Support:

    • Formatted I/O: Fully supports format specifiers (e.g., %d, %f, %c, %s).

    • Unformatted I/O: Does not support format specifiers.

    • User Friendliness:

    • Formatted I/O: Highly user-friendly for structured data presentation.

    • Unformatted I/O: Less user-friendly for complex data.

    • Supported Data Types:

    • Formatted I/O: Compatible with all basic and derived data types (int, float, char, double, etc.).

    • Unformatted I/O: Restricted exclusively to char and string data types.

    • Representative Functions:

    • Formatted I/O: printf(), scanf(), sprintf(), sscanf()

    • Unformatted I/O: getch(), getche(), getchar(), putchar(), putch(), gets(), puts()


Formatted vs Unformatted I/O Functions Comparison