ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CC-112
    Progress0 / 39 topics
    Topics
    1. Introduction to Problem Solving, Algorithms, Programming, and C Language2. Problem Solving, a brief review of Von-Neumann Architecture3. The C Programming Language, Pseudo-code, Concept of Variable4. Data types in Pseudo-code, The C Standard Library and Open Source5. Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6. Concept of Integer division, Flowchart and its notations7. Typical C Program Development Environment, Role of Compiler and Linker8. Test Driving C Application9. Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer10. Memory Concepts, Arithmetic in C, Operators11. Decision Making: Equality and Relational Operators12. Structured Program Development: The if, if...else, while Nested Control Statements13. Program Control: for, switch, do...while, break, continue, Logical Operators14. Functions: Modularizing Program in C, Math Library Functions15. Function Definitions and Prototypes, Function-Call Stack and Stack Frames16. Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17. Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration18. Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays19. Passing Arrays to Function, Sorting and Searching Arrays20. Multidimensional and Variable Length Arrays21. Pointers: Pointer Definitions and Initialization, Pointer Operators22. Passing Arguments to Function by Reference, Using the const and sizeof Operator23. Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24. Function Pointers25. Characters and Strings: Strings and Characters, Character Handling Library26. String Functions, Library Functions27. Formatted Input/Output: Streams, Formatted Output with printf, Formatted Input with scanf28. Structures: Defining Structures, Accessing Structure Member, Structures and Functions29. typedef, Unions30. Bit Manipulation and Enumeration: Bitwise Operators, Bit Fields, Enumeration Constants31. File Processing: Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File32. Preprocessor: #include, #define, Conditional Compilation, #error and #pragma33. # and ## Operators, Predefined Symbolic Constants, Assertions34. Other Topics: Variable Length Argument List, Using Command Line Arguments35. Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36. Suffixes for Integer and Floating-Point Literals, Signal Handling37. Dynamic Memory Allocation: calloc and realloc, goto38. Advance Topics: Self-Referential Structures, Linked Lists39. Efficiency of Algorithms, Selection and Insertion Sort
    CC-112›Suffixes for Integer and Floating-Point Literals, Signal Handling
    Programming FundamentalsTopic 36 of 39

    Suffixes for Integer and Floating-Point Literals, Signal Handling

    7 minread
    1,249words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,249
      Code examples0
      DifficultyIntermediate