The New C Standard (Excerpted material)

Similar documents
BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

CE221 Programming in C++ Part 1 Introduction

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

Array Initialization

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

CS201 - Introduction to Programming Glossary By

The New C Standard (Excerpted material)

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

Short Notes of CS201

The New C Standard (Excerpted material)

1 Lexical Considerations

The New C Standard (Excerpted material)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

The New C Standard (Excerpted material)

Lecture 03 Bits, Bytes and Data Types

The New C Standard (Excerpted material)

Work relative to other classes

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

C++ Programming Chapter 7 Pointers

Review of the C Programming Language

Memory Allocation in C

CS558 Programming Languages

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

File IO and command line input CSE 2451

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

628 Lecture Notes Week 4

Approximately a Test II CPSC 206

Review of the C Programming Language for Principles of Operating Systems

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

de facto standard C library Contains a bunch of header files and APIs to do various tasks

Programming languages - C

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

Lexical Considerations

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CSE 374 Programming Concepts & Tools

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

PROGRAMMAZIONE I A.A. 2017/2018

Programming Languages

Chapter 9. Variable Scope and Functions

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

A Fast Review of C Essentials Part I

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Contents. Chapter 1 Overview of the JavaScript C Engine...1. Chapter 2 JavaScript API Reference...23

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Object Oriented Modeling

The New C Standard (Excerpted material)

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Approximately a Final Exam CPSC 206

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Pace University. Fundamental Concepts of CS121 1

Lexical Considerations

CS2141 Software Development using C/C++ C++ Basics

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

The New C Standard (Excerpted material)

Cpt S 122 Data Structures. Introduction to C++ Part II

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

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

4.8 Summary. Practice Exercises

BLM2031 Structured Programming. Zeyneb KURT

Run-time Environments

Function Call Stack and Activation Records

Run-time Environments

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Chapter 11 Introduction to Programming in C

Array. Prepared By - Rifat Shahriyar

Tokens, Expressions and Control Structures

c-lambda: C FFI via raco ctool

Chapter 2. Procedural Programming

15 FUNCTIONS IN C 15.1 INTRODUCTION

CS 300 Leftovers. CS460 Pacific University 1

Computational Expression

CSE 230 Intermediate Programming in C and C++ Functions

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Contents of Lecture 3

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Full file at

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Why Study Assembly Language?

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Chapter 11 Introduction to Programming in C

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CSE 303: Concepts and Tools for Software Development

Supporting Class / C++ Lecture Notes

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Have examined process Creating program Have developed program Written in C Source code

C programming basics T3-1 -

Transcription:

The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved.

165 5.1.2.2.1 Program startup 5.1.2.2.1 Program startup hosted environment startup The function called at program startup is named main. That is, the user-defined function called at program startup. This function must have external linkage (it is possible for a translation unit to declare a function or object, called main, with internal linkage). There is no requirement that the function appear in a source file having a specific name. Other Languages 162 A Java Machine starts execution by invoking the method main of some specified class,... In Fortran and Pascal the function called on startup is defined using the keyword PROGRAM. Implementations use a variety of techniques to start executing a program image. These usually involve executing some internal, implementation-specific, function before main is called. The association between this internal function and main is usually made at link-time by having the internal function make a call to main, which is resolved by the linker in the same way as other function calls. Having the function main defined in a source file whose name is the same as the name of the executable program is a commonly seen convention. The implementation declares no prototype for this function. There is no declaration of main in any system header, or internally within the translator. For this reason translators rarely check that the definition of main follows one of the two specifications given for it. A function named main is usually treated like any other developer-defined function. Because the implementation is not required to define a prototype for main no consistency checks need be performed against the definition of main written by the developer. 163 It shall be defined with a return type of int and with no parameters: int main(void) { /*... */ } 164 Many lazy developers use the form: 1 main() { /*... */ } declaration at least one type specifier C99 does not allow declaration specifiers to be omitted; a return type must now be specified. Usage There was not a sufficiently large number of instances of main in the.c files to provide a reliable measure of the different ways this function is declared. v 1.1 January 30, 2008

