0.6 – C Language Fundamentals 3

by subbu on July 22, 2013

Statements and instructions:

So far we went through the first two steps in learning C language that are learning characters (C character set) and words (tokens). Now it is time to learn writing statements in C language.

A statement is a line of code in a program. In C language most of the statements are terminated with a semicolon (;)

Where as an instruction is a command given to the computer. An instruction is an inner part of a statement. In simple a Statement has one or more instructions.

 statements instructions

In the above example

  1. int x,y,z; is a declaration statement allocates memory to store integer values with the names x, y and z
  2. z=(x=10, y=20, z=x+y); is a statement with three instructions x=10; y=20; and z=x+y; executes one after another.

Now we will discourse different statements to start programming

  1. declaration statements
  2. Assigning statements
  3. output statements

Declaration statements:

Variables are needed to store and access the data; these variables are declared using declaration statements.

Declaration statements announce the property of variables to the compiler. Variables are declared for different data types using different keywords. These keywords will be discoursed in depth in future lessons.

A variable can’t be used without its declaration. Compiler throws an error if an undeclared variable is used within a program.

int qty;
float price,bill;

In the above example on executing first two statements, memory allocations qty to store an integer value and price, bill to store floating point values are selected in free space (heap) of RAM called variables. Last three statements assign values to the variables.

Initializing while declaration:

int num=25;

In the above example an integer variable num is declared and assigned 25 while its declaration called initializing while declaration.

int a=10,b=25,c=a+b;

The above example is absolutely correct because a and b are declared and initialized before assigning to c.

int a=10,c=a+b,b=20;

The above statement is incorrect because b is used before its declaration.

int a=b=c=5;

The above example is incorrect because only a is declared but b and c are not declared

int a,b,c;

The above example is correct because a, b and c are first declared and assigned with 5  in a separate statement. Here 5 is assigned to c, the value of which is assigned to b and then finally the value of b is assigned to a.

Declaration of variables in C11:

In earlier versions of ANSI C, variables must be declared only at the beginning of the code block (function) that is before writing any executable statements.

Where as,  C11 allows declaring variables any where with in the code block.

int sci, sco, mat;
int tot=sci+sco+mat;

The above code executes perfectly in C11 but doesn’t execute in earlier versions of ANSI C compilers because tot is declared after some executable statements.

Garbage value:

When a variable is declared but not assigned with any value then some unknown and unwanted value is assigned to the variable automatically called garbage value.
It may be a positive or negative value, may change machine to machine and execution to execution.
Most of the compilers throw a warning during the compiling of the program when a variable is declared but not initialized.

Assigning statements (l-value and r-value):

These are the common statements in any program. These statements are used to assign the value of expression to the variable. Operator “=” is used to assign the value of an expression to the variable.
There must be a variable at the left side to “=” called l-value, where as the expression at the right side to “=” is called r-value.
While executing an assigning statement r-value (expression) is first evaluated and then the result of which is assigned to the l-value (variable).

l-value r-value

int x, y, z;

In the above example
While executing “x=10;” the r-value 10 is assigned to l-value x
While executing “y=20;” the r-value 20 is assigned to l-value y
While executing “z=x+y;” the r-value x+y is evaluated first and then assigned to l-value z


It is invalid because 5 is not l-value which must be a variable to store a value


It is invalid because l-value must be a variable but not an expression

Output statements:

C language has no output statement like write. A library function printf() is used to print formatted text on console (monitor).
The best way of understanding printf() is through different examples.


printf("Hello World");

Hello World

Example explained:
The string or text written within “” will be printed as it is. It is called format string.


printf ("Hello");
printf("Dennis Ritchie");


Example explained:
Though text is printed with three printf() statements the output is printed in the same line. We will see how output can be printed in multiple lines in the following example




Example explained:
\n is the next line character sends the output control to the next line. It can be used any where and any number of times within the printf() statement.


int num;


Example explained:

Here printf() prints “num” as the output instead the value of variable num because printf() prints the format string as it is.

If we want to print the value of num instead of “num”, the variable must be specified after the format string separating with comma (,) and its representation must be specified within the format string using %d (non printable integer representation character)


int num;


Example explained:
Here %d is substituted with the value of num wile executing printf() statement. So the output is 45.


printf() demo

Value of x is 45
Value of y is 25
Sum of two numbers 70

Example explained:
In this example, values of x, y and sum are printed in place of representation character %d

The same thing can be printed even by using a single printf() statement

printf(“value of x is %d\nvalue of y is %d\nSum of two numbers %d”,x, y, sum);


printf() in c

Sum of 45.250000 and 25.750000 is 71.000000

Example explained:
Here %f is used to represent floating point values.


printf("%d  %f  %c", -25, 12.25+435.25, 'a');

-25  447.500000  a

Example explained:

In printf() formatting characters not only represent variables but also represent constants and expressions. Here -25 is represented with %d, expression 12.25+435.25 is represented with %f and a single character constant ‘a’ is represented with %c.




Example explained:

Here the garbage value would be the output because no variable, constant or an expression is specified within the printf() statement.

printf() syntax

printf( ) is a function has a format string placed in double inverted commas and a list of variables followed by a comma.

In general the text written as format string prints as it is on the monitor. Different formatting characters are placed in format string to represent variables written after comma.

Every variable in the variable list has its associated formatting character in the format string

There are different formatting characters to represent different data types (will be discoursed in future lessons).

The format string and list of variables are placed in a pair of parenthesis ( )

Statement terminators:

As we terminate a sentence with full stop in English, a C statement is terminated with a semicolon (;)

The C compiler treats a line until semicolon (;) as a statement so that we can write multiple statements in a single line

Even it is possible to write the total C program in a single line but it will be difficult to identify the errors if any.


int fir, sec, sm; fir=45; sec=30; sm=fir+sec; printf("Sum is %d",sm);

The above line is perfectly correct as the compiler treats the above line as 5 different statements like

int fir, sec, sm;
printf("Sum is %d",sm);

Cannot split a C statement:

Multiple statements can be written in a single line separating with statement terminator semicolon (;) but, a single statement can’t be split into multiple lines, results compilation (Syntax) error.


Previous post:

Next post: