1 Simple C Programs. 2 Goals for this Lecture Help you learn about: Simple C programs Program structure Defining symbolic constants Detecting and reporting.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Chapter 11 Introduction to Programming in C
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Introduction to C Programming
Introduction to C Programming CE Lecture 1 Introduction to C.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS 201 Functions Debzani Deb.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Khalid Rasheed Shaikh Computer Programming Theory 1.
 Integers and Characters  Character Strings  Input and Output.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Getting Started with C.
Chapter 2 - Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Chapter 2 - Introduction to C Programming
Memory Allocation CS 217.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 11 Programming in C
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Presentation transcript:

1 Simple C Programs

2 Goals 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 section

3 “Circle” Program File circle.c: #include int main(void) /* Read a circle's radius from stdin, and compute and write its diameter 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; }

4 Building and Running To build (preprocess, compile, assemble, and link): To run: $ gcc217 circle.c –o circle $ circle Enter the circle's radius: 5 A circle with radius 5 has diameter 10 and circumference Typed by user

5 Steps 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 circle Preprocess: circle.c → circle.i Compile: circle.i → circle.s Assemble: circle.s → circle.o Link: circle.o → circle

6 File circle.c: The Preprocessor’s View #include int main(void) /* Read a circle's radius from stdin, and compute and write its diameter 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; } Comment Preprocessor removes Preprocessor directive Preprocessor replaces with contents of file /usr/include/stdio.h

7 File circle.i: Results of Preprocessing 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 = * (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 will have enough information to check subsequent function calls Note: Definitions of printf() and scanf() are not present

8 File circle.i: The Compiler’s View 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; } Function definition Compound statement alias block Function declarations Compiler notes return types and parameter types so it can check your function calls Return type of main() should be int

9 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; } Declaration statements Must appear before any other kind of statement in block; variables must be declared before use Function call statements String constants & (“address of”) operator Explained later in course, with pointers

10 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; } Expression statements Cast operator Unnecessary here, but good style to avoid mixed-type expressions Constant of type double Constant of type int

11 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 = * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; } Function call statements printf() can be called with 1 or more actual parameters Return statement Convention: 0 returned from main() means success; non-0 means failure

12 File circle.s: Still missing definitions of printf() and scanf() Results of Compiling Assembly language.section.rodata.LC0:.string "Enter the circle's radius:\n".LC1:.string "%d" ….text.globl main.type main: pushl %ebp movl %esp, %ebp … pushl $.LC0 call printf addl $16, %esp subl $8, %esp leal -4(%ebp), %eax pushl %eax pushl $.LC1 call scanf …

13 File circle.s: Assembler translates assembly language into machine language Details provided in 2nd half of course The Assembler’s View.section.rodata.LC0:.string "Enter the circle's radius:\n".LC1:.string "%d" ….text.globl main.type main: pushl %ebp movl %esp, %ebp … pushl $.LC0 call printf addl $16, %esp subl $8, %esp leal -4(%ebp), %eax pushl %eax pushl $.LC1 call scanf … Assembly language

14 File circle.o: Object file Still missing definitions of printf() and scanf() Results of Assembling Machine language Listing omitted Not human-readable

15 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 View Machine language Listing omitted Not human-readable

16 File circle: Complete executable binary file Results of Linking Listing omitted Not human-readable Machine language

17 Run-Time View At run-time, memory devoted to program is divided into sections: TEXT RODATA DATA BSS HEAP STACK TEXT (read-only) Stores executable machine language instructions RODATA (read-only) Stores read-only data, esp. string constants STACK (read/write) Stores values of local variables Other sections described later in course

18 Run-Time View: Startup At program startup: TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 RODATA contains every string constant used in program; each is an array of characters, terminated with the null character (‘\0’) STACK is empty main printf scanf TEXT contains machine language code defining main(), printf(), scanf(), etc.

19 Run-Time View: Declarations TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 int radius; int diam; double circum; radius diam circum Computer pushes memory onto STACK for each local variable: 4 bytes for int, 8 bytes for double main printf scanf

20 Run-Time View: Writing a String TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 printf("Enter the circle's radius:\n"); radius diam circum Computer passes address containing ‘E’ to printf(); printf() prints characters until it encounters ‘\0’ main printf scanf

21 Run-Time View: Reading an int TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 scanf("%d", &radius); 5 radius diam circum Computer passes address containing ‘%’ to scanf(); scanf() waits for user input; user types 5; scanf() reads character(s), converts to decimal (d) constant, assigns to radius main printf scanf

22 Run-Time View: Computing Results TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 diam = 2 * radius; circum = * (double)diam; radius diam circum Computer uses radius to compute diam and circum main printf scanf

23 Run-Time View: Writing an int TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 printf("A circle with radius %d has diameter %d\n", radius, diam); radius diam circum Computer passes address of ‘A’, value of radius, and value of diam to printf(). printf() prints until ‘\0’, replacing 1 st %d with character comprising 5, 2 nd %d with characters comprising main printf scanf

24 Run-Time View: Writing a double TEXTSTACKRODATA Enter the circle’s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 printf("and circumference %f.\n", circum); radius diam circum Computer passes address of ‘a’ and value of circum to printf(). printf() prints until ‘\0’, replacing %f with characters comprising main printf scanf

25 Run-Time View: Exiting return 0; Computer reclaims memory used by program; sections cease to exist

26 Toward Version 2 Problem (stylistic flaw): is a “magic number” Should give it a symbolic name to Increase code clarity Thereby increase code maintainability Solution: (In Java: final fields, final variables) In C: three approaches…

