Escolar Documentos
Profissional Documentos
Cultura Documentos
FUNDAMENTALS OF C
LEARNING OUTCOMES
After studying this chapter, you should be
able to:
Describe what is data in C programs;
Write variable declarations and
initialization;
Trace a program with multiple expressions;
and
Apply the fundamentals of C in a working
program.
INTRODUCTION
As discussed in Chapter 1, as a programmer,
you need to analyze what a programming
problem requires first before you got to type
out the programming codes.
printf(“Hello World!\n”);
system (“pause”);
return 0; to mark the end of main()
} main()
STRUCTURE OF A C PROGRAM (cont.)
The structure
includes:
preprocessor
directives
global declarations
local definitions
statements
Statements in main()
system("pause"); program
return 0;
Will cause the black DOS
} window to be paused until
you press any key (to avoid it
immediately close not giving
you the chance to read what
{ } indicates To mark is printed)
the body of the end of
main() program
program
DATA AND MEMORY CONCEPTS
Variable names such as num1, num2 and
total actually correspond to locations in the
computer’s memory.
Every variable has a name, a type and a
value.
Example:
scanf(“%d”, &num1); //read an integer from keyboard
DATA AND MEMORY CONCEPTS (cont.)
Whenever a value is placed in a memory
location, the value replaces other value
that the location previously had.
int num1 = 20;
scanf(“%d”, &num1); // assume user key in 42
Hence the memory location for num1 will
be:
IDENTIFIERS
Identifiers are names that you give to data like
variables, constants, functions, types and labels in
your program.
Example 2.1
Mr. M needs to write a C program that will calculate the area of a rectangle. Each
rectangle will have its own length and width. Mr. M will use the formula:
Area = width * length
The term variable is used because the value stored in the variable
is not fixed (can change).
VARIABLES DECLARATION
A general form of writing a declaration statement is as
follows:
dataType variableName;
Initialization is the term used when the first time a value is stored in a
variable, upon declaration.
CONSTANTS
How about when you have to deal with
fixed data values?
For example, government determined tax rate. The tax rate will be
used many times throughout a program. If the tax rate changes,
programmer will be in a trouble to repair the program by changing all
the values initialized in the program. This may cause problems or
errors later on.
CONSTANTS (cont.)
The statement to define the constant is
#define constantName value
It is a standard practice to use all capital
letters to define a constant.
CONSTANTS (cont.)
• You can also declare constant character or
string like this:
The first call to printf() produces a prompt message asking input from
user.
The scanf() function hold the computer temporarily while user type a
value. By hitting ENTER key, user is actually sending signal to computer to
proceed to the next operation.
MANAGING INPUT AND OUTPUT
(cont.)
COMMENTS
To describe your codes or to make it easier for other
programmers in understanding your codes later.
You can put comments anywhere in your programs (make
sure they are meaningful).
Use /* */ or // symbols.
ESCAPE SEQUENCES
A combination of backslash (\) and a
specific character will result in different
effects to the program.
Normal actions by the character will be
ignored once backslash is used.
EXERCISE 2.3
(page 49-53)
SUMMARY
At this point, you have learned the
Fundamentals of C Programming. The
following are discussed earlier in this
chapter: