Intermediate Programming

with C

 

Table of Contents

Introduction

Basics

Variables

Input and Output

Boolean Expressions and Branching

Loops

Functions

Files

Arrays and Pointers

 

C Programming Final Test

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter 4

Input and Output

Output

C does not have any built in output functions. Instead, C uses functions from the compiler's standard function libraries. The C standard i/o library includes several different output functions.

printf()

The most common output function is the "printf()" function. The printf() function takes a string called a format string as an argument. The format string tells the function how to format the output. Format Strings are made up of three different types of components they are:

 

1. Literal Text - This is text that will be displayed exactly as it looks.

2. Escape Sequences - These provide special fomating control. They allow you to print special characters on the screen. The following is a list of escape sequences.

 

 Sequence

Meaning

\a

Beep

\b

Backspace

\t

Tab

\\

Prints a Backslash

\?

Prints a question mark

\'

Prints a single quote

 \"

Prints a double quote

3. Conversion specifiers - Conversion specifiers tell printf() where variables should be printed and how they should be printed. The following is a list of common conversion specifiers.

 Specifier

Meaning

%c

Character

%d

Signed Integer

%f

Floating point number

%s

Character string

%u

Unsigned Integer

Take the following for example:

int x = 1;
printf("x contains %d \n", x);

The first statement simply initializes the variable "x." The second statement prints the message on the screen. Printf() will print the literal string "X contains" then it will print the contents of x, and then move to the next line.

puts() Sometimes you need to print text without printing any variables. In these instances you should use "puts()" for output. Puts() allows literal strings and escape sequences but not conversion specifiers. The following is an example of the puts() statement. puts("Hello World \n"); This statement will print the words "Hello World" on the screen and then move to the next line. putchar()

There are times when a program only needs to output a single character. Instead of using printf() or scanf() the program can use a third output function putchar(). putchar() takes the ASCII code of the character you want to print and then prints the character to the screen.

  /*Output the letter A to the screen*/
  putchar(65);  

 

Input

Scanf()

The scanf() function reads data according to the specified format, and assigns it to the passed variables.