// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

Similar documents
For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Stream Model of I/O. Basic I/O in C

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

CSCI 135 Software Design and Analysis, C++ Homework 8 Solution

Exercise Session 2 Simon Gerber

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

Intermediate Programming, Spring 2017*

CSE 333 Lecture 2 Memory

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

Topic 6: A Quick Intro To C

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Exercise Session 2 Systems Programming and Computer Architecture

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

PROGRAMMAZIONE I A.A. 2017/2018

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Motivation was to facilitate development of systems software, especially OS development.

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

GCC: the GNU Compiler Collection

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

The Three Attributes of an Identifier

C introduction: part 1

5.Coding for 64-Bit Programs

GCC: the GNU Compiler Collection

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CSE 333 Midterm Exam 5/9/14 Sample Solution

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Functions. Cedric Saule

gcc o driver std=c99 -Wall driver.c bigmesa.c

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

(T) x. Casts. A cast converts the value held in variable x to type T

Motivation was to facilitate development of systems software, especially OS development.

Pointer Casts and Data Accesses

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

As stated earlier, the declaration

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

EL2310 Scientific Programming

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

University of Kelaniya Sri Lanka

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

C / C++ Coding Rules

QUIZ. What are 3 differences between C and C++ const variables?

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CS Programming In C

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

Pointers and scanf() Steven R. Bagley

gcc o driver std=c99 -Wall driver.c everynth.c

Lecture 2: C Programming Basic

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

Kurt Schmidt. October 30, 2018

Bristol Institute of Technology

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Autumn 2014 Midterm Key

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Lecture 3: C Programm

KYC - Know your compiler. Introduction to GCC

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Deep C (and C++) by Olve Maudal

1 You seek performance 3

A Fast Review of C Essentials Part II

QUIZ. Source:

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

ECEN 449 Microprocessor System Design. Review of C Programming

C - Basics, Bitwise Operator. Zhaoguo Wang

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

Creating a String Data Type in C

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Your first C and C++ programs

Lecture 2: C Programm

CS240: Programming in C

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Given that much information about two such rectangles, it is possible to determine whether they intersect.

Introduction: The Unix shell and C programming

Friday, February 10, Lab Notes

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CS 470 Spring Mike Lam, Professor. OpenMP

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

Intermediate Programming, Spring 2017*

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CS 470 Spring Mike Lam, Professor. OpenMP

Functions. Chapter 5

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

2 Compiling a C program

CS16 Exam #1 7/17/ Minutes 100 Points total

A Fast Review of C Essentials Part I

Transcription:

Instructions: This homework assignment focuses primarily on issues arising when compiling and linking C programs that consist of multiple source files. The answers to the following questions can be determined by consulting a C language reference and/or writing short C programs to test your logic. For the purpose of this assignment, we will stipulate that answers are expected to be consistent with the behavior of the gcc C compiler, used with std=c99. The given code does compile, possibly with warnings. After you have analyzed the questions and decided what answers you believe are correct, you may find it useful to write some short programs to test your logic. Submit your answers to the Curator quiz collection point Quiz2. For questions 1 through 8, consider a C program organized in two source files, containing the declarations and definitions shown below (as well as additional code that's not relevant just now): // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2 extern int A2; // 1.3 int func1(int x); // 1.4 int func1(int x) { // 1.5 // body not shown int main() { if ( A2 == 10 ) { // 1.6 int A3 = func2(); // 1.7 printf("yep: %d\n", A3); // 1.8 return 0; // file2.c int A2 = 12; // 2.1 int B2; // 2.2 int func2(); // 2.3 static int func3(); // 2.4 int func2() { // 2.4 int A2 = 10; // 2.5 static int B3 = func3(); // 2.6 return A2; // 2.7 int func3() { // 2.8 int A4 = func2(); // 2.9 A2 = A4; // 2.10 return A4; // 2.11 1. The variable A1 has: 2. The variable B1 has: 1

3. The variable A2 declared in line 2.1 has: 4. The variable A2 declared in line 2.5 has: 5. The variable B3 has: If the given code is compiled as shown below, the resulting diagnostic warning messages are as shown below (with a little editing to make the line numbers conform to the way the code is shown above): Linux> gcc -o Q1 -std=c99 -Wall file1.c file2.c file1.c: In function main : file1.c:1.7: warning: implicit declaration of function func2 [-Wimplicit-function-declaration] file1.c: At top level: file1.c:4:12: warning: B1 defined but not used [-Wunused-variable] file1.c: In function func1 : file1.c:13:1: warning: control reaches end of non-void function [-Wreturn-type] file2.c:15:5: warning: func3 defined but not used [-Wunused-function] 6. The warning for line 1.7 of file1.c is generated, but there is no error message for that line. Why is there no error message? 1) The compiler assumes that func2() is a function with a return type of int, and takes no parameters, and that is correct. 2) The compiler has generated the warning message incorrectly, and there is no problem. 3) The linker finds the actual definition of func2() and it agrees with the compiler's assumption. 4) 1 and 2 only 5) 2 and 3 only only 7. If the program, as given, was executed, what would be written to stdout? 1) Yep: 10 2) Yep: 12 3) Nothing will be written to stdout. 4) Something else will be written to stdout. 2

