Professional Documents
Culture Documents
Basic Functions
A C program consists of one or more functions that contain a group of statements which perform a specific task. A C program must at least have one function: the function main. We can create our own function or use the functions that has been created in the library, in which case we have to include the appropriate header file (example: stdio.h).
In this section, we will learn a few functions that are predefined in the header file stdio.h These functions are: printf() scanf() getchar() & putchar() In addition to those functions, we will also learn about Format Specifier and Escape Sequence which are used with printf() and scanf().
printf()
Used to send data to the standard output (usually the monitor) to be printed according to specific format. General format: printf(control string, variables); Control string is a combination of text, format specifier and escape sequence. Example: printf(Thank you); printf (Total sum is: %d\n, global_var); %d is a format specifier \n is an escape sequence
Format Specifier
No 1 2 3 4 5 6 7 8 9 10 11 12 13 Format Specifier %d %i %o %u %x %X %f %e %E %g %G %x %s Output Type Signed decimal integer Signed decimal integer Unsigned octal integer Unsigned decimal integer Unsigned hexadecimal (small letter) Unsigned hexadecimal (capital letter) Integer including decimal point Signed floating point (using e notation) Signed floating point (using E notation) The shorter between %f and %e The shorter between %f and %E Character String Output Example 76 76 134 76 9c 9C 76.0000 7.6000e+01 7.6000E+01 76 76 7 76'
Tells the printf() function the format of the output to be printed put.
Escape Sequence
Escape Sequence \a \b \f \n \r \t \v \\ \ \o \x \O Effect Beep sound Backspace Formfeed (for printing) New line Carriage return Tab Vertical tab Backslash sign Octal decimal Hexadecimal NULL
scanf()
Read data from the standard input device (usually keyboard) and store it in a variable. General format: scanf(Control string, &variable); The general format is pretty much the same as printf() except that it passes the address of the variable (notice the & sign) instead of the variable itself to the second function argument. Example:
gets a function that will get a string of characters scanf to input individual words from a line of text blank spaces act as delimiters
char s[80]; printf("Please type in some words : "); gets(s); printf("You typed : %s\n", s); return 0;
char word1[80], word2[80]; printf("Please type TWO words : "); scanf("%s %s", &word1, &word2); printf("You typed : %s and %s\n", word1, word2); return 0;
10
char c; printf("Please type ONE character : "); c=getchar(); printf("You typed : %c\n", c); return 0;
getch to input a single character reads a key hit without waiting for you to press enter.
#include <stdio.h> #include <conio.h> int main() { char c; printf("Please type 1 character : "); c=getch(); printf("You typed : %c\n", c); return 0; }
11
#include <stdio.h>
int main() {
#include <stdio.h>
int main() {
#include <stdio.h>
int main() {
char letter; for (letter='A'; letter<='Z'; letter++) putchar (letter); printf("\n"); return 0; 12
Sample run: Enter a string: This is a test input of a string of characters. The string just entered is: This is a test input of a string of characters.
13