Chapter 5. Basic Syntax in C


Contributed By: Shehrevar Davierwala

1. What is C Tokens ? or Tokens?

A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens:
printf(“Hello, World! \n”);
The individual tokens are:
“Hello, World! \n”

2. Why should we use semicolons ?

In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
Given below are two different statements:
printf(“Hello, World! \n”);
return 0;

3. Why are comments use in C and how ?

Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminate with the characters */ as shown below:
/* my first program in C */

You cannot have comments within comments and they do not occur within a string or character literals.

4. What is an Identifier ?

A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers:
mohd  zara  abc  move_name   a_123
myname50   _temp   j    a23b9   retVal

In other words

Is a unique name that simply references to memory locations, which can hold values(data).
Identifiers give unique names to various objects in a program.
Are formed by combining letters (both upper and lowercase), digits (0–9) and underscore ( _ ).

5. What is a keyword & name few keywords in C ?

These reserved words may not be used as constants or variables or any other identifier names.

The following list shows the reserved words in C.

auto    else    long    switch  break   enum       register     typedef
case    extern   return   union   char   float   short   unsigned
const   for   signed    void   continue   goto
sizeof   volatile   default   if   static   while
do    int   struct   _Packed   double

6. What is white space in C ?

Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement:
int age;
there must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish to increase readability.

Whitespace is counted if used as separators or as components of character constants or string literals.
Space, tab, linefeed, carriage-return, form feed, vertical-tab, and newline characters (\n).

7. What are charsets used in C?

Communicating with a computer involves speaking the language the computer understands. In C, various characters have been given to communicate.
Character set in C consists of;
Types                            Character Set
Lower case                          a-z
Upper case                          A-Z
Digits                                  0-9
Special Character               !@#$%^&*
White space              Tab or new lines or space

8. What is a datatype and Name few datatypes in C?

In the C programming language, data types refer to a domain of allowed values & the operations that can be performed on those values.

The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

There are 4 fundamental data types in C, which are- char, int, float &, double. Char is used to store any single character; int is used to store any integer value, float is used to store any single precision floating point number & double is used to store any double precision floating point number. We can use 2 qualifiers with these basic types to get more types.

There are 2 types of qualifiers-
Sign qualifier- signed & unsigned
Size qualifier- short & long
The data types in C can be classified as follows:

Type                                  Storage size                        Value range
char                                    1 byte                                -128 to 127
unsigned char                      1 byte                                  0 to 255
int                                       2 or 4 bytes      -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int                         2 or 4 bytes               0 to 65,535 or 0 to 4,294,967,295
short                                    2 bytes                               -32,768 to 32,767
unsigned short                      2 bytes                                0 to 65,535

long                                     4 bytes                     -2,147,483,648 to 2,147,483,647
unsigned long                       4 bytes                               0 to 4,294,967,295

Type                          Storage size          Value range                 Precision
float                              4 bytes          1.2E-38 to 3.4E+38       6 decimal places
double                           8 bytes           2.3E-308 to 1.7E+308   15 decimal places
long double                  10 bytes       3.4E-4932 to 1.1E+4932    19 decimal places

9. What are Constants? Name its types
A constant is an entity that doesn’t change.
C constants can be divided into two major categories:
 Primary Constants
 Secondary Constants


Here our only focus is on primary constant. For constructing these different types of constants certain rules have been laid down.
Rules for Constructing Integer Constants:
An integer constant must have at least one digit.
a) It must not have a decimal point.
b) It can be either positive or negative.

c) If no sign precedes an integer constant it is assumed to be positive.
d) No commas or blanks are allowed within an integer constant.
e) The allowable range for integer constants is -32768to 32767.
Ex.: 426, +782,-8000, -7605
Rules for Constructing Real Constants:
Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.
Rules for constructing real constants expressed in fractional form:
a) A real constant must have at least one digit.
b) It must have a decimal point.
c) It could be either positive or negative.
d) Default sign is positive.
e) No commas or blanks are allowed within a real constant.

Ex. +325.34, 426.0, -32.76, -48.5792
Rules for constructing real constants expressed in exponential form:
a) The mantissa part and the exponential part should be separated by a letter e.
b) The mantissa part may have a positive or negative sign.
c) Default sign of mantissa part is positive.
d) The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
e) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex. +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5

Rules for Constructing Character Constants:
a) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
b) The maximum length of a character constant can be 1 character.
Ex.: ‘M’, ‘6’, ‘+’

10. What is a Variable ?

Variables are names that are used to store values. It can take different values but one at a time. A data type is associated with each variable & it decides what values the variable can take. When you decide your program needs another variable, you simply declare (or define) a new variable and C makes sure you get it. You declare all C variables at the top of whatever blocks of code need them. Variable declaration requires that you inform C of the variable’s name and data type. Syntax – datatype variablename;
int page_no;
char grade;
float salary;
long y;

11. How to declare a variable ?

There are two places where you can declare a variable:
 After the opening brace of a block of code (usually at the top of a function)
 Before a function name (such as before main() in the program) Consider various examples:
Suppose you had to keep track of a person’s first, middle, and last initials. Because an initial is obviously a character, it would be prudent to declare three character variables to hold the three initials. In C, you could do that with the following statement:
1. main()
char first, middle, last;
// Rest of program follows

2. main()
{ char first;
char middle;
char last;
// Rest of program follows

12. How to initialize a variable ?

When a variable is declared, it contains undefined value commonly known as garbage value. If we want we can assign some initial value to the variables during the declaration itself. This is called initialization of the variable.
Eg- int pageno=10;
char grade=’A’; float salary= 20000.50;

13. What is an expression ?

An expression consists of a combination of operators, operands, variables & function calls. An expression can be arithmetic, logical or relational. Here are some expressions:
a+b – arithmetic operation a>b- relational operation a == b – logical operation func (a,b) – function call
a*(b + c/d)/20 q = 5*2 x = ++q % 3
q > 3

As you can see, the operands can be constants, variables, or combinations of the two. Some expressions are combinations of smaller expressions, called subexpressions. For example, c/d is a subexpression of the sixth example.
An important property of C is that every C expression has a value. To find the value, you perform the operations in the order dictated by operator precedence.

14. What is a statement in C ?

Statements are the primary building blocks of a program. A program is a series of statements with some necessary punctuation. A statement is a complete instruction to the computer. In C, statements are indicated by a semicolon at the end. Therefore
legs = 4
is just an expression (which could be part of a larger expression), but
legs = 4;
is a statement.

What makes a complete instruction? First, C considers any expression to be a statement if you append a semicolon. (These are called expression statements.) Therefore, C won’t object to lines such as the following:
3 + 4;
However, these statements do nothing for your program and can’t really be considered sensible statements. More typically, statements change values and call functions:
x = 25; ++x;
y = sqrt(x);
Although a statement (or, at least, a sensible statement) is a complete instruction, not all complete instructions are statements. Consider the following statement:
x = 6 + (y = 5);

In it, the subexpression y = 5 is a complete instruction, but it is only part of the statement. Because a complete instruction is not necessarily a statement, a semicolon is needed to identify instructions that truly are statements.

15. What are compound statements or blocks in C ?

A compound statement is two or more statements grouped together by enclosing them in braces; it is also called a block. The following while statement contains an example:
while (years < 100)
wisdom = wisdom * 1.05; printf(“%d %d\n”, years, wisdom); years = years + 1;
If any variable is declared inside the block then it can be declared only at the beginning of the block. The variables that are declared inside a block can be used only within the block.



Leave a Reply

Your email address will not be published.