Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

C Programming Tutorial

Learn C programming in an easy way.This C programming tutorial is completly free for your use.

How you can help us back is let us know about more tutorials and send us the tutorial with your small photo and name.We will publish tutorial in your name.

 

<< Tutorial Home

WHAT ABOUT VARIABLES
C provides the programmer with FOUR basic data types . User defined variables must be declared before they can be used in a program.

Get into the habit of declaring variables using lowercase characters. Remember that C is case sensitive, so even though the two variables listed below have the same name, they are considered different variables in C.

sum Sum

The declaration of variables is done after the opening brace of main() ,

#include <stdio.h> main() {

int sum;

sum = 500 + 15;

printf("The sum of 500 and 15 is %d\n", sum); }

Sample Program Output The sum of 500 and 15 is 515

It is possible to declare variables elsewhere in a program, but lets start simply and then get into variations later on.

The basic format for declaring variables is

data_type var, var, ... ;

where data_type is one of the four basic types, an integer , character , float , or double type.

The program declares the variable sum to be of type INTEGER (int). The variable sum is then assigned the value of 500 + 15 by using the assignment operator, the = sign.

sum = 500 + 15;

Now lets look more closely at the printf() statement. It has two arguments, separated by a comma. Lets look at the first argument,

"The sum of 500 and 15 is %d\n"

The % sign is a special character in C. It is used to display the value of variables. When the program is executed, C starts printing the text until it finds a % character. If it finds one, it looks up for the next argument (in this case sum ), displays its value, then continues on.

The d character that follows the % indicates that a decimal integer is expected. So, when the %d sign is reached, the next argument to the printf() routine is looked up (in this case the variable sum , which is 515), and displayed. The \n is then executed which prints the newline character.

The output of the program is thus,

The sum of 500 and 15 is 515 _

Some of the formatters for printf are,

Cursor Control Formatters \n newline \t tab \r carriage return \f form feed \v vertical tab Variable Formatters %d decimal integer %c character %s string or character array %f float %e double The following program prints out two integer values separated by a TAB It does this by using the \t cursor control formatter

#include <stdio.h> main() {

int sum, value;

sum = 10; value = 15;

printf("%d\t%d\n", sum, value); }

Program output looks like 10 15 _

 

DATA TYPES AND CONSTANTS
The four basic data types are

INTEGER
These are whole numbers, both positive and negative. Unsigned integers (positive values only) are supported. In addition, there are short and long integers.
The keyword used to define integers is,

int
An example of an integer value is 32. An example of declaring an integer variable called sum is,

int sum;
sum = 20;

FLOATING POINT
These are numbers which contain fractional parts, both positive and negative. The keyword used to define float variables is,

float
An example of a float value is 34.12. An example of declaring a float variable called money is,

float money;
money = 0.12;

 

DOUBLE
These are exponetional numbers, both positive and negative. The keyword used to define double variables is,

double
An example of a double value is 3.0E2. An example of declaring a double variable called big is,

double big;
big = 312E+7;

 

CHARACTER
These are single characters. The keyword used to define character variables is,

char
An example of a character value is the letter A. An example of declaring a character variable called letter is,

char letter;
letter = 'A';

Note the assignment of the character A to the variable letter is done by enclosing the value in single quotes. Remember the golden rule: Single character - Use single quotes.

Sample program illustrating each data type

#include < stdio.h >

main()
{
int sum;
float money;
char letter;
double pi;

sum = 10; /* assign integer value */
money = 2.21; /* assign float value */
letter = 'A'; /* assign character value */
pi = 2.01E6; /* assign a double value */

printf("value of sum = %d\n", sum );
printf("value of money = %f\n", money );
printf("value of letter = %c\n", letter );
printf("value of pi = %e\n", pi );
}

Sample program output
value of sum = 10
value of money = 2.210000
value of letter = A
value of pi = 2.010000e+06

INITIALIZING DATA VARIABLES AT DECLARATION TIME
Unlike PASCAL, in C variables may be initialized with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialized to 10.

int count = 10;

SIMPLE ASSIGNMENT OF VALUES TO VARIABLES
The = operator is used to assign values to data variables. Consider the following statement, which assigns the value 32 an integer variable count, and the letter A to the character variable letter

count = 32;
letter = 'A';

THE VALUE OF VARIABLES AT DECLARATION TIME
Lets examine what the default value a variable is assigned when its declared. To do this, lets consider the following program, which declares two variables, count which is an integer, and letter which is a character.
Neither variable is pre-initialized. The value of each variable is printed out using a printf() statement.

#include <stdio.h>

main()
{
int count;
char letter;

printf("Count = %d\n", count);
printf("Letter = %c\n", letter);
}

Sample program output
Count = 26494
Letter = f

It can be seen from the sample output that the values which each of the variables take on at declaration time are no-zero. In C, this is common, and programmers must ensure that variables are assigned values before using them.
If the program was run again, the output could well have different values for each of the variables. We can never assume that variables declare in the manner above will take on a specific value.
Some compilers may issue warnings related to the use of variables, and Turbo C from Borland issues the following warning,

possible use of 'count' before definition in function main

RADIX CHANGING
Data numbers may be expressed in any base by simply altering the modifier, e.g., decimal, octal, or hexadecimal. This is achieved by the letter which follows the % sign related to the printf argument.

#include <stdio.h>

main() /* Prints the same value in Decimal, Hex and Octal */
{
int number = 100;

printf("In decimal the number is %d\n", number);
printf("In hex the number is %x\n", number);
printf("In octal the number is %o\n", number);
/* what about %X\n as an argument? */
}

Sample program output
In decimal the number is 100
In hex the number is 64
In octal the number is 144

Note how the variable number is initialized to zero at the time of its declaration.

DEFINING VARIABLES IN OCTAL AND HEXADECIMAL
Often, when writing systems programs, the programmer needs to use a different number base rather than the default decimal.
Integer constants can be defined in octal or hex by using the associated prefix, e.g., to define an integer as an octal constant use %o

int sum = %o567;
To define an integer as a hex constant use %0x

int sum = %0x7ab4;
int flag = %0x7AB4; /* Note upper or lowercase hex ok */


<< Tutorial Home