Introduction to C - Michigan Technological University

Introduction to C - Michigan Technological University

Introduction to C (Reek, Chs. 1-2) 1 CS 3090: Safety Critical Programming in C C: History Developed in the 1970s in conjunction with development of UNIX operating system When writing an OS kernel, efficiency is crucial This requires low-level access to the underlying hardware: UNIX originally written in low-level assembly language but there were problems: 2 e.g. programmer can leverage knowledge of how data is laid out in memory, to enable faster data access

No structured programming (e.g. encapsulating routines as functions, methods, etc.) code hard to maintain Code worked only for particular hardware not portable CS 3090: Safety Critical Programming in C C: Characteristics C takes a middle path between low-level assembly language and a high-level programming language like Java: 3 Direct access to memory layout through pointer manipulation Concise syntax, small set of keywords Block structure

Some encapsulation of code, via functions Type checking (pretty weak) CS 3090: Safety Critical Programming in C C: Dangers C is not object oriented! C has portability issues Cant hide data as private or protected fields You can follow standards to write C code that looks object-oriented, but you have to be disciplined will the other people working on your code also be disciplined? Low-level tricks may make your C code run well on one platform but the tricks might not work elsewhere The compiler and runtime system will rarely stop your C program from doing stupid/bad things

4 Compile-time type checking is weak No run-time checks for array bounds errors, etc. like in Java CS 3090: Safety Critical Programming in C Separate compilation A C program consists of source code in one or more files Each source file is run through the preprocessor and compiler, resulting in a file containing object code Object files are tied together by the linker to form a single executable program Source code file1.c Preprocesso r/ Compiler Object code file1.o Source code file2.c

Preprocesso r/ Compiler Object code file2.o Libraries 5 Linker Executable code a.out CS 3090: Safety Critical Programming in C Separate compilation Advantage: Quicker compilation 6

When modifying a program, a programmer typically edits only a few source code files at a time. With separate compilation, only the files that have been edited since the last compilation need to be recompiled when re-building the program. For very large programs, this can save a lot of time. CS 3090: Safety Critical Programming in C How to compile (UNIX) To compile and link a C program that is contained entirely in one source file: cc program.c The executable program is called a.out by default. If you dont like this name, choose another using the o option: cc program.c o exciting_executable To compile and link several C source files: cc main.c extra.c more.c This will produce object ( .o) files, that you can use in a later compilation: cc main.o extra.o more.c Here, only more.c will be compiled the main.o and extra.o files will be used for linking.

To produce object files, without linking, use -c: cc c main.c extra.c more.c 7 CS 3090: Safety Critical Programming in C The preprocessor The preprocessor takes your source code and following certain directives that you give it tweaks it in various ways before compilation. A directive is given as a line of source code starting with the # symbol The preprocessor works in a very crude, word-processor way, simply cutting and pasting it doesnt really know anything about C! Your source code Preprocesso r 8 Enhanced and

obfuscated source code Object code Compiler CS 3090: Safety Critical Programming in C A first program: Text rearranger Input First line: pairs of nonnegative integers, separated by whitespace, then terminated by a negative integer x1 y1 x2 y2 xn yn -1 Each subsequent line: a string of characters Output 9

For each string S, output substrings of S: First, the substring starting at location x1 and ending at y 1; Next, the substring starting at location x2 and ending at y 2; CS 3090: Safety Critical Programming Finally, the substring starting at location in C x n and ending Sample input/output Initial input: 0 2 5 7 10 12 -1 Next input line: deep C diving Output:

deeC ding Next input line: excitement! Output: exceme! continue ad nauseum Terminate with ctrl-D (signals end of keyboard input) 10 CS 3090: Safety Critical Programming in C Use of comments /* ** This program reads input lines from the standard input and prints ** each input line, followed by just some portions of the lines, to ** the standard output. ** ** The first input is a list of column numbers, which ends with a ** negative number. The column numbers are paired and specify ** ranges of columns from the input line that are to be printed. ** For example, 0 3 10 12 -1 indicates that only columns 0 through 3 ** and columns 10 through 12 will be printed. */ Only /* */ for comments no // like Java or C++

