Week 1
Week 1
Week 1
Introduction
week 1: Setting up
environment
Lecturer : Cao Tuan Dung
Dept of Software Engineering
Hanoi University of
Technology
For HEDSPI Project
Introduction
• C Programming practice in UNIX
(LINUX) environment
• Methods to write and execute simple
programs in C language
• Basic grammar
• Usage of functions in standard
libraries.
1
Some Rules to be Followed
• Attendance is mandatory.
• Regular assignments should be done
fully in the lab class.
• If you face any problems catching
up, draw the attention of one of the
teachers.
2
Linux login
• You have already
your account and
password
3
Important Directories and
Files
• The root - /
/root – Home directory for root
/boot – static files of the boot loader
/bin – files needed to boot the system
/sbin – /usr/sbin
system admin. utilities
/dev – device files
/etc – admin. and configuration files
4
Some Linux Commands
• ls: Lists the directory contents.
• pwd: Show current working directory.
• cd: Change working directory.
• mkdir : Make directories.
• cp : Copy files and directories.
• cat: view the content of a file.
• mv : Move / rename files.
• man: Display on-line manual pages.
• which: print the directory contain the command.
which gcc
Exercise
• Go to your home directory
• Create a directory named Cprogramming
• Then a subdirectory of Cprogramming:
week1
• In this session, your files will be stored in
this directory.
• And in the ~/Cprogramming/week2 for
the next session, and so on.
• Organize well your files in each week
during this course.
10
5
Steps in Running a Program
Emacs A file on
C program
disc, say
on paper Editor
or in your hedspi.c
head
C Compiler
gcc
The
executable
file a.out Library
11
12
6
GUI of Emacs
13
GUI of Emacs
• The display in Emacs is divided into three
basic areas.
• The top area is called the text window. The text
window takes up most of the screen, and is where
the document being edited appears.
• Below the text window, there is a single mode line (in
reverse type). The mode line gives information about
the document, and about the Emacs session.
• The bottom line of the Emacs display is called the
minibuffer. The minibuffer holds space for commands
that you give to Emacs, and displays status
information.
14
7
Emacs Commands
• Emacs uses Control and Escape (or
Alt) characters to distinguish editor
commands from text to be inserted in
the buffer.
– Control-x means to hold down the control
key, and type the letter x.
(You don't need to capitalize the x, or any
other control character)
– [ESCAPE] x means to press the escape
key down, release it, and then type x.
– Abreviation: C-x for Ctrl-X and M-x means
Meta-x
15
16
8
Moving Around
• The arrow keys on the keyboard
work for moving around one line or
one character at a time.
• Some navigation commands:
• Move to the Top of the file: M-<
• Move to the End of the file: M->
• Next screen (page down): C-v
• Previous screen (page up): M-v
• Start of the current line: C-a
• End of the current line: C-e
• Forward one word: M-f
• Backward one word: M-b
17
Type Text
• Once you move the cursor to the
location in the file where you want
to do some editing, you can just
start typing - just like in an
ordinary word processor.
9
Cut, Copy, and Paste
• You can delete or move blocks of text.
– First move the cursor to the beginning (or
end) of the block of text.
– Then set a mark with: C-spacebar
– Now move to the other end of the block of
text and Delete or Copy the block:
• Delete: C-w
• Copy: M-w
– To Paste a copied block, move to the new
location and insert with : C-y
19
10
Exercise 1.2 : Emacs
Configuration
• In the Emacs command line, find and
open ~/.emacs
• Add the following line to configure Emacs
as you want (use the command as much
as possible).
• Set font
(global-font-lock-mode 1 t)
• Set time and Date
(setq display-time-day-and-date t)
21
Exercise: Emacs
Configuration
• Set working mode for C coding
(defun linux-c-mode ()
"C mode with adjusted defaults for use
with the Linux kernel."
(interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))
• Save the change, and restart emacs
22
11
Exercise 1.3: Edit your first
program
• Use emacs to open a new file name hello.c in
your ~/Cprogramming/week1
• Enter the following line:
/* Your name – your class */
/* This is my first program in C */
#include <stdio.h>
main()
{
printf("Welcome to C Programming Introduction.\n");
}
• Save file.
23
12
Exercise 1.5 Creating simple
algorithm
• Suppose that you have been given a
$100 music store gift voucher. Write
an algorithm for buying some CDs
with the voucher.
• Hint: Use selection and iteration.
25
13