Header Ads Widget

Ticker

6/recent/ticker-posts

Variables , Constants and Keywords in C - Part 2

                 


            In this 2nd part of Full C programming language course, we will learn about Variables, Constants and keywords in C language.

If you want to learn programming, you need to know some basic variables first. Variables store data, such as numbers, text, Boolean values (true/false), and objects. You can use them to create calculations and perform operations on those numbers. In this tutorial, we’ll look at what variables are, how they work, and how to declare and initialize them in C.

What Are Variables?

In computer science, a variable is any item that holds a value. These values could be numbers, letters, words, Boolean values (true or false), or even objects. If you have ever worked with spreadsheets before, you probably already have experience with variables. In Excel, for example, cells are considered variables.

When working in C, variables are declared using the keyword “variable” followed by the name of the variable. Variable names should always start with lowercase letters since they are case-sensitive. A variable only stores information; it does not do anything else. To make a change to the variable, you would use assignment statements. Assignments allow you to set the value of a variable.

To declare a variable, use the following statement without single quotes:

`int varName = 10;`


The above code declares a variable named `varName`. In order to assign a value to the variable, use an assignment statement:

`varName  = 20;`

A variable can hold different types of values, depending on its type. Let's say you wanted to declare two variables—one to hold integers, and the other to hold characters. To do this, use the following syntax:

`char myCharVariable = 'a';        //declares an integer variable called myCharVariable`


`char myStringVariable = "Hello World!";    //declares a string variable called myStringVariable`


There are three basic types of variables: numeric, character, and Boolean. Each type has its own special characteristics.

Numeric variables work similar to their counterparts in math. Numeric variables store actual numbers. They may represent whole numbers, integers, fractions, decimals, negative numbers, or floating point numbers. You cannot store non-numeric values (such as strings) in a numeric variable.

Character variables are much simpler than numeric ones. They simply hold characters. This means that each character represents one number, just like the digits 0–9. When declaring a character variable, you don't need to specify the size of the variable. It defaults to one byte in length. To declare a character variable, use the following declaration:

