CP4044 – Lecture 10. 1. C Programming - Introduction. CP4044 – Lecture 10. 2.
Hello World Program. /* The "hello world" program in Java. Date written 2/12/5.
Hello World Program /* The "hello world" program in Java. Date written 2/12/5 */ public class Hw { static public void main(String args[]) { System.out.println("hello, world"); } }
C Programming - Introduction
This is Java code
When this program is run it will output: hello, world
1
CP4044 – Lecture 10
2
CP4044 – Lecture 10
Hello World Program
ANSI C
/* The "hello world" program in C Date written 2/12/5 */ void main(String args[]) { printf("hello, world"); }
This is C code When this program is run it will output:
• Procedural programming language • C source code is compiled to machine code • Hardware dependant – an executable file will only run on one hardware/os platform, unless re-compiled • Very efficient high-level programming language • Particularly suited to systems programming • Closely associated with Unix, but compilers available for all operating systems
hello, world 3
CP4044 – Lecture 10
4
CP4044 – Lecture 10
Creating a C Program
Compiling C on Linux
• Use any text editor to write C source code (e.g. Kate on Linux, Notepad on Windows) • Note that C is case-sensitive • Save source with extension .c • Compile code in a command shell or window using C compiler (e.g. cc or gcc on Linux) • Type the name of the executable file to run the program
• On Linux use gcc – C and C++ compiler • Example: compile and run hw.c gcc hw.c a.out hello, world
Compile source producing output file a.out
gcc –o hw hw.c hw hello, world
Compile source naming output file hw
man gcc
View manual pages for gcc compiler
Run program Output from program
(space bar to page, ‘q’ to quit) CP4044 – Lecture 10
5
CP4044 – Lecture 10
6
Kevan Buckley, Peter Burden, CP4044, 2004
1
Features of C
Standard Output
• A C program consists of one or more functions • Must have a function called main() – the starting point for execution • One function calls another • All functions can be contained in the same source file • Or may be split into a number of source files • Functions can appear in any order in the source file 7
CP4044 – Lecture 10
• printf() – a library function • Used to write to standard output – usually the screen
void main() { printf("hello world\n"); printf("C is fun\n"); }
Strings • Enclose in double quotes • Use escape codes for non-printing characters
8
CP4044 – Lecture 10
Program Structure • Large programs consist of a number of functions which call each other • main() is the entry point
\a "BELL" - i.e. a beep \b Backspace \f Form Feed (new page) \n New Line (line feed) \r Real carriage return \t Tab
void main() { printf("hello, "); printf("world"); printf("\n"); }
\v Vertical Tab
void first() { printf("hello "); }
\\ Backslash \' Single Quote \" Double Quote \nnn Octal code for any char
void second() { printf("world"); } 9
CP4044 – Lecture 10
Programming With Integers
}
10
CP4044 – Lecture 10
Input – scanf()
/* A program to read in two numbers and print their sum */ main() { int x,y;
void main() { first(); second(); printf("\n"); }
scanf("%d",&x);
• scanf() – a library function • Provides a simple way of inputting data from the standard input device – usually the keyboard. • Converts the character stream to the required datatype – "%d" - expect an integer
/* places to store numbers */
printf("Enter x "); scanf("%d",&x); printf("Enter y "); scanf("%d",&y); printf("The sum of x and y was %d\n",x+y);
Enter x 4 Enter y 5
– &x
- store at address &x
The sum of x and y was 9 CP4044 – Lecture 10
11
CP4044 – Lecture 10
12
Kevan Buckley, Peter Burden, CP4044, 2004
2
Format Strings
Another Example • Input two integer, output their sum:
printf("The sum of x and y was %d\n", x+y );
• Within a string, two characters have special significance:
main() { int n1; int n2; printf("Enter two numbers "); scanf("%d%d", &n1, &n2 );
• % description of data to be inserted into string • %d - integer decimal data • x+y -The data, of the correct type, must follow the string
printf("The sum is %d\n", n1+n2 ); }
• \
introduces an escape code • \n - represents the newline character
CP4044 – Lecture 10
13
CP4044 – Lecture 10
14
Kevan Buckley, Peter Burden, CP4044, 2004
3