02-04-2011, 11:19 AM
PRESENTED BY:
Fred Kuhns
C.ppt (Size: 456 KB / Downloads: 338)
Introduction
• The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s
• Influenced by
– ALGOL 60 (1960),
– CPL (Cambridge, 1963),
– BCPL (Martin Richard, 1967),
– B (Ken Thompson, 1970)
• Traditionally used for systems programming, though this may be changing in favor of C++
• Traditional C:
– The C Programming Language, by Brian Kernighan and Dennis Ritchie, 2nd Edition, Prentice Hall
– Referred to as K&R
Standard C
• Standardized in 1989 by ANSI (American National Standards Institute) known as ANSI C
• International standard (ISO) in 1990 which was adopted by ANSI and is known as C89
• As part of the normal evolution process the standard was updated in 1995 (C95) and 1999 (C99)
• C++ and C
– C++ extends C to include support for Object Oriented Programming and other features that facilitate large software development projects
– C is not strictly a subset of C++, but it is possible to write “Clean C” that conforms to both the C++ and C standards.
Elements of a C Program
• A C development environment includes
– System libraries and headers: a set of standard libraries and their header files. For example see /usr/include and glibc.
– Application Source: application source and header files
– Compiler: converts source to object code for a specific platform
– Linker: resolves external references and produces the executable module
• User program structure
– there must be one main function where execution begins when the program is run. This function is called main
• int main (void) { ... },
• int main (int argc, char *argv[]) { ... }
• UNIX Systems have a 3rd way to define main(), though it is not POSIX.1 compliant
int main (int argc, char *argv[], char *envp[])
– additional local and external functions and variables
A Simple C Program
• Create example file: try.c
• Compile using gcc:
gcc –o try try.c
• The standard C library libc is included automatically
• Execute program
./try
• Note, I always specify an absolute path
• Normal termination:
void exit(int status);
– calls functions registered with atexit()
– flush output streams
– close all open streams
– return status value and control to host environment
Source and Header files
• Just as in C++, place related code within the same module (i.e. file).
• Header files (*.h) export interface definitions
– function prototypes, data types, macros, inline functions and other common declarations
• Do not place source code (i.e. definitions) in the header file with a few exceptions.
– inline’d code
– class definitions
– const definitions
• C preprocessor (cpp) is used to insert common definitions into source files
• There are other cool things you can do with the preprocessor