Fe1008 02
Fe1008 02
Fe1008 02
An Overview of C
Why is it called C?
It was created from B!
B was created from BCPL.
2-1 2-2
• A part of all C development environments. • One and only one main() function.
• Provides information on I/O operations (such • The parentheses after main enclose a definition
of what data is to be provided to main() when it
as printf here). starts executing. The word void means that no
• # sign : preprocessor directive. Tells the data needs to be provided to main().
preprocessor to do something before compiling. What is a C function?
• #include <stdio.h> tells preprocessor to • A collection of statements for doing a specific task.
insert the contents of the file stdio.h into our • A central concept of C programming.
program before compilation. 2-11 2-12
Dissection (continued): Dissection (continued):
int main(void)
/* This is a comment */
• A mathematical function has at least one
argument (the independent variable) whereas • This is for inserting comments.
a C function may have 0 or more arguments. • Computer will ignore all the comment
• void means the function has no argument. statements.
• A function may also return a value to its caller. • It is important to insert comments at suitable
places in the program to explain the purpose.
• Here main() returns an integer value to the This is part of program documentation.
operating system.
• int and void are C keywords.
2-13 2-14
2-15 2-16
Dissection (continued): Program 2.2
Declaration of Variables
The line: number1=20; number2 = 30; sum=number1
+number2;
int number1, number2, sum;
is called a declaration statement.
20 30 50
The purpose is to ask the computer to reserve
3 memory locations to store values. These number1 number2
locations are called number1, number2,
sum
sum and they will be used to store integer printf("The sum of %d and %d is %d.",
numbers (numbers without a fractional part, number1, number2, sum);
or without a decimal point). The statement
declares 3 integer variables. Output:
2-19 The sum of 20 and 30 is 50. 2-20
Conversion/Format Specifiers Program 2.3
printf("The sum of %d and %d is %d.", /* Calculate volume of a cylinder */
number1, number2, sum); #include <stdio.h>
New items
Program 2.4 (Continued)
2-31 2-32
Notes on Prog 2.5
Program 2.5 (quadratic equation)
if (a == 0) /* Is coefficient a zero? Note that the double equal sign is used for
Note the double equal sign */ checking equality of two values. This will be
discussed again in Chapter 6.
{
printf("You input a = 0.\n"); The two pairs of braces are needed for
printf("Only one root: %8.3f", -c/b); enclosing each of the blocks of statements
} belonging to the if and to the else.
else
{ root1 = (-b + sqrt (…))/(2*a);
. . .
} 2-33 2-34
2-37 2-38
2-39 2-40
Preprocessor Directives & Macros Preprocessor Directives & Macros
# indicates a preprocessor directive. The PI and GST defined above are also known
Example: #include <stdio.h> as symbolic constants and the names are
usually typed in capital letters.
There is another preprocessor directive:
General Form:
#define that is frequently used to define
constants. It is also said to define a macro. #define macro_name replacement_text
2-45 2-46
Parameterized Macros