Introduction to C/C++ Programming
C/C++ Programming in UNIX
Fundamentals of Programming
C/C++ Programming in UNIX
What is Programming?
• Computers are dumb machines:
▫ Do only what they are told to do so
▫ Basic operations: instruction set
▫ Example: add number, compare with zero,…
• How to solve a problem using computer?
▫ Express in terms of instructions à a program
• Program:
▫ Collection of instructions
▫ Approach/Method used to solve a problem: ALGORITHM
▫ Example: test if a number is odd or even
• Programming:
▫ Write the instructions necessary to implement the
algorithm using programming language
C/C++ Programming in UNIX
Programming Language
• Express the computer instructions in particular statements
• Early ages:
▫ Binary numbers: directly correspond to specific machine
instructions à machine language
• Next:
▫ Use symbolic names to replace machine language à assembly
language
▫ Need an assembler to convert to machine language
▫ Not portable, machine dependant
• High level language:
▫ Similar to everyday English
▫ Use common mathematical notations
▫ Consist of statements, accomplish substantial tasks
▫ Need compiler/translator: convert to a form which is
understandable to particular computer
▫ Example: C, C++, C#, Visual Basic…
C/C++ Programming in UNIX
Operating Systems
• Special software/program
• Interface between hardware and software
• Control the entire operations of a computer system:
▫ Manage input/output
▫ Manage system resources
▫ Handle execution of programs
• Example of OS: UNIX, Linux, Windows, DOS,…
• Multitasking
C/C++ Programming in UNIX
Introduction to UNIX
• History of UNIX:
▫ 1960s: Experimental OS called Multics by MIT, Bell
Labs and GE. Support multi users, for mainframes.
Assembly language was used
▫ 1970s: Unics then Unix
▫ 1973: Unix was rewritten in C
▫ 1980s: AT&T licensed the source code
• Advantages:
▫ Direct interaction
▫ Running on various types of hardware
▫ Easy to move to different machine
▫ Free versions
• Free BSD, Solaris, HP UX,…
C/C++ Programming in UNIX
Free UNIX-like OS
• 1983: Richard Stallman announced GNU project.
“everyone who received a copy of software would
be free to use, study, modify, and redistribute it”
• 1990s: Linus Torvalds wrote the Linux Kernel à
UNIX-like OS
• Linux distributions (distros): Ubuntu, Suse,
Fedora core, Redhat, Debian,…
• Servers, mainframes and super
computers
• Embedded devices: PDA, smart phones
(Google Android, a modified version of
Linux Kernel)
• Desktop
C/C++ Programming in UNIX
Linux directory structure
• Some linux commands:
▫ ls: list file and directory
(#dir in windows)
▫ cd: change directory
▫ mkdir: create a new
directory
▫ vi: view a file
▫ rm: remove directory entries
▫ pwd: show current directory
▫ man <name of command>:
show manual page for a
command
C/C++ Programming in UNIX
The C Programming Language
• Developed in 1972 by Dennis Ritchie
@ Bell Labs. Derived from B language
• To use with UNIX OS
• Were designed for system software,
later also for application software
• 1983: ANSI C, 1990: ISO C
• Most popular programming language
• Powerful programming language: fast,
code portability and ability to access hardware
• C related languages:
▫ C++, Objective-C, C#, C Shell
▫ Java, Perl, PHP, Java Script, Python
C/C++ Programming in UNIX
Compiling C Program
• Editor: write your C program (vi,
gedit)
• Compiler: translate program into
executable form on particular
computer system (gcc)
• Run the executable output file
C/C++ Programming in UNIX
Ubuntu 101
• Installation:
▫ Run along side with other OSes (Windows)
▫ Run on virtual machine: VMWare, Virtual box, MS
Hyper-V
C/C++ Programming in UNIX
Ubuntu 101 (cont.)
• Using terminal:
▫ Boot up Ubuntu
▫ At desktop, use ALT+F2 and enter xterm to open
terminal
▫ or use Applications à Accessories à Terminal
• Using gedit for writing your C program:
▫ Open terminal
▫ Type gedit& (remember the ampersand &)
▫ Work same as Windows Notepad
▫ Remember to save your file with type .c .Example:
myprogram.c (do this before you start writing anything)
▫ Remember the location where you save you .c file as
well
C/C++ Programming in UNIX
Ubuntu 101 (cont.)
• Installing gcc:
▫ Open terminal: sudo apt-get –install build_essential
• Compiling your program:
▫ After finish editing your program, open terminal
▫ Change current directory to where you saved your .c file
▫ Compile your file using gcc <filename.c>
Example: gcc myprogram.c
▫ The above command will produce the output file [Link] by
default. To change the output filename, use the command:
gcc -o <outputfilename> <filename.c>
Example: gcc –o [Link] myprogram.c
• Running your program:
▫ After successfully compile your program
▫ Use ./[Link] or ./<outputfilename> if you compiled with -o