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.
C Arrays
An array is a variable that represents a collection of the same variable types. For instance, a collection of doubles is an array. It can also declare one variable as an array. The size of an array depend on the memory size of the target computer. It can store larger array size if the memory size is bigger.
Below is the arrays declaration format:
typename arrayname[init_literal];
As you remember from previous posts, this format is the same as when declaring strings. A string is a collection of characters, which also is an array.
After an array was declared, each element in the array can be accessed individually. For example:
floatarray[5] = 3.56;
Like other programming languages, the first element of an array in C always starts with 0. Therefore for the above example, floatarray[5] refers to the sixth element of the array.
Arrays are often used with loops to operate with all elements sequentially, especially the for loop. When used inside a loop, an array of n elements always stars from 0 to n – 1. For instance, an array of 10 elements would start from 0 to 9 inside a loop.
Here is the sample code for the arrays:
/*
*
* array.c
*
* Program to illustrate the use of arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
float array[5];
int i, index;
for (i = 0; i < 5; ++i) {
printf("Please enter element number %d: ", i + 1);
scanf("%f", &array[i]);
while (getchar() != 'n' && getchar() != EOF)
printf("");
}
printf("Which element do you want to see? ");
scanf("%d", &index);
while (getchar() != 'n' && getchar() != EOF)
printf("");
printf("Element %d is %fn", index, array[index-1]);
fflush(stdin);
getchar();
}
The following sample code demonstrates how to use strings with arrays:
/*
*
* string.c
*
* Program to illustrate the makeup of a string
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[51];
int length;
printf("Please enter your name: ");
scanf("%[^n]", name);
while (getchar() != 'n' && getchar() != EOF)
printf("");
for (length = 0; name[length] != ''; length++) {}Â Â Â // do nothing
printf("The length of >>>%s<<< is %dn", name, length);
fflush(stdin);
getchar();
}
Notice the name[length] != ” statement. The slash zero inside the quotes represents a NULL character. It means that the loop will end if there is no more character to read.
Multi-dimensional Arrays
A multi-dimensional Array is an array made of multiple arrays. Here is an example:
/*
*
* multi.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int grid[8][3];Â Â Â // It contains 8 arrays and each array has 3 elements
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
grid[i][j] = (i + 1) * (j + 1);
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
printf("%3d ", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is the sample code for the string usage in a multi-dimensional array:
/*
*
* multi2.c
*
* Program to demonstrate multi-dimensional arrays
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char strings[5][20];Â Â Â // It contains 5 strings and each has 20 characters long
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
while (getchar() != 'n' && getchar() != EOF) {}
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
fflush(stdin);
getchar();
}
Array Initialization
Arrays can be initialized at the beginning of the program when they are declared. During the initialization, it’s unneccessay to specify the size of the array. For instance, here is the declaration of an array:
int array[40];
And here is the initialization of an array when it is declared:
int array[] = {3, 6, 9, 12};
Note that it’s also possible to initialize the array when the size of the array is specified, but as stated above, it’s not necessary. For example:
int array[10] = {3, 6, 9, 12};
In the above example, the first four elements in the array got initialized, while the rest of the elements were set to 0.
Here is the sample code to show how the multi-dimensional arrays are initialized:
/*
*
* multi3.c
*
* Program to demonstrate initializing multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char grid[8][8] =
{
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'}
};
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is for the multi-dimensional arrays for strings:
/*
*
* multi4.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[50] = "Mark Virtue";
char strings[][20] = {"This", "is", "how", "to", "initialize"};
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
}
Note that strings in the multi-dimensional arrays can only have one set of arrays that has non-specified size, notably the arrays in the first dimension. The size of arrays in other dimensions needs to be specified.