Cprogramming ICTweek 1
Cprogramming ICTweek 1
Introduction
week 1: Setting up
environment
Lecturer : Do Quoc Huy
Dept of Computer Science
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
Course’s topics
1st Programming environment introduction, code editor
2nd Introduction to C programming language
3rd Standard output introduction
4th Variables, constant, Standard input
5th Expressions
6th Branches
7th Loops
8th Loops
9th Functions
10th Arrays
11th Pointers
12th Arrays and pointers
13th Strings
14th Structures
15th Final Exam
3
2
Environment and tools
• Operating System: Linux Fedora 8.
• Programming language: C
• Compiler: gcc
• Editor : emacs
• Window manager: KDE / Gnome
Linux login
• You have already
your account and
password
3
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
4
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 &
10
5
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.
11
C Compiler
gcc
The
executable
file a.out Library
12
6
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
13
GUI of Emacs
14
7
GUI of Emacs
• The display in Emacs is divided into 3
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.
15
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
16
8
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
17
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
18
9
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.
20
10
Another userful commands
• C-g: keyboard-quit (stop a command
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
21
22
11
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
23
#include <stdio.h>
main()
{
printf("Welcome to C Programming Introduction.\n");
}
• Save file.
24
12
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.
25
26
13