Chapter 6: Declaration and Statements
C Language(Basics & Fundamentals) - Chapter 6

Chapter 6: Declaration and Statements


PART 1: DECLARATION


In programming, a declaration is a statement describing an identifier, such as the name of a variable or a function. Declarations are important because they inform the compiler or interpreter what the identifying word means, and how the identified thing should be used.

A declaration may be optional or required, depending on the programming language. For example, in the C programming language, all variables must be declared with a specific data type before they can be assigned a value.

A declaration is a C language construct that introduces one or more identifiers into the program and specifies their meaning and properties. Declarations may appear in any scope. Each declaration ends with a semicolon (just like a statement) and consists of three distinct parts:

attr-spec-seq ;

where, attr = Attribute spec = Specifier Seq = Sequence

For example:

int a, *b=NULL;
    // "int" is the type specifier, "a" is a declarator, "*b" is a declarator and NULL is its initializer
const int *f(void);
    // "int" is the type specifier, "const" is the type qualifier, "*f(void)" is the declarator.
enum COLOR {RED, GREEN, BLUE} c;
    // "enum COLOR {RED, GREEN, BLUE}" is the type specifier.
    // "c" is the declarator.        

PART 2: STATEMENTS


Statements are fragments of the C program that are executed in sequence. Statements are made by combining various tokens. Each statement which does not have its body must be terminated by the semicolon (;). The statements which should be terminated:

  • All printf(), scanf() or any other function calls.
  • All declarations like variables, constants, function, structures must be terminated by semicolon.
  • All expressions must be terminated by the semicolon (;).

The statements which should not be terminated:

  • Header files include statements.
  • Macro definition statements.
  • If statements, loop statements, function header with the function definitions.

There are five types of statements:

Expression statements, compound statements, selection statements, iteration statements and jump statements.

?

1) Expression statements: It is combination of variables, constants, operators, function calls and followed by a semicolon. Expression can be any operation like Arithmetic operation or Logical Operation.

?Here are few examples for expression statements:

X = Y + 10;
20 > 90;
a ? b : c ;
a = 10 + 20 * 30;
;??//This is NULL Statement        

2) Compound statements: Compound statement is combination of several expression statements. Compound Statement is enclosed within the braces { }. Compound statement is also called as Block Statement.

There is no need of any semicolon at the end of Compound Statement.

Example for Compound Statement:

{
   int a=10,b=20,c;
   c = a + b;
   printf(“value of C is : %d n”,c);
}        

3) Selection statements: Selection Statements are used in decisions making situations. Statements like if, if...else, switch are selection statements.

Syntax of if…else selection statement:

if(expression) { //code?to?be?executed?if?condition?is?true }
else {?//code?to?be?executed?if?condition?is?false }??        

Here is an example of selection statements:

if (i < 1) 
{
   funct(i);
}
else 
{ 
   i = x++; funct(i); 
}        

4) Iteration statements: These are also called as Loops. If we want to execute a part of program many times we may use loops. Some basic loops used in C language are for loop, while loop, do-while loop.

?Syntax of for iteration statement:

for ( expression-1(initialize);expression-2(End);expression-3(action) )
statements        

Example of for loop:

for (int n=1; n <= 10; n++) //Loops from n=1 to n=10.
{
func(n);
};        

?Syntax of while iteration statement:

while(expression)
statements        

Example of while loop:

while (n < 10)
{
???a[n] = n;
???n++;
}        

5) Jump statements: These are unconditional statements; jump statements are useful for transferring the control one part of program to other part of program. Some basic jump statements are goto, continue, break and return.

?Syntax of goto jump statement:

Syntax 1     |   Syntax 2
----------------------------
goto label;  |    label:  
..           |    ..
..           |    ..
..           |    ..
label:       |    goto label;        

Example for goto jump statement:

if (num % 2 == 0)
{ goto even; }?  // jump to even
else
{ goto odd; }  // jump to odd

even:
  ???printf("%d is even", num);
     return;? // return if even
odd:
     printf("%d is odd", num);        

?Labels: Any statement can be labeled, by providing a name followed by a colon before the statement itself. Any statement (but not a declaration) may be preceded by any number of labels, each of which declares identifier to be a label name, which must be unique within the enclosing function (in other words, label names have function scope). Label declaration has no effect on its own, does not alter the flow of control, or modify the behavior of the statement that follows in anyway.


List of articles from the series:

CH 1) Programming environment

CH 2) Syntax and Semantics

CH 3) Libraries & Header files

CH 4) Preprocessor & Objects

CH 5) Commands & Tokens

CH 6) Declaration & Statements

CH 7) Flow control & Functions

CH 8) Errors & Habits


References:?Wikipedia, CMan(Bell Labs), GeeksforGeeks, guru99 and various other blogs, videos and sites on available internet.

If you find any errors or disinformation in this article, then please inform me.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了