Input Output in C Programs

Need of Input - Output

Whenever we are using computers and performing different tasks ,there is alway involvement of input and output. for example, you want to play music the you insert the clip as input and what you get is the sound from the speakers which is a output of given input. 
Therefore , for performing task such as playing music you need input- output . similarly you need input and output for each and every task in computer system.

In C programming during runtime or program development you need input so that program can use those values and perform the particular given task and give specific output.

C libraries provide many functions for input and output which are as follows :

printf() function

This inbuilt function of stdio.h header file is used to print data to the console screen (The black screen which we see when we run program).

There are numerous forms of using printf function which can include printing string,character,integer,floating number,etc

syntax : int printf(const char *format, ...)

This function when successfully printed data on screen will return number of characters printed otherwise it will return negative value.

Example : printf("%d", 90);   printf("feel_the_coder");

scanf() function

This inbuilt function of stdio.h header file is used to scan values to the variable which can be used further in program. 
There are numerous ways of using scanf function when scanning different types of values to different types of variables. 

In order to scan values to variables we use a & (addressof operator) in the scanf function.

syntax : int scanf(const char *format, ...)

This function return number of inputs on successful completion.

Example : int n; scanf("%d",&n);

NOTE : I have already discussed about %d,etc as these are format specifier.

Input-Output in files 

We use different functions for input and output to files namely, fprintf(),fscanf(),fgets(),fputs(),fgetc(),fputc(),etc

We will discuss about these functions in upcoming post of file handling.

Formatted Input-Output

printing values of a pointer to an object

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

int amin()
{
int i;
int *p=&i;
printf("the address if i is %p\n",(void*)p);
return EXIT_SUCCESS;
}

Using <inttypes.h> and uintptr_t

Another way of printing pointer is using uintptr_t from inttypes.h and macros also.

printf("The address of i is 0x%"PRIXPTR"\n",(uintptr_t)p);

In practice, it does not exist that any integer that can hold pointer.

Pointer Difference

To hold pointer difference type , we have ptrdiff_t type and this is included in the header file <stddef.h>

use t as a format specifier
                                                  printf("%t\n",pd)

if two pointers to be subtracted do not point to the same object, then behaviour is undefined.

We can use format specifiers in input-output functions, scanf() and printf () as we are using above and also length specifiers can also be used.

Printing format flags

Format flags can be -,+,<space>,# and ! . Each and every flag has it's own meaning.

These can be printed using basic input output functions.






                  


 


Comments

Post a Comment

Popular posts from this blog

Coding tips for Developers

Master Data Structure & Algorithms in Python

How to Start with Coding: A Beginner's Guide