Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Topics

Recent Status Updates

View All Updates

- - - - -

C for Beginner -- Part4 : C Basics : Expressions, Keywords & Errors

c c for beginner expression operator keyword error

  • Please log in to reply
No replies to this topic

#1 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 05 March 2013 - 12:06 AM

In the last part we had come to know the basics of  variables, datatypes with some explanation. Today we will learn about arithmetic operation, C keywords, compilation and run time errors with explanation.

Arithmetic Operation -- C Expression

Expressions are basically operators(like +,-,*,/ etc) acting on operands(variables or data). Statements like a = b + c; ++i and k+=i; are all expressions. Strictly speaking, even a single variable or constant can be considered an expression. We have seen several expressions above on operators in which the examples involved expressions. When an expression can be interpreted in more than one way, there are rules that govern how the expression gets interpreted by the compiler. Such expressions follow C's precedence and associativity rules. The precedence of operators determine a rank for the operators. The higher an operator's precedence, the higher binding it has on the operands. 
For example, the expression a * b + c can be interpreted as (a * b ) + c or a * (b + c), but the first interpretation is the one that is used because the multiplication operator has higher precedence than addition. Associativity determines the grouping of operations among operators of the same precedence. In the expression a * b / c, since multiplication and division have the same precedence we must use the associativity to determine the grouping. These operators are left associative which means they are grouped left to right as if the expression was (a * b ) / c.
The binary arithmetic operators are  +,  -,  *,  /, and the modulus operator  %. Integer  division truncates any fractional part. The expression  x % y produces the remainder when  x is divided by  y, and thus is zero when  y divides  x exactly. For example, a year is a leap year if it is divisible by 4 but not by 100, except that years divisible by 400 are leap years. Here's an example of % and /
int a = 101, b= 10;char c ='%';//c to show the % character in console window
int reminder, q ;
reminder = a%b;
q = a/b;
printf("a%cb = %d, a/b = %d",c,reminder,q);

 Here we assigned value 101 to a and 10 to b when declared. We can assign value to a variable when it is declared as well as anywhere in the program. The assignment operator works from right to left that means on right side there must be value and on left side there should be variables to assign that value. Dont worry, we can assign variables to other variables, then the value of the variable will be copied to the assigned variable. Here's a program that interchanges value of two variables, we call this a swap program,

int a = 101;
int b = 55, temporay;//initially temporary hold nothing
printf("Before swap a = %d, b = %d\n",a,b);//will show 101,55
temporay = a;//temporary hold value of a
a = b;//value of a replaces with the value of b
b = temporary;//value of temporary is copied to b
printf("After swap a = %d, b = %d",a,b);//will show 10,10

 In the above code we see there are two variables a and b are assigned values on declaration. But in expression temporary  = a , the value of a is assigned to temporary so after execution of that statement value of a and temporary will be 101. Then in the expression a =  b, the value of b is assigned to a, so the value of a should be same as the value of b(55) then temporary is copied to a, thus the swap works;
The % operator cannot be applied to a float or double type variable. The direction of truncation for /  and the sign of the result for  % are machine-dependent for negative operands, as is the action taken on overflow or underflow.The binary  + and  - operators have the same precedence, which is lower than the precedence of  *,  / and  %, which is in turn lower than unary  + and  -. Arithmetic operators associate left to right.  

a + b;
x = y;
t = u + v;
x <= y;

The first expression, which employs the addition operator (+), represents the sum of the values assigned to variables a and b. The second expression involves the assignment operator (=), and causes the value represented by y to be assigned to x. In the third expression, the value of the expression (u + v) is assigned to t. The fourth expression takes the value 1 (true) if the value of x is less than or equal to the value of y. Otherwise, the expression takes the value 0 (false). Here, <= is a relational operator that compares the values of x and y. The final example causes the value of j to be increased by 1. Thus, the expression is equivalent to

 j = j + 1;

The increment (by unity) operator ++ is called a unary operator, because it only possesses one operand. So binary operators must have two operands on both side of itself.
The operators' order of precedence from highest to lowest and their associativity is shown in this table:
Identifier, constant or string literal, parenthesized expression—with associativity
 The ++ and on the second row of the table are the postfix increment and decrement operators. The func(arglist) on the second row is a function call. Functions will be covered in detail in another part of this tutorial.The ++ and on the third row are the prefix increment and decrement operators. The single + and on the third row are unary operators used to indicate a positive value or negate a value such as +3 or -a. The & on the third row is the address-of operator; the & on the 10th row is the bitwise AND operator. The (type-name) on the fourth row is an explicit cast which will be covered later in this tutorial.
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language. In C a keyword is a reserved word which identifies a syntactic form. So we cant use those keywords as identifier like variable name. There are 32 keywords in C language.

In addition to the standard keywords, many compilers add nonstandard keywords that better exploit their operating environment. For example, several compilers include keywords to manage the memory organization of the 8086 family of processors, to support interlanguage programming, and to access interrupts. Here is a list of some commonly used extended keywords:
“auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while“
Try it with naming a variable of the name of something above keywords. There will be error on compilation.

Compile Time Error, Run Time Error
The error shown by the compiler when we compile a program is called compile time error. These errors generally occurs when some silly mistakes taken place in the program like statement missing—there must be a semicolon at the end of a statement, when we miss it this error can occur. As we discussing earlier that if we use a keyword as an identifier in our program there will be error on compile time. Multiple declaration of a variable, there’s no declaration but we  used a variable causes a error. There are lots of compile time errors can happen so we have to be careful on coding. The main thing we have to think on coding that each statement must be ended with a semicolon, there should not be syntactically error in any statement and the best way is to be familiar with those errors so that we can write our code very efficiently. Here’s an example of some errors occurring 

//compile time error program named error.c with lots of errors
void main(){
    Int a,b,c;
    a = 10;
    a + b = c;
    printf("a = %d"a);

When we compiled the program,  VISUAL STUDIO 10 has shown the error messages:

See with others error there is a error in line 10 for missing a comma before the a variable is used in the printf().
Syntax errors are those which are caused by incorrect usage of the programming language. All programming language compilers are designed to detect and report such errors done by the programmer. Run time errors are those which are caused by incorrect usage of programming logic. for example a run time divide method will throw a run time error if the divisor is '0' because numerically you cannot divide a number by 0. Run time error may occur when the program starts running/executing.
int a = 100;
int b = 0;
The above program will cause a running time error and program execution will terminate unexpectedly. So we must be careful in using programming logic.

  • 0

Also tagged with one or more of these keywords: c, c for beginner, expression, operator, keyword, error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download