Parts of A C Program: Void Main (Variable Declaration Body of Program)
Parts of A C Program: Void Main (Variable Declaration Body of Program)
<Header files>
void main( )
{
Variable Declaration
Body of program
}
Parts of a C Program
Header files - are other programs you would like to
include in your program. Ex: stdio.h which holds
functions for printing.
void main( ) - a function where all execution
begins.
Variable Declaration - this is where you specify all
variables needed in your program
Body of program - this is where you specify your
commands.
The printf() Function
The printf() function simply prints out a
result/string to the screen.
printf(State here what you want to print);
Anything inside the quotation mark will be
printed by the printf() function.
The printf() Function
The printf() function prints its arguments
continuously across the computer screen.
#include <stdio.h>
main ()
{
printf (Introduction to );
printf ( C-Language\n);
}
The printf() Function
The following program segment will print
the stringIntroduction to and the string C-
Language on separate lines:
printf (Introduction to\n);
printf (C-Language\n);
printf (Introduction to\n C-Language\n);
Escape Sequence
An escape sequence is a special type of
character that performs a special task
when used inside a printf() function.
The printf() Function
EXERCISE 1
Create a program that will print your
names in two separate lines
Create a program that will output the
following:
Technological
University of the Philippines
Ayala Blvd.
Ermita, Manila
Data Types and Use of Variables
What is a data type?
A data type is a classification of data.
Data Types and Use of Variables
int
Variables of type int can hold integer
quantities that do not require fractional.
-32,768 to 32,767
float
Variables of type float are for
numbers with a fractional component
3.4 x 10
-38
to 3.4 x 10
38
.
Data Types and Use of Variables
double
Variables of type double are similar to
float variables but can hold a wider range
of numbers.
1.7 x 10
-308
to 1.7 x 10
308
char
Variables of type char hold 8-bit ASCII
characters such as A, B, C,
More on printf()
In order for the value of variables to be
printed, a format specifier is used.
Example:
printf(%d,age); //prints the value of age
printf(%f,salary); //prints the value of salary
printf(%c,sex); //prints the value of sex
Naming Conventions for Identifiers
Recall the following from flowcharting :
Do not use reserved words as your
variable names.
Name your variables close to its
functionality.
Always start with a letter.
Naming Conventions for Identifiers
In addition, you may include these
naming conventions:
If the name of the is long, you may
remove some characters but make sure its
still readable.
Try to name your identifiers as simple
as possible.
Naming Conventions for Identifiers
Example:
Birthday ==> Bday
Company_Yearly_Income ==>
cmpny_yrly_inc
Naming Conventions for Identifiers
The first character must always be a
letter or an underscore ( _ ) followed by
characters being letters, numbers, or
underscores.
C is case sensitive, meaning uppercase
letters are different from lowercase letters
Naming Conventions for Identifiers
Sample valid identifiers:
tax_rate
final_grade
_counter
Sample invalid identifiers:
1st_data
high+low
last*temperature
Naming Conventions for Identifiers
The following are examples of keywords in
C that should never be used as identifiers:
Naming Conventions for Identifiers
Try to give better names for these identifiers:
1) Gross income
2) Accounts Payable
3) Number of stocks
4) Employee Name
5) Total Annual Income
EXERCISE 2
Defining Variables
<DATA TYPE> VARIABLE;
To declare a variable, we must know what
data type must be used and an appropriate
variable name.
This is the syntax for variable declaration
Example:
int year;
float Weight;
char Name;
Defining Variables
Case 1
Assume that you are to create a program that
lets the user enter the age of a person and
compute its age 5 years from now.
EXERCISE 3
Case 2
Lets assume that you are to make a program
that will allow the user to compute for their
yearly salary by first entering the monthly
salary.
Defining Variables
Case 3
Assume that you are asked to create a
program that will let the user enter its name.
When asked by a question Whats your
name?, we dont say My name is 1234.
EXERCISE 3