Basic I/O Operations in C

BASIC I/O OPERATIONS in C


In this tutorial we will learn to read data from standard input device and write data on standard output devices

.
The header file required by every program containing standard input/output functions is “stdio.h”

 

Syntax:- #include <stdio.h>

 

  • Stdio stands for “standard input/output”.
  • The contents of header files becomes a part of compiled code.


READING A CHARACTER

 

For reading a single character from input device, getchar() is used.

 

Syntax:-  variable_name= getchar();


Statements stores the character entered from input device into the variable  “variable_name”.

 

WRITING A CHARACTER


Putchar() function is used to write a single character on console.

 

Syntax:- putchar(variable_name);

Scanf() Function


Scanf() function is used to take multiple inputs of different or same data types at a time.

 

Syntax:- scanf(“control string", arg1, arg2,.....argn);

 

Where,

Control string describes the data type to be entered and the field width of data type.
arg n defines the address of the memory location in which that data type variable is stored.

 

For Example:-

 

Scanf(“%5d %c %s”, &num, &choice, name);

 

Here in this example scanf() takes input to 3 variables of three different data type i.e. integer, character and string variables respectively. During the run time scanf() will accept an integer value from the standard input device and after that a character and then a string. 

 

If the variables are given values in any other order then unexpected results will be obtained depending on different situation.

 

  • NOTE:- it is legal to use a non-whitespace character between field specifications. However, the scanf expects a matching character in the given location.

For Example:-

 

Scanf(%c-%d”, &choice,&num);

 

Accepts input like

 

Y-123

 

to assign Y to choice and 123 to num.

 

  • In case of taking string as a input, it is also possible to restrict the input string to contain only a specified set of character by writing control string as %[characters]

For Example:-

 

scanf(“%[a-z]”,address);

 

Here scanf() will accept only alphabets from a to z. Anything other than that will be ignored.

 

Printf() Function

 

printf() function is used to print any string or any textual line you want to add to the output.

 

It also prints multiple values of multiple data types along with a any textual string the coder wants to add, all at a same time, and it also makes our output much more readable and formatted.

 

SYNTAX:-  printf(“control string”, arg1, arg2,.....arg n);


Here, control string is almost same as in scanf() function. But here we can add any textual matter of our choice to the screen.

 

Unlike of scanf(), here arg n contains the variable instead of address of variable.

 

For Example:-
If a=5, b=6

 

We give following statement in our code:-
Printf(“Output of the program is a = %d and b=%b”, a,b);


Then the output will be like:-

 

Output of the program is a = 5 and b=6
 

Comments

Member since:
24 August 2009
Last activity:
7 years 25 weeks

pragma is a directive ... I dont know a lot about it... 

 

#pragma <directive name>

 

 

directive name consists of 

 

warn,startup,exit

 

 

eg:

 

#pragma warn -rvl   (this line would suppress the warning ,"function shoul return a value"

 

#pragma startup <function name> [priority]

 

#pragma exit  <function name > [priority]

 

 

 

The first pragma will load the function in startup (ie) befor the main function loads.

 

The second pragma will load the function in exit of the main function ..

 

The priority will be from 64 to 255 (because 0 to 63 are used by c libaries).

 

the smaller the number the higher the priority of the function..

 

For startup higher priority function will load first... and for the exit  higher priority function will load at last...

 

 

 

 

Member since:
24 August 2009
Last activity:
7 years 25 weeks

volatile is a qualifier.. in c if we declare like

 

volatile int j; 

 

that does two jobs

 

1. Actually for faster execution ,variables will be stored in cpu register by the compiler... if we denote the variable as volatile , the compiler go to stack (memory) and fetch the value and store it in register for operation .. after that it writes back the result of the variable to the stack ...

 

2.If we denote the variable as volatile.. the compiler should be known that the variable can be changed by any externa process ...

 

eg:  volatile float temperature;

 

( The variable temperature might get modified through the thermometer attached to the computer. )

Member since:
17 June 2009
Last activity:
7 years 35 weeks

I am having problems with Key word "volatile" and "Pragma" can you kindly help me
Thanks and Regards...