1 / 24

Introducing C

Introducing C. SE1SA5 Sue Walmsley. Outline. Why C? Standard C libraries A Program Comments Compiling and Running Variables Input/Output Data type qualifiers. Why C?. C is a classic language which forms the foundation of many other programming languages

zed
Download Presentation

Introducing C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introducing C SE1SA5 Sue Walmsley SE1SA5

  2. Outline • Why C? • Standard C libraries • A Program • Comments • Compiling and Running • Variables • Input/Output • Data type qualifiers 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 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 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. SE1SA5

  5. 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 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 */ } SE1SA5

  7. 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 < and > • 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 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 SE1SA5

  9. Preprocessor program processes the code. Compiler creates object code and storesit on disk. Compiler Linker links the object code with the libraries Primary Memory Loader Loader puts program in memory. Primary Memory CPU takes each instruction and executes it, possibly storing new data values as the program executes. CPU Preprocessor Linker Editor Disk Disk Disk Disk Disk . . . . . . . . . . . . Compiling and Running Program is created in the editor and stored on disk. • Phases of C Programs: • Edit • Preprocess • Compile • Link • Load • Execute

  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 number • CANNOT be C keywords • For example: • Num1, num1, _answer SE1SA5

  11. Example of variable use num1 num2 sum int num1, num2; int sum; num1 = 2; num2 = 5; sum = num1 + num2; SE1SA5

  12. Basic types Look up bits and bytes 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; } SE1SA5

  14. Output num is 12 letter is A fNum is 12.340000 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. SE1SA5

  16. Output please enter a character: A please enter a float: 12.34 num is 12 letter is A fNum is 12.340000 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); SE1SA5

  18. 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 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); SE1SA5

  20. 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. 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; } SE1SA5

  22. Output Max int: 2147483647 Max long int: 2147483647 Max shortint: 32767 Min int: -2147483648 Min long int: -2147483648 Min short int: -32768 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 2 sizeof tall 4 sizeof short int 2 sizeof float 4 sizeof double 8 SE1SA5

  24. 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 SE1SA5

More Related