C Programming
C Programming
DEFINITION OF A COMPUTER
A computer is a fast electronic calculating machine that accepts
input information, processes it according to a list of internally stored
instructions called a program, and produces the resultant output
information.
DEFINITION OF AN ALGORITHM
•A sequence of well-understood steps that one takes to do something.
•A set of instructions for a person to follow in order to accomplish a certain
task.
•A sequence of instructions that tell how to solve a particular problem.
Examples:
1. directions for going to a certain place
2. baking a chocolate cake
3. computing income tax
4. searching for a name in a telephone directory
CHARACTERISTICS OF AN ALGORITHM
1. Machine Language
The natural or primitive language that the computer actually
understands. This programming language consists of 0’s and 1’s
which makes programming very difficult.
MOV AX, 05
MOV BX, 03
ADC AX, BX
LET A = 5
LET B = 3
LET C = A + B
COMPARISON BETWEEN LOW-LEVEL AND HIGH-LEVEL
LANGUAGES
C is a “middle-level” language.
C-language is easier to use compared to machine or assembly
language and it is more efficient than other high-level languages.
OTHER FEATURES OF C
This is the ability of a language to section off and hide from the rest
of the program all information and instructions necessary to perform a
specific task.
#include <stdio.h>
main ()
{
printf (“Introduction to C-Language\n”);
}
The important parts of the program above are:
1. #include <stdio.h>
This is a preprocessor directive (or compiler directive). All
preprocessor directives start with a # symbol. This particular
directive tells the compiler to include or to insert a copy of the
header file stdio.h at this point of the program. The stdio.h file
(standard input-output) tells C how to perform or execute input
and output functions.
2. main ()
main() is the name of a function. Every C program has a main()
function where execution begins. The parentheses following main
indicate that it is a function. All function names have a ().
3. { and }
The left brace indicates the start of a function while a right brace
denotes the end of a function. In other word, braces group
statements or instructions together.
4. printf()
printf() is one of the several built-in functions of C that programmers can
use in their programs. This purpose of this function is to print something
on the computer screen. C has a large collection or library of built-in
functions. The stdio.h file contains information needed in executing this
function.
5. “Introduction to C-Language\n”
A string constant in C is a series of characters surrounded by double
quotes. This string is an argument to the function printf(). In other
words, it tells the printf() function what to print on the computer screen.
The two characters \n at the end of the string (read “backslash n”)
represents a single character called newline. It is a nonprinting character
that tells C to advance the cursor on the screen to a new line.
Arguments are values that are passed to a function at the time it is called.
Some functions may have several arguments (commas separate multiple
arguments). However, there are some functions that do not require
arguments (like the main() function).
6. printf (“Introduction to C-Language\n”);
#include <stdio.h>
main ()
{
printf (“Introduction to ”);
printf ( “C-Language\n”);
}
The following program segment will print the string Introduction to
and the string C-Language on separate lines:
Sample program:
#include <stdio.h>
main()
{
int gross, net, cost;
gross = 100;
cost = 10;
net = gross - cost;
printf(“If the gross income is %d\n”, gross);
printf(“ and the cost of materials is %d,\n”, cost);
printf(“ then the net income is %d.\n”, net);
}
The important parts of the program above are:
1. # include <stdio.h>
main()
{
int gross, net, cost;
The first line within the body of function main() is the declaration.
This is the part of the program that declares where all variables of the
function main(). This program has three integer variables. The variable
names are gross, net, and cost. Integer variables can have integer values
(whole numbers). Like all statements, declarations end with a semicolon.
The value of the expression on the right side of the equal sign
(=) is assigned to the variable on the left side. The equal sign is the
basic assignment operator in C.
3. printf(“If the gross income is %d\n”, gross);
preprocessing directives
main()
{
declarations
statements
}
1. Addition (+)
Example: sum = num1 + num2;
2. Subtraction (-)
Example: diff = num – 10
3. Multiplication (*)
Example: product = 20 * num
4. Division (/)
Example: quotient = num / 43
1. int
Variables of type int can hold integer quantities that do not require
fractional components such as 0, 12, 135, and -24. int variables can usually hold
integer numbers between -32,768 to 32,767. Typical memory requirement is 2
bytes.
2. float
Variables of type float are for numbers with a fractional component
(floating-point numbers) or for numbers with very large values such as 12.34,
-0.5, 9,456,776, and 87.45 x 1023. float variables can usually hold real numbers
between 3.4 x 10-38 to 3.4 x 1038. Typical memory requirement is 4 bytes.
3. double
Variables of type double are similar to float variables but can hold a
wider range of numbers. double variables can have numbers whose values range
from 1.7 x 10-308 to 1.7 x 10308. Typical memory requirement is8 bytes.
4. char
Variables of type char hold 8-bit ASCII characters such as ‘A’, ‘B’, ‘C’, or
any other 8-bit quantity. Typical memory requirement is 1 bytes.
MORE ON THE CONVERSION OR FORMAT
SPECIFIERS
Some of the conversion specifiers of C are:
1. # include <stdio.h>
main()
{
char sample;
sample = ‘M’;
printf (“The letter is %c.”, sample);
}
The output of this program is:
The letter is M.
2. #include <stdio.h>
main()
{
float x, y, sum;
x = 1.5;
y = 2.0;
sum = x + y;
printf(“The sum of x and y is %f.\n”, sum);
}
Example:
printf (“%c%3c%7c\n”, ‘A’, ‘B’, ‘C’);
space
Example:
printf (“%.1f %.2f %.3f\n”, 1.0, 2.0, 3.0);
printf (“%7.1f%7.2f%7.3f\n”, 4.0, 5.0, 6.0);
Example:
The program:
#include <stdio.h>
main()
{
int x = 10, y = 20;
printf (“x is %d and y is %d, x, y);
}
is equivalent to:
#include <stdio.h>
main()
{
int x, y;
x = 10;
y = 20;
printf (“x is %d and y is %d, x, y);
}
THE scanf() FUNCTION
This statement causes the program to pause and wait for the
user to input data at the keyboard. Once the user types in a number,
that number is then stored or placed in variable num.
Sample Output:
Enter the no. of dollars you want to convert : 3.55
$ 3.55 is equal to 95.67 pesos.
2. Program to compute the area of a circle
#include <stdio.h>
main()
{
float radius, area, pi = 3.14;
printf (“Enter the radius of the circle: “);
scanf (“%f”, &radius);
area = pi * radius * radius;
printf (“\n\nThe area of the circle is %.2f.”, area);
}
Sample Output:
Enter the radius of the circle: 2.0
The area of the circle is 12.56.
3. Program to prompt the user to enter his initials and age
#include <stdio.h>
main()
{
char first, middle, last;
int age;
printf (“Enter your three initials and age: “);
scanf (“%c%c%c%d”, &first, &middle, &last, &age);
printf (“\n\nGreetings %c.%c.%c. Next year your age will
be %d.”, first, middle, last, age + 1);
}
Sample Output:
Enter your three initials and age: MMA21
Greetings M.M.A. Next year your age will be 22.
SYMBOLIC CONSTANTS (THE #define DIRECTIVE)
Example:
The gravitational attractive force between two bodies of mass m1
and m2 separated by distance d is given by:
F = G m1 m2
d2
Write a C program that will read in the mass of two objects and the
distance between them and will then output the gravitational force between
them.
Solution:
# include <stdio.h>
#define GRAVITY 6.673e-8
main()
{
float mass1, mass2, distance, force;
printf ("Enter the mass of the first object > ");
scanf ("%f", &mass1);
printf ("Enter the mass of the second object > ");
scanf ("%f", &mass2);
printf ("Enter the distance between the two > ");
scanf ("%f", &distance);
force = GRAVITY * mass1 * mass2 / (distance * distance);
printf ("\n\nThe Gravitational Attractive Force between the two objects is
%e.",
force);
}
C-LANGUAGE CHARACTER SET
++ age;
Operators Associativity
1. 2 * 5 + 5 * 3 + 4 * 10
= 10 + 5 * 3 + 4 * 10
= 10 + 15 + 4 * 10
= 10 + 15 + 40
= 25 + 40
= 65
2. -8*5/4*2
= - 40 / 4 * 2
= - 10 * 2
= - 20
3. 4%2+3-4*5
= 0+3-4*5
= 0 + 3 - 20
= - 17
4. x * y - z / 2 where x = 5, y = 4 and z = 12
= 5 * 4 - 12 /2
= 20 - 12 / 2
= 20 - 6
= 14
5. 7 - - a * ++b where a = 2 and b = 4
= 7 - (-2) * ++b
= 7 - (-2) * (5)
= 7 - (-10)
= 7 + 10
= 17
6. ++u * v - w -- where u = 1, v = 2, and
w=3
= ++u * v - 3
= 2*v-3
= 2*2-3
= 4-3
= 1
Examples:
1. 5+3*4+2
= 5 + 12 + 2 = 17 + 2 = 19
2. 5 + 3 * (4 + 2)
= 5+3*6 = 5 + 18 = 23
3. (5 + 3) * (4 + 2)
= 8 * (4 + 2) = 8*6 = 48
PERFORMING ARITHMETIC OPERATIONS ON DIFFERENT DATA
TYPES
gets the value of x and y, add them together, then store the
result in x.