Download presentation
Presentation is loading. Please wait.
1
240-222 Computer Programming Techniques Semester 1, 1998
1. Introduction to C and UNIX Objectives of these slides: introduce C using a few examples introduce gcc
2
Overview 1. The History of C 2. 1st C Program 3. Compiling C using gcc
4. #include 5. 2nd C Program 6. Memory as Many Boxes 7. 3rd C Program 8. A C Program ‘Skeleton’
3
1. The History of C (D&D Sec. 1.7)
Martin Richards. No types BPCL 1967 1970 B Ken Thompson 1972 Dennis Ritchie Some Types C Bjarne Stroustrup OOP C++ 1986 ANSI C The standard. Strong Typing 1989
4
2. 1st C Program (hello.c) /* my first program */ #include <stdio.h> int main() { printf("Hello World\n"); return 0; }
5
3. Compiling using gcc $ gcc hello.c $ a.out Output: Hello World
$ gcc -o hello hello.c $ hello Output: Hello World $ gcc -Wall -o hello hello.c $ hello Output: Hello World
6
3.1. Compilation in 4 Stages (Sec. 1.11)
Preprocessing Compilation Optimization Linking
7
4. #include #include <stdio.h>
This header file contains declarations and function ‘headers’, called function prototypes. The angled brackets tell the compiler that the file stdio.h is in the directory: /usr/include
8
Library files are stored in the directory:
stdio.h only contains function ‘headers’; where is the actual code? At linking time, gcc automatically includes certain standard libraries. Library files are stored in the directory: /usr/lib
9
5. 2nd C program (add.c) Fig. 2.5 /* Addition program */ #include <stdio.h> int main() { int x, y, z; printf("Enter first integer\n"); scanf("%d", &x); printf("Enter second integer\n"); scanf("%d", &y); z = x + y; printf("Sum is %d\n", z); return 0; }
10
Compile and Run $ gcc -Wall -o add add.c $ add Enter first integer /* I type this */ Enter second integer /* Me again */ Sum is 5 /* Correct! */
11
6. Memory as Many Boxes Sec. 2.4
Each box is labelled with an identifier; it is the name of the box. e.g. x, y, z The address of a box is & and its name. e.g. &x, &y, &z
12
C Programs Mess With Boxes
Boxes are typed (e.g. int, float, etc). This specifies their size and shape Boxes contain values (e.g. 5, 3.4) C Programs Mess With Boxes
13
7. 3rd C Program (bigger.c) Fig. 2.13
/* Using the if statement, relational operators and equality operators */ #include <stdio.h> int main() { int a, b; printf("Enter two integers, and I will tell you the relationships they satisfy:"); scanf("%d%d", &a, &b); continued
14
if(a == b) printf("%d is equal to %d\n", a, b); if(a
if(a == b) printf("%d is equal to %d\n", a, b); if(a != b) printf("%d is not equal to %d\n", a, b); if(a < b) printf("%d is < than %d\n", a, b); continued
15
if(a > b) printf("%d is > than %d\n", a, b); if(a <= b) printf("%d is <= to %d\n", a, b); if(a >= b) printf("%d is >= to %d\n", a, b); return 0; }
16
Compile and Run $ gcc -Wall -o bigger bigger.c
$ bigger Enter two integers, and I will tell you the relationships they satisfy: is not equal to 7 3 is < than 7 3 is <= to 7
17
8. A C Program ‘Skeleton’ (skeleton.c)
/* Name, student no. ( addr.) Date Description of Code (5-10 lines) */ #include <stdio.h> int main() { /* declare some variables */ /* do something */ return 0; }
18
Compile and Execute: $ gcc -Wall -o skeleton skeleton.c $ skeleton
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.