5.1.2.2.1 Program startup 167 165 or with two parameters (referred to here as argc and argv, though any names may be used, as they are local main prototype to the function in which they are declared): int main(int argc, char *argv[]) { /*... */ } The parameters of main are treated just like the parameters in any other developer-defined function. Other Languages..., passing it a single argument, which is an array of strings. Java class Test { public static void main(string[] args) { /*... */ } } The identifiers argc and argv are commonly used by developers as the names of the parameters to main (the standard does not require this usage). 166 or equivalent; 9) Many developers use the form: 1 int main(int argc, char **argv) on the basis that arrays are converted to pointers to their element type. The Standard gives no such explicit permission. 167 or in some other implementation-defined manner. The Committee recognized that there is additional information that a host might need to pass to main on startup, but they did not want to mandate anything specific. The following invocations are often seen in source code: and 1 void main() 1 int main(argc, argv, envp) C90 Support for this latitude is new in C99. The Standard explicitly gives permission for an implementation to define this function using different parameter types, but it specifies that the return type is int. 3.6.1p2 January 30, 2008 v 1.1

170 5.1.2.2.1 Program startup It shall have a return type of int, but otherwise its type is implementation-defined.... [Note: it is recommended that any further (optional) parameters be added after argv. ] In a wide character hosted environment, main may be defined as: 1 int main (int argc, wchar_t *argv[]) Some implementations [1] support a definition of the form: 1 int main (int argc, char *argv[], char *env[]) where the third parameter is an array environment strings such as "PATH=/home/derek". The POSIX execv series of functions pass this kind of additional information to the invoked program. However, main is still defined to take two parameters for these functions; the environment information is assigned to an external object extern char **environ. Programs that need to make use of the implementation-defined functionality will obviously define main appropriately. If it is necessary to access environmental information, it is best done through use of implementationsupported functionality. object role If they are declared, the parameters to the main function shall obey the following constraints: This is a list of requirements on implementations. It lists the properties that the values of argc and argv can be relied on, by a program, to possess. The standard does not define any constraints on the values of any, implementation-defined, additional parameters to main. The constraints have proved to be sufficiently broad to be implementable on a wide range of hosts. These constraints are the minimum specification that can be relied on to be supported by an implementation. The value of argc shall be nonnegative. The value of argc, as set by the implementation on startup is nonnegative. The parameter has type int and could be set to a negative value within the program. Most hosts have a limit on the maximum number of characters that may be passed to a program on startup (via, for instance, the command line). This limit is also likely to be a maximum upper limit on the value of argc. Knowing that argc is always set to a nonnegative value, a developer reading the code might expect it to always have a nonnegative value. Assigning a negative value to argc as a flag to indicate a special condition is sometimes seen. The extent to which this might be considered poor practice because of violated developer expectations is discussed elsewhere. argv[argc] shall be a null pointer. 168 169 170 v 1.1 January 30, 2008

5.1.2.2.1 Program startup 172 Some algorithmic styles prefer to decrement a count of the remaining elements. Some prefer to walk a data structure until a null pointer is encountered. The standard supports both styles. 171 If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall argv contain pointers to strings, which are given implementation-defined values by the host environment prior to values program startup. The storage occupied by the strings has static storage-duration. This wording could be interpreted to imply static storage duration an ordering between the contents of the host environment and the contents of argv. What the user typed on a command line may not be what gets passed to the program. The host environment is likely to process the arguments first, possibly performing such operations as expanding wildcards and environment variables. A white-space character is usually used to delimit program parameters. Many hosts offer some form of quoting mechanism to allow any representable character, in the host environment, to be passed as a value to argv (including character sequences that contain white space). The behavior most often encountered is that the elements of argv, in increasing subscript order, correspond to the white-space delimited character sequences appearing on the command line in left-to-right order. POSIX defines _POSIX_ARG_MAX, in <limits.h>, as The length of the arguments for one of the exec functions, in bytes, including environment data. and requires that it have a value of at least 4,096. Under MS-DOS the command processor, command.com, reads a value on startup that specifies the maximum amount of the space to use for its environment (where command line information is held). Most implementations have a limit on the total number of characters, over all the strings, that can be passed via argv. There is a possibility that one of the character sequences in one of the strings pointed to by argv will be truncated. For GUI-based applications the values passed to argv may be hidden from the user of the application. Many hosts offer some form of quoting mechanism to allow any typeable character, in that environment, to be passed to a program. If argv is used, a program may need to handle strings containing characters that are not contained in the basic execution character set. 172 The intent is to supply to the program information determined prior to program startup from elsewhere in the main parameters hosted environment. intent The argc/argv mechanism provides a simple-to-use, from both the applications users point of view and the developer s, method of passing information (usually frequently changing) to a program. The alternative being to use a file, read by the application, which would need to be created or edited every time options changed. This is not to say that an implementation might not choose to obtain the information by reading from a file (e.g., in a GUI environment where there is no command line). The Standard does not specify any intent behind its support for this functionality. If the program was invoked from the command line, the arguments are usually the sequence of characters appearing after the name of the program on the line containing the program image name. The order of the strings appearing in successive elements of the argv array is the same order as they appeared on the command line and the space character is treated as the delimiter. January 30, 2008 v 1.1

