Chap 1: Overview of C |
1.0 Introduction |
What is a computer? -->hardware
What is a computer program? -->software |
Computer Organization:
- Input unit: obtain informaion (data and computer program) from various input device, e.g. keyboard - Output unit: e.g. screen, printer - Memory unit - Arithmetic and logic unit (ALU) - Central processing unit (CPU): coordinator - Secondary storage unit |
What is a program: a set of instructions for computing a job, e.g. compute a leap year |
What can programs do?
- mathematical computing: leap year, trigonometry, etc - string manipulation, printing - storage (file) - user interations (VR, multimedia) - music, video .... |
C language: just like any other human languages-->has a set of rule (syntax) |
Why C language?
What are other languages: - Basic, Cobol, Fortran, C++, Lisp, Smalltalk, Ada, Pascal, Prolog, Assembly, ..... |
Writing a program: mapping from problem domain to computer language domain (hard, learn from example) |
Need data types:
E.g. Robot Cops: shoot red, protect green need data types because we need "exact" communication between human and computers ==>Basic data types: int, char, float |
1.1 The Basics of C Environment ![]() |
1.2 Program Output |
#include <stdio.h>
int main(void)
|
#include <stdio.h>
int main(void)
|
#include <stdio.h>
int main(void)
|
#include <stdio.h>
int main(void)
|
1.3 Variables, Expressions, Assignment |
/* The distance of a marathon in kilometers. */
#include <stdio.h> int main(void)
miles = 26;
|
1.4 The Use of #define and #include |
#define LIMIT 100
#define PI 3.14159 #defince C 299792.458 /*speed of light in km/sec */ printf("PI = %f\n", PI); |
#include "my_file.h"
#include <stdio.h> |
In file pacific_sea.h:
#include <stdio.h> #define AREA
2337
|
In file pacific_sea.c:
/*Measuring the Pacific Sea*/
int main(void)
printf("\nThe Pacific Sea covers an area");
|
1.5 The Use of printf() and scanf() |
printf("abc");
printf("%s", "abc"); printf("%c%c%c", 'a', 'b', 'c'); |
printf() ==>
%c as a character %d as a decimal integer %e as a floating point number in scientific notation %f as a floating point number %g in the e-format or f-format, whichever is shorter %s as a string |
printf("%c%3c%5c\n", 'A', 'B', 'C'); |
scanf("%d", &x); ==> address |
scanf() ===>
%c to a character %d to a decimal integer %f to a floating point number (float) %lf to a floating point number (double) %LF to a floating point number (long double) %s to a string |
#include <stdio.h>
int main(void)
printf("\n%s\n%s",
|
1.6 Flow of Control |
if (expr)
statement |
a = 1;
if (b == 3) a = 5; printf("%d", a); |
Compund statement:
if (a == 3) {
|
if (expr)
statement1 else statement2 |
if (cnt == 0) {
a = 2; b = 3; c = 5; } else { a = -1; b = -2; c = -3; } printf("%d", a+b+c); |
#include <stdio.h>
int main(void)
while (i <= 5) {
|
for (expr1; expr2; expr3)
statement; is equivalent to: expr1;
|
for (i = 1; i <= 5; ++i)
sum += i; |
/* Compute the minimum, maximum, sum, and average */
#include <stdio.h>
int main(void)
if(scanf("%lf", &x)!=1) {
|
1.7 Functions |
function declaration:
double pow(double x, double y); function prototype: double pow(double, double); |
#include < stdio.h>
float maximum(float x, float y);
int main(void)
prn_info();
float maximum(float x, float y)
float minimum(float x, float y)
void prn_info(void)
|
CALL BY VALUE
#include < stdio.h> void try_to_change_it(int); int main(void)
printf("%d\n", a); /* 1 is printed*/
void try_to_change_it(int a)
|
1.8 Arrays, Strings, and Pointers |
Arrays:
int a[3]; |
#include <stdio.h>
#define CLASS_SIZE 5 int main(void)
printf("Input %d scores: ", CLASS_SIZE);
|
STRINGS:
/*Have a nice day! */ #include <ctype.h>
#define MAXSTRING 100 int main(void)
printf("\nHi! What is your name? ");
|
POINTERS:
#include <stdio.h>
#define MAXSTRING 100 int main(void)
p = &c;
|
1.9 Files |
#include <stdio.h>
int main(void)
ifp = fopen("my_file", "r"); |
Three modes for a file
"r" for read "w" for write "a" for append |
#include <stdio.h>
int main(int argc, char *argv[])
|
/*Count uppercase letters in a file. */
#include <stdio.h>
int main(int argc, char *argv[])
if(argc != 3) {
|
1.10 Operating System Considerations |
steps to be followed in writing and running a C
program:
1. vi pgm.c 2. cc pgm.c 3. a.out |
Interrupting a program:
ctrl-c |
Redirection of the input and the output:
ls
|