Chapter 2
Chapter 2
Chapter 2
CHAPTER 2
OUTLINE
History of C
C Language Elements
Data Types and Variable Declarations
Executable Statements
Input and Output Functions
General form of a C program
Arithmetic Expressions
Formatting Numbers in Program Output
HISTORY OF C
C was developed in 1972 by Dennis Ritchie at
AT&T Bell Laboratories.
Cwas designed as a programming language to
write the Unix Operating System.
C became the most commonly used language
for writing system software.
C is machine independent: C programs can be
compiled to run on a wide variety of processors
and operating systems.
WHY LEARN C?
Many companies and software projects do their
programming in C.
Looks good on your resume.
Small, compact size code.
Produces optimized programs that run fast.
Low-level access to computer memory via pointers.
Can be compiled to run on a variety of computers.
C LANGUAGE ELEMENTS IN
MILES-TO-KILOMETERS CONVERSION
PROGRAM
PROGRAM IN MEMORY: BEFORE (A)
AND AFTER EXECUTION OF A
PROGRAM (B)
PREPROCESSOR DIRECTIVES
Preprocessor directives are commands that give
instructions to the C preprocessor.
The preprocessor modifies a C program prior to
its compilation.
Preprocessor directives begin with #
#include <stdio.h>
Include Standard I/O Library header file (.h file)
#include <math.h>
Include Standard Math Library header file (.h file)
#define PI 3.141593
Define the constant PI
#INCLUDE DIRECTIVE
#include directive is used to include other
source files into your source file.
The #include directive gives a program access
to a standard library.
StandardLibraries contains useful functions and
symbols that are predefined by the C language.
You must include <stdio.h> if you want to use the
printf and scanf library functions.
stdio.h is called a header file (.h file). It contains
information about standard input and output functions
that are inserted into your program before compilation.
#DEFINE DIRECTIVE
The #define directive instructs the preprocessor
to replace each occurrence of a text by a particular
constant value before compilation.
#define replaces all occurrences of the text you
specify with the constant value you specify
#define NAME value
#define KMS_PER_MILES 1.609
#define PI 3.141593
THE main FUNCTION
int main(void) marks the beginning of the main
function where program execution begins.
Every C program has a main function.
Braces { and } mark the beginning and end of the body of
function main.
A function body has two parts:
Declarations - tell the compiler what memory cells are
needed in the function
Executable statements - (derived from the algorithm)
are translated into machine language and later
executed by the computer
RESERVED WORDS
A word that has special meaning to C and can not be
used for other purposes.
These are words that C reserves for its own uses
Built-in Types: int, double, char, void, etc.
Control flow: if, else, for, while, return, etc.
Always lower case
STANDARD IDENTIFIERS
Identifier - A name given to a variable or a function
Standard Identifier - An identifier defined in a
standard C library and has special meaning in C.
Examples: printf, scanf
Standard identifiers are not reserved words
is not recommended.
For example, if you define your own function printf,
KmsPerMile or Kms_Per_Mile
NEXT . . .
History of C
C Language Elements
Data Types and Variable Declarations
Executable Statements
Input and Output Functions
General form of a C program
Arithmetic Expressions
Formatting Numbers in Program Output
DATA TYPES
Data Types: a set of values and a set of operations
that can be performed on those values
int: Stores signed integer values: whole numbers
Examples: 65, -12345
double: Stores real numbers that use a decimal point
Examples: 3.14159 or 1.23e5 (which equals 123000.0)
char: Stores character values
Each char value is enclosed in single quotes: 'A', '*'
Can be a letter, digit, or special character symbol
Arithmetic operations (+, -, *, /) and compare can be
performed on int and double variables. Compare
operations can be performed on char variables.
INTEGER AND FLOATING-POINT DATA TYPES
Integer Types in C
Type Size in Memory Range
short 2 bytes = 16 bits -32768 to +32767
unsigned short 2 bytes = 16 bits 0 to 65535
int 4 bytes = 32 bits -2147483648 to +2147483647
unsigned int 4 bytes = 32 bits 0 to 4294967295
long 4 bytes = 32 bits Same as int
long long 8 bytes = 64 bits -9×1018 to +9×1018
Floating-Point Types in C
Type Size in Memory Approximate Range Significant Digits
float 4 bytes = 32 bits 10-38 to 10+38 6
double 8 bytes = 64 bits 10-308 to 10+308 15
CHARACTERS AND ASCII CODE
Character Type in C
Type Size in Memory ASCII Codes
char 1 byte = 8 bits 0 to 255
return statement
variable = expression;
scanf("%c%c%c", Letters
C a r
Entered
&letter1, letter1
C
&letter2,
letter2
&letter3); a
letter3
r
RETURN STATEMENT
Syntax: return expression ;
Example: return (0);
Returningfrom the main function terminates the
program and transfers control back to the
operating system. Value returned is 0.
Thereturn statement transfers control from a
function back to the caller.
Once you start writing your own functions, you
will use the return statement to return the
result of a function back to the caller.
NEXT . . .
History of C
C Language Elements
Data Types and Variable Declarations
Executable Statements
Input and Output Functions
General form of a C program
Arithmetic Expressions
Formatting Numbers in Program Output
GENERAL FORM OF A C PROGRAM
Preprocessordirectives
modify the text of a C
program before compilation.
Everyvariable has to be
declared before using it.
Executable
statements are translated into machine
language and eventually executed.
Executablestatements perform computations on
the declared variables or input/output operations.
COMMENTS
Comments making it easier for us to understand the program,
but are ignored by the C compiler.
Two forms of comments:
/* C comment */ anything between /* and */ is
considered a comment, even if it spans on multiple lines.
// C++ comment anything after // is considered a
comment until the end of the line.
Comments are used to create Program Documentation
Help others read and understand the program.
The start of the program should consist of a comment that
includes programmer’s name, date, current version, and a
brief description of what the program does.
Always Comment your Code!
PROGRAMMING STYLE
Why we need to follow conventions?
A program that looks good is easier to read and
sum + 1
(a + b) * (c – d)
(-b + sqrt(delta))/(2.0 * a)
ARITHMETIC OPERATORS
Operator Meaning Examples
5 + 2 is 7
+ addition 5.0 + 2.0 is 7.0
'B' + 1 is 'C'
5 – 2 is 3
– subtraction 5.0 – 2.0 is 3.0
'B' – 1 is 'A'
5 * 2 is 10
* multiplication
5.0 * 2.0 is 10.0
5 / 2 is 2
/ division
5.0 / 2.0 is 2.5
% remainder 5 % 2 is 1
OPERATORS / AND %
Example Result Explanation
8 / 5 1 Integer operands integer result
8.0/5.0 1.6 floating-point operands and result
8 /-5 -1 One operand is negative negative result
-8 /-5 1 Both operands are negative positive result
8 % 5 3 Integer remainder of dividing 8 by 5
8 %-5 3 Positive dividend positive remainder
-8 % 5 -3 Negative dividend Negative remainder
int main(void)
{
int total; /* total score */
int students; /* number of students */
double average; /* average score */
Evaluation
Tree
SUPERMARKET COIN PROCESSOR
SUPERMARKET COIN PROCESSOR (CONT'D)
FORMATTING INTEGERS IN PROGRAM OUTPUT
You can specify how printf will display integers
For integers, use %nd
% start of placeholder
n is the optional field width = number of columns to display