Discover Input output in C and size of a variable in C

Today we are going to learn about basic input output in C and different methods to find out the size of a variable in C. Do you know why we are using an ampersand (&) in scanf and not in printf function?

Revision

In the previous article we learned about different rules and regulations regards C program writing. Such as graphic character, escape sequences, whitespace character, Keywords, token, Identifiers, Different datatype. Today in this tutorial we will learn about basic input output in C language


Introduction

Every C compiler is having certain Input and output library function which are capable to perform input output operations. Those functions are not a part of a language but they are considered standard for all I/O (Input and Output) operations.

The set of library functions that performs input output operations are known as standard I/O library.

For basic I/O function C is having library file known as

#include<stdio.h>

stdio = std (Standard) + io (Input/Output)

Assignment statement is a simple method to provide value to a variable.

Eg.

int data = 1010;
char r = 't';

The above method is invalid if the program user needs to enter some value.

As solution there are two functions are available

scanf() which take input from a keyboard. We can give value to the variable in run time.

printf() is used for the output.


Format specifiers

The functions printf () and scanf() both use format specifiers to specify the type and size of data.

Format specifiers begin with a percent sign (%), some format specifiers are given below which is applicable for both printf and scanf.

Conversion CharacterUsed for
%csingle char
%dSigned decimal integer
%uUnsigned decimal integer
%oUnsigned Octal integer
%x , %XUnsigned Hexadecimal (%x = a, b, c, d, e, f) (%X = A, B, C, D, E, F)
%fFloating point number
%eFloating point number in exponential format
%EFloating point number in exponential format but capital E
%sstring
%%print a % sine (for printf only)
%pprint a pointer in hexadecimal format (for printf only)

The size modifier h can be used with d, o, u, x, X to specify short integer.

The size modifier l can be used with d, f, e, E, g, G to specify long integer.

Eg.

%ld = long int

%hd = short int

%lf = long double


Reading Input data

In a simplified term: Reading means C code is reading; Input data means user given input data to a program by using a keyboard.

To take input in the code we are having a library function scanf which is located in stdio.h header file.

The scanf() function syntax is given below.

scanf("control string", address1, address2, ......);

The control string is containing a format specifier. It should be in a double quote.

The address is the address of a variable. An ampersand (&) is used to get an address.

Eg.

scanf("Result: %d", &data);

Why we use “&” (address) of a variable in a scanf?

The main reason for using address is we are writing data (value) inside the memory block so to write anything on memory needs an address that’s a reason for giving an address.

For multiple inputs we use a delimiter to separate different data values ​​like space, tab, or newline (default). A specific character can also be placed between two formate specifier character as a delimiter.

Eg.

scanf("%d %c %f", &data, &ch, &marks);

Input should be:

1100 D 3.45 <press enter> //here data are separated by space or tab.

or

1100 <enter>

D <enter>

3.45 <enter> //Here, the data is separated by a new line.

scanf("%d:%f", &basic, &rha);

Input should be:

1500:33.6 <click enter> //here data are separated by :

scanf("%d%f", &basic, &rha);

Input should be:

In either way you can give it by space separator or tab or new line.

1500 33.6 <click enter>


Writing Output data

In a simplified term: Writing means C code is writing data from computer memory; Output data means the program’s output for user on the standard output device (monitor).

By using printf() library function we can print string, character, and numeric values.

printf() function syntax is given below:

printf("control string", variable1, variable2, ......);

The control string contains format specifiers and text. name of a variable should not contain an ampersand (&) on starting. it should be a variable name only.

Why in printf variable we are not using the address of a variable?

The reason is, printf is for output only, Output means reading from memory, the main task is to read from memory and printing data on a screen.

Eg.

printf("I love C language.\n");

Output:

I love C language.

printf("Enter your age : ");
scanf("%d", &age);

Here printf is not using any formate specifier and it is used to display a message and tells to a user that enter your age, next statement is scanf which stores user entered value inside age variable

Output:

Enter your age : ____ <—- here user will enter his/her age and press enter.

printf("%d %f", data, marks);

Here, two formate specifiers are used.

Output:

99 31.5


How to check the size of a variable in C in your system?

To check the size of a variable in C there are certain methods are available. Either by using sizeof operator which gives the size of a variable in bytes. Or by using the system library to get size of a variable in terms of range. We will look at both of them in detail.

check size of a variable using sizeof operator.

Sizeof operator is most used in C or C++.

It is a compile time unary operator which is used to get the size of its operand.

The output of sizeof is unsigned integral which is denoted by size_t.

