C program to perform input/output of all basic data types
In this article, you will learn how to perform input/output of all basic data types in the c language.
To understand this topic you should have knowledge of the following C programming topics:
- C main() function
- C printf() function
- C getchar() function
In this topic, we will use all types of data types i.e char, short, int, long, long long, float, double, and long double to perform the input/output operation.
Source Code
// C program to perform input/output of all basic data types
#include <stdio.h>
int main()
{
/**Declare all primitive and derived types **/
char charVal;
unsigned char uCharVal;
short shortVal;
unsigned short uShortVal;
int intVal;
unsigned int uIntVal;
long longVal;
unsigned long uLongVal;
long long longLongVal;
unsigned long long uLongLongVal;
float floatVal;
double doubleVal;
long double longDoubleVal;
/**Read input in each type **/
printf("Enter a character: ");
charVal = getchar();
getchar();
printf("Enter another character: ");
uCharVal = getchar();
getchar();
printf("Enter a signed short value: ");
scanf("%hi", &shortVal);
printf("Enter an unsigned short value: ");
scanf("%hu", &uShortVal);
printf("Enter an signed integer value: ");
scanf("%d", &intVal);
printf("Enter an unsigned integer value: ");
scanf("%u", &uIntVal);
printf("Enter a signed long value: ");
scanf("%ld", &longVal);
printf("Enter an unsigned long value: ");
scanf("%lu", &uLongVal);
printf("Enter a signed long long value: ");
scanf("%lld", &longLongVal);
printf("Enter an unsigned long long value: ");
scanf("%llu", &uLongLongVal);
printf("Enter a float value: ");
scanf("%f", &floatVal);
printf("Enter a double value: ");
scanf("%lf", &doubleVal);
printf("Enter a long double value: ");
scanf("%Lf", &longDoubleVal);
/**Print the value of all variable **/
printf("\nYou entered character: '%c' \n", charVal);
printf("You entered unsigned character: '%c' \n\n", uCharVal);
printf("You entered signed short: %hi \n", shortVal);
printf("You entered unsigned short: %hu \n\n", uShortVal);
printf("You entered signed int: %d \n", intVal);
printf("You entered unsigned int: %u \n\n", uIntVal);
printf("You entered signed long: %ld \n", longVal);
printf("You entered unsigned long: %lu \n\n", uLongVal);
printf("You entered signed long long: %lld \n", longLongVal);
printf("You entered unsigned long long: %llu \n\n", uLongLongVal);
printf("You entered float: %f \n", floatVal);
printf("You entered double: %lf \n", doubleVal);
printf("You entered long double: %Lf \n", longDoubleVal);
return 0;
}
Output
Enter a character: s
Enter another character: a
Enter a signed short value: 12
Enter an unsigned short value: 123
Enter an signed integer value: 45
Enter an unsigned integer value: 456
Enter a signed long value: 1234
Enter an unsigned long value: 12456
Enter a signed long long value: 1234567
Enter an unsigned long long value: 12345678
Enter a float value: 12.01
Enter a double value: 12.00045
Enter a long double value: 12.1002111
You entered character: 's'
You entered unsigned character: 'a'
You entered signed short: 12
You entered unsigned short: 123
You entered signed int: 45
You entered unsigned int: 456
You entered signed long: 1234
You entered unsigned long: 12456
You entered signed long long: 1234567
You entered unsigned long long: 12345678
You entered float: 12.010000
You entered double: 12.000450
You entered long double: 12.100211
Note: In this program, we used different types of values as per different data types.
We used the built-in function getchar()
to take the user's character input and other numeric values taken by the built-in scanf()
function. After that input values printed by usual, their data types.