Chapter 02
Chapter 02
#include <stdio.h>
int main ()
{
printf(“Welcome to CS 1621!\n”);
}
Outline
II. Program Basics
A. Program skeleton
preprocessor directives
global declarations
functions
local declarations
statements
B. Comments and Documentation
C. Names (identifiers)
reserved words
Outline (cont)
II. Program Basics (cont)
D. Variable declarations
1. Memory allocation
2. Atomic types
void, int, float, char
E. Constants
1. literal
2. defined
3. memory
Outline (cont)
II. Program Basics (cont)
F. Formatted input/output
1. Files
2. Printf (monitor output)
a. format strings
field specifications
b. data list
3. Scanf (keyboard input)
a. format strings
b. address list
4. Prompting for Input
History of C
1960: ALGOL (ALGOrithmic Language)
1967: BCPL (Basic Combined Programming
Language)
1970: B programming language (typeless)
1972: C: BCPL plus B with types
1978: Kernighan + Ritchie standard for C
1989: ANSI standard for C
C Program Structure
P re p ro c e s s o r D ire c tiv e s
• Program defined by:
– global declarations
– function definitions
G l o b a l D e c la ra ti o n s
F u n c tio n D e fi n i ti o n s
• May contain preprocessor
in t ma in ( ) {
directives
• Always has one function
L o c a l D e c la ra tio n s
#include <stdio.h> P r e p r o c e s s o r D ir e c t iv e
int x; G lo b a l D e c la r a t io n
int main () {
int y; L o c a l D e c la r a tio n
F u n c tio n
printf("Enter x and y: ");
scanf(&x,&y); S ta te m e n ts
printf("Sum is %d\n",x+y);
}
Preprocessor Directives
• Begin with #
• Instruct compiler to perform some
transformation to file before compiling
• Example: #include <stdio.h>
– add the header file stdio.h to this file
– .h for header file
– stdio.h defines useful input/output functions
Declarations
• Global
– visible throughout program
– describes data used throughout program
• Local
– visible within function
– describes data used only in function
Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later
Main Function
• Every program has one function main
• Header for main: int main ()
• Program is the sequence of statements
between the { } following main
• Statements are executed one at a time from
the one immediately following to main to
the one before the }
Comments
• Text between /* and */
• Used to “document” the code for the human
reader
• Ignored by compiler (not part of program)
• Have to be careful
– comments may cover multiple lines
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
Comment Example
#include <stdio.h>
s h o rt in t 2 16 -3 2 7 6 8 32767
in t 4 32 -2 1 4 7 4 8 3 6 4 8 2 1 4 7 4 8 3 6 4 7
lo n g in t 4 32 -2 1 4 7 4 8 3 6 4 8 2 1 4 7 4 8 3 6 4 7
Why Limited?
• With a fixed number of bits, only a certain
number of possible patterns
• 16 bits, 65,536 possible patterns
– 32768 negative numbers
– 1 zero
– 32767 positive numbers
• Overflow: attempt to store a value to large
in a variable (40000 in short int)
Two‟s Complement
Integers:
positive number: 0, number in binary
97 in binary 1*64 + 1*32 + 1*1 (1100001)
pad with leading zeroes (0 00000001100001) - 16 bits
zero: 0, all zeroes
negative number: 1, (inverse of number + 1)
-97 (1, 111111110011110 + 1)
1 111111110011111
Unsigned Integers
• Type: unsigned int
• No negative values
• unsigned int:
– possible values: 0 to 65536
• Representation: binary number
Integer Literal Constants
Syntax:
1 or more digits
Optional leading sign (+ or -)
Optional l or L at the end for long
Optional u or U for unsigned
Examples:
5, -35, 401, 4010L, -350L, 2000UL
Floating-Point Type
• Type names:
– float
– double
– long double
• Possible values: floating point numbers, 5.0
-3.5, 4.01
• Operations: arithmetic (addition,
subtraction, multiplication, …), and others
Floating-Point Representation
• float: 4 bytes, 32 bits
• double: 8 bytes, 64 bits
• long double: 10 bytes, 80 bits
• Representation:
– magnitude (some number of bits) plus exponent
(remainder of bits)
– 3.26 * 10^4 for 32600.0
Floating-Point Limitations
• Maximum, minimum exponents
– maximum possible value (largest positive
magnitude, largest positive exponent)
– minimum value (largest negative magnitude,
largest positive exponent)
– can have overflow, and underflow
• Magnitude limited
– cannot differentiate between values such as
1.00000000 and 1.00000001
Floating-Point Literals
• Syntax:
– Zero or more digits, decimal point, then zero or
more digits (at least one digit)
– Whole numbers also treated as float
– Optional sign at start
– Can be followed by e and whole number (to
represent exponent)
– f or F at end for float
– l or L at end for long double
• Examples: 5, .5, 0.5, -1.0, 2.1e+3, 5.1f
Character Type
• Type name: char
• Possible values: keys that can be typed at
the keyboard
• Representation: each character assigned a
value (ASCII values), 8 bits
– A - binary number 65
– a - binary number 97
– b - binary number 98
– 2 - binary number 50
Character Literals
• Single key stroke between quote char „
• Examples: „A‟, „a‟, „b‟, „1‟, „@‟
• Some special chars:
– „\0‟ - null char
– „\t‟ - tab char
– „\n‟ - newline char
– „\‟‟ - single quote char
– „\\‟ - backslash char
String Literals
• No string type (more later)
• Contained between double quote chars (“)
• Examples:
“” - null string
“A string”
“String with newline \n char in it”
“String with a double quote \” in it”
Constants
• Literal constants - tokens representing
values from type
• Defined constants
– syntax: #define Name Value
– preprocessor command, Name replaced by
Value in program
– example: #define MAX_NUMBER 100
Constants (cont)
• Memory constants
– declared similar to variables, type and name
– const added before declaration
– Example: const float PI = 3.14159;
– Can be used as a variable, but one that cannot
be changed
– Since the value cannot be changed, it must be
initialized
Formatted Input/Output
• Input comes from files
• Output sent to files
• Other objects treated like files:
– keyboard - standard input file (stdin)
– monitor - standard output file (stdout)
• Generally send/retrieve characters to/from
files
Formatted Output
• Command: printf - print formatted
• Syntax: printf(Format String, Data List);
– Format string any legal string
– Characters sent (in order) to screen
• Ex.: printf(“Welcome to\nCS 1621!\n”);
causes
Welcome to
CS 1621!
to appear on monitor
Formatted Output (cont)
• Successive printf commands cause output to
be added to previous output
• Ex.
printf(“Hi, how “);
printf(“is it going\nin 1621?”);
prints
Hi, how is it going
in 1621?
To the monitor
Field Specifications
• Format string may contain one or more field
specifications
– Syntax: %[Flag][Width][Prec][Size]Code
– Codes:
• c - data printed as character
• d - data printed as integer
• f - data printed as floating-point value
– For each field specification, have one data
value after format string, separated by commas
Field Specification Example
printf(“%c %d %f\n”,‟A‟,35,4.5);
produces
A 35 4.50000
(varies on different computers)
4.056 56
-543 stored in x, A stored in c, 4.056 stored in
y, space and 56 still waiting (for next scanf)
Prompting for Input
• Using output statements to inform the user
what information is needed:
printf(“Enter an integer: “);
scanf(“%d”,&intToRead);
• Output statement provides a cue to the user:
Enter an integer: user types here