It can be applied to any datatype.

Syntax:

size_t sizeof(operand);

Eg.

printf("%lu", sizeof(int));

or

sizeOfInt = sizeof(int);

Practical use:

This is a code

 #include<stdio.h>
 
 int main(void)
 {
         printf("These are the size of a variable in my system.\n");
 
         printf("char = %lu \n", sizeof(char));
         printf("unsigned char  = %lu \n", sizeof(unsigned char));
         printf("int  = %lu \n", sizeof(int));
         printf("unsigned int = %lu \n", sizeof(unsigned int));
         printf("short int = %lu \n", sizeof(short int));
         printf("unsigned short int = %lu \n", sizeof(unsigned short int));
         printf("long int = %lu \n", sizeof(long int));
         printf("unsigned long int = %lu \n", sizeof(unsigned long int));
         printf("float = %lu \n", sizeof(float));
         printf("double = %lu \n", sizeof(double));
         printf("long double = %lu \n", sizeof(long double));
         return 0;
 }
code for checking a size of a variable in c using sizeof operator with input output in c

Generated output for my system is

These are the size of a variable in my system.
char = 1 
unsigned char  = 1 
int  = 4 
unsigned int = 4 
short int = 2 
unsigned short int = 2 
long int = 8 
unsigned long int = 8 
float = 4 
double = 8 
long double = 16 
generated output for sizeof variable to check sizeof a variable in c

Check size of a variable in c using header file

The range of integral type of datatype is given in limits.h header file and the range of floating-point type datatype are given in float.h

Check this code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

    printf("CHAR_BIT    :   %d\n", CHAR_BIT);
    printf("CHAR_MAX    :   %d\n", CHAR_MAX);
    printf("CHAR_MIN    :   %d\n", CHAR_MIN);
    printf("INT_MAX     :   %d\n", INT_MAX);
    printf("INT_MIN     :   %d\n", INT_MIN);
    printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
    printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
    printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
    printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
    printf("SHRT_MAX    :   %d\n", SHRT_MAX);
    printf("SHRT_MIN    :   %d\n", SHRT_MIN);
    printf("UCHAR_MAX   :   %d\n", UCHAR_MAX);
    printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
    printf("ULONG_MAX   :   %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX   :   %d\n", (unsigned short) USHRT_MAX);

    printf("\n\n");
    
    printf("Storage size for float : %lu \n", sizeof(float));
    printf("FLT_MAX     :   %g\n", (float) FLT_MAX);
    printf("FLT_MIN     :   %g\n", (float) FLT_MIN);
    printf("-FLT_MAX    :   %g\n", (float) -FLT_MAX);
    printf("-FLT_MIN    :   %g\n", (float) -FLT_MIN);
    printf("DBL_MAX     :   %g\n", (double) DBL_MAX);
    printf("DBL_MIN     :   %g\n", (double) DBL_MIN);
    printf("-DBL_MAX     :  %g\n", (double) -DBL_MAX);
    printf("Precision value: %d\n", FLT_DIG );

    return 0;
}

Generated output:

CHAR_BIT    :   8
CHAR_MAX    :   127
CHAR_MIN    :   -128
INT_MAX     :   2147483647
INT_MIN     :   -2147483648
LONG_MAX    :   9223372036854775807
LONG_MIN    :   -9223372036854775808
SCHAR_MAX   :   127
SCHAR_MIN   :   -128
SHRT_MAX    :   32767
SHRT_MIN    :   -32768
UCHAR_MAX   :   255
UINT_MAX    :   4294967295
ULONG_MAX   :   18446744073709551615
USHRT_MAX   :   65535


Storage size for float : 4 
FLT_MAX     :   3.40282e+38
FLT_MIN     :   1.17549e-38
-FLT_MAX    :   -3.40282e+38
-FLT_MIN    :   -1.17549e-38
DBL_MAX     :   1.79769e+308
DBL_MIN     :   2.22507e-308
-DBL_MAX     :  -1.79769e+308
Precision value: 6
Generated output by using header file and limits to get size of a variable in a C using basic input output operation in c

Conclusion

In a nutshell, this is how we can use printf and scanf function to do basic input output in C language. Moreover, we learned two methods to find out the size of a variable in C for a particular system.


Assignment

  • Find the size of a variable in c for your PC and comment it down.
  • Do practice for input output in c.
    • Write a program to take input from user Name, Age, Birthdate, and hobby. and print all of them.

NOTE: For solutions ask or look in our Facebook group.


Leave a Comment