C introduction


Published on

c intro

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C introduction

  1. 1. Introduction to C (Reek, Chs. 1-2) CS 3090: Safety Critical Programming in C
  2. 2. C: History <ul><li>Developed in the 1970s – in conjunction with development of UNIX operating system </li></ul><ul><ul><li>When writing an OS kernel, efficiency is crucial </li></ul></ul><ul><ul><li>This requires low-level access to the underlying hardware: </li></ul></ul><ul><ul><ul><li>e.g. programmer can leverage knowledge of how data is laid out in memory, to enable faster data access </li></ul></ul></ul><ul><ul><li>UNIX originally written in low-level assembly language – but there were problems: </li></ul></ul><ul><ul><ul><li>No structured programming (e.g. encapsulating routines as “functions”, “methods”, etc.) – code hard to maintain </li></ul></ul></ul><ul><ul><ul><li>Code worked only for particular hardware – not portable </li></ul></ul></ul>CS 3090: Safety Critical Programming in C
  3. 3. C: Characteristics <ul><li>C takes a middle path between low-level assembly language… </li></ul><ul><ul><li>Direct access to memory layout through pointer manipulation </li></ul></ul><ul><ul><li>Concise syntax, small set of keywords </li></ul></ul><ul><li>… and a high-level programming language like Java: </li></ul><ul><ul><li>Block structure </li></ul></ul><ul><ul><li>Some encapsulation of code, via functions </li></ul></ul><ul><ul><li>Type checking (pretty weak) </li></ul></ul>CS 3090: Safety Critical Programming in C
  4. 4. C: Dangers <ul><li>C is not object oriented! </li></ul><ul><ul><li>Can’t “hide” data as “private” or “protected” fields </li></ul></ul><ul><ul><li>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? </li></ul></ul><ul><li>C has portability issues </li></ul><ul><ul><li>Low-level “tricks” may make your C code run well on one platform – but the tricks might not work elsewhere </li></ul></ul><ul><li>The compiler and runtime system will rarely stop your C program from doing stupid/bad things </li></ul><ul><ul><li>Compile-time type checking is weak </li></ul></ul><ul><ul><li>No run-time checks for array bounds errors, etc. like in Java </li></ul></ul>CS 3090: Safety Critical Programming in C
  5. 5. Separate compilation <ul><li>A C program consists of source code in one or more files </li></ul><ul><li>Each source file is run through the preprocessor and compiler , resulting in a file containing object code </li></ul><ul><li>Object files are tied together by the linker to form a single executable program </li></ul>CS 3090: Safety Critical Programming in C Source code file1.c Preprocessor/ Compiler Object code file1.o Source code file2.c Preprocessor/ Compiler Object code file2.o Linker Libraries Executable code a.out
  6. 6. Separate compilation <ul><li>Advantage: Quicker compilation </li></ul><ul><ul><li>When modifying a program, a programmer typically edits only a few source code files at a time. </li></ul></ul><ul><ul><li>With separate compilation, only the files that have been edited since the last compilation need to be recompiled when re-building the program. </li></ul></ul><ul><ul><li>For very large programs, this can save a lot of time. </li></ul></ul>CS 3090: Safety Critical Programming in C
  7. 7. How to compile (UNIX) <ul><li>To compile and link a C program that is contained entirely in one source file: </li></ul><ul><li>cc program.c </li></ul><ul><ul><li>The executable program is called a.out by default. </li></ul></ul><ul><ul><li>If you don’t like this name, choose another using the –o option: </li></ul></ul><ul><li>cc program.c –o exciting_executable </li></ul><ul><li>To compile and link several C source files: </li></ul><ul><li>cc main.c extra.c more.c </li></ul><ul><ul><li>This will produce object ( .o ) files, that you can use in a later compilation: </li></ul></ul><ul><li>cc main.o extra.o more.c </li></ul><ul><ul><li>Here, only more.c will be compiled – the main.o and extra.o files will be used for linking. </li></ul></ul><ul><li>To produce object files, without linking, use -c : </li></ul><ul><ul><li>cc –c main.c extra.c more.c </li></ul></ul>CS 3090: Safety Critical Programming in C
  8. 8. The preprocessor <ul><li>The preprocessor takes your source code and – following certain directives that you give it – tweaks it in various ways before compilation. </li></ul><ul><li>A directive is given as a line of source code starting with the # symbol </li></ul><ul><li>The preprocessor works in a very crude, “word-processor” way, simply cutting and pasting – </li></ul><ul><li>it doesn’t really know anything about C! </li></ul>CS 3090: Safety Critical Programming in C Your source code Preprocessor Enhanced and obfuscated source code Compiler Object code
  9. 9. A first program: Text rearranger <ul><li>Input </li></ul><ul><ul><li>First line: pairs of nonnegative integers, separated by whitespace, then terminated by a negative integer </li></ul></ul><ul><ul><li>x 1 y 1 x 2 y 2 … x n y n -1 </li></ul></ul><ul><ul><li>Each subsequent line: a string of characters </li></ul></ul><ul><li>Output </li></ul><ul><ul><li>For each string S, output substrings of S: </li></ul></ul><ul><ul><ul><li>First, the substring starting at location x 1 and ending at y 1 ; </li></ul></ul></ul><ul><ul><ul><li>Next, the substring starting at location x 2 and ending at y 2 ; </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>Finally, the substring starting at location x n and ending at x n . </li></ul></ul></ul>CS 3090: Safety Critical Programming in C
  10. 10. Sample input/output <ul><li>Initial input: 0 2 5 7 10 12 -1 </li></ul><ul><li>Next input line: deep C diving </li></ul><ul><li>Output: deeC ding </li></ul><ul><li>Next input line: excitement! </li></ul><ul><li>Output: exceme! </li></ul><ul><li>… continue ad nauseum… </li></ul><ul><li>Terminate with ctrl-D (signals end of keyboard input) </li></ul>CS 3090: Safety Critical Programming in C
  11. 11. Use of comments <ul><li>/* </li></ul><ul><li>** This program reads input lines from the standard input and prints </li></ul><ul><li>** each input line, followed by just some portions of the lines, to </li></ul><ul><li>** the standard output. </li></ul><ul><li>** </li></ul><ul><li>** The first input is a list of column numbers, which ends with a </li></ul><ul><li>** negative number. The column numbers are paired and specify </li></ul><ul><li>** ranges of columns from the input line that are to be printed. </li></ul><ul><li>** For example, 0 3 10 12 -1 indicates that only columns 0 through 3 </li></ul><ul><li>** and columns 10 through 12 will be printed. </li></ul><ul><li>*/ </li></ul><ul><li>Only /* … */ for comments – no // like Java or C++ </li></ul>CS 3090: Safety Critical Programming in C
  12. 12. Comments on comments <ul><li>Can’t nest comments within comments </li></ul><ul><ul><li>/* is matched with the very next */ that comes along </li></ul></ul><ul><li>Don’t use /* … */ to comment out code – it won’t work if the commented-out code contains comments </li></ul><ul><li>/* Comment out the following code </li></ul><ul><li>int f(int x) { </li></ul><ul><li>return x+42; /* return the result */ </li></ul><ul><li>} </li></ul><ul><li>*/ </li></ul><ul><li>Anyway, commenting out code is confusing, and dangerous (easy to forget about) – avoid it </li></ul>CS 3090: Safety Critical Programming in C Only this will be commented out This will not!
  13. 13. Preprocessor directives <ul><li>#include <stdio.h> </li></ul><ul><li>#include <stdlib.h> </li></ul><ul><li>#include <string.h> </li></ul><ul><li>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. </li></ul><ul><li>These files contain information about some library functions used in the program: </li></ul><ul><ul><li>stdio stands for “standard I/O”, stdlib stands for “standard library”, and string.h includes useful string manipulation functions. </li></ul></ul><ul><li>Want to see the files? Look in /usr/include </li></ul>CS 3090: Safety Critical Programming in C
  14. 14. Preprocessor directives <ul><li>#define MAX_COLS 20 </li></ul><ul><li>#define MAX_INPUT 1000 </li></ul><ul><li>The #define directives perform </li></ul><ul><li>“ global replacements”: </li></ul><ul><ul><li>every instance of MAX_COLS is replaced with 20 , and every instance of MAX_INPUT is replaced with 1000 . </li></ul></ul>CS 3090: Safety Critical Programming in C
  15. 15. The main() function <ul><li>main() is always the first function called in a program execution. </li></ul><ul><li>int </li></ul><ul><li>main( void ) </li></ul><ul><li>{ … </li></ul><ul><li>void indicates that the function takes no arguments </li></ul><ul><li>int indicates that the function returns an integer value </li></ul><ul><ul><li>Q: Integer value? Isn’t the program just printing out some stuff and then exiting? What’s there to return? </li></ul></ul><ul><ul><li>A: Through returning particular values, the program can indicate whether it terminated “nicely” or badly; the operating system can react accordingly. </li></ul></ul>CS 3090: Safety Critical Programming in C
  16. 16. The printf() function <ul><li>printf( &quot;Original input : %s &quot;, input ); </li></ul><ul><li>printf() is a library function declared in <stdio.h> </li></ul><ul><li>Syntax: printf( FormatString , Expr , Expr ...) </li></ul><ul><ul><li>FormatString : String of text to print </li></ul></ul><ul><ul><li>Expr s: Values to print </li></ul></ul><ul><ul><li>FormatString has placeholders to show where to put the values (note: #placeholders should match # Expr s) </li></ul></ul><ul><ul><li>Placeholders: %s (print as string), %c (print as char), </li></ul></ul><ul><ul><li>%d (print as integer), </li></ul></ul><ul><ul><li>%f (print as floating-point) </li></ul></ul><ul><ul><li> indicates a newline character </li></ul></ul>CS 3090: Safety Critical Programming in C Make sure you pick the right one! Text line printed only when encountered Don’t forget when printing “final results”