0% found this document useful (0 votes)
38 views

Unit 3 - Structure of A C Program

This document discusses the basic elements of a C program structure, including constants and variables, expressions, operators, identifiers, statements, and statement blocks. Constants contain fixed values while variables can change. Expressions combine constants, variables, and operators to perform computations. Common operators include +, -, *, /, and %. Identifiers name variables, functions, and keywords. Statements contain an expression and end with a semicolon. Statement blocks group multiple statements and are delimited by curly braces.

Uploaded by

barnabas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Unit 3 - Structure of A C Program

This document discusses the basic elements of a C program structure, including constants and variables, expressions, operators, identifiers, statements, and statement blocks. Constants contain fixed values while variables can change. Expressions combine constants, variables, and operators to perform computations. Common operators include +, -, *, /, and %. Identifiers name variables, functions, and keywords. Statements contain an expression and end with a semicolon. Statement blocks group multiple statements and are delimited by curly braces.

Uploaded by

barnabas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT -3 : STRUCTURE OF A C PROGRAM

The Basics of a C Program

As a building is made of bricks, a C program is made of basic elements, such as


expressions, statements, statement blocks, and function blocks. These elements are
discussed in the following sections. But first, you need to learn two smaller but important
elements, constant and variable, which make up expressions.

Constants and Variables

As its name implies, a constant is a value that never changes. A variable, on the other hand,
can be used to present different values.

You can think of a constant as a music CD-ROM; the music saved in the CD-ROM is never
changed. A variable is more like an audio cassette: You can always update the contents of
the cassette by simply overwriting the old songs with new ones.
You can see many examples in which constants and variables are in the same statement.

For instance, consider the following:


i = 1;

where the symbol 1 is a constant because it always has the same value (1), and the symbol i
is assigned the constant 1. In other words, i contains the value of 1 after the statement is
executed. Later, if there is another statement, i = 10; after it is executed, i is assigned the
value of 10. Because i can contain different values, it’s called a variable in the C language.

Expressions

An expression is a combination of constants, variables, and operators that are used to


denote computations.

For instance, the following:


(2 + 3) * 10 is an expression that adds 2 and 3 first, and then multiplies the result of the
addition by 10. (The final result of the expression is 50.)

Similarly, the expression 10 * (4 + 5) yields 90. The 80/4 expression results in 20.
Here are some other examples of expressions:

Expression Description

6 An expression of a constant.
i An expression of a variable.
6+i An expression of a constant plus a variable.
exit(0) An expression of a function call.

Operators

As you’ve seen, an expression can contain symbols such as +, *, and /. In the C language,
these symbols are called arithmetic operators.

Symbol Meaning

+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder (or modulus)

You might already be familiar with all the arithmetic operators, except the remainder (%)
operator. % is used to obtain the remainder of the first operand divided by the second
operand. For instance, the expression

6%4

yields a value of 2 because 4 goes into 6 once with a remainder of 2.

The remainder operator, %, is also called the modulus operator. Among the arithmetic
operators, the multiplication, division, and remainder operators
have a higher precedence than the addition and subtraction operators. For example, the
expression

2 + 3 * 10

yields 32, not 50, because of the higher precedence of the multiplication operator.
3 * 10 is calculated first, and then 2 is added into the result of the multiplication.

As you might know, you can put parentheses around an addition (or subtraction) to force the
addition (or subtraction) to be performed before a multiplication, division, or modulus
computation. For instance, the expression

(2 + 3) * 10

performs the addition of 2 and 3 first before it multiplies the result by 10.
Other operators, which are used for syntax, include the comma and semicolon. The
semicolon is generally used to indicate the end of a statement, as you will see later. The
comma is used in certain instances where a statement is comprised of a list of expressions
or declarations.
Identifiers
Along with numbers (such as the constant 7) and operators (such as the symbol +),
expressions can also contain words that are called identifiers. Function names (such as exit)
and variable names (such as i), as well as reserved keywords, are all identifiers in C.

The following is the set of characters you can use to make a valid identifier. Any characters
or symbols that do not follow these rules are illegal to use in an identifier.

• Characters A through Z and a through z.


• Digit characters 0 through 9 (but these cannot be used as the first character of an
identifier).
• The underscore character (_).

For instance, stop_sign, Loop3, and _pause are all valid identifiers.
The following are illegal characters; that is, they do not meet the above set of rules for
identifiers:

• C arithmetic signs (+, -, *, /).


• Period, or dot character (.).
• Apostrophes (‘) or quotes (“).
• Any other special symbols such as *, @, #, ?, and so on.

Some invalid identifiers, for example, are 4flags, sum-result, method*4, and
what_size?.

Statements
In the C language, a statement is a complete instruction, ending with a semicolon. In many
cases, you can turn an expression into a statement by simply adding a semicolon at the end
of the expression.

For instance, the following:


i = 1; is a statement. You might have already figured out that the statement consists of an
expression of i = 1 and a semicolon (;).

Here are some other examples of statements:

i = (2 + 3) * 10;
i = 2 + 3 * 10;
j = 6 % 4;
k = i + j;

Statement Blocks
A group of statements can form a statement block that starts with an opening brace ({) and
ends with a closing brace (}). A statement block is treated as a single statement by the C
compiler.

For instance, the following:

for(. . .) {
s3 = s1 + s2;
mul = s3 * c;
remainder = sum % c;
}

3
is a statement block that starts with { and ends with }. Here for is a keyword in C that
determines the statement block.

A statement block provides a way to group one or more statements together as a single
statement. Many C keywords can only control one statement. If you want to put more than
one statement under the control of a C keyword, you can add those statements into a
statement block so that the block is considered as one statement by the C keyword.

You might also like