{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03-C-NoEvil - C No Evil A practitioner's guide Copyright...

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

View Full Document Right Arrow Icon
Copyright ©: University of Illinois CS 241 Staff 1 C No Evil A practitioner's guide
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
Playing with fire Program arguments Pointer arithmetic Output Stack memory Copyright ©: University of Illinois CS 241 Staff 2
Image of page 2
ARGCount ARGValues int main(argc, char** argv) int main(argc, char* argv[]) argc Argument count The number of arguments that are passed to main in the argument vector argv . the value of argc is always one greater than the number of command-line arguments that the user enters. Copyright ©: University of Illinois CS 241 Staff 3
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
ARGCount ARGValues int main(argc, char** argv) int main(argc, char* argv[]) argv argument vector An array of string pointers passed to a C program's main function argv[0] is always the name of the command argv[argc] is a null pointer Copyright ©: University of Illinois CS 241 Staff 4
Image of page 4
ARGCount ARGValues Copyright ©: University of Illinois CS 241 Staff 5
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
ARGCount ARGValues int main(argc, char** argv) int main(argc, char* argv[]) *(argv + argc) is NULL argv[argc] is NULL Copyright ©: University of Illinois CS 241 Staff 6
Image of page 6
Type questions char**argv; What type is argv ? What type is *argv ? What type is **argv ? Copyright ©: University of Illinois CS 241 Staff 61
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
Followup: Can add integers to pointers Compiler uses the type information long *p ; p [long][long][long] What address is p + 2 ? p + sizeof(long) * 2 Copyright ©: University of Illinois CS 241 Staff 7
Image of page 8
Followup: output C stdio library functions printf("Hello %x %s %d", arguments…) fprintf(STDERR, "%x%s%d",…) Later… system call write(int,void*,size_t) Copyright ©: University of Illinois CS 241 Staff 8
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
printf Format Identifiers %d %i Decimal signed integer. %o Octal integer. %x %X Hex integer. %u Unsigned integer. %c Character. %s String. %f Double. %p Pointer. All of the parameters should be the value to be inserted. EXCEPT %s, this expects a pointer to be passed Copyright ©: University of Illinois CS 241 Staff 9
Image of page 10
printf Basic Data Types #include <stdio.h> // for printf int main(int argc, char *argv[]) { // print "the date is: 01/25/2010", // i.e. 2- or 4-digit with leading zeros // using 32-bit 'long' datatype long day = 25; long month = 1; long year = 2010; printf("the date is: %02ld/%02ld/%04ld\n", month, day, year); // - print 8-digit hex value // - print a pointer value unsigned long ulID = 0x12345678; unsigned long *pID = &ulID; printf("hex value: 0x%02lX at address: %p\n", ulID, pID); 10 Copyright ©: University of Illinois CS 241 Staff
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