Introducing C
description
Transcript of Introducing C
Week 1 Lecture 2 SE1SA5 1
Introducing C
SE1SA5
Sue Walmsley
Week 1 Lecture 2 SE1SA5 2
Outline
Why C?Standard C librariesA ProgramCommentsCompiling and RunningVariablesInput/OutputData type qualifiers
Week 1 Lecture 2 SE1SA5 3
Why C?
C is a classic language which forms the foundation of many other programming languages
C is widely available, under many operating systemsAs you study C (and then C++) you will learn the theory
of programming and this will help you appreciate other languages You will study C++ later this year
Those following degree courses in the School of Systems Engineering (SSE) will go on to use both C and C++, and many of you will also utilise other languages
Week 1 Lecture 2 SE1SA5 4
Standard C libraries
ANSI C defines a number of standard libraries
The libraries have “header files” which normally have the extension “.h”
For example, most C programs use:stdio.h
Contains input and output functions, types and macro definitions.
Week 1 Lecture 2 SE1SA5 5
Other libraries include:
ctype.hstring.hmath.hstdlib.htime.hlimits.hfloat.h
Look these upand find out what they contain
Hint: before writing something complicated check if there is a library function that does it
Week 1 Lecture 2 SE1SA5 6
A Program
#include <stdio.h>
/* this program produces three lines of output */int main ( ){
printf("This module is called: \n");printf("\t Programming \n");printf("This program is written in: ");printf("C\n");return 0; /* returns successfully */
}
Week 1 Lecture 2 SE1SA5 7
Notes on the program
Save it as name.cC always need a main( ) function
This one returns an int 0Lines starting # are instructions to the preprocessorNames of library files must be in angular brackets < and
>The braces { and } contain statements to be executed In C semicolons ( ; ) are used to terminate statementsC is free format – line breaks and white space can go
between itemsC is case sensitive Int is different to int
Week 1 Lecture 2 SE1SA5 8
Comments and coding standards
In C comments are delimited by /* and */
Use comments to make your program more easily understood by other people and yourself
Commercial firms often have standards for coding and these will include how to comment
Reproduced from the PowerPoints for C How to Program, 4/e by Deitel and Deitel 2004. Reproduced by permission of Pearson Education, Inc. (selected and adapted by SMW)
• Phases of C Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Program is created inthe editor and storedon disk.
Preprocessor programprocesses the code.
Loader puts program in memory.
CPU takes eachinstruction and executes it, possibly storing new data values as the program executes.
Compiler creates object code and storesit on disk.
Linker links the objectcode with the libraries
Loader
Primary Memory
Compiler
Editor
Preprocessor
Linker
Primary Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
CPU
Disk
Disk
Compiling and Running
Week 1 Lecture 2 SE1SA5 10
Variables
A variable is like a container in a C program in which a data value can be stored in the computer memory
Variable names:Consist of letters, numbers and underscores ( _ )CANNOT start with a numberCANNOT be C keywords
For example:Num1, num1, _answer
Week 1 Lecture 2 SE1SA5 11
Example of variable use
int num1, num2;int sum;num1 = 2;num2 = 5;sum = num1 + num2;
num1 num2 sum
Week 1 Lecture 2 SE1SA5 12
Basic typesData type Description Examplechar A single byte, holds one
character'A'
int An integer whole number
12
float A floating point number using 32 bits
12.34
double More accurate, using 64 bits
9.123456789
Look up bits and bytes
Week 1 Lecture 2 SE1SA5 13
Data types and printing them
#include <stdio.h>/* illustrates printf */int main ( ){
int num =12;char letter;float fNum;letter = 'A';fNum = 12.34;printf ("num is %d \n", num);printf ("letter is %c \n", letter);printf ("fNum is %f \n", fNum);return 0;
}
Week 1 Lecture 2 SE1SA5 14
Output
num is 12letter is AfNum is 12.340000
Week 1 Lecture 2 SE1SA5 15
Using scanf to read values into variables
int num =12;char letter;float fNum;printf ("please enter a character: ");scanf ("%c", &letter);printf ("please enter a float: ");scanf ("%f", &fNum);printf ("num is %d \n", num);printf ("letter is %c \n", letter);printf ("fNum is %f \n", fNum);return 0;
Note with scanf you must use the ampersand (&) with basic data types (such as int).In this case & is the “addressof” operator and tells the program to store the data in the variable name that follows.
Week 1 Lecture 2 SE1SA5 16
Output
please enter a character: Aplease enter a float: 12.34num is 12letter is AfNum is 12.340000
Week 1 Lecture 2 SE1SA5 17
Formatting Output (int)
int num =12;printf ("num using %%d is %d \n", num);printf ("num using %%1d is %1d \n", num);printf ("num using %%2d is %2d \n", num);printf ("num using %%3d is %3d \n", num);printf ("num using %%4d is %4d \n", num);printf ("num using %%05d is %05d \n", num);printf ("num using %%06d is %06d \n", num);printf ("-num using %%4d is %4d \n", -num);
Week 1 Lecture 2 SE1SA5 18
Output
num using %d is 12num using %1d is 12num using %2d is 12num using %3d is 12num using %4d is 12num using %05d is 00012num using %06d is 000012-num using %4d is -12
Week 1 Lecture 2 SE1SA5 19
Formatting Output (floats)
float fNum = 12.3456;printf ("fNum using %%8.0f is %8.0f end\n", fNum);printf ("fNum using %%-8.0f is %-8.0f end\n", fNum);printf ("fNum using %%8.2f is %8.2f end\n", fNum);printf ("fNum using %%-8.4f is %-8.4f end\n", fNum);
Week 1 Lecture 2 SE1SA5 20
Output
fNum using %8.0f is 12 endfNum using %-8.0f is 12 endfNum using %8.2f is 12.35 endfNum using %-8.4f is 12.3456 end
Note the rounding in the third line.
Week 1 Lecture 2 SE1SA5 21
Data type qualifiers
#include <stdio.h>#include <limits.h>int main ( ){
short int small; long int tall;printf ("Max int:\t %d\n", INT_MAX);printf ("Max long int:\t %d\n", LONG_MAX);printf ("Max shortint:\t %d\n", SHRT_MAX);
printf ("Min int:\t %d\n", INT_MIN); printf ("Min long int:\t %d\n", LONG_MIN); printf ("Min short int:\t %d\n", SHRT_MIN);return 0;
}
Week 1 Lecture 2 SE1SA5 22
Output
Max int: 2147483647Max long int: 2147483647Max shortint: 32767Min int: -2147483648Min long int: -2147483648Min short int: -32768
Week 1 Lecture 2 SE1SA5 23
sizeof operator
printf ("sizeof small %d\n", sizeof (small));printf ("sizeof tall %d\n", sizeof (tall));printf("sizeof short int %d\n", sizeof (short int));printf("sizeof float %d\n", sizeof (float));printf("sizeof double %d\n", sizeof (double)); --sizeof small 2sizeof tall 4sizeof short int 2sizeof float 4sizeof double 8
Week 1 Lecture 2 SE1SA5 24
Summary
Standard C librariesPrograms and commentsCompiling and RunningVariables Input/OutputData type qualifiersNext week –will include
External, static, register variables Casting Arrays Constants and enumeration Preprocessor directives