Professional Documents
Culture Documents
Main Program in C
In C the main program is a function called main
The body of the function is enclosed in left (
) and right (
Chapter 2
K.N. King
Chapter 3
K.N. King
Chapter 4
K.N. King
Chapter 7
K.N. King
) curly braces.
In Standard C, the return type of main must be int. If we do not specify the
return type, it defaults to int. We might also be able to get away without
having a return statement.
Good Style: Specify int as the return type for main and always have an
explicit return statement!
There are 3 steps to get a C program from text to executable code:
preprocessing, compiling and linking
If you want to give it a try before the first tutorial, use the following command:
gcc first.c -o first
50
51
C Programming Language
/*******************************************
Name: first.c
Purpose: Prints a traditional C greeting.
Author: Iqbal Mohomed
Date: September 10th 2002
*******************************************/
#include <stdio.h>
int main()
{
/* Print Greeting */
printf("Hello World\n");
return 0;
}
52
53
Good Style: are easier to read, understand, modify and get correct.
54
55
( underscore )
Use
keyword(s)
constants
integers
real numbers
float , double
characters
char
a, A, 3, +
Mnemonic means that the identifier describes its purpose in the program, e.g.
Notation: the phrase type-name will be used to denote any valid type in C.
int , double and char are instances of type-name .
56
57
Integer Constants
Type
Digits
Starts with
Examples
decimal integer
0123456789
1..9
1 123L 456790U
Use short or char when saving space is really important AND ITS KNOWN
octal integer
01234567
01 0123 0456707U
that the range of values for the variable will fit in -32768 .. 32767.
hexadecimal integer
0x or 0X
values. Dont assume long is larger than int unless you check.
( 01238
8310 )
58
59
Type
Contains
Starts and
character
a single character
string
arbitrary characters
(single quote)
(double quote)
Examples
ends with
sizeof( long )
a @ 1 C
The type of a variable determines the kind of values that it can hold and the
The backslash (
WARNING: be careful to use character constants where a single character is required and
string constants where a string is required.
60
61
int I , J , K
Assignment Statement
Y=2;
is used. Variables are used when they occur in any form of expression.
int X = 1 ;
int M , N = 0
Examples
/* only initializes N. */
T=X+3;
if ( Z ) ...
Good Style: All variables should be initialized with some value before they
are used in a program.
a
As a matter of Good Technique: we ignore the default initialization of all variables to zero that
some C compilers do automatically. We think that relying on default initialization is a BAD HABIT that
should be avoided.
62
When completing the statement you must account for all possible ways that
Do this systematically line by line, starting at the first line in the program.
You must also consider the possibility that a value might not be assigned to a
variable due to conditional statements or an unchecked scanf.
int Y ;
int Z ;
...
...
...
if ( something )
X=1;
Y=2;
int X ;
This is why we suggest that every scanf statement must be checked to make sure it read values
64
65
Named Constants
b x
x2
0 */
double a, b, c ;
/* Coefficients */
/* Discriminant */
double x1, x2 ;
/* roots */
== 0.0 )
== 0.0 )
if ( a
Named constants are often used to provide a single point of definition for a
if ( b
Using named constant makes programs more easily modifiable and easier to
else
else
understand.
discsq = b * b - 4.0 * a * c ;
if ( discsq
>= 0.0 )
Named constants makes program more readable, use mnemonic name for
constant.
x2 = ( - b - disc ) / 2.0 * a ;
printf(Two real roots: %g and %g \n, x1 , x2 ) ;
else
x1 = - b / ( 2.0 * a ) ;
x2 = sqrt( - discsq ) / ( 2.0 * a );
printf(Two complex roots: %g+%gI and %g-%gI \n, x1 , x2 , x1 , x2 );
Use named constants for all values that have any significant impact on the
programs operation.
67
66
A named type is an type that has been associated with a specific identifier.
Named types are created using the typedef declaration.
Named types make program more easily modifiable, since there is a single
point of definition for the type.
Good Style: Use UPPER CASE names for defined constants to make them
If mnemonic names are used for the types, named types make programs
more readable.
Examples:
69
Typedef Declaration
A named type is created with the declaration
Builtin library functions are used for all reading and printing.
type-name can be any valid type including compound types or a new type
definition.
available.
The printf function does simple printing
portableInt I , J , A[ 100 ] ;
realType xAxis , yAxis , zAxis
stdio.h
at the start of every program to make the builtin input and output functions
#include
The description of printf and scanf below is intended to get you started. Reading and Printing will
70
71
Format Strings
A format string is used to specify how printf and scanf should operate.
For printf the format string specifes exactly how the printed output should look.
For scanf the format string specifes the exact form of the input that will be
Type
Specifier
int
%d or %i
char
%c
Strings
%s
double
%f or %e or %g printf
double
float
%f or %e or %g
short
%hd
long
%ld
read.
The format string consists of
conversion specifications a percent sign ( % ) followed by some optional
information, followed by a conversion specifier character that indicates the type of
data to be printed or read.
ordinary text Everything else. Printed as is by printf. Matched against the data
being read by scanfa
a
Note that printf and scanf use different specifiers for double .
Use %% to print a %. Use \n to print a newline.
72
73
Printing Technique
, expressionList a ) ;
The type of each expression must be compatible with the % item used in the
%10d
%16e
format-string
The printf prints to standard output.
By default each expression is printed using the minimum number of
There are Unix/Windows commands that let you redirect standard out to a file.
75
74
, variable-address-list a ) ;
Examples:
Any ordinary text in the format string must match the input exactly.
scanf attempts to read values for each of the variables in the order given.
The type of each variable must be compatible with the % item used in the
format-string
printf("%d",
printf("%c",
printf("%f",
printf("%e",
printf("%g",
i)
c)
x)
x)
y)
;
;
; /* decimal form
*/
; /* scientific form
*/
; /* decimal or scientific form */
76
77
scanf returns the number of variables that it successfully read and stored.
Returns special value EOF if an error or end of input was detected.
Use the address-of operator
arguments to scanf.
The address-of operator is almost always REQUIRED.
int i , k ;
char c ;
double y ;
scanf("%d", &i) ;
/* read
scanf("%c", &c) ;
/* read
scanf("%lf", &y) ; /* read
scanf("%d%d", &i, &j ); /*
We may sometimes not do this check in these slides in order to keep the examples simple,
78
79
one integer */
one character */
one double value */
read two integers */