1 Topic One - Introduction
1 Topic One - Introduction
1
References
• C Programming For The Absolute Beginner,
Second Edition. Michael Vine
• C For Dummies 2nd Edition. Dan Gookin
2
Topic One: Introduction
3
What is a Computer?
• An electronic device for storing and processing
data, typically in binary form, according to
instructions given to it in a variable program.
• From the above definition we see that
computer can process data in binary form only
(series of 0s and 1s).
4
Computer Programming
• Programming languages bridge the gap between human
thought processes and computer binary circuitry.
• Computer programming (often shortened to programming) is a
process that leads from an original formulation of a computing
problem to executable computer programs.
• Programming language: A series of specifically defined
commands designed by human programmers to give directions
to digital computers.
• Commands are written as sets of instructions, called programs.
• All programming language instructions must be expressed in
binary code before the computer can perform them.
5
C Programming Language
• C programming language is a general-purpose, high-level
language that was originally developed by Dennis M.
Ritchie to develop the UNIX operating system at Bell Labs.
• It was originally first implemented on the DEC PDP-11
computer in 1972.
• In 1978, Brian Kernighan and Dennis Ritchie produced the
first publicly available description of C, now known as the
K&R standard.
• The UNIX operating system, the C compiler, and essentially
all UNIX applications programs have been written in C.
6
• C was invented to write an operating system
called UNIX.
• C is a successor of B language, which was
introduced around 1970.
• The UNIX OS was totally written in C by 1973.
• Today, C is the most widely used and popular
System Programming Language.
• Most of the state-of-the-art software have been
implemented using C.
• Today's most popular Linux OS and RDBMS
MySQL have been written in C.
7
• The C has now become a widely used
professional language for various reasons
which are;
o Easy to learn
o Structured language
o It produces efficient programs.
o It can handle low-level activities.
o It can be compiled on a variety of computer
platforms.
8
C development life cycle
Step 1: Use an editor to write your source code. By
tradition, C source code files have the extension .C
(for example, MYPROG.C, DATABASE.C, and so on).
The source code file is a text file on disk. The file
contains instructions for the computer that are
written in the C programming language.
9
• Step 2: Compile the program using a compiler. If the
compiler doesn't find any errors in the program, it
produces an object file. The compiler produces object
files with an .OBJ extension and the same name as
the source code file (for example, MYPROG.C
compiles to MYPROG.OBJ). If the compiler finds
errors, it reports them. You must return to step 1 to
make corrections in your source code.
• Compiler is a special program that reads the
instructions stored in the source code file, examines
each instruction, and then translates the information
into the machine code understood only by the
computer’s microprocessor.
10
Step 3: Link the program using a linker. If no errors
occur, the linker produces an executable program
located in a disk file with an .EXE extension and the
same name as the object file (for example,
MYPROG.OBJ is linked to create MYPROG.EXE).
11
• Step 4: Execute the program. You should test
to determine whether it functions properly. If
not, start again with step 1 and make
modifications and additions to your source
code.
12
Example 1
#include <stdio.h>
main( )
{
printf(“This is my first C - program”);
return 0;
}
13
Example 2
#include <stdio.h>
main( )
{
printf(“Hello World”);
printf(“This is my first C - program”);
}
14
Exercise
Write a program to display First and Second
names, Age, Gender and address. Save the file
as MyParticulars
15
The Components of a C Program
• A Short C Program
• The Program's Components
– The main() Function
– The #include Directive
– The Variable Definition
– Program Statements
– The Function Definition
– Program Comments
– Braces
– Running the Program
16
Every C program consists of several components
combined in a certain way. To get the overall
picture, however, you should begin by seeing a
complete (though small) C program with all its
components identified.
17
A Short C Program
18
Before looking at the sample program, you need to know
what a function is, because functions are central to C
programming.
A function is an independent section of program code
that performs a certain task and has been assigned a
name. By referencing a function's name, your program
can execute the code in the function. The program also
can send information, called arguments, to the function,
and the function can return information to the main part
of the program.
The two types of C functions are library functions, which
are a part of the C compiler package, and user-defined
functions, which you, the programmer, create. You will
learn about both types of functions in the next sections.19
/*The program to calculate the product of two numbers. */
#include <stdio.h>
main()
{
int a, b, c;
/* Input the first number */
printf("Enter a first number: ");
scanf("%d", &a);
/* Input the second number */
printf("Enter another number: ");
scanf("%d", &b);
/* Calculate and display the product */
c = a*b;
printf (“The product of two numbers is %d”, c);
return 0;
20
}
The Program's Components
21
The main() Function
22
The #include Directive
27
printf()
• The printf() statement is a library function that
displays information on-screen. The printf()
statement can display a simple text message
or a message and the value of one or more
program variables.
• The function is called printf() for a reason. The
f stands for formatted. The advantage of the
printf function over other, similar display-this-
or-that functions in C is that the output can be
formatted.
printf(“format_string”[,var[,...]]);
28
• What appears in the double quotes is really a formatting
string. It’s still text that appears in printf()’s output, but
secretly inserted into the text are various conversion
characters, or special “placeholders,” that tell the printf()
function how to format its output and do other powerful
stuff.
• After the format string comes a comma (still inside the
parentheses) and then one or more items called arguments.
The argument shown in the preceding example is var, which
is short for variable. We can use printf() to display the
content or value of one or more variables. We do this by
using special conversion characters in format string.
29
scanf()
30
The return statement
32
• Functions allow you to group a logical series of
activities, or program statements, under one
name
• Each function implementation requires that
you use a beginning brace ({) and a closing
brace (})
33
Program Comments
Any part of your program that starts with /* and ends with */ is
called a comment. The compiler ignores all comments, so they
have absolutely no effect on how a program works. You can put
anything you want into a comment, and it won't modify the way
your program operates. A comment can span part of a line, an
entire line, or multiple lines. Here are three examples:
/* A single-line comment */
int a,b,c; /* A partial-line comment */
/* a comment
spanning
multiple lines */
35
Keywords
• There are 32 words defined as keywords in the
C programming language. These keywords
have predefined uses and cannot be used for
any other purpose in a C program. These
keywords are used by the compiler as an aid
to building the program.
• Note that these keywords must always be
written in lowercase
36
Keyword Description
auto Defines a local variable as having a local lifetime
break Passes control out of the programming construct
case Branch control
char Basic data type
const Unmodifiable value
continue Passes control to loop’s beginning default Branch control
do Do While loop
double Floating-point data type
else Conditional statement
enum Defines a group of constants of type int
extern Indicates an identifier as defined elsewhere
float Floating-point data type
for For loop
goto Transfers program control unconditionally
if Conditional statement
int Basic data type
37
long Type modifier
register Stores the declared variable in a CPU register
return Exits the function
short Type modifier
signed Type modifier
sizeof Returns expression or type size
static Preserves variable value after its scope ends
struct Groups variables into a single record
switch Branch control
typedef Creates a new type
union Groups variables that occupy the same storage space
unsigned Type modifier
void Empty data type
volatile Allows a variable to be changed by a background
routine
while Repeats program execution while the condition is true
38
Escape Sequences
• Escape sequences are specially sequenced characters
used to format output.
• When combined with the backslash (\), special
characters such as n, t, r, ‘ and “ make up an escape
sequence.
Escape Sequence Purpose
\n Creates a new line
\t Moves the cursor to the next tab
\r Moves the cursor to the beginning of
the current line
\\ Inserts a backslash
\" Inserts a double quote
\' Inserts a single quote 39
Escape Sequence \n
• This particular escape sequence (\n) tells the
program to add a new line.
• Take a look at the following program
statement. How many new lines are added to
standard output with these printf()
statements?
printf("\nC you later\n");
printf("line 1\nline2\nline3\n");
40
Escape Sequence \t
• Escape sequence \t moves the cursor to the next tab space.
This escape sequence is useful for formatting output in
many ways. For example, a common formatting desire is to
create columns in your output, as the following program
statements demonstrate.
printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
printf("\t\t\t\t1\t2\t3\n");
printf("4\t5\t6\t7\t8\t9\t10\n");
printf("11\t12\t13\t14\t15\t16\t17\n");
printf("18\t19\t20\t21\t22\t23\t24\n");
printf("25\t26\t27\t28\t29\t30\t31\n");
41
Escape Sequence \\
42
• The following program statement
demonstrates escape sequence \\. printf("c:\\
cygwin\\bin must be in your system path");
The output is
c:\cygwin\bin must be in your system path
43
Escape Sequence \”
• Another reserved character in the printf()
function is the double quote (") character. To
insert a quote into your outputted text, use
the escape sequence \" as demonstrated in
the following program statement.
printf("\"This is quoted text\"");
• The output is
“This is quoted text”
44
Escape Sequence \’
• Similar to the double quote escape sequence
(\") is the single quote (also called an
apostrophe) escape sequence (\'). To insert a
single quote into your outputted text, use the
escape sequence \' as demonstrated in the
following program statement
printf("\nA single quote looks like \'\n");
• The output is
A single quote looks like ‘
45
Common Errors
• The following are some of the common errors
that many beginner programmers can
encounter during compiling the source code.
– Missing Program Block Identifiers
– Missing Statement Terminators
– Invalid Preprocessor Directives
– Invalid Escape Sequences
– Invalid Comment Blocks
46
• A single error at the top of your program can
cause a lot of errors to be seen during compile
time.
• The best place to start debugging compile
errors is with the first error
47