Typedef

Typedef

In C, typedef is a keyword used to create a new type name that can be used as an alias for an existing data type. It is often used to simplify complex data types and make the code more readable and maintainable.

Here is an example of using typedef:

				
					#include <stdio.h>
typedef int my_integer;
int main() 
{
    my_integer x = 10;
    printf("x: %d\n", x);
    return 0;
}

				
			

In this example, we define a new type name my_integer using thetypedef  keyword, which is an alias for the int data type. We then declare a variable x of type my_integer in the main function and assign a value of 10 to it. We use the printf function to print out the value of x.

By using typedef, we can simplify complex data types and make the code easier to read and maintain. For example, we can define a structure using typedef:

				
					typedef struct 
{
    char name[50];
    int age;
    float height;
} Person;	

				
			

In this example, we define a new type name Person as an alias for the anonymous struct containing three members name, age, and height. We can then declare variables of type Person instead of having to write struct Person every time.

File I/O

In C, file input/output (I/O) is accomplished using the stdio.h library. There are several functions available in this  library that allow you to read from and write to files.

To open a file for reading or writing, you can use the fopen function, which takes two arguments: the name of the file to open, and the mode in which to open the file. The mode can be one of the following:

  • “r”: open the file for reading
  • “w”: open the file for writing (if the file exists, its contents are truncated; if the file does not exist, it is created)
  • “a”: open the file for writing (if the file exists, new data is appended to the end; if the file does not exist, it is created)
  • “r+”: open the file for both reading and writing
  • “w+”: open the file for both reading and writing (if the file exists, its contents are truncated; if the file does not exist, it is created)
  • “a+”: open the file for both reading and writing (if the file exists, new data is appended to the end; if the file does not exist, it is created)

Here is an example of opening a file for writing:

				
					#include <stdio.h>
int main()
 {
    FILE *fp;
    char str[] = "Hello, World!";
    fp = fopen("myfile.txt", "w");
    fprintf(fp, "%s", str);
    fclose(fp);
    return 0;
}                                                                                                                             

				
			

In this example, we use the fopen function to open a file named myfile.txt for writing. We then use the fprintf function to write a string to the file. Finally, we close the file using the fclose function.

To read from a file, you can use the fscanf function, which works similarly to scanf. Here is an example of reading from a file:

				
					#include <stdio.h>
int main()
 {
    FILE *fp;
    char str[50];
    fp = fopen("myfile.txt", "r");
    fscanf(fp, "%s", str);
    printf("Contents of file: %s\n", str);
    fclose(fp);
    return 0;
}

				
			

In this example, we use the fopen function to open a file named myfile.txt for reading. We then use the fscanf function to read a string from the file into a character array. Finally, we print out the contents of the array using printf.

Join To Get Our Newsletter
Spread the love