11 CS 3090: Safety Critical Programming in C Comments on comments Cant nest comments within comments /* is matched with the very next */ that comes along Dont use /* */ to comment out code it wont work if the commented-out code contains comments /* Comment out the following code int f(int x) { return x+42; /* return the result */ } This will */ not! Only this will be commented

out Anyway, commenting out code is confusing, and dangerous (easy to forget about) avoid it 12 CS 3090: Safety Critical Programming in C Preprocessor directives #include #include #include The #include directives paste the contents of the files stdio.h, stdlib.h and string.h into your source code, at the very place where the directives appear. These files contain information about some library functions used in the program: stdio stands for standard I/O, stdlib stands for standard library, and string.h includes useful string manipulation functions. Want to see the files? Look in 13

/usr/include CS 3090: Safety Critical Programming in C Preprocessor directives #define MAX_COLS 20 #define MAX_INPUT 1000 The #define directives perform global replacements: 14 every instance of MAX_COLS is replaced with 20, and every instance of MAX_INPUT is replaced with 1000. CS 3090: Safety Critical Programming in C Function prototypes int read_column_numbers( int columns[], int max ); void rearrange( char *output, char const *input, int n_columns, int const columns[] );

These look like function definitions they have the name and all the type information but each ends abruptly with a semicolon. Wheres the body of the function what does it actually do? (Note that each function does have a real definition, later in the program.) 15 CS 3090: Safety Critical Programming in C Function prototypes Q: Why are these needed, if the functions are defined later in the program anyway? A: C programs are typically arranged in top-down order, so functions are used (called) before theyre defined. (Note that the function main() includes a call to read_column_numbers().)

