csc374And407_lect1 - CSC 374/407 Computer Systems II...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 374/407: Computer Systems II Lecture 1 Joseph Phillips De Paul University 2016 September 5 Copyright © 2011-2016 Joseph Phillips All rights reserved
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Reading Bryant & O'Hallaron “ Computer Systems, 3 rd Ed. Chapter 5.1-5.6, 5.10-5.15 Hoover “ System Programming Chapter 1
Image of page 2
Topics Review: Pointers and objects About machine-independent optimization Using registers instead of RAM Code motion Common expression computation Reduction in strength Limitations Profiling So, how do I actually program knowing this?
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
But first . . let's review some C:
Image of page 4
A little C (Output) Output in C with printf() (“print-formatted”) printf("template",expr 1 ,...expr n ) Constant formatting: printf("\tI just print \"hello\".\n"); What do these mean?  \t \" \n Substitution formatting: int i=1;printf("%d %d %d Go!\n",3,1+1,i); %d    = decimal integer %x %X = hexadecimal integer %c    = single char %s    = C-string ( i.e . pointer to char: char* ) %f %g = double or floating point %p    = An address ( e.g . a pointer's value)
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
But printf()’s man page says: int printf(const char *format, ...); So that means I should write: #include <stdlib.h> #include <stdio.h> int main () {    int  printf( const char*  "Hello world\n");   return(EXIT_SUCCESS); } Right?
Image of page 6
A little more C (Input): Almost always should get any input as string Convert string to integer or float #include <stdio.h> #define LINE_LEN 10 . . . char line[LINE_LEN]; printf("Please enter a number: "); fgets(line,LINE_LEN,stdin); int   i = strtol(line,NULL,10); float f = strtod(line,NULL); printf("i = %d, f = %g\n",i,f);
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
About strtol() and strtod() strtol (const char* text,  char**    endPtr,  int       base) ( STR ing TO L ong) strtod (const char* text,  char**    endPtr) ( STR ing TO D ouble) text : pointer to text to convert endPtr : address of pointer to receive just beyond last char converted (or NULL if you don't care) base : Base ( 2 - 36 ) to use. Or 0 , in which case it uses the same rules as C int constants.
Image of page 8
strtol() example #include<stdlib.h> #include<stdio.h> #define TEXT_LEN 64 int main () { char text[TEXT_LEN];   char* cPtr;   while  (1)   {     printf("Please enter a number: ");     fgets(text,TEXT_LEN,stdin);     int i = strtol(text,&cPtr, 0 );  //We're using base 0     if  (cPtr == text)       printf("Phooey!\n");     else       printf("dec:\t%d\n"              "hex:\t%X\n",i,i);   }   return(EXIT_SUCCESS); }
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
strtol() example (cont'd) ./strtol  Please enter a number:  20    // Ordinary decimal dec: 20 hex: 14 Please enter a number:  020   // Leading 0 => octal dec: 16 hex: 10 Please enter a number:  0x20  // Leading 0x => hex dec: 32 hex: 20 Please enter a number:  twenty // No digit => ERROR! Phooey! Please enter a number:
Image of page 10
You knew this was coming: addresses and pointers! (1) What is an address?
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern