Basic Programming Handouts

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

C Programming Handout #3: Input/Output 1

Handout #3

Input/Output

Does C language include commands for input/output?

NO, actually the C programming language by itself does not include commands for input/output of data.
However, there is a pre-defined standard input/output library that can be linked with the programmer’s object file.

What is the library command used for output of data?

There are actually several of them, but we’ll learn to use first the printf() statement. The syntax is:

printf(<format string> [, <argument list>])

The format string may contain:


▪ ordinary characters (excluding %)
▪ conversion specification – starting with %.

The conversion specifications that are often used are:


▪ %c for character
▪ %d for integer
▪ %f for float
▪ %lb for double (long float)
▪ %s for string

For floating point values, by default, six digits after the decimal point will be printed. It is possible to override this
default by writing “%.<number>f” or “%.<number>lf” where <number> is an integer value (greater than or
equal to zero), that indicates the number of digits after the decimal point.

Example:

/* sample program illustrating the use of printf() */


#include <stdio.h>

void main(void)
{
char ch;
int i;
float f;
double d;

/* initialize variables */
ch = ‘Z’;
i = 70;
f = 0.123456789;
d = 0.123456789123456789;

printf(“Hello\n”); /* print a string */


printf(“%s\n”, “Bye”); /* print string */

1
C Programming Handout #3: Input/Output 2

printf(“%c\n”, ‘A’); /* print char constant */


printf(“%c\n”, ch); /* print value of a char variable */
printf(“%c\n”, i); /* print value of int variable */

printf(“%d\n”, 567); /* print an int constant */


printf(“%d\n”, i); /* print value of int variable */
printf(“%d\n”, ch); /* print ASCII code of char variable */
printf(“%f\n”, 1.25); /* print float constant */
printf(“%f\n”, f); /* print value of float variable */
printf(“%.2f\n”, f); /* print two digits after decimal point */

printf(“%lf\n”, 3.1416); /* print double constant */


printf(“%.10lf\n”, d); /* print ten digits after decimal point */

printf(“ch = %c\n”, ch);


printf(“ch = %d\n”, ch);
printf(“ch = %c, ch = %d\n”, ch, ch);

printf(“i = %d, f = %f, d= %lf\n”, i, f, d);


}

What is the library command used for input of data?

There are also several of them, but we’ll learn to use first the scanf() statement. The syntax is:

scanf(<conversion specification>, <argument list>)

The conversion specification that are used are the same as those described above for printf().

Example:

/* sample program illustrating the use of scanf() */


#include <stdio.h>

void main(void)
{
char ch;
int i;
float f;
double d;

scanf(“%c”, &ch);
scanf(“%d”, &i);
scanf(“%f”, &f);
scanf(“%lf”, &d);

printf(“ch = %c, i = %d, f = %f, d = %lf\n”,


ch, i, f, d);
}

Example:

2
C Programming Handout #3: Input/Output 3

/* modified program with prompts */


#include <stdio.h>

void main(void)
{
char ch;
int i;
float f;
double d;

printf(“Input value of char variable ch: ”);


scanf(“%c”, &ch);

printf(“Input value of int variable i: ”);


scanf(“%d”, &i);

printf(“Input value of float variable f: ”);


scanf(“%f”, &f);

printf(“Input value of double variable d: ”);


scanf(“%lf”, &d);

printf(“ch = %c, i = %d, f = %f, d = %lf\n”,


ch, i, f, d);
}

NOTE:

1. When using scanf() with char, int, float and double data type variables, always precede the
variable names with an ampersand symbol, i.e., &.
2. Forgetting the ampersand will not result into a syntax error, however, the input data will not be saved properly
in the variable – thus causing a logical error. This is a very common C programming error.

You might also like