C Programming Language - Lecture 1

download C Programming Language - Lecture 1

of 38

description

C Programming Language

Transcript of C Programming Language - Lecture 1

C Programming Language

C Programming LanguageLecture 11C Programming LanguageLecture contentThe evolution of CConformanceStrengths and weaknessesOverview of C programmingLexical elementsThe C preprocessorQ&A

C Programming Language - Lecture 122The evolution of CWhenWho, whereCommentsmid-1960Ken Thompson, Bell LaboratoriesDeveloped B language (based on BCPL which in turn is based on Algol-60) for UNIX Operating System developmentA higher-level programming language was needed for further development of the system (prior, assembly language was used) 1971Denis Ritchie, Bell LaboratoriesDeveloped an extended version of B called NB (New B) and then changed its name into C1973-UNIX was rewritten in C1978Brian Kernighan and Denis RitchieThe C Programming Language 1st edition, referred as K&R - it was the de facto standardC Programming Language - Lecture 13The evolution of CWhenComments1983ANSI starts the standardization process of the C programming language1989Standard adopted as ANSI Standard X3.159-1989 ANSI-C1990Standard adopted by ISO as an international standard ISO/IEC 9899:1990 C89 or C90 1995Amendment 1 extension to C89 C89 with Amendamne1 or C951999New C standard adopted as ISO/IEC 9899:1999 C99Changes are in the spirit of C they do not change the fundamental nature of the languageC99 isnt yet universal and it will take time before all compilers are C99 compliantC Programming Language - Lecture 14For more information about C standards, follow the link below http://www.open-std.org/JTC1/SC22/WG14/ConformanceBoth C programs and C implementations can conform to Standard C:A C program is said to be strictly conforming to Standard C if that program uses only the features of the language and library described in the StandardThe program's operation must not depend on any aspect of the C language that the Standard characterizes as unspecified, undefined, or implementation-definedC Programming Language - Lecture 15ConformanceThere are two kinds of conforming implementations:Hosted implementation it accepts any conforming programFreestanding implementation accepts any conforming program that uses no library facilities other than those provided in the header files float.h, iso646.h , limits.h, stdarg,h, stdbool.h, stddef .h and stdint.h Freestanding conformance is meant to accommodate C implementations for embedded systems or other target environments with minimal run-time support. For example, such systems may have no file systemC Programming Language - Lecture 16Strengths and WeaknessesC is a low level programming language:Is suitable for system programmingProvides access to machine level concepts (bytes and addresses)Provides operations that correspond closely to a computers built-in instructionsC is a small languageProvides a limited set of features than other languagesIt relies on a library of standard functionsC is permissiveDoes not implement detailed error-checking mechanismsIt assumes that programmer knows what hi is doing so it allows a wider degree of freedom than other languagesC Programming Language - Lecture 17Strengths and WeaknessesStrengthsWeaknessesEfficiency: programs execute fast and use limited amounts of memoryPrograms can be error-prone: C allows some programming mistakes to go undetected (e.g. the use of & operator) Portability: programs may run on different computing systemsPrograms can be difficult to understand: C allows to write programs in a quite cryptic manner (e.g. a[(i>>1) + j] += i>>j;)Power: C has a large collection of types and operatorsPrograms can be difficult to modify: Large programs can be hard to modify if they havent been designed with maintenance in mindFlexibility: C may be used to write programs for embedded systems or for commercial data processing systemsStandard library: has plenty of functions for I/O, string processing, storage allocation, etc.C Programming Language - Lecture 18Overview of C programmingC Programming Language - Lecture 19EXAMPLE

#include

#define SIZE = 10 int size(int a[SIZE]){ int ret; ret=printf("size of array is:%d\n", sizeof(a)); return ret;}

int main(){ int a[SIZE]; (void)size(a); return 0;}Q: Will the program compile?Q: What would be the output of the program?Q: What would be the value returned by the function size?A C program is composed of one or more source files or translation units, each of which contains some part of the entire C program: typically some number of external functionsCommon declarations are often collected into header files and are included into the source files with a special #include commandOne external function must be named main and this function is where the program starts

