To reinforce the ideas discussed so far, let us write a program that adds the numbers 14 and 25 and prints the sum.
We would need storage locations for the two numbers and the sum. The values to be stored in these locations are integer values. To refer to these locations, we make up the names a, b, and sum, say. (Any other names would do. In C, as in all programming languages, there are rules to follow for making up variable names, for instance, a name must start with a letter and cannot contain spaces. We will see the C rules in the next chapter.)
One possible algorithm might look like this:
set a to 14 set b to 25 set sum to a + b print sum
The algorithm consists of four statements. The following explains the meaning of each statement:
• set a to 14: store the number 14 in memory location a; this is an example of an assignment statement.
• set b to 25: store the number 25 in memory location b.
• set sum to a + b: add the numbers in memory locations a and b and store the sum in location sum. The result is that 39 is stored in sum.
• print sum : print (on the screen) the value in sum, i.e., 39.
Program P1.4 shows how we can write this algorithm as a C program.
Program P1.4
//This program prints the sum of 14 and 25. It shows how //to declare variables in C and assign values to them.
#include <stdio.h>
int main() { int a, b, sum;
a = 14;
b = 25;
sum = a + b;
printf("%d + %d = %d\n", a, b, sum);
}
When run, this program will print the following:
14 + 25 = 39
In C, variables are declared as integer using the required word int. (In programming terminology, we say that int is a reserved word.) Thus, the statement
int a, b, sum;
declares that a, b, and sum are integer variables. In C, all variables must be declared before they are used in a program. Note that the variables are separated by commas, with a semicolon after the last one. If we need to declare just one variable (a, say), we will write
int a;
The statement a = 14;
is C’s way of writing the assignment statement set a to 14
It is sometimes pronounced “a becomes 14.” In C, an assignment statement consists of a variable (a in the example), followed by an equals sign (=), followed by the value to be assigned to the variable (14 in the example), followed by a semicolon. In general, the value can be a constant (like 14), a variable (like b), or an expression (like a + b). Thus,
set b to 25
is written as b = 25;
and
set sum to a + b is written as sum = a + b;
One final point: you may have gathered from a previous exercise that, for this problem, the variable sum is not really necessary. We could, for instance, have omitted sum from the program altogether and used this:
int a, b;
a = 14;
b = 25;
printf("%d + %d = %d\n", a, b, a + b);
to give the same result since C lets us use an expression (e.g., a + b) as an argument to printf. However, if the program were longer and we needed to use the sum in other places, it would be wise to calculate and store the sum once (in sum, say). Whenever the sum is needed, we use sum rather than recalculate a + b each time.
Now that we have a general idea of what is involved in writing a program, we are ready to get down to the nitty-gritty of C programming.
exerCises 1
1. What makes it possible to do such a variety of things on a computer?
2. Computers can execute instructions written in what language?
3. give two advantages of assembly language over machine language.
4. give two advantages of a high-level language over assembly language.
5. Describe two main tasks performed by a compiler.
6. Describe the steps required to solve a problem on a computer.
7. Distinguish between an algorithm and a program.
8. programming instructions fall into three main categories; what are they?
9. Distinguish between a syntax error and a logic error.
10. What is meant by “debugging a program”?
11. name five data types commonly used in programming and give examples of constants of each type.
12. What are the different classes into which characters can be divided? give examples in each class.
13. What is the purpose of comments in a program?
14. Write a program to print Welcome to C on the screen.
15. Write a program to print the following:
There is a tide in the affairs of men
Which, taken at the flood, leads on to fortune 16. Write a program to print any four lines of your favorite song or poem.
17. same as exercise 16, but print a blank line after each line.
18. if a is 29 and b is 5, what is printed by each of the following statements?
printf("The product of %d and %d is %d\n", a, b, a * b);
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d x %d = %d\n", a, b, a * b);
19. if a is 29 and b is 14, what is printed by the following statements?
printf("%d + \n", a);
printf("%d\n", b);
printf("--\n");
printf("%d\n", a + b);
20. if rate = 15, what is printed by (a) printf("rate\n")?
(b) printf("%d\n", rate)?
C – The Basics
In this chapter, we will explain the following:
• What is an alphabet, a character set, and a token
• What is a syntax rule and a syntax error
• What is a reserved word
• How to create identifiers in C
• What is a symbolic constant
• The C data types—int, float, and double
• How to write int and double expressions
• How to print an integer using a field width
• How to print a floating-point number to a required number of decimal places
• What happens when int and double values are mixed in the same expression
• What happens when we assign int to double and double to int
• How to declare a variable to hold a string
• How to assign a string value to a string variable
• Some problems to avoid when using the assignment statement
2.1 Introduction
In this chapter, we discuss some basic concepts you need to know in order to write programs in the C programming language.
A programming language is similar to speaking languages in many respects. It has an
alphabet (more commonly referred to as a character set) from which everything in the language is constructed. It has rules for forming words (also called tokens), rules for forming statements, and rules for forming programs. These are called the syntax rules of the language and must be obeyed
when writing programs. If you violate a rule, your program will contain a syntax error. When you attempt to compile the program, the compiler will inform you of the error. You must correct it and try again.
The first step to becoming a good programmer is learning the syntax rules of the
programming language. This is the easy part, and many people mistakenly believe that this makes them a programmer. It is like saying that learning some rules of English grammar and being able to write some correctly formed sentences makes one a novelist. Novel-writing skills require much more than learning some rules of grammar. Among other things, it requires insight, creativity, and a knack for using the right words in a given situation.
In the same vein, a good programmer must be able to creatively use the features of the language to solve a wide variety of problems in an elegant and efficient manner. This is the difficult part and can be achieved only by long, hard study of problem-solving algorithms and writing programs to solve a wide range of problems. But we must start with baby steps.