Upgrade to Pro — share decks privately, control downloads, hide ads and more …

05-InputOutput.pdf

Avatar for William Albritton William Albritton
September 04, 2015
230

 05-InputOutput.pdf

Avatar for William Albritton

William Albritton

September 04, 2015
Tweet

Transcript

  1. Memory Allocation  Overview of C  Hello World Program

     Program Execution in Unix  C Programming Basics – variables, input, and output
  2. Overview of C  Preceded by BCPL and B 

    Developed by Dennis Ritchie  Bell Labs in 1972  Portable (machine-independent)  Used in UNIX, networks
  3. Hello World Program  Basic C program which outputs: Aloha!

     See example program file in Laulima: aloha.c /*Will ouput "Aloha!"*/ #include <stdio.h> int main(void){ printf("Aloha!\n"); return 0; }
  4. Comments  Comments explain code to humans  Start with

    a forward slash and asterisk (/*)  End with an asterisk and forward slash (*/)  Ignored by the C compiler /* One line of comments */ /* Two lines of comments */
  5. Preprocessor  Lines beginning with pound sign (#) are processed

    by preprocessor before program is compiled  This line of code tells the preprocessor to include the contents of the stdio.h file in the program #include <stdio.h>
  6. Header Files  stdio.h - standard input/output header file 

    Contains information and declarations about functions  In this case, the printf() library function #include <stdio.h>
  7. Function Header  The function header is the first line

    of a function definition, where the code of the function is written  Has a return type: int  Has a name: main (special function to start program)  Has parameters: void (no parameters) int main(void)
  8. Function Body  The function body contain the code of

    a function  Starts/ends with left/right curly brackets: { }  Has some code: printf("Aloha!\n");  Returns a value: return 0; (matches the return type) { printf("Aloha!\n"); return 0; }
  9. 6 Steps to Execute a C Program 1. Edit –

    write program code and store file on disk  Edit program with Emacs (or Pico)  Command is: emacs programName.c  Creates a C program text file, which ends in ".c"  Only the percent sign (%) is shown for the UNIX prompt % emacs program.c
  10. 6 Steps to Execute a C Program 2. Preprocess –

    add other files and text replacement 3. Compile – creates object (machine) code, which is stored in a file ending in ".o"  Command is: gcc –c programName.c % gcc –c program.c
  11. 6 Steps to Execute a C Program 4. Link –

    links code and libraries to make an executable file, which is used to run the program  Command is: gcc programName.o -o executableFileName % gcc program.o -o program
  12. 6 Steps to Execute a C Program 5. Load –

    takes from disk and put in memory 6. Execute – CPU executes each instruction  Command is: ./executableFileName  The "dot slash" (./) is for the current directory (file) % ./program
  13. Makefile  Instead of typing these commands, we can use

    a file to output the compile and link commands for us  This is particularly useful if we have multiple C files  The command "make" finds the text file "makefile" and executes the commands listed in it!
  14. Making a Makefile  First, we will use an editor

    to make (create) the makefile text file  Type the command: emacs makefile  Note that there is no extension on the makefile file % emacs makefile
  15. Makefile Rules  Makefiles have sets of three lines: 1.

    Target: list of dependencies 2. <tab>Commands needed to build target 3. Blank line (newline character)
  16. Makefile Example  Note: the last line in this file

    is a newline! program: program.o gcc program.o -o program program.o: program.c gcc -c program.c
  17. Makefile Formatting  Be aware of these picky formatting issues:

     Must use a tab at the beginning of each command line  Must put a blank line (newline character – '\n') after each group of statements, including the last line  If you create it on your PC and ftp (file transfer protocol) it to UNIX, the newline characters will cause problems in UNIX, so need to create with a UNIX editor
  18. File Transfer Issue  Potential error message after creating files

    on your PC and ftp (file transfer protocol) to UNIX:  Warning: invalid white space character in directive  UNIX newline is one character: 0x0A  PC newline is two characters: 0x0D0A  0x0D = carriage return  0x0A = line feed
  19. File Transfer Solution  Make sure you ftp in ASCII

    mode, not in binary mode  In SSH click: Operation < File Transfer Mode < ASCII
  20. Makefile in Action  On the commandline, type "make" 

    Should get the following output  gcc -c program.c  gcc program.o -o program  Type “./program” to run executable  The dot forward slash ("./“) is the current directory
  21. Makefile in Action  You should get output like this:

    % make gcc -c program.c gcc program.o -o program % ./program Aloha!
  22. Variables in C  Variables are used to store data

    in a program  By convention, variables are usually declared at beginning of a function  Before any executable statements  Should initialize variables when declare them  Uninitialized variables have unpredictable values
  23. Data Types in C  These are the basic data

    types in the C language  char = 1 byte (-128 to 127)  short = 2 bytes (-32,768 to 32,767)  int = 4 bytes (-2,147,483,648 to +2,147,483,647)  float = 4 bytes (± ~10-44 to ~1038 )  double = 8 bytes (± ~10-323 to ~10308 )
  24. Variable Initialization  Syntax for initializing a variable  variableType

    variableName = initialValue; char character1 = 'c'; double number2 = 0.0;
  25. Unsigned Integers  Represents positive integers only  Not necessary

    to indicate a sign, so all 8 or 16 bits can be used for the magnitude:  unsigned char = 1 byte = 8 bits = 28 = 256  So the range in values is 0 to 255
  26. Unsigned Integers  unsigned short = 2 bytes = 16

    bits = 216 = 65,536  So the range in values is 0 to 65,535  unsigned int = 4 bytes = 32 bits = 232 = 4,294,967,296  So the range in values is 0 to 4,294,967,295
  27. Restrictions on Variable Names in C  1st character must

    be a letter  Underscore ("_") counts as a letter  Case sensitive  APPLE and apple are different variables  Must be less than 31 characters
  28. Restrictions on Variable Names in C  Cannot use reserved

    keywords as variable names auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
  29. Inputting Characters in C  Use the getchar() function to

    input characters  Reads one character from the input stream, which can be from the keyboard, or from a file  Returns the character  See example program at: inputchar.c char charcter1 = ‘z’; character1 = getchar();
  30. Formatted Output  The function printf() is used for formatted

    output  Function prototype:  int printf(char *format, arg1, arg2, …);  Translates variables (arg1, arg2, …) to character output in a specified format  Returns the number of characters printed
  31. Formatted Output  Function call:  printf("arg1=%c, arg2=%f…\n", arg1, arg2,

    …)  Formatted output is between the double parenthesis  Usually ends with a newline ('\n')  Each argument has a corresponding “conversion specifier”, which is preceded by a percent sign (%)
  32. Example printf() Function Call  This printf() function call: 

    Outputs the following: char letter = 'A'; printf(“letter = '%c', \n", letter); letter = 'A'
  33. Inputting Numbers in C  To input numbers in C

    securely (without the program crashing or risking a buffer overflow), we need knowledge about arrays, pointers, and strings, which will be covered later in the semester  For now, we have provided a makefile and C program files to use with programs inputting doubles
  34. Inputting Numbers in C  Download these files to your

    UH UNIX account:  inputdouble.c – shows how to use function getdouble()  getdouble.c – function definition getdouble()  getdouble.h – function prototype getdouble()  makefile-double - makefile code to combine 3 files double number = 0.0; number = getdouble();
  35. Just as a Reference  You can compile and link

    a file(s) with one command  Which creates the default executable file “a.out" % gcc program.c % gcc program1.c program2.c program3.c % ./a.out
  36. Memory Management  Overview of C  Hello World program

     Unix environment  C Programming Basics – variables, input, and output