Vous êtes sur la page 1sur 9

Chapter TWO

Identifiers, Operators and I/O Statements


According to Albert Einstein:
All data objects that a program manipulate to produce information must be
represented by some names. C just as other programming languages uses
variable names to represent data in program. Variable names used to
indicate that the values of these data changes due to the actions performed
by the program. Aside from variables there are also constant data that are
used. Some data are declared constants just to make sure that their values
will never be altered in the program. In this chapter you will see both
variables and constants and how they are used in C.
Variable names
Constant names

Data Objects

Fig. 2.1. Data Objects


Variable and constant names are important so that data objects may be
accessed and identified properly in a computer program. All objects on a
computer not just data though are represented by names. These include
programs, devices and files. Naming all objects is done through the use of
identifiers. Variable names and constant data names are therefore identifiers.
Valid Identifiers for User Data
char_1, your_age, member_name, p6
are few example of valid identifiers that can be use to call data objects.
As a general rule identifiers is composed of:

meaningful names that starts with an alphabet;


lower case characters;
numbers may be used within the variable name e.g. student_6;
the _ may be used to come up with more meaningful names

Invalid Identifier for User Data


3My_Address, Student-10, printf
An Identifier cannot begin with a number

No -, +, /, * sign must appear in an identifier because these are


mathematical operators.
Reserve and library identifiers are invalid user defined names

Reserved Words and Identifiers


The C language and other high-level programming languages are composed
of reserved words or vocabulary of the language. Reserved words have
special meaning when they appear in a computer program.
Examples of these reserved words are:
int, char, return, main
These words should never be used as user as user defines constant and
variable names because the compiler will always look at them to mean their
original intended meanings and not as identifiers to data.
There are Identifiers that are also defined in some standard libraries in C.
Such as printf and scanf. These must not be also used as names for
constants and variables.
Syntax
Syntax is the way every line of C program must be written. Any derivation
from the correct syntax of a C program line such:
wrong spelling of a reserve word
missing punctuation mark such as a semicolon ; at the end of a
statement
missing comma in between input of a printf or scanf
will lead an error message on the screen and C compiler will not continue its
translation of the program not until you fixed the problem.
#include Directive for Defining identifiers from Standard Libraries

syntax: #include <standard header files>


Example: #include <stdio.h>
#include <math.h>
#define Derivatives for Creating Constant Macros

Syntax: #define Name value


Example: #define Kilograms_per_Pound .454
#define PI 3.1416
#define Days_in_a_Year 365
Example Program:
Problem: Convert your age into number of days

Flowchart:

Start

Set Constant
No. of days in a year

Enter Name,
Age in Years

Compute
corresponding
Age in Days

Output result

End
/*This program asked
you of your name and age in years
*then convert it into number of days, assuming that there are
*365 days in one year
*/
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#define Days_in_a_Year 365

/*where printf and scanf are defined*/


/*conversion factor*/

int main()
{
char char_1, char_2, char_3; /*use to enter three character name*/
int age_in_years;
/*entered age in years*/
int age_in_days;
/*converted age in days*/
cout <<Enter your 3 letter short name: ;

/*enter nickname*/

cin>>char_1>>char_2>>char_3; /*thru keyboard*/


cout<< Enter your age in years: ;
cin>> age_in_years ;
age_in_days = age_in_years * Days_in_a_Year;
cout<< char_1<<char_2<< << char_3<< you are now approximately
<<age_in_days<< days old.\n;
getch();
return(0);
}

Running the program gives you the following results:


Enter your 3 letter short name: Lan
Enter your age in years: 38
Lan you are now approximately 13870 days
old.
Fig. 2.2 C program that Enters your name and age in years and converts it in
number of days
The statement:
cin>>char_1>>char_2>>char_3;
ask three character word and assign each word to char_1 , char_2 and char_3
varieble respectively. In the example execution L became the value of
char_1, a the value to char_2 and n the value of variable char_3.
The statement:
cout<< char_1<<char_2<< << char_3<< you are now
approximately <<age_in_days<< days old.\n;
is cout statement which taken in five different items to display on the screen:
1. %c%c%c you are approximately %d days old.\n
2. char_1
3. char_2
4. char_3
5.
age_in_days

memory

memory
machine language
age in days
program

machine language
age in days
program

age_in_years

38
age_in_days

age_in_days