27 Symbolic Constants: #define Approach 1: #define void f(void) { #define START_STATE 0 #define POSSIBLE_COMMENT_STATE 1 #define COMMENT_STATE 2... int state;... state = START_STATE;... state = COMMENT_STATE;... } Preprocessor replaces with 0 Preprocessor replaces with 2 Preprocessor directive: replace START_STATE with 0

28 Symbolic Constants: #define Approach 1 strengths Preprocessor does substitutions only for tokens Preprocessor does not do substitutions within string constants Simple textual replacement; works for any type of data int mySTART_STATE; /* No replacement */ printf("What is the START_STATE?\n"); /* No replacement */ #define PI

29 Approach 1 weaknesses Symbolic Constants: #define void f(void) { #define MAX 1000 … } void g(void) { { int MAX = 2000; … } void f(void) { #define MAX 1000 … int MAX = 2000; } Preprocessor does not respect scope Preprocessor does not respect context Preprocessor replaces with 1000 !!! Conventions: Use all uppercase for constants -- and only for constants Place #defines at beginning of file, not within function definitions

30 Symbolic Constants: const Approach 2: “constant variables” (oxymoron!!!) void f(void) { const int START_STATE = 0; const int POSSIBLE_COMMENT_STATE = 1; const int COMMENT_STATE = 2;... int state;... state = START_STATE;... state = COMMENT_STATE;... } Compiler does not allow value of START_STATE to change

31 Symbolic Constants: const Approach 2 strengths Works for any type of data Handled by compiler, not preprocessor; compiler respects context and scope Approach 2 weaknesses Does not work for array lengths (unlike C++) const double PI = ; const int ARRAY_LENGTH = 10;... int a[ARRAY_LENGTH]; /* Compiletime error */

32 Symbolic Constants: enum Approach 3: Enumerations void f(void) { enum State {START_STATE, POSSIBLE_COMMENT_STATE, COMMENT_STATE,...}; enum State state;... state = START_STATE;... state = COMMENT_STATE;... } Defines a new type named “enum State” The constants of type “enum State” are START_STATE, … Defines a variable named “state” to be of type “enum State”

33 Symbolic Constants: enum Approach 3 note Enumerated constants are interchangeable with ints START_STATE is the same as 0 POSSIBLE_COMMENT_STATE is the same as 1 Etc. state = 0; /* Can assign int to enum. */ i = START_STATE; /* Can assign enum to int. */

34 Symbolic Constants: enum Approach 3 strengths Can explicitly specify values for names Can omit type name, thus effectively giving names to int literals Works when specifying array lengths enum State {START_STATE = 5, POSSIBLE_COMMENT_STATE = 3, COMMENT_STATE = 4,...}; enum {MAX_VALUE = 9999};... int i = MAX_VALUE; enum {ARRAY_LENGTH = 10};... int a[ARRAY_LENGTH];...

35 Symbolic Constants: enum Approach 3 weakness Does not work for non-integral data types enum {PI = }; /* Compiletime error */

36 Symbolic Constant Style Rules In summary of symbolic constants… Style rules: 1.Use enumerations to give symbolic names to integral constants 2.Use const variables to give symbolic names to non-integral constants 3.Avoid #define altogether

37 File circle.c (version 2): “Circle” Program (Version 2) #include int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0. */ { const double PI = ; int radius; int diam; double circum; printf("Enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = PI * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; }

38 Toward Version 3 Problem: Program does not handle bad user input $ circle Enter the circle's radius: abc User enters a non-number. How can the program detect that? What should the program do?

39 Detecting Bad User Input Solution Part 1: Detecting bad user input scanf() returns number of values successfully read Example: Or, more succinctly: Or, for more than one variable: int returnValue; … returnValue = scanf("%d", &i); if (returnValue != 1) /* Error */ … if (scanf("%d", &i) != 1) /* Error */ … if (scanf("%d%d", &i, &j) != 2) /* Error */

40 Reporting Failure to User Solution Part 2: Reporting failure to the user To report failure to user, should write a message to stderr StreamDefault Binding PurposeC Functions stdinKeyboard“Normal” inputscanf(…); fscanf(stdin, …); stdoutVideo screen “Normal” outputprintf(…); fprintf(stdout, …); stderrVideo screen “Abnormal” output fprintf(stderr, …);

41 Reporting Failure to OS Solution Part 3: Reporting failure to the operating system To generate status code x, program should: Execute return x statement to return from main() function, or Call exit(x) to abort program Shell can examine status code Note: In main() function, return statement and exit() function have same effect In other functions, they have different effects Nature of Program Completion Status Code that Program Should Return to OS Successful0 EXIT_SUCCESS (#defined in stdlib.h as 0) UnsuccessfulEXIT_FAILURE (#defined in stdlib.h as ???) System-dependent; on hats, 1

42 File circle.c (version 3): “Circle” Program (Version 3) #include #include int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0 if successful. */ { const double PI = ; int radius; int diam; double circum; printf("Enter the circle's radius:\n"); if (scanf("%d", &radius) != 1) { fprintf(stderr, "Error: Not a number\n"); exit(EXIT_FAILURE); /* or: return EXIT_FAILURE; */ } diam = 2 * radius; circum = PI * (double)diam; printf("A circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; }

43 Summary Simple C programs Program structure Defining symbolic constants #define, constant variables, enumerations Detecting and reporting failure The stderr stream The exit() function Functionality of the gcc command Preprocessor, compiler, assembler, linker Memory layout of a Linux process TEXT, RODATA, STACK sections (More sections – DATA, BSS, HEAP – later in the course)