Content:
Reading: Textbook Chapter 2
Lecture 2:
Comments and Program Documentation
Ways of coding constants in a program
Brief introduction of formatted I/O
Lecture 2
Powerpoint file 02 slide 3 (fig. 2-2)
Global declaration section:
Functions:
The first program (Program 2-1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* The greeting program. This
program demonstrates some of the components of a simple C program. Written by: (your name here) Date written: (today's date here) */ #include <stdio.h> int main (void) { /* Local Declarations */ /* Statements */ printf (“Hello World!\n”); return 0; } /* main */ |
There are no global and local declarations in this program.
Line 6 is the preprocessor directive which must always begin with a “#”.
Line 6 means to include the standard input-output header file (stdio.h) in the program, in order to output a message to the monitor.
Line 8 is the header of the function main, the only function in this program.
Int means the function will return an integer to the operating system, and (void) means no parameter will be passed to this function.
Lines 9 and 17 contain braces that show the beginning and the end of the function main.
Line 14 calls the function printf from the stdio.h library and passes the message to be displayed on screen to the function; the “\n" meaning to advance to the next line.
Line 16 terminates the program and returns control to the operating system. This line may be omitted.
Preprocessor directives
Powerpoint file 02 slide 4 (fig. 2-3)
Comments and Program Documentation
Powerpoint file 02 slide 5 (fig. 2-4)
The compiler ignores comments when it translates the program into machine language.
Comments start by “/*” and end by “*/” and can appear anywhere in the program.
Powerpoint file 02 slide 6 (fig. 2-5)
Lines 1-5, 10, 12, and 17 of program 2-1 are comments.
Rules for identifiers:
C is case sensitive: the same letter in uppercase and lowercase are treated as 2 different characters.
Names with all capital letters are often used for preprocessor-defined names.
Examples of valid names: a, a1, student_name, stdntNm, _aSystemName, TRUE.
Examples of invalid names: $sum, 2names, stdnt Nmbr, int.
Good identifier names are descriptive but short. To make them short, we often use abbreviations.
Standard data types in C:
each data type has its own internal format in C.
void
int
char
Examples: ASCII Character Integer Representation
'A' 65
'1' 49
float
Summary
void |
void |
character |
char |
integer |
unsigned short int unsigned int unsigned long int |
short int int long int |
|
float |
float double long double |
Lecture 3
Declaration and definition
Powerpoint file 02 slide 10 (fig. 2-9)
A variable is usually declared and defined at the same time.
Declaration gives variables a symbolic name and definition reserves memory for them.
A variable can be of types char, int, or float, but cannot be of type void.
To create a variable, specify the type and then its identifier. E.g. float price; (more examples below)
short int maxItems; /* Word seperator: Capital */ long int national_debt; /* Word separator: underscore */ float payRate; /* Word separator: Capital */ double tax; char code, kind; /* Poor style: multi-declarations in a line */ int a, b; /* Poor style */ |
Initialization
int count = 0; /* initialize count */ |
int count, sum = 0; /* initialize sum only ! */ |
int count = 0, sum = 0; /* initialize both */ |
int count = 0; /* preferred version */ int sum = 0; |
Integer constants
Literal |
Value |
Type |
+123 -378 -32271L 76542LU |
123 -378 -32,271 76,542 |
int int long int unsigned long int |
Float constants
Literal |
Value |
Type |
0. .0 2.0 3.1416 -2.0f 3.1415926536L |
0.0 0.0 2.0 3.1416 -2.0 3.1415926536 |
double double double double float long double |
Character constants
ASCII Character |
Symbolic Name |
Null character alert (bell) backspace horizontal tab new line vertical tab form feed carriage return single quote backslash |
‘\0’ ‘\a’ ‘\b’ ‘\t’ ‘\n’ ‘\v’ ‘\f’ ‘\r’ ‘\’’ ‘\\’ |
String constants
“” (null string)
“h”
“Hello World”
Ways of coding constants in a program
Literal constants
Defined constant
Memory constants
Brief introduction of formatted I/O
Powerpoint file 02 slide 13 (fig. 2-12)
printf
Code |
Meaning |
Argument Type |
Example |
c |
character |
char |
%c |
d |
integer |
int |
%d |
f |
floating point |
float double |
%f %f |
h used for short int: %hd
l used for long int: %ld
L used for long double: %Lf
e.g. %4d means at least 4 positions for the integer value.
e.g. %2hd means short integer with at least 2 positions.
e.g. %7.2f can print a maximum floating-point value of 9999.99.
e.g. %10.3Lf can print a maximum long double value of 999999.999.
- for left justify, e.g. %-8d.
0 used with width for leading zeros, e.g. %08d.
printf (“%d\t%c\t%5.1f\n”, 23, ‘Z’, 14.2); printf (“%d\t%c\t%5.1f\n”, 107, ‘A’, 53.6); printf (“%d\t%c\t%5.1f\n”, 1754, ‘F’, 122.0); printf (“%d\t%c\t%5.1f\n”, 3, ‘P’, 0.1);
|
23 Z 14.2 107 A 53.6 1754 F 122.0 3 P 0.1
|
printf (“The number%dis my favorite number.”, 23);
|
The number23is my favorite number.
|
printf(“The number is %6d”, 23);
|
printf(“The tax is %6.2f this year.”, 233.12);
|
The tax is 233.12 this year.
|
printf(“The tax is %8.2f this year.”, 233.12);
|
The tax is 233.12 this year.
|
printf(“The tax is %08.2f====this year.”, 233.12);
|
The tax is 00233.12= = = = this year.
|
printf(“\”%8c===%d\””, ‘h’, 23);
|
|
scanf
End of file reached;
Maximum number of characters (specified in field specification) reached;
A whitespace character (space, tab, or new-line) is found after a digit in a numeric specification;
An error is detected.
214 156 14Z
|
scanf(“%d%d%d%c”, &a, &b, &c, &d);
(note: no space before %c)
|
2314 15 2.14
|
scanf(“%d %d %f”, &a, &b, &c);
(note: the spaces in format list can be omitted)
|
14/26 25/66
|
scanf(“%2d/%2d %2d/%2d”, &num1, &den1,
&num2, &den2);
|
11-25-56
|
scanf(“%d-%d-%d”, &a, &b, &c);
|
- End of Lectures 2 and 3-