Input Output statements in C

0

introduction Input output statements in c


Input/output (I/O) statements are an essential part of any programming language, and C is no exception. In C programming, we use input/output statements to read input from the user or from a file and to write output to the console or to a file. Here are some examples of input/output statements in C:

'printf()' statement: This statement is used to print output to the console. It takes a string as an argument and can also take format specifiers to format the output. Here's an example: 

#include<stdio.h>

int main() {
   printf("Hello, world!\n");
   return 0;
}
In this example, the 'printf()' statement is used to print the string "Hello, world!" to the console. The '\n' character is used to insert a newline after the string.

'scanf()' statement: This statement is used to read input from the user. It takes one or more format specifiers as arguments to specify the type of data to be read. Here's an example:

#include<stdio.h>

int main() {
   int num;
   printf("Enter a number: ");
   scanf("%d", &num);
   printf("You entered: %d\n", num);
   return 0;
}
In this example, the 'scanf()' statement is used to read an integer value from the user, which is stored in the variable num. The 'printf()' statement is then used to print the value of num to the console.

'fopen()', 'fread()', 'fwrite()', and 'fclose()' statements: These statements are used to perform file input/output operations in C. The 'fopen()' statement is used to open a file, 'fread()' and 'fwrite()' statements are used to read and write data to/from a file, and 'fclose()' statement is used to close the file. Here's an example:

#include<stdio.h>

int main() {
   FILE *fp;
   char str[100];

   fp = fopen("file.txt", "w+");
   printf("Enter a string: ");
   gets(str);
   fwrite(str, sizeof(char), strlen(str), fp);
   fseek(fp, 0, SEEK_SET);
   fread(str, sizeof(char), strlen(str), fp);
   printf("Data read: %s\n", str);
   fclose(fp);

   return 0;
}
In this example, we first declare a file pointer fp and a character array str. We then open a file named "file.txt" in write mode using the 'fopen()' statement. We use the 'gets()' function to read a string from the user, and we write the string to the file using the 'fwrite()' statement. We then use the 'fseek()' function to set the file position to the beginning of the file and read the data from the file using the 'fread()' statement. Finally, we print the data read from the file using the 'printf()' statement and close the file using the 'fclose()' statement.

In summary, input/output statements are an essential part of C programming and are used extensively for reading input from the user, writing output to the console, and performing file input/output operations.

I/O Functions
Input/output (I/O) functions in C are used to read data from an input source (such as the keyboard or a file) and write data to an output destination (such as the screen or a file). In C, there are several standard I/O functions that are commonly used:

printf() - used to display output to the screen or console.

scanf() - used to read input data from the keyboard or another input stream.

getchar() - used to read a single character from the input stream.

putchar() - used to write a single character to the output stream.

fgets() - used to read a line of text from an input stream.

fputs() - used to write a line of text to an output stream.

fprintf() - used to format and write output to a file.

fscanf() - used to read formatted input from a file.

These functions can be used to read and write various types of data, including integers, floating-point numbers, characters, and strings. The specific function used will depend on the type of data being read or written, as well as the source and destination of the data.

In addition to the standard I/O functions, C also provides functions for working with files, such as fopen(), fclose(), fread(), and fwrite(). These functions are used to open and close files, read and write data to and from files, and perform other operations on files.

Formatted I/O Functions:
Formatted I/O functions in C are used to read and write data in a specific format, such as a string, a floating-point number, or an integer. The formatted I/O functions in C include:

printf() - used to write formatted output to the standard output stream, such as the console or terminal window.

scanf() - used to read formatted input from the standard input stream, such as the keyboard.

fprintf() - used to write formatted output to a file.

fscanf() - used to read formatted input from a file.

These functions allow the programmer to specify a format string, which contains a combination of literal text and conversion specifiers. The conversion specifiers tell the function how to format and interpret the input or output data.

For example, the %d conversion specifier is used to read or write integers in decimal format, while the %f conversion specifier is used to read or write floating-point numbers. The %s conversion specifier is used to read or write strings of characters.

Formatted I/O functions in C provide a powerful way to manipulate and display data in a wide variety of formats. However, care must be taken to ensure that the input data matches the format specified in the format string, or the program may produce unexpected results or even crash.

