DOC PREVIEW
Princeton COS 217 - Simple C Programs

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11Simple C Programs2Goals for this Lecture• Help you learn about:• Simple C programs• Program structure• Defining symbolic constants• Detecting and reporting failure• Functionality of the gcc command• Preprocessor, compiler, assembler, linker• Memory layout of a Linux process• Text section, rodata section, stack section23“Circle” Program• File circle.c:#include <stdio.h>int main(void)/* Read a circle's radius from stdin, and compute and write itsdiameter and circumference to stdout. Return 0. */{int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}4Building and Running• To build (preprocess, compile, assemble, and link):• To run:$ gcc217 circle.c –o circle$ circleEnter the circle's radius:5A circle with radius 5 has diameter 10and circumference 31.415900.Typed byuser35Steps in the Build Process• To build one step at a time:• Why build one step at a time?• Helpful for learning how to interpret error messages• Permits partial builds (described later in course)$ gcc217 –E circle.c > circle.i$ gcc217 –S circle.i$ gcc217 –c circle.s$ gcc217 circle.o –o circlePreprocess:circle.c → circle.iCompile:circle.i → circle.sAssemble:circle.s → circle.oLink:circle.o → circle6• File circle.c:The Preprocessor’s View#include <stdio.h>int main(void)/* Read a circle's radius from stdin, and compute and write itsdiameter and circumference to stdout. Return 0. */{int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}CommentPreprocessor removesPreprocessor directivePreprocessor replaces with contentsof file /usr/include/stdio.h47• File circle.i:Results of Preprocessingint printf(char*, …);int scanf(char*, …);…int main(void){int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}Declarations of printf(), scanf(),and other functions; compiler willhave enough information to check subsequent function callsNote: Definitions of printf() and scanf()are not present8• File circle.i:The Compiler’s Viewint printf(char*, …);int scanf(char*, …);…int main(void){int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}FunctiondefinitionCompoundstatementalias blockFunction declarationsCompiler notes return typesand parameter types so it cancheck your function callsReturn type of main()should be int59• File circle.i:The Compiler’s View (cont.)int printf(char*, …);int scanf(char*, …);…int main(void){int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}Declaration statementsMust appear before any otherkind of statement in block;variables must be declaredbefore useFunction callstatementsString constants& (“address of”) operatorExplained later in course,with pointers10• File circle.i:The Compiler’s View (cont.)int printf(char*, …);int scanf(char*, …);…int main(void){int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}ExpressionstatementsCast operatorUnnecessary here, but good style to avoidmixed-type expressionsConstant of type doubleConstant of type int611• File circle.i:The Compiler’s View (cont.)int printf(char*, …);int scanf(char*, …);…int main(void){int radius;int diam;double circum;printf("Enter the circle's radius:\n");scanf("%d", &radius);diam = 2 * radius;circum = 3.14159 * (double)diam;printf("A circle with radius %d has diameter %d\n",radius, diam);printf("and circumference %f.\n", circum);return 0;}Function call statementsprintf() can be called with1 or more actual parametersReturn statementConvention: 0 returned from main() means success; non-0means failure12• File circle.s:• Still missing definitions of printf() and scanf()Results of CompilingAssembly language.section .rodata.LC0:.string "Enter the circle's radius:\n".LC1:.string "%d"….text.globl main.type main, @functionmain:pushl %ebpmovl %esp, %ebp…pushl $.LC0call printfaddl $16, %espsubl $8, %espleal -4(%ebp), %eaxpushl %eaxpushl $.LC1call scanf…713• File circle.s:• Assembler translates assembly language into machine language• Details provided in 2nd half of courseThe Assembler’s View.section .rodata.LC0:.string "Enter the circle's radius:\n".LC1:.string "%d"….text.globl main.type main, @functionmain:pushl %ebpmovl %esp, %ebp…pushl $.LC0call printfaddl $16, %espsubl $8, %espleal -4(%ebp), %eaxpushl %eaxpushl $.LC1call scanf…Assembly language14• File circle.o:• Object file• Still missing definitions of printf() and scanf()Results of AssemblingMachine languageListing omittedNot human-readable815• File circle.o:• The linker:• Observes that• Code in circle.o calls printf() and scanf()• Code in circle.o does not define printf() or scanf()• Fetches machine language definitions of printf() and scanf() from standard C library (/usr/lib/libc.a on hats)• Merges those definitions with circle.o to create…The Linker’s ViewMachine languageListing omittedNot human-readable16• File circle:• Complete executable binary fileResults of LinkingListing omittedNot human-readableMachine language917Run-Time View• At run-time, memory devoted to program is divided into sections:TEXTRODATADATABSSHEAPSTACK• TEXT (read-only)• Stores executable machine language instructions•RODATA(read-only)• Stores read-only data, esp. string constants•STACK(read/write)• Stores


View Full Document

Princeton COS 217 - Simple C Programs

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Simple C Programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Simple C Programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Simple C Programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?