ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-112Literals Signal Handli…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    7 min read
    Topic 36 of 39
    Topic Study Note Illustration
    36

    Suffixes for Integer and Floating-Point Literals, Signal Handling

    In C programming, understanding how to properly define integer and floating-point literals with the correct suffixes and handling signals is important for managing data types and program...

    Suffixes for Integer and Floating-Point Literals, Signal Handling

    In C programming, understanding how to properly define integer and floating-point literals with the correct suffixes and handling signals is important for managing data types and program flow control. Let's explore both topics in detail.


    1. Suffixes for Integer and Floating-Point Literals

    C allows us to specify integer and floating-point literals with suffixes to define their type. These suffixes help the compiler recognize the intended type of the literal, especially in cases where type conversions may occur or the precision needs to be explicitly stated.


    Integer Literals and Their Suffixes

    Integer literals can have suffixes that indicate their type. The main suffixes are:

    • U or u: Used to specify an unsigned integer literal.
    • L or l: Used to specify a long integer literal.
    • UL or ul: Used to specify an unsigned long integer literal.
    • LL or ll: Used to specify a long long integer literal (introduced in C99).
    Examples:
    #include <stdio.h>
    
    int main() {
        // Integer literals with suffixes
        unsigned int u = 123U;         // Unsigned integer
        long l = 123L;                 // Long integer
        unsigned long ul = 123UL;      // Unsigned long integer
        long long ll = 123LL;          // Long long integer
    
        // Output the variables
        printf("Unsigned int: %u\n", u);
        printf("Long: %ld\n", l);
        printf("Unsigned long: %lu\n", ul);
        printf("Long long: %lld\n", ll);
    
        return 0;
    }
    

    Explanation:

    • 123U: This literal represents an unsigned integer.
    • 123L: This literal is a long integer.
    • 123UL: This is an unsigned long integer.
    • 123LL: This is a long long integer.

    Without these suffixes, the default integer type would be int.

    Integer Literal Types:

    • By default, an integer literal (e.g., 123) is considered to be of type int unless otherwise specified.
    • The suffixes help in cases where the default type might not match the intended size or sign (such as long vs. int or unsigned vs. signed).

    Floating-Point Literals and Their Suffixes

    Floating-point literals can have suffixes as well. The main suffixes for floating-point literals are:

    • F or f: Used to specify a float literal (i.e., a single-precision floating-point number).
    • L or l: Used to specify a long double literal (i.e., a double-precision floating-point number with extended precision).
    Examples:
    #include <stdio.h>
    
    int main() {
        // Floating-point literals with suffixes
        float f = 3.14F;         // Float literal
        double d = 3.14;         // Default is double precision
        long double ld = 3.14L;  // Long double literal
    
        // Output the variables
        printf("Float: %.2f\n", f);
        printf("Double: %.2f\n", d);
        printf("Long double: %.2Lf\n", ld);
    
        return 0;
    }
    

    Explanation:

    • 3.14F: This is a float literal.
    • 3.14: This is a double literal, as the default floating-point type in C is double.
    • 3.14L: This is a long double literal, which has extended precision over double.

    Floating-Point Literal Types:

    • By default, floating-point literals are considered double.
    • The suffixes are useful when you need a specific precision:
      • float is typically used when memory and precision constraints are critical.
      • long double is used when you need higher precision than double.

    2. Signal Handling in C

    Signals are a mechanism used in C to notify a program that a specific event or condition has occurred. Signals can be generated by hardware, the operating system, or other programs. In C, we can use the signal handling mechanism to capture and respond to certain signals, such as interruptions or errors.

    Common Signals in C:

    • SIGINT: Generated when the user interrupts the program, usually by pressing Ctrl+C.
    • SIGFPE: Raised when an arithmetic exception occurs, such as division by zero.
    • SIGSEGV: Raised when a program attempts to access restricted memory, such as a segmentation fault.
    • SIGTERM: Sent to terminate a program (commonly by the operating system).
    • SIGILL: Raised when an illegal instruction is encountered.
    • SIGABRT: Raised when a program calls abort().

    Signal Handling Functions:

    In C, the signal() function is used to define custom handlers for specific signals.

    Syntax:
    #include <signal.h>
    
    void (*signal(int sig, void (*handler)(int)))(int);
    
    • sig: The signal number to handle.
    • handler: A pointer to the function that will handle the signal. This function should take a single int argument (the signal number).

    Example: Handling SIGINT (Ctrl+C Interrupt)

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    // Signal handler function
    void handle_signal(int sig) {
        printf("Signal %d received. Program will exit gracefully.\n", sig);
        exit(0);  // Exit the program gracefully
    }
    
    int main() {
        // Register the signal handler for SIGINT (Ctrl+C)
        signal(SIGINT, handle_signal);
    
        printf("Program running. Press Ctrl+C to interrupt.\n");
    
        while (1) {
            // Infinite loop to keep the program running
        }
    
        return 0;
    }
    

    Explanation:

    • signal(SIGINT, handle_signal): This registers the handle_signal() function as the handler for the SIGINT signal, which occurs when the user presses Ctrl+C.
    • When the user presses Ctrl+C, the handle_signal() function is invoked, printing a message and gracefully terminating the program using exit(0).

    Other Signal Functions:

    • raise(int sig): Sends a signal to the current process.
    • abort(): Raises the SIGABRT signal and terminates the program immediately.
    • kill(pid_t pid, int sig): Sends a signal to a specific process.

    Signal Handling with sigaction():

    The sigaction() function provides more advanced and reliable signal handling than signal(). It allows for more control over the signal handling behavior, including signal masking and blocking.

    Syntax of sigaction:
    #include <signal.h>
    
    int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
    
    • signum: The signal to be handled.
    • act: A pointer to a struct sigaction that defines the new signal handling action.
    • oldact: A pointer to a struct sigaction where the previous action is stored.
    Example using sigaction():
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    // Signal handler function
    void handle_signal(int sig) {
        printf("Caught signal %d\n", sig);
        exit(0);
    }
    
    int main() {
        struct sigaction sa;
    
        // Set up the signal handler
        sa.sa_handler = handle_signal;
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);  // Initialize the signal set to be empty
    
        // Register the signal handler for SIGINT (Ctrl+C)
        sigaction(SIGINT, &sa, NULL);
    
        printf("Program running. Press Ctrl+C to interrupt.\n");
    
        while (1) {
            // Infinite loop to keep the program running
        }
    
        return 0;
    }
    

    Explanation of sigaction:

    • The sigaction structure defines how the signal should be handled.
    • sa.sa_handler = handle_signal;: The signal handler function is set.
    • sigemptyset(&sa.sa_mask);: Ensures that no signals are blocked during the handling of SIGINT.

    Summary Table

    Concept Description
    Integer Suffixes U for unsigned, L for long, UL for unsigned long, LL for long long
    Floating-Point Suffixes F for float, L for long double
    Signal Handling The signal() function allows defining custom handlers for signals like SIGINT.
    Signal Functions abort(), raise(), kill(), sigaction() offer various ways to manage signals.
    atexit() Registers a function to be called at normal program termination.

    By using suffixes appropriately, you can ensure the correct data type is used for literals, and with signal handling, you can manage events like user interruptions or errors more gracefully in your program.

    ← Previous topic 35
    Compiling Multiple-Source-File Programs, Program Termination with exit and atexit
    Next topic 37 →
    Dynamic Memory Allocation: calloc and realloc, goto

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals