Lecture #2
Lecture #2
BİL1102
Programlamaya Giriş
Ders #2
• Overview of C
• Variables
• Data types
• Assignment Statements
How Do I Prepare My Program?
• Preparing a program is converting the steps of an algorithm into the
programming language commands.
• The set of language statements should be typed, by using an editor and saved
in a file.
• Your high level program is called the Source Program and the file containing
your source code is called Source File.
• Most of the programming languages today, have their Program
Development Tools. These tools contain an editor for preparing (typing,
changing, saving, retrieving, etc.) the source code, in addition to many
other facilities that make life easy for a programmer.
Elements in a C program
Comments
/* You can write anything
descriptive here */
// single line comment
PreprocesorDirectives
#include <stdio.h> /* Standard I/O Library*/
#define PI 3.14 /* Conversion constant*/
Main Function Prototype
int main (void)
Variable declaration
double pounds; /* input -weight in pounds */
Statements
printf(”Hello World ");
Comments
• Comments are useful in programs for supplementing information about
the program code to the reader of the program. Since they are not for the
computer, they are ignored by the compiler
• C has two type of comments
1-Multi-line comments
/* write
here */
2-Single line comments
// write here
Preprocessor Directives
• A preprocessor directive has the sign # as the first non-blank character of
the line
• The include directive
✓ tells the preprocessor to include a special file together with your
source program
• Why we need to include other files?
• In order to input something typed from the keyboard or output something
to the screen, the necessary commands must be given. The people who
have written the C Compiler for us, have also written many programs to
make life easier for us.
Preprocessor Directive : include
• Syntax: #include < standard header file >
• #include <stdio.h>
• stdio.h is the header file for standard input and output. This is useful for
getting the input from the user (Keyboard) and output result text to the
monitor (screen)
• #include <math.h>
• math.h is used to add mathematical functions such as power, abs etc.
Preprocessor Directive : define
• Syntax: #define NAME value
• The define directive is used to create names for values.
• It tells the preprocessor to replace every occurrence of the name in the source
code with the value it is representing.
• Only data values that never change during the execution of our program should
be given names through the define directive because a program cannot change
the value of a name defined as a constant.
• Example: #define PI 3.14159
Main function prototype
1-13
Variables
• The memory of the computer can be considered to be made up of little boxes.
• Each of these boxes can store a value in it.
• When we tell the computer to read an input (this may be a number or a
character), we must also tell the computer where to store this input, so that it
can be used later.
• Similarly, when a result has been calculated, the computer needs to know
where to put this result, i.e., which box in memory to use.
1-14
Variables
• A name must be given to each of these memory locations in order to be able
to refer to them.
• Also, their content must be defined so that the compiler will know the
amount of memory to allocate for them.
• These memory locations are called variables because their content may
change during execution of our program.
• A memory location which we will use to put (store) a value in is called a
variable, and the name which we give to that box is called a variable name.
1-15
Reserved Words (Keywords) in C
1-17
Variable naming
• The variable name is chosen by us.
• Thus, we can choose any name we wish. However, we should pay attention to
use meaningful names, that reflect the value we will store in that variable.
• For example, the name salary will be a good name for a memory location that
will be used to store a person’s salary, whereas the name s is a bad choice.
1-18
Variable naming
1-19
Variable naming
1-20
Variable declarations
• We introduce variables that we are going to use with the help of declaration
statements.
• We will learn about data types and how to declare them in the next section.
• Examples:
1-21
Variable declarations (con’t)
meatballs
garbage
FE07 int
Exercise
• Which of the following identifiers are valid? Explain the reason for invalid
ones.
1-23
Which Are Legal Identifiers?
• AREA • num$
• area_under_the_curve • %done
• 3D • lucky***
• num45
• Last-Chance
• #values
• x_yt3
• pi
Statements
• The body of a program contains statements which are actually the commands
that we are going to give to our computer in order to perform the tasks that
we want.
• Examples:
scanf("%lf", £s);
1-25
26
Data types
• You have to declare all of the variables you need to use in your
program, before using them
• In a variable declaration, you must indicate the name of the
variable and the type of the data you will store in that variable
27
int type
• Integer numbers are whole numbers, such as –3, 0, 15.
• They do not contain a decimal point, and they may be positive or negative.
• A positive integer can be preceded or not with a plus sign. E.g., 15 and +15
are the same numbers. On the other hand, minus sign must be used
before a negative number. E.g., -15.
• An integer can not contain a comma, even if it contains too many digits.
E.g., 1,000,000 is wrong, it should be written as 1000000.
• Decimal points can not be used when writing integers. E.g., 15 and 15.0
have the same numeric value, but 15.0 is not of type integer (but double).
• If we want to declare a variable in which we will store an integer, e.g. year,
we declare it as int year;
• If we want to declare more than one integer variables, we can list them
separating with commas: int year, age, number;
1-28
double type or float type
• These type are used to store real numbers. A real number has an
integral part and a fractional part which are separated by a decimal
point, such as 3.0, 0.75, -2.47.
• A real number can be positive or negative, and the rules for + and –
signs are the same as for the integers. E.g., 0.75 = +0.75 -0.75.
• The zero before or after the decimal point can be omitted. Thus, 3.0
can also be written as 3. But do not forget the decimal point, otherwise
it becomes an integer.
• Notice that the arithmetic values of 3.0 and 3 are the same, but the
type of these two numbers are different: First one is double, second
one is int.
• Similarly, 0.75 can be written as .75 in C language.
• Float and double both have varying capacities when it comes to the
number of decimal digits they can hold. float can hold up to 7 decimal
digits accurately while double can hold up to 15
1-29
double type or float type
• We can use exponential notation, especially for very small or very large
real numbers. This notation is very similar to scientific notation in
arithmetics.
• You know from arithmetics that 6.5x102 = 650 and 0.00012 =
1.2x10-4. These values can be expressed as 6.5E+2 (or 65E+1) and
1.2E-4 (or 12E-5) respectively. This notation is referred as exponential
notation.
• Read the letter E as “times 10 to the power”. E.g.,
read 1.2E-4 as “1.2 times 10 to the power minus 4”.
• If we want to declare a variable to contain a real number, e.g. weight, we
declare it as double weight;
1-30
char type
• char type: This data type is used to store a single character
value, such as a letter, a digit, or a special symbol.
• A character value must be enclosed in single quotes (‘) in a
program,. E.g., ‘A’, ‘z’, ‘2’, ‘5’, ‘*’, ‘:’, ‘ ‘ (blank character).
• It is very important to understand the difference between 5
and ‘5’ appearing in a program. The first one is an integer, thus,
it has an arithmetic value, and it can be used in arithmetic
operations. On the other hand, the second one is a character,
and has no arithmetic value. It can not be used in arithmetic
operations.
• If we want to declare a variable to contain a character, e.g.
option, we will declare it as char option;
1-31
Constants
• A constant is a value that does not change during the execution of a program.
• You can use constant values in many different statements in your programs.
You can also give names to some constant values using #define preprocessor
directive, and use those names in the statements.
• Eg.:
• #define PI 3.14159
• #define MAX 1000
• #define BLANK ' ’
1-32
Integer Constants
• C has four types of constants:
• integer
• floating-point
• character
• Enumeration (out of scope)
• Integer constants: are positive and negative whole numbers with no fractional
part
• it must begin with a nonzero digit, (except zero), and can contain values 0
to 9
• If the sign is missing it is considered as a positive value
• Examples:
• -15 legal
• 0 legal
• +250 legal
• 0179 legal
• 1,756 illegal
1-33
Floating-point Constants
• One positive or negative decimal numbers with an integer part, a decimal point and a
fractional part.
• They can be represented either in conventional or scientific notation
• Example:
• 20.35
• 0.2035E+2 or 0.2035e+2 (E+2 means power of 2)
• 20.35 can be represented in many ways in C as
0.2035e2 or 2.035e1 or 20.35e0, 203.5e-1 etc.
1-34
Floating-point Constants
• If the sign is ommitted, the computer assumes a positive value
• 20.35 is the same as +20.35
• If decimal integer is omitted, a value of 0 is assumed for it
• .2 instead of 0.2 (both legal)
• .2035e2 instead of 0.2035e2 (both legal)
• In scientific notation for floating-point numbers, the decimal point may be omitted.
• -8e0 instead of -8.0e0 (both legal)
• Some illegal floating-point numbers
• .1234e (missing exponent)
• 15e0.3 (invalid exponent 0.3)
• 12.5e.3 (invalid exponent .3)
• 15,709.52 (comma is not allowed)
1-35
Character Constants
1-36
Punctuators
• The following table shows the special characters which are used as
punctuation symbols in C programs:
1-37
Assignment Statements
Assignment Statements
1-39
Assignment Statements
salary1 = 15.780;
salary2 = salary1;
• The variable on the left hand side and the value on the right
must match in data type. Real numbers must be assigned to
double variables, integers to integer variables, and characters
to character variables.
1-40
Assignment Statements
• What happens if you try to assign an integer value to a double variable? Since
any integer can be converted to a real number just by appending .0 at the end
of it, no problem will arise.
salary1 = 900;
1-41
Assignment Statements
• The visa-versa is not valid in all programming languages. That means, if you
try to assign a real value to an integer variable, it will be invalid in most of the
programming languages, such as Pascal.
• However, the C language allows such assignments, but it gives a warning
message, because it causes the fractional part of the real number to be lost,
since it can not be represented in an integer variable. Thus, only the integral
part of the real number will be stored in the variable. This is called as
truncation.
• Example: age = 18.5;
• Such an assignment puts the value 18 into the variable age in C.
1-42
Assignment Statements
• Assignments to a character variable require the character
value to be enclosed in single quotation marks.
Example: gender = ‘M’;
is invalid.
1-43
Example: Declarations and Assignments
#include <stdio.h>
int main( void )
{
int inches, feet, fathoms ;
fathoms = 7 ;
feet = 6 * fathoms ;
fathoms
inches = 12 * feet ; 7
•
• feet
• 42
inches
504