When the compiler sees a call to read_column_numbers() , it must check whether the call is valid (the right number and types of parameters, and the right return type). But it hasnt seen the definition of read_column_numbers() yet! The prototype gives the compiler advance information about the function thats being called. Of course, the prototype and the later function definition must match in terms of type information. 16 CS 3090: Safety Critical Programming in C The main() function main() is always the first function called in a program execution. int main( void ) { void indicates that the function takes no arguments

int indicates that the function returns an integer value 17 Q: Integer value? Isnt the program just printing out some stuff and then exiting? Whats there to return? A: Through returning particular values, the program can indicate whether it terminated nicely or badly; the operating system can react accordingly. CS 3090: Safety Critical Programming in C The printf() function printf( "Original input : %s\n", input ); printf() is a library function declared in Syntax: printf( FormatString, Expr, Expr...)

FormatString: String of text to print Exprs: Values to print FormatString has placeholders to show where to put the values (note: #placeholders should match # Exprs) Placeholders: %s (print as string), %c (print as char), %d (print as integer), Make sure you pick %f (print as floating-point) the right one! \n indicates a newline character Text line printed only 18 CS 3090: Safety Critical Programming in C when \n encountered Dont forget \n when printing final

return vs. exit Lets look at the return statement in main(): return EXIT_SUCCESS; EXIT_SUCCESS is a constant defined in stdlib ; returning this value signifies successful termination. Contrast this with the exit statement in the function read_column_numbers(): puts( Last column number is not paired. ); exit( EXIT_FAILURE ); EXIT_FAILURE is another constant, signifying that something bad happened requiring termination. exit differs from return in that execution terminates immediately control is not passed back to the calling function main(). 19 CS 3090: Safety Critical Programming in C Pointers, arrays, strings

In this program, the notions of string, array, and pointer seem to be somewhat interchangeable: In main(), an array of characters is declared, for purposes of holding the input string: char input[MAX_INPUT]; Yet when its passed in as an argument to the rearrange() function, input has morphed into a pointer to a character (char *): void rearrange( char *output, char const *input, 20 CS 3090: Safety Critical Programming in C Pointers, arrays, strings In C, the three concepts are indeed closely related:

A pointer is simply a memory address. The type char * pointer to character signifies that the data at the pointers address is to be interpreted as a character. An array is simply a pointer of a special kind: 21 The array pointer is assumed to point to the first of a sequence of data items stored sequentially in memory. How do you get to the other array elements? By incrementing the pointer value. A string is simply an array of characters unlike Java, which has a predefined String class. CS 3090: Safety Critical Programming in C String layout and access p o i

n t e r NUL (char) (char) (char) (char) (char) (char) (char) (char) NUL is a special value indicating end-ofstring (char *) What is input? Its a string!

Its a pointer to char! Its an array of char! 22 input How do we get to the n? Follow the input pointer, then hop 3 to the right *(input + 3) - or input[3] CS 3090: Safety Critical Programming in C

Recently Viewed Presentations

  • SAN CLARIION Clariion Agenda  Introduction  Hardware overview  Software

    SAN CLARIION Clariion Agenda Introduction Hardware overview Software

    — Connect to previous enclosures via the PRI Port. • Power supplies with integrated fan packs. In DAEs that contain ATA drives, the LCC card is modified to convert serial ATA to Fibre Channel. The modified cards. ... sd "...
  • Digital Radio: Step to the Future

    Digital Radio: Step to the Future

    DAB in brief. 6/1995. Firstdigital station in Norway (NRK Klassisk) 5/1999. First public test in theCzechRepublic. 4/2011. TELEKO launchesregularDAB broadcasting in L-band
  • Dyslexia - C H Lopez Consulting

    Dyslexia - C H Lopez Consulting

    Dyslexia is a specific learning disability that is neurological in origin. That is, it is based upon the brain and its functioning. It is characterized by difficulties with accurate and/or fluent word recognition and by poor spelling and decoding abilities.
  • Differences btwn mainteance and development Smaller configuration management

    Differences btwn mainteance and development Smaller configuration management

    Strategic B usines s Control Requirements Management Model-Based Design and Optimization Automated Code Generation Model-Based SoftwareTesting Optim iz ation of Production Software quality Often: the static counterpart to testing SQA works much with processes and reviews Predict quality and prevent...
  • Le ceneri di Gramsci - biagiocarrubba.com

    Le ceneri di Gramsci - biagiocarrubba.com

    Pier Paolo Pasolini Breve biografia di Pier Paolo Pasolini Pier Paolo Pasolini nacque a Bologna il 5 marzo 1922 da Carlo Alberto Pasolini (1892 - 1958), tenente di fanteria, di antica famiglia romagnola, e da Susanna Colussi (1891 - 1981),...
  • Wholesale Lending FHA Procedure One-Time Set-Up 1. 2.

    Wholesale Lending FHA Procedure One-Time Set-Up 1. 2.

    Wholesale Lending FHA Procedure One-Time Set-Up Verify your company is approved for FHA and is sponsored by Flagstar Bank. Your corporate office will have this information and your HUD-approved ID number.
  • Philippine Plan of Action for Nutrition (PPAN) 2017-2022

    Philippine Plan of Action for Nutrition (PPAN) 2017-2022

    All laws, orders, issuances, and rules and regulations or parts thereof inconsistent with this Executive Order are hereby repealed or modified accordingly. There is also a provision on semestral reporting to the Office of the President, and on funding. ......
  • Child Victims of Trafficking - OhioSPF

    Child Victims of Trafficking - OhioSPF

    Regarding bipolar disorder, mortality estimates are 10-15% for bipolar I, same for bipolar II. Suicidal Behavior in Context of Axis I and II Psychopathologies Anxiety disorders: Mortality estimates are less clear here (fearfulness is an issue), but there is risk,...