Overview of C programmingC Programming Language - Lecture 110C source fileCompileLinkObject fileExecutable moduleLibraryC source fileCompileObject fileA C compiler independently processes each source file and translates the C program text into instructions understood by the computerThe output of the compiler is usually called object code or an object moduleWhen all source files are compiled, the object modules are given to a program called the linker The linker resolves references between the modules, adds functions from the standard run-time libraryThe linker produces a single executable program which can then be invoked or run

Lexical elementsC Programming Language - Lecture 111ClassCharacters52 Latin capital and small lettersA B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k 1 m n o p q r s t u v w x y z10 digits0 1 2 3 4 5 6 7 8 9 SPACEhorizontal tab (HT), vertical tab (VT), and form feed (FF) control charactersA C source file is a sequence of characters selected from a character set C programs are written using the following characters (source character set) defined in the Basic Latin block of ISO/IEC 10646

Lexical elementsC Programming Language - Lecture 112ClassCharacters29 graphic characters and their official names! + #={%~}^[,&].EXCLAMATION MARKPLUS SIGN QUOTATION MARKNUMBER SIGNEQUALS SIGN LEFT CURLY BRACKET PERCENT SIGN TILDERIGHT CURLY BRACKET CIRCUMFLEX ACCENT LEFT SQUARE BRACKET COMMA AMPERSAND RIGHT SQUARE BRACKET FULL STOP *_\/);?-:ASTERISK APOSTROPHELESS-THAN SIGN LEFT PARENTHESIS VERTICAL LINE GREATER-THAN SIGN LOWLINE (underscore)REVERSE SOLIDUS (backslash)SOLIDUS (slash, divide sign)RIGHT PARENTHESIS SEMICOLONQUESTION MARK HYPHEN-MINUSCOLON EX: Name three uses of the & symbol in a C source file.EX: Name two uses of the % symbol in a C source file.

Lexical elementsDividing the source program into lines can be done with a character or character sequenceAdditional characters are sometimes used in C source programs, including: formatting characters such as the backspace (BS) and carriage return (CR) characters treated as spaces additional Basic Latin characters, including the characters $ (DOLLAR SIGN), @ (COMMERClAL AT), and ` (GRAVE ACCENT) may appear only in comments, character constants, string constants, and file names

C Programming Language - Lecture 113Lexical elementsThe character set interpreted during the execution of a C program is not necessarily the same as the one in which the C program is writtenCharacters in the execution character set are represented by their equivalents in the source character set or by special character escape sequences that begin with the backslash (\) characterIn addition to the standard characters mentioned before, the execution character set must also include: a null character that must be encoded as the value 0 used for marking the and of strings a newline character that is used as the end-of-line marker: used to divide character streams into lines during I/Othe alert, backspace, and carriage return charactersThese source and execution character sets are the same when a C program is compiled and executed on the same computerFor programs that are cross-compiled, when a compiler calculates the compile-time value of a constant expression involving characters, it must use the target computer's encoding, not the more natural source encodingC Programming Language - Lecture 114Lexical elementsIn C source programs the blank (space), end-of-line, vertical tab, form feed, and horizontal tab (if present) are known collectively as whitespace characters. Comments are also whitespaceThe end-of-line character or character sequence marks the end of source program lines. In some implementations, the formatting characters carriage return, form feed, and (or) vertical tab additionally terminate source lines and are called line break charactersA source line can be continued onto the next line by ending the first line with a reverse solidus or backslash (\) character. The backslash and end-of-line marker are removed to create a longer, logical source line

C Programming Language - Lecture 115EXAMPLE

if (a==b) X=1; el\ se X=2; Is equivalent to the single line

if (a == b) X=1; else X=2; EXAMPLE

#define nine (3*3) Is equivalent to

#define nine /* this is nine*/ (3*3)15Lexical elementsComments:Traditionally, a comment begins with an occurrence of the two characters /* and ends with the first subsequent occurrence of the two characters */Beginning with C99, a comment also begins with the characters // and extends up to (but does not include) the next line breakComments are not recognized inside string or character constants or within other commentsComments are removed by the compiler before preprocessingStandard C specifies that all comments are to be replaced by a single spaceC Programming Language - Lecture 116EXAMPLE// Program to compute the squares of // the first 10 integers#include

void Squares ( /* no arguments */ ) { int i; /* Loop from 1 to 10, printing out the squares */ for (i=1; i