C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Character Variables

C only has a concept of numbers and characters. It very often comes as a surprise to some programmers who learnt a beginner's language such as BASIC that C has no understanding of strings but a string is only an array of characters and C does have a concept of arrays which we shall be meeting later in this course. 

To declare a variable of type character we use the keyword char. - A single character stored in one byte.
For example


  char c;


To assign, or store, a character value in a char data type is easy - a character variable is just a symbol enclosed by single quotes. For example, if c is a char variable you can store the letter A in it using the following C statement.


     c='A'


Notice that you can only store a single character in a char variable. Later we will be discussing using character strings, which has a very real potential for confusion because a string constant is written between double quotes. But for the moment remember that a char variable is 'A' and not "A".

Constants

ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed. 

The const keyword is to declare a constant, as shown below.


   int const a = 1;

   const int a =2;


The preprocessor #define is another more flexible (see Preprocessor Chapters) method to define constants in a program. 

You frequently see const declaration in function parameters. This says simply that the function is not going to change the value of the parameter. 

 

The following function definition used concepts we have not met (see chapters on functions, strings, pointers, and standard libraries) but for completenes of this section it is is included here: 


 void strcpy(char *buffer, char const *string)

Assignment Statement

The easiest example of an expression is in the assignment statement. An expression is evaluated, and the result is saved in a variable. A simple example might look like
y = (m * x) + c
This assignment will save the value of the expression in variable y.

Arithmetic operators

Here are the most common arithmetic operators 

            +    Addition
            -     Subtraction
            *    Multiplication
            /    Division
            %   Modulo Reduction


*, / and % will be performed before + or - in any expression. Brackets can be used to force a different order of evaluation to this. Where division is performed between two integers, the result will be an integer, with remainder discarded. Modulo reduction is only meaningful between integers. If a program is ever required to divide a number by zero, this will cause an error, usually causing the program to crash.

Here are some arithmetic expressions used within assignment statements.

velocity = distance / time; 
 force = mass * acceleration;
 count = count + 1;

C has some operators which allow abbreviation of certain types of arithmetic assignment statements

Shorthand

Equivalent

i++; or ++i; 

i=i+1;

i--; or  --i;

i=i-1;

These operations are usually very efficient. They can be combined with another expression.


  x= a *b++; is equivalent  to  x= a * b, b=b+1;

Versions where the operator occurs before the variable name change the value of the variable before evaluating the expression, so

  
  x=-- i * ( a + b)  is equivalent to x= i-1; x= i * (a+ b)


These can cause confusion if you try to do too many things on one command line. You are recommended to restrict your use of ++ and - to ensure that your programs stay readable.

Shorthand

 Equivalent

i +=10
i -=10
 i *=10
 i /=10

i = i+10
i = i-10
 i = i*10
 i = i/10

Back Next
 

Google