Unformatted I/O Functions:
In C, unformatted I/O functions are used to read and write data as binary data without any formatting. They allow for direct input and output of data in its raw binary form. The most commonly used unformatted I/O functions in C are:

fread() - used to read data from a file or stream.

fwrite() - used to write data to a file or stream.

fgetc() - used to read a single character from a file or stream.

fputc() - used to write a single character to a file or stream.

fgets() - used to read a line of text from a file or stream.

fputs() - used to write a line of text to a file or stream.

These functions allow for the direct reading and writing of data from and to files or streams. Unformatted I/O functions are generally used when dealing with binary data that may not be in human-readable format or when there is no need to format the data for display.

For example, the fread() function can be used to read binary data from a file into an array in memory, and the fwrite() function can be used to write binary data from an array in memory to a file.

Unformatted I/O functions in C provide a low-level mechanism for reading and writing binary data, but they are more complex to use than formatted I/O functions and require a good understanding of the underlying data structure.

program to find simple interest in c

Here's an example program in C to calculate the simple interest:

#include<stdio.h>

int main()
{
    float principle, rate, time, SI;

    // Input the principle, rate and time
    printf("Enter principle amount: ");
    scanf("%f", &principle);

    printf("Enter rate of interest: ");
    scanf("%f", &rate);

    printf("Enter time (in years): ");
    scanf("%f", &time);

    // Calculate the simple interest
    SI = (principle * rate * time) / 100.0;

    // Display the simple interest
    printf("Simple Interest = %.2f\n", SI);

    return 0;
}
In this program, we declare four variables principle, rate, time and SI of type float. We use 'printf()' and 'scanf()' functions to read the values of principle, rate and time from the user.

Then we calculate the simple interest using the formula (principle * rate * time) / 100.0 and store the result in the SI variable.

Finally, we display the simple interest using the 'printf()' function with the format specifier "%.2f" to display the result with two decimal places. 

Note: The formula used in this program assumes that the rate of interest is a percentage per year and the time is in years. If the rate is given as an annual rate but the time is in months, the rate should be divided by 12 to get the monthly rate, and the time should be divided by 12 to get the number of years. Similarly, if the rate is given as a monthly rate, it should be multiplied by 12 to get the annual rate.

program to swap two numbers in c
Here's an example program in C to swap two numbers using a temporary variable:


#include<stdio.h>

int main()
{
    int num1, num2, temp;

    // Input two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Display the original numbers
    printf("Original numbers: num1 = %d, num2 = %d\n", num1, num2);

    // Swap the numbers using a temporary variable
    temp = num1;
    num1 = num2;
    num2 = temp;

    // Display the swapped numbers
    printf("Swapped numbers: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}
In this program, we declare three variables num1, num2 and temp of type int. We use printf() and scanf() functions to read the values of num1 and num2 from the user. 
 
Then we display the original numbers using the printf() function. Next, we swap the values of num1 and num2 using a temporary variable temp. Finally, we display the swapped numbers using the printf() function.

Note: There are other methods to swap two numbers without using a temporary variable, such as using arithmetic operations or using bitwise XOR operator.

point at a glance for input/output statements in c

Here are some points at a glance for input/output statements in C:

  1. printf() function is used to print output to the screen/console. It is formatted output, meaning you can format the output in various ways using format specifiers.
  2. scanf() function is used to read input from the user. It is formatted input, meaning you can specify the format of the input you expect to receive using format specifiers.
  3. Format specifiers are used to format the input or output. Examples include %d for integers, %f for floating-point numbers, %c for characters, %s for strings, etc.
  4. getchar() function is used to read a single character from the user.
  5. putchar() function is used to print a single character to the screen/console.
  6. gets() function is used to read a string from the user.
  7. puts() function is used to print a string to the screen/console.
  8. fprintf() function is used to print output to a file.
  9. fscanf() function is used to read input from a file.
  10. fgets() function is used to read a string from a file.
  11. fputs() function is used to print a string to a file.
  12. stdin is a standard input stream, used by default with scanf() and getchar() functions.
  13. stdout is a standard output stream, used by default with printf() and putchar() functions.
  14. stderr is a standard error stream, used to output error messages.

Post a Comment

0Comments
Post a Comment (0)