Programming in C
Lecture 1: INTRODUCING C (1)
CHRIS STAFF
Dept. of Computer Science and Artificial Intelligence
University of Malta
Lecture Outline
Recommended reading
Aims and Objectives
What is C?
Preamble
The first C Program
Writing more complex programs
Group Exercise
Recommended Reading (aka ASSIGNED TEXTBOOK)
- Kernighan, B.W. & Ritchie, D.M. The C Programming
Language. Prentice Hall
- Deitel, HM, and Deitel, PJ. C: How to Program. Prentice Hall. (Includes sections on C++ and Java, and a CD-ROM with Borland C++ and JBuilder)
Aims and Objectives
Of the course
- Assessment is through a single assignment only
Of the lecture
- Introduction to C
- The Structure of a C program
- Some basic C syntax
What is C?
- C is a general purpose programming language
- C contains some features that are normally associated with low-level
programming languages
- C is basis for Java, C++
- C# shares properties with C, C++, Java, and Delphi
- C deals with the same objects that are manipulated by computers: single
characters, numbers and memory addresses
- Any other type of object is created, by the programmer, by combining those
objects (e.g., character strings, arrays, records, fields, etc.)
- C does not have any built-in functions to perform complex input and output!
Again, the programmer must explicitly write these functions.
- Luckily, ANSI C contains a set of prepared functions to perform some routine
functions, BUT THESE ARE NOT AN OFFICIAL PART OF THE C PROGRAMMING LANGUAGE!
- To start with, we will ignore the fact that some of these functions exist, so
that we can learn how C operates...
Preamble
- THIS IS NOT A CONVERSION COURSE!
- I will assume that you will be using the C compiler on one of the UNIX host
computers (e.g., zeus, babe). You will write your programs using a text
editor (e.g., vi), and you will use the gcc C
compiler.
- You must save your program as prog.c, otherwise the
gcc
compiler won't work
- To compile your program, type
gcc prog.c
- Your compiled program will be stored in a.out
- a.out is a binary or executable file
- To run your program, type
a.out
The first C Program
#include <stdio.h>
main()
{
printf("hello, world\n");
}
- The program simply prints the words "hello, world" on the screen
(standard
output)
- C commands are all lower case
The Program line by line
#include <stdio.h>
- Tells the C compiler to include the library header file
stdio.h in the program. This file contains several functions to do with
input and output. The function printf() is in this library routine. If
stdio.h was not included in the program, C would not be able to find the
printf() function
main()
{
}
printf("hello, world\n");
Writing more complex programs
- In order to write a more complex program, some more of the C syntax must be
introduced
Variables
- A variable is a placeholder for data.
- Variables must be declared before they can be used
- Declaring a variable serves two purposes: C knows what data type the variable
is, and C reserves space for the data
Some Variable types
char - a single character (one byte)
int - an integer
short - a short integer
long - a long integer
float - a floating point number
double - a double-precision float
- The size of the numeric data types is dependent on the machine
architecture
Assignment
- To set a variable to a value
variable = expression
a = b + 5
Conditionals
expression operator expression
a <= b
Looping constructs (simple)
while ( conditional ) {
statements ;
}
- while the conditional is true (evaluates to anything other than 0) perform statements
- If there is only one statement, the braces can be omitted, but it is better
to leave them just in case later you want to add more statements and you forget
to put the braces in (defensive programming!)
Comments
/* free text */
- comments cannot be nested, but can spread across several lines. Comments are
ignored by the compiler
/* some
free
text
*/
is ok, but
/* some free text
/* some more free text */
*/
is illegal, and will generate a syntax error (/* cannot be used inside a
comment!)
// ignore text to end of line
- Use comments liberally - it makes programs readable!
Formatted printing
printf("format"[, expression [,
expression]]);
- format can contain free text, escape characters, and arguments
- Some escape characters are
\n - newline
\t - tab
\b - backspace
\\ - slash
\" - double quote
- Arguments are placeholders for values inside the format, e.g., if
you want to print the contents of a variable.
- Arguments are composed of % followed by an optional print-format, followed by
a data type
Some examples
%d - print as decimal integer
%6d - print as decimal integer, at least 6 characters wide
%f - print as floating point
%6f - print as floating point, at least 6 characters wide
%.2f - print as floating point, two character after decimal point
%6.2f - print as floating point, at least 6 wide
%ns - print as string, at least n wide
%c - print as single character
Exercises
Next Lecture...
Introducing C (2)