13870

Fig. 2.3. (a) Memory before execution of program


(b) Memory after execution of program
Variable Data Types and Their Declaration in C Programs
Aside from constants whose values have to be the same in every program
execution there are data whose values could change in a program execution.
These data called variable data because their vales may be changed during
program execution.
The data definitions:
char char_1, char_2, char_3; /*use to enter a three character name*/
int year
/*entered age in years*/
int age_in_days
/*converted age in days*/

all of these data will change their values within program run. In data
definitions or data declaration each data is defined as to what type data they
are, char_1 is to type character and year is type integer.
Type
char
int
float

Description
Sample Values
Character data type
a, B, !
Integer numbers (whole numbers)
343, 100, -5
Float Precision numbers
3.1416, 12e+6

Assignment Statements
The assignment = operator or the assignment statement is used to store a
certain value into a variable name. The assignment statement:
age_in_days = year * days_in_a_year;
assigns the value of the computation into the variable name
age_in_days

Syntax Display for Assignment Statement


Syntax:
result = expressions
Example: sum = a+ b;

Table 2.1 Some Arithmetic Operators


Arithmetic Operator
Meaning
+
addition
subtraction
*
multiplication
/
division

Using scanf and printf Function


cin and cout are input and output functions that came from <iostream.h>
library file. From their name cin and scanf inputs data from the keyboard and
cout and printf outputs data on the screen of the computer. A function needs
input data or input parameters then process these data inside the function
and return some kind of a result.
scanf(%f,&pounds) makes the computer ask a floating point number and
then stores it inside the memory location named pounds.
%f is called an input format or in C parlance it is called a placeholder. A
placeholder is like the dress pattern a dress maker used to cut a dress
correctly.
%f is the placeholder used to enter floating point number or float data
type.
%d is the placeholder used to enter integer or whole numbers data type.
%c is the placeholder to enter a single character.
%c%c%c is the placeholder to enter three characters.
Syntax Display for scanf Function call
scanf(format, input list);
EXAMPLE:

scanf(%c%c%c%d, &char_1,&char_2,char_3, & age_in_years);


format

input list

Syntax Display for print Function call


Syntax: printf(format, print list);
EXAMPLE: printf(Hello %c%c%c you are %d days
old!/\n, &char1,&char2,&char3, & age_in_years);
INTERPRETATION: The printf function displays on the
computer screen the value of the format string after
substituting the values of the placeholders from their
respective values in the print list.

INTERPRETATION:
The scan function copies into memory data typed at the keyboard during
program execution. The format is a quoted string of placeholders, one
placeholder for each variable in the input list. Input list is variables separated
by comma.
The \n at the end of the format will print a new line.
The \n terminator will print the next output on a new line on the screen.
Example:
printf(First output line.);
printf(Second output line.\n);

gives the result:


First
output line.Second output line.

Be careful not to include \ unintentionally in the output line or some


strange results come out on the screen.
Example:
printf(This is a sentence, \n of1 two sentences.);

gives the output:


This is a sentence,
of two sentences.

Review Questions and Exercises:


1. What pre-processor directive is used to change the text of C program
before it is compiled?
2. What are words that can not be used to name variables and constants in a
C program?
3. _____________ is the way every statement in C must be written.
4. Why is constant directives sometimes necessary in C programs?
5. Mark each variable valid and invalid:
a. my-salary
_______________
b. total absences _______________
c. a9class
_______________
d. Payrollfile
_______________
e. return
_______________
6. Show the output of the program lines below when the data entered from
the keyboard are 10 and 5.
printf(Please enter two integers:);
scanf(%d%d, &a, &b);
a = a + 10
b = a + b *2 5
print( a = %d\n b = %d\n, a, b);
7. Show the contents of the memory before and after the execution of price
is 50.
printf(I am);
printf(Juan Dela Cruz.\n);
printf(I came from Ozamis City. \nMy computer books);
printf(is %d pesos.\n, price);
printf(I hope to learn programming from it.\n);
printf(Of course I have to read the book hard. \n);
Programming Exercises:

1. Write a C program that will ask the radius of a circle in inch (es) and
then computes the area and circumference of the circle. The formulas
useful for this problem are:
area_circle = PI *radius *radius;
circumference = 2 * PI * radius;
where PI is a constant that has a value 3.14159

Vous aimerez peut-être aussi