C Tutorial – variables and constants

In this C programming language tutorial we take a look at variables and constants.

Variables

If you declare a variable in C (later on we talk about how to do this), you ask the operating system for a piece of memory. This piece of memory you give a name and you can store something in that piece of memory (for later use). There are two basic kinds of variables in C which are numeric and character.

Numeric variables

Numeric variables can either be of the type integer (int) or of the type real (float). Integer (int) values are whole numbers (like 10 or -10). Real (float) values can have a decimal point in them. (Like 1.23 or -20.123).

Character variables

Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If you declare a character variable you must always put the character between single quotes (like so ‘A’ ). So remember a number without single quotes is not the same as a character with single quotes.

Constants

The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is lockedfor the duration of the program). Constants can be very useful, Pi for instance is a good example to declare as a constant.

Data Types

So you now know that there are three types of variables: numeric – integer, numeric-real and character. A variable has a type-name, a type and a range (minimum / maximum). In the following table you can see the type-name, type and range:

Type-name Type Range
int Numeric – Integer -32 768 to 32 767
short Numeric – Integer -32 768 to 32 767
long Numeric – Integer -2 147 483 648 to 2 147 483 647
float Numeric – Real 1.2 X 10-38 to 3.4 X 1038
double Numeric – Real 2.2 X 10-308 to 1.8 X 10308
char Character All ASCII characters

Declaring

So we now know different
type-names and types of variables, but how do we declare them. Declaring a variable is very easy. First you have to declare the type-name. After the type-name you place the name of the variable. The name of a variable can be anything you like as long it includes only letters, underscores or numbers (However you cannot start the name with a number). But remember choose the names wisely. It is easier if a variable name reflects the use of that variable. (For instance: if you name a float PI, you always know what it means).

Now let’s declare some variables, a variable MyIntegerVariable and MyCharacterVariable:


	int main()
	{
		int MyIntegerVariable;
		int MyCharacterVariable;
		return 0;
	}
	

It is possible to declare more than one variable at the same time:


	int main()
	{
		int Variable1, Variable2, Variable3;
		int abc, def, ghi;
		return 0;
	}
	

To declare a constant is not much different then declaring a variable. The only difference is that you haveΒ the word const in front of it:


	int main()
	{
		const float PI = 3.14;
		char = 'A';
		return 0;
	}
	

Note: As you can see, you can assign a value with the equal sign during declaration.

Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can be either negative or
positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.


	int main()
	{
		unsigned int MyOnlyPositiveVar;
		signed int MyNegativeAndPositiveVar;
		int MyNegativeAndPositiveVar;
	}
	

Calculations and variables

There are different operators that can be used for calculations which are listed in the following table:

Operator

Operation

+

Addition

Subtraction

*

Multiplication

/

Division

%

Modulus(Remainder of integer
division)

Now that we know the different operators, let’s calculate something:


int main()
{
	int a, b;
	a = 1;
	b = a + 1;
	a = b - 1;
	return 0;
}

Reading and printing

Calculating something without reading input or printing something on the screen is not much fun. To read input from the keyboard we will use the command scanf. (How to print something to the screen we all ready
know).

So let’s make a program that can do all these things:


#include<stdio.h>

int main()
{
	int inputvalue;

	scanf("%d", &inputvalue);
	inputvalue = inputvalue * 10;
	printf("Ten times the input equals %d\n",inputvalue);
	return 0;
}

Note: The input must be a whole number (integer).

The & sign will be explained in a later tutorial. The %d is for reading or printing a decimal integer value (It is also possible to use %i). In the table below you can find the commands for other types:

%i or %d int
%c char
%f float
%lf double
%s string

That was all for now. In the next tutorial we take a look at the “if statement” and “switch statement”.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 43 responses to “C Tutorial – variables and constants”