175 5.1.2.2.1 Program startup argv lowercase argv program name In GUI environments more information is likely to be contained in configuration files associated with the program to be executed. Many GUIs implement click, or drag-and-drop, by invoking a program with argv[1] denoting the name of the clicked, or dropped-on, file and argv[2] denoting the name of the dropped file, or some similar convention. Making use of argv necessarily relies on implementation-defined behavior. Where the information is obtained, and the ordering of strings in argv, are just some of the issues that need to be considered. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase. This choice reflects C s origins in Unix environments. Some older host environments convert, by default, all command line character input into a single case. A commonly occurring program argument is the name of a file. Unix filenames tend to be in lowercase and the file system is case-sensitive. Having an application accept non-filename arguments in lowercase has a lower cost than insisting that filenames be in uppercase. The Standard is silent on this issue. In some mainframe environments, the convention is often to use uppercase letters. Although entering lowercase letters is difficult, it has usually proven possible to implement. Most modern hosts support the use of both upper- and lowercase characters. The extent to which a program needs to take account of those cases where they might not both be available will depend on the information passed via argv and the likelihood that the program will need to be ported to such a host. If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; What is the program name? It is usually thought of as the sequence of characters used to invoke the program, which in turn relates in some way to the program image (perhaps the name of a file). Under MS-DOS typing, abc on the command line causes the program abc.exe to be executed. In most implementations, the value of argv[0] in this case is abc.exe even though the extension.exe may not have appeared on the command line. Unix based environments use the character sequence typed on the command line as the name of the program. This does not usually include the search path along which the program was found. For symbolic links the name of the symbolic link is usually used, not the name of the file linked to. Some programs use the name under which they were invoked to modify their behavior, for instance, the GNU file compression program is called gzip. Running the same program, having renamed it to gunzip, causes it to uncompress files. Running the same program, renamed to gzcat, causes it to read its input from stdin and send its output to stdout. The standard distribution of this software has a single program image and various symbolic links to it, each with different names. The extent to which the behavior of a program depends on the value of argv[0] is outside the scope of these coding guidelines. argv[0][0] shall be the null character if the program name is not available from the host environment. 173 174 175 v 1.1 January 30, 2008

5.1.2.2.1 Program startup 177 Not all host environments can provide information on the name of the currently executing program. Most hosts can provide the name of the program via argv[0]. 176 If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters program parameters. This defines the term program parameters. It does not require that the value of the parameters come from the command line, although this is the background to the terminology. Example 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) 4 { 5 printf("the program parameters were:\n"); 6 7 for (int a_index = 0; a_index < argc; a_index++) 8 printf("%s\n", argv[a_index]); 9 } 177 The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination. Although the strings pointed to by the argv array are required to be modifiable, the array itself is not required to be modifiable. Calling main recursively (permitted in C90, but not in C99) does not cause the original values to be assigned to those parameters. The Standard is silent on this issue. The addresses of the storage used to hold the argv strings may be disjoint from the stack and heap storage areas assigned to a program on startup. Some implementations choose to place the program parameter strings in an area of storage specifically reserved, by the host environment, for programs parameters. January 30, 2008 v 1.1

References 1. HP. DEC C Language Reference Manual. Compaq Computer Corporation, aa-rh9na-te edition, July 1999. v 1.1 January 30, 2008