Download presentation
Presentation is loading. Please wait.
Published byLouisa Patterson Modified over 8 years ago
1
Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley
2
Week 1 Lecture 2SE1SA52 Outline Why C? Standard C libraries A Program Comments Compiling and Running Variables Input/Output Data type qualifiers
3
Week 1 Lecture 2SE1SA53 Why C? C is a classic language which forms the foundation of many other programming languages C is widely available, under many operating systems As 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
4
Week 1 Lecture 2SE1SA54 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.
5
Week 1 Lecture 2SE1SA55 Other libraries include: ctype.h string.h math.h stdlib.h time.h limits.h float.h Look these up and find out what they contain Hint: before writing something complicated check if there is a library function that does it
6
Week 1 Lecture 2SE1SA56 A Program #include /* 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 */ }
7
Week 1 Lecture 2SE1SA57 Notes on the program Save it as name.c C always need a main( ) function This one returns an int 0 Lines starting # are instructions to the preprocessor Names of library files must be in angular brackets The braces { and } contain statements to be executed In C semicolons ( ; ) are used to terminate statements C is free format – line breaks and white space can go between items C is case sensitive Int is different to int
8
Week 1 Lecture 2SE1SA58 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
9
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 in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler creates object code and stores it on disk. Linker links the object code with the libraries Loader Primary Memory Compiler Editor Preprocessor Linker Primary Memory........................ Dis k CPU Dis k Compiling and Running
10
Week 1 Lecture 2SE1SA510 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 number CANNOT be C keywords For example: Num1, num1, _answer
11
Week 1 Lecture 2SE1SA511 Example of variable use int num1, num2; int sum; num1 = 2; num2 = 5; sum = num1 + num2; num1num2sum
12
Week 1 Lecture 2SE1SA512 Basic types Data typeDescriptionExample charA single byte, holds one character 'A' intAn integer whole number 12 floatA floating point number using 32 bits 12.34 doubleMore accurate, using 64 bits 9.123456789 Look up bits and bytes
13
Week 1 Lecture 2SE1SA513 Data types and printing them #include /* 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; }
14
Week 1 Lecture 2SE1SA514 Output num is 12 letter is A fNum is 12.340000
15
Week 1 Lecture 2SE1SA515 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.
16
Week 1 Lecture 2SE1SA516 Output please enter a character: A please enter a float: 12.34 num is 12 letter is A fNum is 12.340000
17
Week 1 Lecture 2SE1SA517 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);
18
Week 1 Lecture 2SE1SA518 Output num using %d is 12 num using %1d is 12 num using %2d is 12 num using %3d is 12 num using %4d is 12 num using %05d is 00012 num using %06d is 000012 -num using %4d is -12
19
Week 1 Lecture 2SE1SA519 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);
20
Week 1 Lecture 2SE1SA520 Output fNum using %8.0f is 12 end fNum using %-8.0f is 12 end fNum using %8.2f is 12.35 end fNum using %-8.4f is 12.3456 end Note the rounding in the third line.
21
Week 1 Lecture 2SE1SA521 Data type qualifiers #include 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; }
22
Week 1 Lecture 2SE1SA522 Output Max int: 2147483647 Max long int: 2147483647 Max shortint: 32767 Min int: -2147483648 Min long int: -2147483648 Min short int: -32768
23
Week 1 Lecture 2SE1SA523 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 2 sizeof tall 4 sizeof short int 2 sizeof float 4 sizeof double 8
24
Week 1 Lecture 2SE1SA524 Summary Standard C libraries Programs and comments Compiling and Running Variables Input/Output Data type qualifiers Next week –will include External, static, register variables Casting Arrays Constants and enumeration Preprocessor directives
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.