Professional Documents
Culture Documents
Introduction to C Programming
Why use C?
Mainly because it produces code that runs nearly as fast
as code written in assembly language. Some examples
of the use of C might be:
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Data Bases
Language Interpreters
Utilities
C Programming Language
Developed by Dennis Ritchie at Bell Laboratories in 1972
based upon the B programming language.
It was then used widely in Unix systems.
The C Language spread like wild fire, over various
hardware platforms, all over the IT world, to such an
extend that variations and incompatibilities started
creeping up.
Therefore there was a need for a standard code so that any
programs written in C can be compiled by any compilers.
In 1983, the X3J11 Technical Committee came up with the
draft proposal for the ANSI system, which was approved
in 1989 and referred to as theANSI/ISO 9899 : 1990 or
simply the ANSI C, which is now the global solution.
C is used:
Development with C
Four stages
Editing: Writing the source code by using some IDE or editor
Preprocessing or libraries: Already available routines
compiling: translates or converts source to object code for a specific
platform source code -> object code
linking:
resolves external references and produces the executable
module
C Code
Binary code
Hard drive
Software side
Hardware side
(realtime)
the program)
C Standard Library
Two parts to learning the C world
Learn C itself
Take advantage of rich collection of existing functions called
C Standard Library
Basics of C Environment
C systems consist of 3 parts
Environment
Language
C Standard Library
Edit
Pre-processor
Compile
Link
Load
Execute
Basics of C Environment
Program edited in
Phase 1
Editor
Disk
Phase 2 Preprocessor
Disk
program processes
the code
Creates object code
Phase 3
Compiler
Disk
Phase 4
Linker
Disk
Basics of C Environment
Primary memory
Phase 5
Loader
memory
Primary memory
Phase 6
CPU
Puts program in
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main()
{
printf("Hello World \n");
}
Simple C Program
Line 1: #include <stdio.h>
As part of compilation, the C compiler runs a
program called the C preprocessor. The
preprocessor is able to add and remove code from
your source file.
In this case, the directive #include tells the
preprocessor to include code from the file stdio.h.
This file contains declarations for functions that
the program needs to use. A declaration for the
printf function is in this file.
Simple C Program
Line 2: void main()
This statement declares the main function.
A C program can contain many functions but must
always have one main function.
A function is a self-contained module of code that
can accomplish some task.
Functions are examined later.
The "void" specifies the return type of main. In
this case, nothing is returned to the operating
system.
Simple C Program
Line 3: {
This opening bracket denotes the start of the
program.
Simple C Program
Line 4: printf("Hello World From About\n");
Printf is a function from a standard C library that is
used to print strings to the standard output, normally
your screen.
The compiler links code from these standard libraries
to the code you have written to produce the final
executable.
The "\n" is a special format modifier that tells the
printf to put a line feed at the end of the line.
If there were another printf in this program, its
string would print on the next line.
Simple C Program
Line 5: }
The Preprocessor
The C preprocessor permits you to define simple macros
that are evaluated and expanded prior to compilation.
Commands begin with a #. Abbreviated list:
Preprocessor: Macros
Using macros as functions, exercise caution:
flawed example: #define mymult(a,b) a*b
Source: k = mymult(i-1, j+5);
Post preprocessing: k = i 1 * j + 5;
#elif defined(sun)
static inline int64_t
gettime(void) {return (int64_t)gethrtime()}
#else
static inline int64_t
gettime(void) {... gettimeofday()...}
#endif
Escape Sequence
\n
\t
\r
\a
\\
\
new line
tab
carriage return
alert
backslash
double quote
Arithmetic in C
C operation
Addition(+)
Subtraction (-)
Multiplication(*)
Division(/)
Modulus(%)
Algebraic C
f+7
p-c
bm
x/y, x , x y
r mod s
f+7
p-c
b*m
x/y
r%s
Memory concepts
Every variable has a name, type and value
Variable names correspond to locations in
computer memory
New value over-writes the previous value
Destructive read-in
Value reading called Non-destructive read-out
2. A Simple C Program
1
2
3
4
5
6
7
8
9
10
Comments
Text surrounded by /* and */ is ignored by computer
Used to describe useful program information (description,
author, date)
#include <stdio.h>
Preprocessor directive
Tells computer to load contents of a certain library header file
2.
A Simple C Program
int main()
C++ programs contain one or more functions, exactly one of
which must be main
Parenthesis used to indicate a function
int means that main "returns" an integer value
Braces ({ and }) indicate a block
The bodies of all functions must be contained in braces
2.
A Simple C Program
2.
A Simple C Program
return 0;
A way to exit a function
return 0, in this case, means that the main() function
terminates normally and returns to the Windows operating
system.
Right brace }
Indicates end of main has been reached
Linker
When a function is called, linker locates it in the library
Inserts it into object program
If function name is misspelled, the linker will produce an
error because it will not be able to find function in the
library
#include <stdio.h>
int main() {
printf("Hello
World!\n");
return 0;
}
28
29
30
31
3.
Addition program */
3 #include <stdio.h>
4
5 int main()
6 {
7
/* declaration */
/* prompt */
10
/* read an integer */
11
12
/* read an integer */
13
/* assignment of sum */
14
/* print sum */
8
9
15
16
return 0;
17 }
Enter first integer
45
Enter second integer
72
Sum is 117
3.
As before
Comments, #include <stdio.h> and main
= (assignment operator)
Assigns a value to a variable
Is a binary operator (has two operands)
sum = variable1 + variable2;
sum gets variable1 + variable2;
35
4. Variable Concepts
Variables
Variable names correspond to locations in the computer's
memory
Every variable has a name, a type, a size and a value
Whenever a new value is placed into a variable (through
scanf, for example), it replaces (and destroys) the previous
value
Reading variables from memory does not change them
A visual representation
integer1
45
36
5.
Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
7 / 5 evaluates to 1
Operator precedence
Some arithmetic operators act before others (i.e.,
multiplication before addition)
Use parenthesis when needed
37
Arithmetic operators:
Arithmetic
38
C operation
Addition
Subtraction
Multiplication
Division
Modulus
Operator(s)
()
*, /, or %
+ or -
Operation(s)
Parentheses
Arithmetic
operator
+
*
/
%
Algebraic
expression
f+7
pc
bm
x/y
rmods
C expression
f
p
b
x
r
+
*
/
%
7
c
m
y
s
6.
Executable statements
Perform actions (calculations, input/output of data)
Perform decisions
May want to print "pass" or "fail" given the value of a
test grade
if control structure
Simple version in this section, more detail later
If a condition is true, then the body of the if statement
executed
0 is false, non-zero is true
Keywords
Special words reserved for C
Cannot be used as identifiers or variable names
39
6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1. Declare variables
int main()
{
int num1, num2;
2. Input
2.1 if statements
if ( num1 == num2 )
printf( "%d is equal to %d\n", num1, num2 );
3. Print
if ( num1 != num2 )
printf( "%d is not equal to %d\n", num1, num2 );
if ( num1 < num2 )
printf( "%d is less than %d\n", num1, num2 );
if ( num1 > num2 )
printf( "%d is greater than %d\n", num1, num2 );
if ( num1 <= num2 )
printf( "%d is less than or equal to %d\n",
num1, num2 );
40
41
6.
Decision Making: Equality and
Relational Operators
29
30
31
32
num1, num2 );
33
34
return 0;
35 }
Program Output
Enter two integers, and I will tell you
the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12
Enter two integers, and I will tell you
the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7
6.
Standard algebraic
equality operator or
relational operator
C equality or
relational
operator
Example of C Meaning of C
condition
condition
==
x is equal to y
not =
!=
x == y
x != y
>
<
>
<
x > y
x < y
x is greater than y
>=
>=
x >= y
x is greater than or
equal to y
<=
<=
x <= y
x is less than or
equal to y
EqualityOperators
x is not equal to y
RelationalOperators
x is less than y
42
Keywords Of C
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
43