Why not let us know what you think by adding your own comment!

  1. chaitu on October 5th, 2010:

    really usefull info

  2. furqan on November 4th, 2010:

    Thanks buddy u help me alot so a lot of thanks if
    u publish a book from that it will be a good Idea
    And i need info about Socket Programing if u include Socket programing in it
    so it wil b grate

  3. Mohammad Sabri on November 10th, 2010:

    God, bless you.

  4. waghmare sandeep on November 25th, 2010:

    Thank you this is very useful.

  5. rahul on February 22nd, 2011:

    you rock man

  6. Louis on May 8th, 2011:

    really usefull info,but how to susbcribe ?

  7. gaurav on October 6th, 2011:

    nice work guys.. πŸ˜€

  8. Rohit on November 14th, 2011:

    Thanks Man

  9. saiful on December 10th, 2011:

    thanks

  10. Yogesh on February 18th, 2012:

    How Many variables can we declare of a type,in single line???

  11. admin on February 18th, 2012:

    @Yogesh: Don’t know, I guess it depends on what compiler you use and how long the text line is that it can interpret. Why don’t you give it a try and declare a lot of integers on the same line?? (This is a quick test that you can do yourself!).

  12. Sonagara on June 7th, 2012:

    main()
    {
    int x=20,y=35;
    x=y++ + x++;
    y= ++y + ++x;
    printf(β€œ%d%dn”,x,y);
    }
    how can i solve it please help me freinds…

  13. Shafqat Ullah on July 16th, 2012:

    I found this site Accidentally. But its very good because it is a good source for the beginners. My request to you is to please continue this practice for others Languages. thanks.

  14. chaitanya on July 25th, 2012:

    The site is good. Plz.give also example of programming output.

  15. soumikatorz on October 4th, 2012:

    Wow! I am lucky to find this site.
    The stepwise example made us clear about the things i am searching for.
    Great jobs guys.
    This site helped me alot.
    Thanks

  16. Paul on October 5th, 2012:

    This is a great resource for a beginner. I am new to this and so far this has been the best site to get information on C programming. I am loving it. Thanks

  17. Abdul Rasheed Hassan Khan on November 19th, 2012:

    I think we can declare a lot of variables in a single line, However i’ll rise this question infront of our teacher, BTW~!! Useful site, now i came to know what’s the difference b/w variable and Constant .. Site Bookmarked πŸ˜€

  18. Bilal on December 13th, 2012:

    Thanks man! Great JOB….U R AWESOME

  19. Rahul Shakya on December 13th, 2012:

    Thank you this is very useful. and important for fresher…..

  20. sultan on December 18th, 2012:

    awsm…. thnx πŸ™‚

  21. Blessing on January 6th, 2013:

    I wouldn’t say i found dis site accidentally…. Dis is a gud job.. I must really commend u guys 4 dis great work……. But wat are d features of dis language (c language)

  22. sushant on January 14th, 2013:

    great work

  23. Nitesh Kumar on February 8th, 2013:

    Well, But i think, every page of this site should must provide output with more programming and explaination.
    By the way, thanks to provide these precious knowledge.

  24. vanessa on April 23rd, 2013:

    This is great help for me because it has helped understand with ease. Thanks again

  25. Somebody commented on May 20th, 2013:

    #include
    int main(){
    int i;
    for(i=1;i<1337;i++){
    printf("THANK YOU!!!\n");
    }
    getchar();
    return 0
    }

  26. sahil on June 2nd, 2013:

    awsome !!

  27. someone on June 23rd, 2013:

    awsome

  28. harishma on june 24th,2013 on June 25th, 2013:

    well………..good tutorial………..
    tnx for sharing……..

  29. kaushal on July 19th, 2013:

    Thanks a lot …
    it’s easy to learn from c tutorial

  30. Harish on September 5th, 2013:

    Thanks a lot ,all concepts are nicely explained.

  31. saurabh on October 6th, 2013:

    thanks a lot ,your way of understanding is very nice ,i am very thankful to you for the answer……………..its really workz…………awesome yaar…

  32. K.Revathy on October 10th, 2013:

    Nice tutorial thankss…………s alot.

  33. ravi on October 23rd, 2013:

    main()
    {
    int x=20,y=35;
    x=y++ + x++;
    y= ++y + ++x;
    printf(β€œ%d%dn”,x,y);
    }
    how can i solve it please help me freinds…
    The O/P is x=56, y=93

  34. uppili on November 21st, 2013:

    nice

  35. Santhosh on November 26th, 2013:

    @ravi: x=56 bcoz in the 1st set of incrementation “x=y++ x++” it is post increment so only the incremented value of y is taken and x remains same i.e.,35. Hence x=36+20=56;
    in next set “y=++y + ++x” it is pre increment both x and y gets incremented and x is not 20 as we have declared but it will take the last step’s value x=56 now;after incrementation x=57 and y=37 therefore y=57+37=94..not 93

  36. varma on December 6th, 2013:

    @santosh: ……why y++ is incremented. in postfix increment y++ should not be incremented

  37. dilipkumar barai on December 23rd, 2013:

    wonderful,dilip

  38. Genevieve on January 11th, 2014:

    This article is super helpful. The way it is structured and these concepts are explained makes for much easier comprehension. Thank you so much for publishing this!

  39. bissha on January 13th, 2014:

    I have actually seen the solution, Thanks

  40. Sagar Kadam on March 6th, 2014:

    Thanx ..awsome site.good job..carry on

  41. sreekuttan k s on March 6th, 2014:

    just like refreshing

  42. punitha valli on March 17th, 2014:

    this is very good job

  43. Ahtisham on May 19th, 2014:

    how to declare a constant in c language..?? give an easy explanation and an easy prgrm…