You are on page 1of 17

C Programming

Introduction
ME 325
Spring, 2007
C Compiler
• Below is a link for a free C/C++
compiler

• http://www.bloodshed.net/dev/devcpp.
html
Overview
• History and Philosophy of C

• Program Development Cycle

• Compiler

• First program

• printf
History
• The initial development of C occurred at AT&T Bell Labs
between 1969 and 1973; It was named "C" because many of its
features were derived from an earlier language called "B" .

• By 1973, the C language had become powerful enough that


most of the Unix kernel, originally written in PDP-11 assembly
language, was rewritten in C.

• In 1978, Dennis Ritchie and Brian Kernighan published the first


edition of The C Programming Language. The version of C that
it describes is commonly referred to as "K&R C".

• The second edition of the book covers the later ANSI C


standard.
– K&R introduced several language features:
• struct data types. long int data type, unsigned int data type, The =-
operator was changed to -=

• During the late 1970s, C began to replace BASIC as the leading


microcomputer programming language. During the 1980s, it
was adopted for use with the IBM PC
Philosophy
• C is a minimalistic programming language.
• Does not require extensive run-time support.
• Encourage machine-independent
programming, and portably
• Provide low-level access to memory.

As a result, C code is suitable for many


systems-programming applications that had
traditionally been implemented in
assembly language.
The language has become available on a very
wide range of platforms, from embedded
microcontrollers to supercomputers.
Characteristics
• Small size
• Extensive use of function calls
• Loose typing
• Structured language
• Low level (BitWise) programming
readily available
• Pointer implementation - extensive use
of pointers for memory, array,
structures and functions.
• Small set (around 30) of reserved
keywords
Program Development Cycle
Program Performing a Task
• Determine Output

• Identify Input

• Determine process necessary to turn given


Input into desired Output
Pseudocode
• Pseudocode is a compact and informal
high-level description of a
computer programming algorithm that uses
the structural conventions of
programming languages, but omits detailed
subroutines, variable declarations or
language-specific syntax.

• Example
if Credit Card Valid
Process valid credit card routine
else
Process invalid credit card routine
end if
Flowchart

• A flow chart is a schematic representation


of an algorithm or a process.

• Symbols
– Start and end symbols, represented as ovals
or rounded rectangles
– Arrows, showing what's called "flow of
control" in computer science.
– Processing steps, represented as
rectangles. Examples: "Add 1 to X"; "replace
identified part"; "save changes" or similar.
– Input/Output, represented as a
parallelogram. Examples: Get X from the
user; display X.
– Conditional (or decision), represented as a
diamond (rhombus). These typically contain a
Yes/No question or True/False test. This
symbol is unique in that it has two arrows
coming out of it.
– A Data File represented by a cylinder
Program Development Cycle
Write
Pseudocode

Program
Source Code

Compile/ Libraries
Link

Executable

Test

Document
C Compile Model
• C is compiled
language. This means
that once you write
your C program, you
must run it through a C
compiler to turn your
program into an
executable that the
computer can run
(execute).
• The C program is the
human-readable form,
while the executable
that comes out of the
compiler is the
machine-readable
The C Compilation Model
The Preprocessor
• The Preprocessor accepts source code as input
and is responsible for
– removing comments
– interpreting special preprocessor directives denoted
by #.
• #include -- includes contents of a named file. Files
usually called header files. e.g
• #define -- defines a symbolic name or constant. Macro
substitution.
• #define MAX_ARRAY_SIZE 100
Compiler
• The C compiler translates source to assembly
code. The source code is received from the
preprocessor.
Assembler
• The assembler creates object code. (.OBJ on
MSDOS) to indicate object code files.
Link Editor
• If a source file references library functions or
functions defined in other source files the link
editor combines these functions (with main()) to
create an executable file.
Hello World,
a Program is born
#include <stdio.h>
int main()
{
printf(“Hello World\n");
return 0;
}
Hello World,
Explanation
• #include <stdio.h>, this line includes the "standard I/O
library" into your program. The standard I/O library lets you
read input from the keyboard (called "standard in"), write
output to the screen (called "standard out"), process text files
stored on the disk, and so on. C has a large number of standard
libraries like stdio, including string, time and math libraries. A
library is simply a package of code that someone else has
written to make your life easier.
• The line int main() declares the main function. Every C
program must have a function named main somewhere in the
code. At run time, program execution starts at the first line of
the main function.
• In C, the { and } symbols mark the beginning and end of a
block of code. In this case, the block of code making up the
main function contains two lines.
• The printf statement in C allows you to send output to
standard out (the screen). The portion in quotes is called the
format string and describes how the data is to be formatted
when printed. The format string can contain string literals such
as “Hello World" symbols for carriage returns (\n), and
operators as placeholders for variables.
printf ( const char * format,
... );
• Output formatted string to the standard
output (stdout).

• The string constant format provides a


description of the output, with
placeholders marked by "%"
escape characters “\”, to specify both
the relative location and the type of
output that the function should
produce.
%[flags][width][.precision][leng
th]specifier
Specifier Output Example
c Character A
d or i Signed decimal integer 392
e or E Scientific notation using e/E character 3.92e+2
f Decimal floating point 392.65
s String of characters “sample”
u Unsigned integer
x,X Unsigned hexadecimal 2a , 2A

Flags description
- Left-justify within the given field width; Right
justification is the default
+ Forces to preceed the result with a plus or minus sign
0 (+ or -) the number with zeroes (0) instead of spaces
Left-pads
(width)
• #include <stdio.h>
• int main()
• {
• printf ("Characters: %c %c \n", 'a', 65);
• printf ("Decimals: %d %ld\n", 1977, 650000);
• printf ("Preceding with blanks: %10d \n", 1977);
• printf ("Preceding with zeros: %010d \n", 1977);
• printf ("Some different radixes: %d %x %#x \n", 100, 100, 100);
• printf ("floats: %4.2f %E \n", 3.1416, 3.1416);
• printf ("Width trick: %*d \n", 5, 10); /* This will place 5 spaces thin print 10 */
• printf ("%s \n", "A string");
• return 0;
• }

Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 0x64
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string

You might also like