More C Strings Input and Output Functions
For the input function, the gets function is much more simpler to use than the scanf function. The scanf function is often used in the advanced C programs. The same is for the output function. the puts function is more simpler to use than the printf function. Here are the definitions for these functions:
- puts – Put String
- gets – Get String
- fgets – File Get String
As you can see in the above definitions, the fgets function is used to get the value from a file. However, fgets is also often used in place of the gets function because gets is mostly misused by programmers.
Here is the sample code for these functions:
/*
*
* string.c
*
* Program to illustrate the use of gets, puts and fgets
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <string.h>
main() {
char name[21];
puts("This is a basic programn");
fputs("Please enter your full name (20 characters max): ", stdout);
fgets(name, 21, stdin);
fputs("Your full name is ", stdout);
puts(name);
fflush(stdin);
if (strlen(name) >= 20)
while (getchar() != 'n' && getchar() != EOF) {}
getchar();
}
Note that when I used GCC to compile the above code with gets function, the GCC compiler would give me a warning stated that the gets function is dangerous and should not use it. Therefore I recommend to use fgets to make things easier.
Also notice that the above code contains a new function, fputs. As my current knowledge, the difference between fputs and puts is that puts automatically adds a break (n) at the end of each output, whereas fputs does not. The fputs function is good to display the prompt for user to enter the data.
More About printf and scanf (Part 2)
This is part 2 of the printf and scanf. I will talk about scanf usage in this post. Before go to the source code, take a note that I replaced some fflush(stdin) statements to two-line dummy statements which is explained in the source code below. The purpose of these is to clean up any previous input values without use another function other than scanf since fflush(stdin) is not used to flush input. According to this page and the materials I learned, the scanf function does not use often by the programmers. However, since I’m still learning, I will learn from the basics.
/*
*
* scanf.c
* Demonstrate the use of the scanf() function
* By Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int x;
float y;
char string[100];
/*
*
* String input
*
*/
printf("Enter one word: ");
// %s in scanf will only store one word. This means that it will not store the remaining words after the first space.
// Note there is no & before string. String is the only variable type that do not need &
scanf("%s", string);
printf("The word you entered was >>%s<<n", string);
// Below are the dummy lines to clean up the input value
while(getchar() != 'n' && getchar() != EOF)
printf("%s", string);
printf("Enter many words: ");
// Read up to a newline (multiple words)
// ^ means not, which ^n means do not read new line
scanf("%[^n]", string);
printf("The text you entered was >>%s<<n", string);
/*
*
* Integer input
*
*/
printf("Please enter a number: ");
// Note the &
scanf("%d", &x);
printf("The number you entered was %dn", x);
/*
*
* Character input
* The following code is self-explanatory
*
*/
while(getchar() != 'n' && getchar() != EOF)
printf("%s", string);
printf("Please enter a single character: ");
scanf("%c", &string[0]);
printf("The character that you entered was '%c'n", string[0]);
while(getchar() != 'n' && getchar() != EOF)
printf("%s", string);
printf("Please enter 4 characters: ");
// Note that characters are not limited to letters. It can be numbers.
scanf("%4c", &string[0]);
printf("The characters you entered were >>>%c%c%c%c<<<n", string[0], string[1], string[2], string[3]);
/*
*
* Floating-point input
*
*/
printf("Please enter a decimal number: ");
scanf("%f", &y);
printf("The number you entered was %fn", y);
fflush(stdin);
getchar();
}
More About printf and scanf (Part 1)
I make this post two part because each of the printf and scanf usage is quite long. And since I could only explain them by example, let’s straight to the source code.
Note that I copied the following code from the tutorial. Therefore it has author’s name. However, most of the comments are written by me.
/*
*Â Â Â printf.c
*
*Â Â Â Demonstrate the use of the printf() function
*
*Â Â Â by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <string.h>
main() {
int x = 415;
double y = 3.14159;
int len = 7;
char string[30];
// String is the only variable not initialized
strcpy(string, "Hello");
/*
*
* String formatting options
*
*/
// These numbers act as a reference to the output below.
printf("n123456789012345678901234567890n");
// This simply prints out the string. The first arrow indicates the end position of the string.
printf("%s<<<<n", string);
// This outputs 10 minimum characters where the string is right justified. Note that the output will exceed 10 characters if the string is more than 10 characters long.
printf("%10s<<<<n", string);
// This is similar to the above output except the string is left justified.
printf("%-10s<<<<n", string);
// This outputs 3 maximum characters. Note that it is reverse to the above two outputs. If the string has more than 3 characters, it will output first 3 characters of the string. However, it will output the whole characters if the string is less than or equal to 3 characters long.
printf("%.3s<<<<n", string);
// This has the same output as above despite the number of characters is less than 3.
printf("%-.3s<<<<n", string);
// The * symbol represents an integer variable. This outputs the maximum of 7 characters, which is the value of the integer variable len. However, it will output 5 characters since the string is five characters long.
printf("%.*s<<<<n", len, string);
/*
*
* Integer formatting options
* (Most of the outputs below are similar to the above. I will only explain the special ones.)
*
*/
printf("n123456789012345678901234567890n");
printf("%d<<<<n", x);
printf("%10d<<<<n", x);
// This is similar to the above output except the output replaces spaces with 0s.
printf("%010d<<<<n", x);
// This is similar to the last * statement. It outputs minimum of 7 characters.
printf("%*d<<<<n", len, x);
printf("%-10d<<<<n", x);
// This statement has no effect to digits. It cannot output the maximum of 1 digit with the number-related variable.
printf("%.1d<<<<n", x);
/*
*
* Character formatting options
*
*/
printf("n123456789012345678901234567890n");
// %c is for one character. string[0] is the first character of string variable which is H.
printf("%c<<<<n", string[0]);
printf("%10c<<<<n", string[0]);
/*
*
* Floating-point formatting options
*
*/
printf("n123456789012345678901234567890n");
// %f is for floating-point, which is double vriable.
printf("%f<<<<n", y);
printf("%10f<<<<n", y);
// This outputs minimum of 10 characters along with 3 decimal places.
printf("%10.3f<<<<n", y);
// Same as above. It outputs 8 decimal places.
printf("%10.8f<<<n", y);
/*
*
* Other features
*
*/
printf("n123456789012345678901234567890n");
// %% outputs one %.
printf("%%n");
fflush(stdin);
}
C scanf Function
Continue from last C post, I will discuss scanf function today. The scanf function is similar to printf function with the opposite functionality. The printf function outputs the string on the screen whereas scanf function lets the user input values from the keyboard. The example of scanf function which I learned is scanf (“%d”, &number). As you can see, right now I only know how to use integer C variable type. I will learn to use other variable types as the tutorial progresses. Notice that there is an & operator in front of number variable. In my experience for basic C++, I remembered that it is either a reference or pointer. Here is my entire program code for the scanf exercise:
/* Generated by NetBeans */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
/* End of generation */
int brothers, sisters, children;
printf("How many brothers and sisters do you have?n");
printf("Brothers: ");
scanf("%d", &brothers);
printf("Sisters: ");
scanf("%d", &sisters);
printf("How many children do you have? ");
scanf("%d", &children);
printf("You have %d brothers and %d sisters.nYou have %d children.n", brothers, sisters, children);
fflush(stdin);
/* Generated by NetBeans */
return (EXIT_SUCCESS);
}
As you can see in the code above, I use NetBeans both for my PHP development and C programming IDE. NetBeans automatically included the necessary C libraries and common statements for the project. The fflush(stdin) statement escapes the enter key according to the tutorial.