8. Where could a call to func3() be added to the given code, without producing a compiler or linker error message? 1) within func1() 2) within func2() 3) within func3() 4) All of the above 5) 1 and 2 only only only 8) None of these 9. A C programmer wants to complete the function below so that successive calls will return successive characters of the string until the terminator is reached: char nextelement() { char* string = "Brian Kernighan"; if ( string[pos] == '\0' ) return string[pos]; else return string[pos++]; In other words, the result of executing the following code should be to print the characters Brian Kernighan.... char ch; while ( ( ch = nextelement() )!= '\0' ) { printf("%c", ch);... The programmer must modify the given code for nextelement() (which will not compile), but she is not allowed to modify the interface of the function or make any use of file-scoped values. The key is to add a declaration of the variable pos. What declaration should she add to the function body? 1) int pos = 0; 2) unsigned int pos = 0; 3) static int pos = 0; 4) Any of those would do. 5) 1 or 2 only would do. 6) 1 or 3 only would do. 7) 2 or 3 only would do. 8) None of them would do. 10. Reconsider the previous question, under the restriction that the programmer is not allowed to modify the interface or the body of the function, but she is allowed to make use of file-scoped values. What declaration should she add at the file scope level? 1) int pos = 0; 2) unsigned int pos = 0; 3) static int pos = 0; 4) Any of those would do. 5) 1 or 2 only would do. 6) 1 or 3 only would do. 7) 2 or 3 only would do. 8) None of them would do. 3

For questions 11 and 12, consider the following small C program, which is divided into three files: // main.c #include <stdio.h> #include <stdint.h> #include <inttypes.h> #include "Puzzle.h" int main() { int32_t Sum = 0; for (int k = 0; k < 100; k++) { Sum += increment(); printf("sum is %"PRId32".\n", Sum); // Puzzle.h #ifndef PUZZLE_H #define PUZZLE_H int increment(); #endif // Puzzle.c int increment() { int puzzle = 0; return 0; return ++puzzle; 11. What value will the variable Sum hold when the loop exits? 1) 0 2) 99 3) 100 4) 101 5) 4950 6) 5050 12. Aside from the arithmetic statement in the loop, why will Sum hold that particular value? 1) increment() returns k when it's called for the k-th time, because puzzle has static storage duration 2) increment() returns 0 every time it's called, because puzzle has automatic storage duration 3) increment() returns 1 every time it's called, because puzzle has automatic storage duration 4) None of these For questions 13 through 15, suppose that the file Puzzle.c shown above is replaced by: // Puzzle.c int puzzle = 0; int increment() { return ++puzzle; 13. What value will the variable Sum hold when the loop exits? 1) 0 2) 99 3) 100 4) 101 5) 4950 6) 5050 4

14. Aside from the arithmetic statement in the loop, why will Sum hold that particular value? 1) increment() returns k when it's called for the k-th time, because puzzle has static storage duration 2) increment() returns 0 every time it's called, because puzzle has automatic storage duration 3) increment() returns 1 every time it's called, because puzzle has automatic storage duration 4) None of these 15. An objection is made to the change made in the second version of Puzzle.c on the grounds that the variable puzzle is now accessible to code in other files. Which of the following statements are true? 1) The variable puzzle in the second version of Puzzle.c could not be accessed from the file main.c, no matter what changes were made to main.c. 2) The variable puzzle in the second version of Puzzle.c could be accessed from the file main.c, if a single line of code was added to main.c. 3) We could prevent access to the variable puzzle by adding private to the declaration of puzzle. 4) 1 and 3 are true. 5) 2 and 3 are true. 6) None of the statements are true. 5