C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Output C Programming Data On The Screen

In this tutorial, we will learn how to output various types of data on the screen in C programming language. The printf() function is the most commonly used function for displaying data on the screen.

printf() function:

The printf() function is part of the standard I/O library (stdio.h) and is used to output formatted text on the screen. The function takes a format string as its first argument, followed by a variable number of arguments representing the data to be displayed.

The syntax for the printf() function is:

int printf(const char *format, ...);

Here, format is a string that contains placeholders for the data, and ... represents the additional arguments, which correspond to the placeholders in the format string.

Placeholders and format specifiers:

Placeholders in the format string are preceded by a % character and followed by a format specifier that indicates the type of data to be displayed. Some common format specifiers are:

  • %d or %i: Display an integer value.
  • %u: Display an unsigned integer value.
  • %f: Display a floating-point value.
  • %lf: Display a double-precision floating-point value.
  • %c: Display a character.
  • %s: Display a string.
  • %%: Display a literal '%' character.

Example: Outputting various types of data:

#include <stdio.h>

int main() {
  int num = 42;
  float pi = 3.14159f;
  double e = 2.718281828459045;
  char initial = 'A';
  const char *message = "Hello, world!";

  printf("The number is: %d\n", num);
  printf("The float value of pi is: %f\n", pi);
  printf("The double value of e is: %lf\n", e);
  printf("The initial is: %c\n", initial);
  printf("The message is: %s\n", message);

  return 0;
}

Output:

The number is: 42
The float value of pi is: 3.141590
The double value of e is: 2.718282
The initial is: A
The message is: Hello, world!

In the example above, we use the printf() function to display various types of data, including integers, floating-point numbers, double-precision floating-point numbers, characters, and strings.

In summary, to output various types of data on the screen in C programming language, you can use the printf() function with appropriate placeholders and format specifiers. This allows you to display different data types in a formatted and readable manner.

  1. Using printf for Formatted Output in C:

    #include <stdio.h>
    
    int main() {
        // Using printf for formatted output
        int num = 42;
        printf("The answer is: %d\n", num);
        return 0;
    }
    

    Output:

    The answer is: 42
    

    printf is a powerful function for formatted output in C.

  2. Print Statements and Display Data in C Language:

    #include <stdio.h>
    
    int main() {
        // Print statements to display data
        float value = 3.14;
        printf("The value of PI is: %f\n", value);
        return 0;
    }
    

    Output:

    The value of PI is: 3.140000
    

    printf can handle various data types, including floats.

  3. C Code Examples Demonstrating Output to the Screen:

    #include <stdio.h>
    
    int main() {
        // Code examples demonstrating output to the screen
        printf("Hello, ");
        printf("World!\n");
        return 0;
    }
    

    Output:

    Hello, World!
    

    Multiple printf statements can be used for sequential output.

  4. Formatting Options in printf for Various Data Types in C:

    #include <stdio.h>
    
    int main() {
        // Formatting options in printf for various data types
        int integerVal = 123;
        float floatVal = 3.14159;
        char charVal = 'A';
    
        printf("Integer: %d, Float: %.2f, Char: %c\n", integerVal, floatVal, charVal);
        return 0;
    }
    

    Output:

    Integer: 123, Float: 3.14, Char: A
    

    %d, %.2f, %c, etc., are format specifiers for different data types.

  5. Writing to Standard Output in C Programming:

    #include <stdio.h>
    
    int main() {
        // Writing to standard output
        fprintf(stdout, "This is written to standard output.\n");
        return 0;
    }
    

    Output:

    This is written to standard output.
    

    fprintf allows writing to different output streams, and stdout is the standard output.

  6. Escape Sequences for Special Characters in C Output:

    #include <stdio.h>
    
    int main() {
        // Escape sequences for special characters
        printf("This is a new line.\n");
        printf("This is a tab\tcharacter.\n");
        return 0;
    }
    

    Output:

    This is a new line.
    This is a tab    character.
    

    Escape sequences (\n, \t, etc.) provide special formatting in the output.

  7. Error Handling in Output Statements in C:

    #include <stdio.h>
    
    int main() {
        // Error handling in output statements
        int result = printf("This is a valid statement.\n");
        if (result < 0) {
            fprintf(stderr, "Error during output.\n");
            return 1;
        }
        return 0;
    }
    

    printf returns the number of characters printed. Negative values indicate errors.

  8. Writing to Files vs Printing to Console in C:

    #include <stdio.h>
    
    int main() {
        // Writing to files vs printing to console
        FILE *file = fopen("output.txt", "w");
        if (file != NULL) {
            fprintf(file, "This is written to a file.\n");
            fclose(file);
            printf("Data written to file.\n");
        } else {
            fprintf(stderr, "Error opening the file.\n");
        }
        return 0;
    }
    

    Output:

    Data written to file.
    

    fprintf can be used to write to files instead of the console.