Escolar Documentos
Profissional Documentos
Cultura Documentos
C was originally proposed mainly as a procedure language for scientific calculations, in which routines
were defined according to a plan. However, its use of data structures has led to the concept of encapsulation of
not only various data types but also functions to form classes, which has transformed the old data structures into
"living" objects, the core of object-oriented languages such as C++.
Comments:
Operators:
+, -, *, /, %, ++, --, =, +=, - =, ( ), >, <, >=, <=, ==, !=, &&, ||, etc.
!!! expression with operator "=" means assignment, NOT A MATHEMATICAL EQUATION!!!
a=a+b;
a=7/2; (int a=3 or float a=3.000000).
a=7/2.0; (int a=3 or float a=3.5000).
a++ or ++a ??? (both when used as a single expression are equivalent to a=a+1;). But they are different.
See the following example:
/*********************************************************************************
* Example1_1: demonstrate the difference between prefix and post fix increments *
* H. Yin *
*********************************************************************************/
#include<stdio.h>
int main(void)
{
int a, b, c=0;
Loops:
Functions:
A string is an array of characters with '\0' NULL character (not '0') at the end.
int main(void)
{
char name[20]="Abc D. Efghi"; /* initialisation */
int i;
return 0;
}
/************************** End of Example1_2 ********************************/
Once a string is declared as an array of characters, its members can only be changed either
individually (assigning each character a value) or using string functions.
Pointers:
scanf("formats", variable_addresses);
e.g.
scanf("%f", &a); /* take a real number from input stream (keyboard) and assign it to variable a */
e.g.
printf("\nThe value of a is: %f", a); /* print out (on screen) the message and
the value of a in float format */
Files:
fopen(.), fclose(.), fprintf(.),fscanf(.), etc.
/***************************************************************/
/* comments */ (can be any where)
preprocessors or micros (header files)
function prototypes
type main(void)
{
variable/structure declarations with/without initialisations;
expression/assignment;
function calls;
flow controls;
} statements
return values;
}
function definitions
{
……
}
/*****************************************************************/
Compilation:
………………………………………………………..…………
: Edit C program code and saved as a source file (program.c) :
:…………………………………………………………………:
↓
compiling process:
↓
Pre-processer: include a copy of header files or function prototypes;
provide access to relevant parts of function libraries.
Compiler: check the syntax of the source code, and if there are no errors,
translate the program into machine language - object file
in UNIX: program.o
in MS-DOS/WINDOWS: program.obj
Linker(Loader): combine the program object file with other pre-defined (library) functions
and/or pre-compiled function object files, into an executable file.
in UNIX: execute_name.anything/nothing, or, a.out by default.
in MS-DOS/WINDOWS: execute_name.exe
The compiler may produce warnings and errors. Warnings may not affect compilation and/or execution
of the program, but an error will stop compilation. There are syntax errors and compile-time errors.
Debugging:
If program is compiled without errors does not necessarily mean that the program is bug-free or even
executable. If errors occur during the execution, the program may stop, or hang, or produce
meaningless outputs or even damaging actions. These errors are called run-time errors. Debugging can
be carried out by single-step execution or by setting breakpoints at selected statements.
Data requirements:
Input: miles
Output: kilometers
Formula: 1 mile=1.609 kilometers
Algorithm:
A flow chart or diagram is always drawn against the algorithm. Pseudo-code methods could also be
used at this stage for programs of small or medium size.
Implementation:
/******************************************************************************
* mile2km.c: Converting distance in miles to kilometers *
* H. Yin *
******************************************************************************/
#include<stdio.h>
#define KMS_PER_MILE 1.609
/* symbolic constants: by tradition such identifiers in pre-processors are written in capital letters though not necessary */
Compile to object file ( .o or .obj, e.g. mile2km.obj) or executable file (a.out or .exe, e.g. mile2km.exe):
cc –o mile2km mile2km.c
mile2km
Enter the distance in miles> 10.0
10.000000 miles equal 16.090000 kilemeters.
/*****************************************************************************
* maxmin.c: Read in n numbers, count them and compute the maximum, *
* minimum, sum and average. *
* H. Yin *
*****************************************************************************/
#include<stdio.h>
A running result:
C:\People\Hujun\Teaching\4303-2001\examples>maxmin
How many numbers do you want to enter? 6
/******************************************************************************
* name1.c: Enter a name and print a greeting message *
* H. Yin *
******************************************************************************/
#include<stdio.h>
#include<ctype.h>
#define MAXSTRING 100
int main(void)
{
char c, name[MAXSTRING];
int i, sum=0;
A testing result:
C:\People\Hujun\Teaching\4303-2001/examples>name1
/**************************************************************************
* names2.c: scanf and gets for strings *
* H. Yin *
**************************************************************************/
#include<stdio.h>
#include<ctype.h>
#define MAXSTRING 100
int main(void)
{
char name1[MAXSTRING], name2[MAXSTRING];
return 0;
}
/*************************** End of name2.c ***********************************/
A running result:
C:\People\Hujun\Teaching\4303-2001/examples>names2
What is your name? Alan Smith
Pleased to meet you
Alan Smith
Hello Alan !
1. A Book on C, 3rd edition, Al Kelley and Ira Pohl, The Benjamin Cummings Publishing Company, Inc.,
1995, ISBN 0-8053-1677-9. (£25)
2. The Indispensable Guide to C, Paul Davies, Addison-Wesley, 1995, ISBN 0-201-62438-9. (£25)
3. C How to Program – With Introducing C++ and Java, 3 rd edition, H. M. Deitel and P. J. Deitel, Prentice
Hall, 2000, ISBN-0-13-089572-5.
The first one is highly recommended by professional programmers and is well written, well structured,
compact but also comprehensive. It will be the main textbook for this module. The lecture notes are abstract of
the book with a number of expansions. The second is full of details and explained examples, and is particular
helpful to the beginners. The third book is a newly revised one and covers all aspects of C with fairly through
introduction to C++ and Java.
The C Programming Language, B. W. Kernighan and D. M. Ritchie, (1 st edition: 1978), (regarded as C Bible)
The C++ Programming Language, Bjarne Stroustrup, (1st edition: 1985), (regarded as C++ Bible).
1. Write a program that prompts user to enter a power figure (e.g. 60) of a light bulb in watts and then to
calculate both RMS and peak currents (in Amp) it takes and prints on screen.
2. Use printf( ) and scanf( ) functions to write a program that asks you to input your last name, first name,
course title, reg. No. and university name respectively and then prints them in lines on screen.
3. Print all possible characters on your screen (using printf("%c ", a); a=0 to 255).