C - Input and Output
Input
In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.
scanf() function
This is the function which can be used to to read an input from the command line.Try following program to understand scanf() function.
#include <stdio.h> main() { int x; int args; printf("Enter an integer: "); if (( args = scanf("%d", &x)) == 0) { printf("Error: not an integer\n"); } else { printf("Read in %d\n", x); } }
Output :
In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.
Here we will discuss only one input function and one putput function just to understand the meaning of input and output.
printf() function
This is one of the most frequently used functions in C for output. ( we will discuss what is function in subsequent chapter. ).Try following program to understand printf() function.
#include <stdio.h> main() { int dec = 5; char str[] = "abc"; char ch = 's'; float pi = 3.14; printf("%d %s %f %c\n", dec, str, pi, ch); } |
5 abc 3.140000 c |
A complete syntax of printf() function is given in
This program will prompt you to enter a value. Whatever value you will enter at command prompt that will be output at the screen using printf() function. If you enter a non-integer value then it will display an error message.
Enter an integer: 20 Read in 20
puts() vs printf() for printing a string
1) puts(str);
2) printf(str);
puts() can be preferred for printing a string because it is generally less expensive (implementation of puts() is generally simpler than printf()), and if the string has formatting characters like ‘%’, then printf() would give unexpected results. Also, if str is a user input string, then use of printf() might cause security issues (see this for details).
Also note that puts() moves the cursor to next line. If you do not want the cursor to be moved to next line, then you can use following variation of puts().
fputs(str, stdout)You can try following programs for testing the above discussed differences between puts() and printf().
Program 1
#include<stdio.h> int main() { puts ( "allintutor" ); puts ( "
); getchar (); return 0; } |
Program 2
#include<stdio.h> int main() { fputs ( "
, stdout); fputs ( "
, stdout); getchar (); return 0; } |
Program 3
#include<stdio.h> int main() { // % is intentionally put here to show side effects of using printf(str) printf ( "
); getchar (); return 0; } |
Program 4
#include<stdio.h> int main() { puts ( "
); getchar (); return 0; } |
Scansets in C
scanf family functions support scanset specifiers which are
represented by %[]. Inside scanset, we can specify single character or
range of characters. While processing
scanset, scanf will process only those characters which are part of
scanset. We can define scanset by putting characters inside squre
brackets. Please note that the scansets are case-sensitive.
/* A simple scanset example */ #include <stdio.h> int main(void) { char str[128]; printf("Enter a string: "); scanf("%[A-Z]s", str); printf("You entered: %s\n", str); return 0; }
[root@centos-6 C]# ./scan-set Enter a string: GEEKs_for_geeks You entered: GEEKIf first character of scanset is ‘^’, then the specifier will stop reading after first occurence of that character. For example, given below scanset will read all characters but stops after first occurence of ‘o’
scanf("%[^o]s", str);Let us see with example.
/* Another scanset example with ^ */ #include <stdio.h> int main(void) { char str[128]; printf("Enter a string: "); scanf("%[^o]s", str); printf("You entered: %s\n", str); return 0; }
[root@centos-6 C]# ./scan-set Enter a string: http://geeks for geeks You entered: http://geeks f [root@centos-6 C]#
Let us implement gets() function by using scan set. gets() fucntion reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF found.
/* implementation of gets() function using scanset */ #include <stdio.h> int main(void) { char str[128]; printf("Enter a string with spaces: "); scanf("%[^\n]s", str); printf("You entered: %s\n", str); return 0; }
[root@centos-6 C]# ./gets Enter a string with spaces: all in tutor You entered: all in tutor [root@centos-6 C]#
What is return type of getchar(), fgetc() and getc() ?
char ch; /* May cause problems */ while ((ch = getchar()) != EOF) { putchar(ch); }Here is a version that uses integer to compare the value of getchar().
int in; while ((in = getchar()) != EOF) { putchar(in); }
Related Posts: C programing tutorials,
input/output operations
0 comments:
Post a Comment
Dont Forget for Commets