0% found this document useful (0 votes)
22 views

Cprogramming ICTweek 1

This document introduces C programming by discussing setting up the programming environment in Linux and learning basic C concepts over several weeks. It covers installing tools like the GCC compiler and emacs editor, explains basic Linux commands, and provides exercises for using emacs and writing a simple C program.

Uploaded by

Mr. Creeper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Cprogramming ICTweek 1

This document introduces C programming by discussing setting up the programming environment in Linux and learning basic C concepts over several weeks. It covers installing tools like the GCC compiler and emacs editor, explains basic Linux commands, and provides exercises for using emacs and writing a simple C program.

Uploaded by

Mr. Creeper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C Programming

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

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
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

• Use them to open


a working
session, then
setting up your
directory, editor
and compiler.

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

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
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 &

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

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

Steps in Running a Program


C program Emacs A file on
on paper disc, say
Editor ict.c
or in your
head

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

• An emacs window is created. You can


select the font size. You can now type in a
program.
• To close the emacs window
– Select menu Files of emacs
– Select exit emacs

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.

• The delete key should work to


remove characters and inserted
text will push existing text over.
19

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

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

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)

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

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.

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

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.

26

13

You might also like