1. printf
The printf function is the main method used for outputting text in C programs. printf is a library function contained in the stdio library. The general form of printf is shown below.
Syntax: printf ( <format string>, arg1, arg2, arg3, ... ) ;
The format string can contain text, control characters and also markers that indicate where the value of a variable should be displayed within the output text. Following the format string are a list of variables (or constants) that should be output as part of the message. These variables are separated with commas. For each variable marker in the format string, there should be a corresponding variable included in the argument list. The following examples will show how printf is used.
Display text:
printf("Hello World!");
Display text and move to the next line:
printf("Hello World!\n");
The character sequence \n indicates that you should move to the next line. If this is not included, the next printf will print on the same line as this one. The newline character \n is a single character. It is not possible to enter the character directly. The \ indicates that you are specifying a special character, and the following letter, an n in this case, specifies which special character.
Display text and the value of an integer:
printf("We have been in school %d days.\n", dayCount);
/*dayCount is a variable*/
The %d in the format string indicates that the printf command should insert the value of the first variable in the argument list, in this case, the value dayCount.
A constant can be used instead of a variable:
printf("We have been in school %d days.\n", 23);
This constant could also be put directly in the format string instead of the %d.
Different control charactes correspond to different variable types. The %d character can be used with integer variables. Control characters for the various data types are shown in the table below.
int %d float %f double %lf string %s single character %c a format string may reference several variables. Example statements and their output are shown in the sample code below.
#include <stdio.h>
int main(int argc, char* argv[])
{
int count = 9;
float fract = 3.5;
double percent = 37.6389;
char singleChar = 'A';
char * testString = "happy";
printf("The student's average was: %lf\n", percent);
/*Output: The student's average was: 37.638900;*/
printf("A fraction %f, a string %s and a character %c\n", fract,testString, singleChar);
/*Output: A fraction 3.500000, a string happy and a character A*/
printf("Numbers without spaces %d%lf%f", count, percent, fract);
/*Output: Numbers without spaces 937.6389003.500000;*/
getch();
}
2. scanf
The command scanf can be used in C programs to input data. scanf has similar syntax to printf. The general form is shown below.
scanf ( <format string>, arg1, arg2, arg3, ... ) ;
With scanf, the format string specifies the type of data the program is expecting to get from the user. The list of arguments are the variables where the data should be stored. printf and scanf use the same control characters for the various variable types.
scanf will store the data it receives in the memory location where the variable is stored. In order to do this, it needs the memory location of the variable. Preceding the variable name with an & indicates the address of the variable, rather than the variable itself. For instance, the following command will read an integer and store it in the variable index.
int index;
scanf("%d", &index);
The sample code below shows various examples of scanf usage.
#include <stdio.h>
int main(int argc, char* argv[])
{
int sample;
float fl;
char string[33]; /*string that will hold 32 characters plus the termination character*/
printf("Type an integer: \n");
scanf("%d", &sample);
printf("Type a string: \n");
scanf("%s", string);
printf("Type a float: \n");
scanf("%f", &fl);
printf("You typed the integer %d, the string %s, the float %f\n", sample, string, fl);
/*you can also capture multiple values with one scanf*/
/*scanf returns the number of values it receives, so a
program can check to make sure all the desired data was
received*/
int dataReceived = 0;
int testInt;
float testFloat;
printf("Enter an int and a float separated by a space\n");
dataReceived = scanf("%d %f", &testInt, &testFloat);
printf ("Scanf received %d items of data. You entered the int %d and the float %f .\n", dataReceived, testInt, testFloat);
getch();
}
Output:
Type an integer:
45
Type a string:
happy23
Type a float:
6.879
You typed the integer 45, the string happy23, the float 6.879000
Enter an int and a float separated by a space
51 1.235
Scanf received 2 items of data. You entered the int 51 and the float 1.235000 .