1. Computing & Technology

Discuss in my forum

C Tutorial - Lesson Four - About Expressions

By , About.com Guide

1 of 4

Expressions and Statements
In Lesson two we saw how to assign values to ints and floats. These assignment statements have a very simple structure.

 Variable = Expression
 
We know what a variable is, i.e. somewhere to store the value of the expression, but just what is an expression? It's a statement that resolves to a value: either numeric or character.

What Exactly is a Statement?

A statement is a building block of a program. For example the assignment statement
 int a=10; 
This is also an expression and has the value 10. If we write this code
 int b= (a=10) ; 
Both a and b will be set to the value 10.

In Lesson 5 we'll meet other statements such as if or while. Both of these require a conditional expression.

Conditional Expression

A conditional expression is one which evaluates as true (a non zero value) or false (0). True is almost always 1 but any non zero value is also true!
 #include <stdio.h>
 int main() { 
 int a=10;
 int b=( a = 10) ;
 int c=( a==10) ;
 printf("Value of b is %i\n\r",b) ;
 printf("Value of c is %i\n\r",c) ;
 return 0; } 
Compile and run the example above. It should print out the following.
 Value of b is 10
 Value of c is 1
 
On the next page : Learn about the preprocessor.

©2012 About.com. All rights reserved.

A part of The New York Times Company.