You are on page 1of 12

C INPUT AND OUTPUT

FUNCTIONS
As we all know the three essential functions of a computer are reading,
processing and writing data. Majority of the programs take data as
input, and then after processing the processed data is being displayed
which is called information. In C programming you can use scanf() and
printf() predefined function to read and print data.

I/O operations are useful for a program to interact with users. stdlib is
the standard C library for input-output operations. While dealing with
input-output operations in C, there are two important streams that play
their role. These are:

 Standard Input (stdin)


 Standard Output (stdout)
Standard input or stdin is used for taking input from devices such as
the keyboard as a data stream. Standard output or stdout is used for
giving output to a device such as a monitor. For using I/O functionality,
programmers must include stdio header-file within the program.

Format specifiers can be defined as the operators which are used in


association with printf() function for printing the data that is referred
by any object or any variable. When a value is stored in a particular
variable, then you cannot print the value stored in the variable
straightforwardly without using the format specifiers. You can retrieve
the data that are stored in the variables and can print them onto the
console screen by implementing these format specifiers in a printf()
function.

Format specifiers start with a percentage % operator and followed by a


special character for identifying the type of the data.
There are mostly six types of format specifiers that are available in C.
LIST OF FORMAT SPECIFIERS IN C
Format
Description
specifier

Integer Format
%d
Specifier

%f Float Format Specifier

Character Format
%c
Specifier

%s String Format Specifier

Unsigned Integer
%u
Format Specifier

Long Int Format


%ld
Specifier
Syntax-

printf("%d",<variable name>);

scanf("control string", arg1, arg2, ..., argn);

Here the % sign denotes the conversion specification; w signifies the


integer number that defines the field width of the number to be read. d

READING CHARACTERS IN C
The easiest and simplest of all I/O operations are taking a character as
input by reading that character from standard input (keyboard).
getchar() function can be used to read a single character. This function
is alternate to scanf() function.

Syntax:
var_name = getchar();

Example:
#include<stdio.h>

void main()
{
char title;
title = getchar();
}
There is another function to do that task for files: getc which is used to
accept a character from standard input.

Syntax:
int getc(FILE *stream);

Writing Character In C
Similar to getchar() there is another function which is used to write
characters, but one at a time.
Syntax:
putchar(var_name);

Example:
#include<stdio.h>

void main()
{
char result = 'P';
putchar(result);
putchar('\n');
}

Similarly, there is another function putc which is used for sending a


single character to the standard output.
Syntax:
int putc(int c, FILE *stream);
DATA TYPES IN C
Each variable in C has an associated data type. Each data type
requires different amounts of memory and has some specific
operations which can be performed over it.
A data-type in C programming is a set of values and is determined to
act on those values. C provides various types of data-types which
allow the programmer to select the appropriate type for the variable to
set its value. Let us briefly describe them one by one:
C Data Types are used to:
 Identify the type of a variable when it declared.
 Identify the type of the return value of a function.
 Identify the type of a parameter expected by a function.
ANSI C provides three types of data types:
 Primary(Built-in) Data Types:
void, int, char, double and float.

As the name suggests it holds no value and is generally used


voi for specifying the type of function or what it returns. If the
d function has a void type, it means that the function will not
return any value.

char: The most basic data type in C. It stores a single character


and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an
integer.
float: It is used to store decimal numbers (numbers with floating
point value) with single precision.
double: It is used to store decimal numbers (numbers with
floating point value) with double precision.

 Derived Data Types:


Array, References, and Pointers.

Arrays are sequences of data items having homogeneous


Arrays
values. They have adjacent memory locations to store values.

Referen Function pointers allow referencing functions with a particular


ces signature.

Pointer These are powerful C features which are used to access the
s memory and deal with their addresses.


 User Defined Data Types:
Structure, Union, and Enumeration.
C allows the feature called type definition which allows programmers to define their
identifier that would represent an existing data type. There are three such types:

Data
Description
Types

It is a package of variables of different types under a single


Structu
name. This is done to handle data efficiently. "struct" keyword
re
is used to define a structure.

These allow storing various data types in the same memory


location. Programmers can define a union with different
Union
members, but only a single member can contain a value at
given time. It is used for

Enumeration is a special data type that consists of integral


Enum constants, and each of them is assigned with a specific name.
"enum" keyword is used to define the enumerated data type.
Following are the examples of some very common data types used in
C:

Different data types also have different ranges upto which they can
store numbers. These ranges may vary from compiler to compiler.
Below is list of ranges along with the memory requirement and format
specifiers on 32 bit gcc compiler.
Data Type Memory (bytes) Range Format
Specifier
int 2 -32,768 to 32,767 %d
unsigned short int 2 0 to 65,535 %hu
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned char 1 0 to 255 %c
float 4 %f
double 8 %lf
long double 12 %Lf

GETS() & PUTS() FUNCTIONS

The gets() function reads a line from stdin(standard input) into the
buffer pointed to by str pointer, until either a terminating newline or
EOF (end of file) occurs. The puts() function writes the string str and a
trailing newline to stdout.
str → This is the pointer to an array of chars where the C string is
stored.
#include<stdio.h>

void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}

DIFFERENCE BETWEEN SCANF() AND GETS()

The main difference between these two functions is that scanf() stops
reading characters when it encounters a space, but gets() reads space
as character too.
If you enter name as Study Tonight using scanf() it will only read and
store Study and will leave the part after space. But gets() function will
read it completely.
COMMENTS
In computer programming, a comment is a programmer-readable
explanation or annotation in the source code of a computer program.
They are added with the purpose of making the source code easier for
humans to understand, and are generally ignored by compilers and
interpreters.[1][2] The syntax of comments in various programming
languages varies considerably.
Comments are sometimes processed in various ways to generate
documentation external to the source code itself by documentation
generators, or used for integration with source code management
systems and other kinds of external programming tools.
The flexibility provided by comments allows for a wide degree of
variability, but formal conventions for their use are commonly part of
programming style guides.
Syntax-:

BLOCK COMMENT
The following code fragments in C demonstrate just a tiny example of
how comments can vary stylistically, while still conveying the same
basic information:
/*
This is the comment body.
Variation One.
*/
INLINE COMMENT (END-OF-LINE)
In this form, all the text from the ASCII characters // to the end of the
line is ignored.
// begin: Variation Three.
// -------------------------
// This is the comment body.
// -------------------------

Different styles can be chosen for different areas of code, from


individual lines to paragraphs, routines, files, and programs. If the
syntax supports both line comments and block comments, one
method is to use line comments only for minor comments
(declarations, blocks and edits) and to use block comments to
describe higher-level abstractions (functions, classes, files and
modules).

You might also like