`char myCharVariable = 'a';

Variables

In programming languages variables are stored memory locations that hold values. A variable's value can then be changed to store any type of data. In c++, we define variables using "variable name" followed by ":" (colon) and followed by its type. E.g., double x 5; means double x is a variable of type double, holding a value of 5.

Types

Types consist of a set of rules for how each piece of data is interpreted. If you give a program a number, it will probably do something different than if you gave it a string. There are two types of variables, integer and floating point. Integer numbers are whole numbers without decimals (0-255). Floating points numbers have decimal places (0-999), and can go much higher than integers can (I'm not sure what happens after that.)

Values

The value of a variable is whatever data was assigned to it at some point in time. Once a variable has a value, it cannot change until the line where it was declared again. E.g., once variable x equals 0, it cannot be modified to equal 10 unless it is re-assigned. The same goes for strings. You can't modify them once they've been initialized. So if you declare a string s"hello", then write s"world", it won't work because you're trying to assign a new string to a variable that already has one. Instead, you need to use strcpy(new_string,"world") or strcat(currentString," world").

Declaring variables

You can declare variables before assigning them a value, which is called declaring them "before use." When you declare a variable, you tell the compiler that you intend to use it later. To declare a variable, just put it inside parentheses. E.g., int x10; declares a variable named x that holds the number 10.

Rules for naming Variables

1. You can name a variable any way you want. If it's a string, use double quotes around the name. Otherwise don't bother.

2. Variables do not need to start off with lowercase letters.

3. Variable names should be between 4 and 30 characters long and may only contain numbers, letters (A-Z), underscores (_) and hyphens (-).

4. Do not put spaces after commas or semicolons.

5. Variables that hold data type information (int, float etc.) should always have _ at the end of their name.

6. Only use underscore (_) if there is a space between words. Use a single underscore for a singular word and two or more underscores for a plural word.

Variable names should be simple, descriptive, and meaningful.

It's important to remember that even though we may not know the meaning behind a variable name, our code should make sense to us.

The following rules help guide you in choosing good variable names:

- Variable names should be short.

A long variable name makes the program hard to read and understand. Shorter variable names save time while writing your programs.

- Variable names should start with lowercase letters.

Use uppercase only if you need to distinguish the variable from other variables.

- Variables should have clear meanings.

If your variable stands for something specific and unique, use one word. If your variable represents any kind of thing, use several words. For example, instead of using 'int i' for the number of times a loop runs, use 'count'.

- Variable names don't need to contain numbers.

Variable names should follow the same capitalization rules as other parts of the code.

- Variables names should not begin with a number.

Numbers shouldn't appear at the beginning of variables names unless they're used as indexes or counters.

- Use underscores instead of spaces between words.

Assigning values

Once you declare a variable, it can be given a value. A value is basically a data object. Any data type can be assigned to a variable. E.g., a char, short, float, long, long long, etc... Variable names are case sensitive. One exception to this rule is that you can leave off a variable's type, as long as you know exactly what kind of data you want it to hold. E.g., char *str instead of char*str would mean the pointer variable str holds a character array, i.e. a string. You can only do this when you're pretty sure about the data type you want to assign. On the other hand, simply typing int x; assigns the current value of x to x, and does nothing else.

Assignment operators

An assignment operator takes 2 things, the right side of the assignment and the left side of the assignment. So the statement xy; means take the value y and assign it to the variable x. E.g., x5; means take the value 5 and assign it to x.

 Incrementing/Decrementing operators

There are many ways to increase a number, but the most common ones are ++ and --. ++ increments a number by 1, while -- decrements a number by 1.

Constants

Constant variables keep their value throughout the execution of the code and cannot change once they have been initialized. Variables marked constant retain their values even after changing them (due to reassignment) and those that are not declared constant do not retain their values unless they are re-assigned.

Constants:

A variable whose address is stored at compile time and the same value is assigned each time it is referenced.

An integer constant, character literal, floating point constant, enumeration constant, long literal, short literal or void expression.

Variables:

A variable whose memory location is determined only at run time. A variable's contents may be changed whenever its value is altered.

The type of the variable determines what kind of information about the variable (its name, address, size and whether it is static or dynamic) may be accessed by the compiler and/or runtime system.

In C, the keyword const is used instead of the word “constant”.

Keywords

 Keyword is a word or sentence that describes a particular object or concept. When you search something on Google, if you type keywords, then you’ll get the best results.

 For example : If we have a database table with name 'employee' and columns id,name,salary. Then keyword could be name, salaries, employee.

Keywords in C Language

 In programming languages, keywords are special words which tell the compiler what exactly the program should do. There are two types of keywords in C programming language -- 

1) identifiers; 
2) Reserved Words.

 Identifiers (also called keywords) indicate what kind of data a variable represents. In C language, variables represent only numeric values, strings, pointers, etc. Identifiers are written between double quotation marks. The following are some examples of identifiers in C programming language:

 • int i  10; - identifier i denotes the integer i with value 10.
 • char c  ‘a'; - identifier c denotes the character ‘a’ with the value 65.
 • double *pi 

 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555932185900479166009240512056026102808101404808201805152915075682868906005630427005481526020016023350875416626044085378276305474536938183054733086031112037098505487374490749096518791925080957064622137147365186523560518518050691393311030010536161636872169464965624328718839677336268278610719322383113666896648932676723393127668867551334239073585556054245381352261742536889149657398205018455640518521061789049156290208031774951651752062882402273722298334994603544553492504679987032979902002122487334231876888834963285326904287508067056877088073744838437828248485352294890763265678817660933960627769627568313830686273144594048329092256610401570727730049600093702150175411301116959100119744530467260061032583236887110958977818412648436803855870546869683946582324279002625490190800410846101089647888091291661732516877969800344884293558854299237698251831068218164685246115992166845869191025428542175736683761589438292604653271121461961991493644752497982097795769990892040070159538851195158203125306205163306409193702242892552585546875500699665775093419679854855038061355904116295415068470869687689151615510852033818212220869565217185

For example, if, while, auto, struct etc are all keywords in C language. and you cannot use this as the names of variables. since they are already reserved in c language. There are a total of 32 keywords in C language. 

Access previous parts of this course by clicking the links below:

Part 1 -- Click Here!!

Post a Comment

0 Comments