CIntro Week01
CIntro Week01
INTRODUCTION
WEEK1: SETTING UP ENVIRONMENT
2
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.
3
Environment and tools
• Operating System: Linux Fedora 8.
• Programming language: C
• Compiler: gcc
• Editor : emacs
• Window manager: KDE / Gnome
4
Linux login
• You have already
your account and
password
5
Linux file system
• Structured like an
upside-down tree.
• Directories / sub-
directories: Special files
used for grouping. The
Root directory is /
• Each user has a home
directory: ~ or
home/userid
6
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
7
Open a command window
• Click on Terminal icon (or press Alt-F2: type xterm
or konsole)
• Then you can use Linux commands.
• You may create other windows from the first one by
typing xterm &
8
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
9
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
Steps in Running a Program
C Compiler
gcc
The
executable
file a.out Library
11
Start the emacs Editor
• Select a window and enter the command
emacs & or emacs –nw
• To use Emacs to edit a file, type:
emacs filename
12
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
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
Emacs commands for a file
• Find and open a file
– C-x C-f file_name
– You can use TAB key to view the list of file in current
directory
– When you tape a character, all files whose name start with
this character will be displayed.
• Save a file as you are working on it, type:
– C-x C-s
• Exit emacs: C-x C-c
16
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
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
18
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.
19
Another userful commands
• C-g: keyboard-quit (stop a comman which is taking
too long)
• C-x 1 One window (i.e., show only one window)
• C-x 2 Splits the screen into two windows
• C-x o Move the cursor to the bottom window.
• C-x b Switch to buffer
• C-s search a string
20
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
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
Exercise 1.4 Creating simple
algorithm
• Write an algorithm for choosing clothes to wear
for different activities involving different weather
conditions (e.g., going to the beach when it is hot,
skiing in the snow, etc.). The algorithm should make
it clear how the choice of clothes depends on
the weather and the activity, and should be able to
cope with a range of weather conditions.
• Hint: Use selection.
24
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
Thank you
foryour
attentions!