This lesson will show you
how to:
• Compile and run source code
• Save source code
• Use libraries
• Use the printf() function
• Use the scanf() function
• Use the getch() function
Q: How can you tell when a lawyer is lying?
A: His lips are moving.
Note: All the keywords is C must be lowercase.
Libraries
Another component common to C
programs is the header file. This supplies information about
the standard library functions. These files all end with
the .h extension and are added to the program using the
#include pre-processor directive. All C compilers use a
pre-processor as their first phase of compilation to manipulate
the code of the source file before it is compiled.
Pre-processor directives are not actually part of the C
language, but rather instructions from you to the compiler.
The #include directive tells the pre-processor you want
to read another file and include it with your program.
The most commonly required header file is stdio.h. The
directive to include this file is:
#include <stdio.h>
The filename can be in upper or lower case, but lower case
is the norm. The stdio.h header file contains, among other
things, information related to the printf() function. Notice
that the #include directive does not end with a semi-colon.
This is because #include is not a keyword that can define
a statement, it is an instruction to the compiler.
With few exceptions, C ignores spaces. Having said that
spaces and indentation are very important when it comes
to reading and debugging the code.
Example 1:
Since all C programs share common
elements, understanding one program will help you understand
many others. One of the simplest programs is:
#include <stdio.h>
int main(void)
{
printf(“My first program in
C.”);
return 0;
}
The fourth line:
printf(“This is a short C program.”):
The printf() function outputs the characters that are contained
between the quotes to the screen. printf() is a standard
library function which displays the string on the screen.
Input from the keyboard:
scanf() is a standard library function to read a value
such as an integer or string from the keyboard.
Example 2:
#include <stdio.h>
int main(void)
{
int Number;
printf(“Enter a Number “);
scanf(“%d”, &Number);
getch();
return 0;
}
Lines 3 (int Number) will be examined in a further lesson.
The seventh line:
getch()
The function named getch() is a get character function that
reads the character in from the keyboard, and puts it directly
into the program where it is operated on immediately. This
program therefore reads a character and immediately displays
it on the screen. Notice the use of the keyword void in
between the brackets - this signals no vales are being passed
to the function main when execution of the function begins
- use this from now on.
back to top exercises back
to lesson 1 go
to lesson 3