Skip to main content

Variables and Constants in C


Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:
int playerScore = 95;
Here, playerScore is a variable of integer type. The variable is assigned value: 95.
The value of a variable can be changed, hence the name 'variable'.
In C programming, you have to declare a variable before you can use it.
Rules for naming a variable in C
A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error.
There is no rule on how long a variable can be. However, only the first 31 characters of a variable are checked by the compiler. So, the first 31 letters of two variables in a program should be different.
C is a strongly typed language. What this means it that, the type of a variable cannot be changed.
Constants/Literals
A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy", etc.
As mentioned, an identifier also can be defined as a constant.
const double PI = 3.14
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.
Below are the different types of constants you can use in C.
1. Integer constants
An integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.
2. Floating-point constants
A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
3. Character constants
A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'
4. Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.

Escape Sequences
Character
\b
Backspace
\f
Form feed
\n
Newline
\r
Return
\t
Horizontal tab
\v
Vertical tab
\\
Backslash
\'
Single quotation mark
\"
Double quotation mark
\?
Question mark
\0
Null character
5. String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline
6. Enumeration constants
Keyword enum is used to define enumeration types. For example:
enum color {yellow, green, black, white};
Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.


Comments

  1. Thanks a lot for the useful post. Keep updating. It really helped me get my work done.

    C++ Training in Chennai | C Language Training in Chennai

    ReplyDelete
  2. Thanks for this blog. You described each and every basic topic very well. Keep sharing more notes like this.

    Big Data Certification in Pune
    Big Data Testing Classes

    ReplyDelete
  3. I found best article.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  4. Your articles are very useful. They give me a lot of information. I may learn knowledge from them. I can't help reading your posts. It's very kind of you to share your insight. I really like your pieces of writing. They help me so much. Thanks a million. I am waiting new posts. Đăng ký VD300 VinPhone, Đăng ký MAXSV2 VinaPhone, Đăng ký 3G VinaPhone 1 ngày

    ReplyDelete
  5. Looking forward to reading more. Great blog.Really looking forward to read more. Keep writing
    sale notification website 2018
    sales pop master online

    ReplyDelete
  6. Looking forward to reading more. Great blog.Really looking forward to read more. Keep writing
    Discount master app


    Discount master app

    ReplyDelete
  7. For space outside the milk tea shop closer to nature, outdoor space should use speakers with greater capacity, products such as garden speakers, imitation stone speakers, are products with designs The most natural to create interesting feeling when enjoying. Because it must work in the weather conditions heat so the shell is made very firmly with high durability, the quality of the speaker is imported genuine. Good sound, making good clear sound even when used in wide space
    Gái gọi Mỹ Đình - Đình Thôn, gái gọi Nguyễn Khánh Toàn, gái gọi hà nội, gái gọi Phố Cổ - Quán Sứ

    ReplyDelete
  8. Looking forward to reading more. Great blog.Really looking forward to read more. Keep writing
    app autoketing
    https://apps.shopify.com/shipping-bar-master
    https://bit.ly/2BXG37u

    ReplyDelete
  9. This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks
    currency converter free, currency converter portable, autoketing

    ReplyDelete

Post a Comment

Popular posts from this blog

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …