Chapter 1 Introduction to C
Language
By C. Shing
ITEC Dept
Radford University
Objectives
Understand brief history of C
Describe C components
Understand C features
Understand Program Structure in C
Understand how to run a C program
using GNU C compiler and in .NET environment
Understand how to use a vi editor
Slide 2
C Brief History
1967 Martin Richards
1969 Ken Thompson, Bell Lab
Object-oriented
Super-set of C
1989 ANSI C (e.g. GNU C: gcc)
1999 updated ANSI C
developed C language on DEC PDP-11 (traditional C: cc)
machine independent
1978 C++: Bjarne Stroustrup
Develop B language for Unix (on DEC PDP-7)
machine dependent
1970 Dennis Ritchie, Bell Lab
Develop BCPL language for writing OS and compilers
Variable-length array
Restrict pointer modifier
2002 Microsoft C# for .NET framework
Slide 3
C Components
Compiler
Preprocessor: code begins with # at column 1
traditional
ANSI
Process code before compiler starts translating
Library
Standard Library (/usr/lib/libc.a)
Examine table of contents: ar t /usr/lib/libc.a
Math Library (/usr/lib/libm.a)
Utility Library
Slide 4
C Features
Middle-level language
Machine independent
Case sensitive
Type language (weak type)
Can access bit level
Variable must be declared a type
Allow automatic type conversion
No boolean data type (0: false, non-zero: true)
No run-time error checking
Programmer’s responsibility: array boundary
Slide 5
C Features
Small language, very efficient
32 keywords in ANSI C compared to BASIC
(> 100 reserved words)
All subprograms are functions
Function uses pass-by-value
Function cannot be nested within another function
Can manipulate memory
System programming tool
Slide 6
Running C Program
C source program -> C Compiler
-> Object program -> Link
(to get executable program)
->(feed data) Execution
Slide 7
Program Structures
File Extension
.c (or .cpp for C++)
Compile & link (Solaris) , then execute
gcc mainfile.c [sub1filename.c sub2filename.c]
or in traditional C: (if math library is called in the program)
cc mainfile.c [sub1filename.c sub2filename.c] –lm
then
a.out
or
gcc -o executable mainfile.c
[sub1filename.c sub2filename.c]
then
executable
Slide 8
Program Structures (Cont.)
Compile & link (Solaris) to your library,
then execute
gcc mainfile.c [sub1filename.c sub2filename.c]
-lyourlibraryname
or in traditional C: (if yourlibrary is called in the program)
cc mainfile.c [sub1filename.c sub2filename.c]
–llyourlibraryname
then
a.out
or
gcc -o executable mainfile.c
[sub1filename.c sub2filename.c] –llyourlibraryname
then
executable
Slide 9
Program Structures (Cont.)
Compile only (Solaris)
gcc –c filename.c
or
cc -c filename.c (traditional C)
Slide 10
Program Structures (Cont.)
mainfile.c
/*
**********************************
* This is a comment
*
* Block
*
**********************************
*/
// Comment line
// Declaration for standard library: in /usr/include/stdio.
// The following line is required
#include <stdio.h>
Slide 11
Program Structures (Cont.)
#include <math.h> //if math library is used
// The following file yourdeclaration.h
// is in your current directory
#include “yourdeclaration.h”
// define macro constants
#define PI 3.14159
#define MY_MESSAGE “Good Morning!”
Slide 12
Program Structures (Cont.)
// declare named global constants
const int number = 4;
// declare types
typedef char myCharacter;
// declare global variables
int a;
char b=‘a’, c=‘\n’;
myCharacter e;
long d = 14000L; // this means long int (default int)
float x=-2.5F;
double y=2.5;
Slide 13
Program Structures (Cont.)
// declare function prototype
int sub1(int);
// define main function
int main (void)
{
// declare local variables
int lb, lc;
lc = 2;
// call function
lb = sub1(lc);
printf(“%d\n”,lb);
return 0; // no error exit
}
Slide 14
Program Structures (Cont.)
// define function
int sub1 (int l)
{
// declare local variables
int la;
// call function
la = 5*l;
return la;
}
Slide 15
Appendix:
Traditional C (not recommended to write)
No const data type (use #define instead)
No void data type
Main function does not return anything
No function prototype
Return type for function default is int if not specified
Function formal parameters are defined after ()
Slide 16
Program Structures (Traditional C)
mainfile.c
/*
**********************************
* This is a comment
*
* Block
*
**********************************
*/
// Comment line
// Declaration for standard library: in /usr/include/stdio.
// The following line is required
#include <stdio.h>
Slide 17
Program Structures (Cont.)
#include <math.h> //if math library is used
// The following file yourdeclaration.h
// is in your current directory
#include “yourdeclaration.h”
// define macro constants
#define PI 3.14159
#define MY_MESSAGE “Good Morning!”
#define NUMBER 4
Slide 18
Program Structures (Cont.)
// declare types
typedef char myCharacter;
// declare global variables
int a;
char b=‘a’, c=‘\n’;
myCharacter e;
long d = 14000L;
float x=-2.5F;
double y=2.5;
Slide 19
Program Structures (Cont.)
// define function sub1 must appear before main function
sub1 ()
int l;
{
// declare local variables
int la;
// call function
la = 5*l;
return la;
}
Slide 20
Program Structures (Cont.)
// define main function
main ()
{
// declare local variables
int lb, lc;
lc = 2;
// call function
lb = sub1(lc);
printf(“%d\n”,lb);
}
Slide 21
vi/vim editor
vi filename.c
Please refer to
http://www.chem.brown.edu/instructions/vi.html
Slide 22
Use vi Editor
2 modes:
Editor mode (press <esc>): make correction
Insert/open/append mode
(press<i>/<o>/<a>): add text
Slide 23
Use vi Editor (Cont.)
Editor mode: (Common keys)
<h>:move left, <l>: move right,
<j>: move down, <k>: move up
<0>: beginning of line, <$>: end of line
<w>: next word, <b>: previous word
<:><n>: go to line n
Slide 24
Use vi Editor (Cont.)
Editor mode: (Cont.)
<x>: delete character, <d><w>: delete word
<d><d>: delete line (to clip board), <Y>: copy line
<p>: paste from clip board after/below cursor
<P>: paste from clip board before/above cursor
Move cursor to line m,<d><d>, then move
cursor to line n, <p>: move line m to below line n
Move cursor to line m,<Y>, then move
cursor to line n, <p>: copy line m to below line n
Slide 25
Use vi Editor (Cont.)
Editor mode: (Cont.)
Note: put a number n before an action will repeat
the action n times.
For example:
10<j>: move cursor down 10 lines
10<x> delete 10 characters
10<d><d>: delete 10 lines
10<Y>: copy 10 lines
Slide 26
Use vi Editor (Cont.)
Editor mode: (Cont.)
<r>: replace a character
<c><w> type in word<esc>: replace a word
<R> type in words<esc>: replace/type over words
st
</>type in word<enter>: search the 1 occurrence of the word
<:><w>: save, <:><w><q>: save and quit,
<:><w><q>type in filename<enter>: save to filename and quit
<:><s></><word1></><word2></><enter>:
substitute the 1st occurrence of word1 for word2
<:><1><,><$><s></><word1></><word2></><enter>:
substitute word1 for word2 from line 1 to lat line
Slide 27
Use vi Editor (Cont.)
Editor mode: (Cont.)
<J>: join the cursor line and the line below into
one line
Move cursor to position <i><esc>:
split at the cursor position into 2 lines
<.>: repeat the previous command
<n>: next searched word
<G>: go to last line
<ctrl><g>: file status
Slide 28
Use vi Editor (Cont.)
Insert/open/append mode: (Common keys)
<i>type in words<esc>:insert words before the cursor
<a>type in words<esc>:append words after the cursor
<o>type in words<esc>:open lines after the cursor
and append words
<O>type in words<esc>:open lines before the cursor
and append words
Slide 29
Class Example
Hands On Example0:
edit the following 2 lines in file .exrc (vi ~/.exrc)
:abbr #b /************************
:abbr #e ************************/
Then when you use
vi template.c
And after press<i>/<a>/<o> to insert text, you type
#b followed by <Enter>, you add a line /******** in.
Similarly, when you type
#e followed by <Enter>, you add a line ********/ in
Example 1
Slide 30
Programming Environment in .NET
Running C/C++ Instruction (Reference)
Not used in this course
Slide 31
Reference:
Brian W. Kernighan & Dennis M. Ritchie:
C Programming Language, ANSI Edition
Prentice Hall
Al Kelley & Ira Pohl: A Book on C, 4th ed.
Addison Wesley
Deitel & Deitel: C How to Program, 4th ed.,
Chapter 1, Prentice Hall
Slide 32
© Copyright 2026 ExpyDoc