Variables and Data Types

Keywords / Reserve Words 

• Keywords are predefined, reserved words used in programming that have special meanings to the compiler. C is a case sensitive language, all keywords must be written in lowercase.

Identifiers 

• An identifier in C consists of a sequence of letters, digits, or underscore characters. However, if you give meaningful name to an identifier, it will be easy to understand and work on for you and your fellow programmers.

  • Rules for writing an identifier -
  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. 
  2.  The first letter of an identifier should be either a letter or an underscore. 
  3. However, it is discouraged to start an identifier name with an underscore. 
  4. There is no rule on length of an identifier. 
  5. However, the first 31 characters of identifiers are discriminated by the compiler.
Variable 
• Programming language enable you to assign symbolic names, known as variable names, for storing program computations and results.

• Variables can be used to store integers, floating-point numbers, characters, strings and even pointers to locations inside the computer’s memory.

Data Types 
• Now we know that, variables are placeholders used to store values. The data type of a variable determines how the bits representing those values are stored in the computer's memory. When you declare a variable, you can also supply a data type for it. All variables have a data type that determines what kind of data they can store.

• The C programming language provides int, float and char as basic data types. 
• Additionally there are double and _Bool types available. 
  • int – for storing integral numbers. 
  • float – for storing floating-point numbers. 
  1.  double – for storing double precision floating point numbers. 
  • char – for storing a single character. 
  • _Bool - for indicating an on/off, yes/no, or true/false situation. For storing value 1 or 0.
Storage size and Ranges 
Every value, whether it’s a character, integer, or floating-point number, has a range of values associated with it.

Data Types & Constants

In C, any number, single character, or character string is known as a constant
There are five different types of constants available in C programming language; 
  1. Integer constants-  -5 , 0, 100, oX50, oxfff, oxABF, 050
  2. Floating point constants - 125.8, –.0001,
  3. Character constants - ‘A’, ‘a’, ‘0’, ‘&’, ‘3’
  4. Character String Constants -A sequence of zero or more characters enclosed within double quotation marks represents a character string constant.
  5. Enumeration Constants
Constant Expression 
• Expressions consisting entirely of constant values are called constant expressions. 
 E.g. 
  1. Expression 128 + 7 – 17 is a constant expression because each of the terms of the expression is a constant value. 
  2. 128 + 7 – i would not represent a constant expression beause i was represented as a variable.
Statements 
• A program statement is any valid expression (usually an assignment or function call) that is immediately followed by a semicolon, or it is one of the special statements like compound, statement, break, continue, do, for , go to, if, return, switch, while and null statement. 

Variable Declaration 
• A variable that is declared to be of any data type does have storage reserved for it.
• Declaration is done by simply listing the variables before the terminating semicolon of the definition. 
EX:- data_type variable; int age;

• You can declare multiple variables in same type using a single statement as follows; 
Ex:-int height, width;

Assigning Values to Variables 
• After declaring a variable with a type, you can add some content or value to that variable.
• There are two ways to assign value to a variable: 

1.Assign the value directly in the program. 

Method 1: varible declaration and the assigning values as two separate statements; 

data_type 
variable; variable = value; 
int length; 
length = 20; 

 2.Ask from user to input a value and then assign that value

Method 2: direct method for variable declaration and initialization; 

data_type variable = value; int length = 20; 

scanf("%i", &number); 

The scanf routine, which accepts the response, has two arguments. The first ("%i") specifies what type of data type is expected (ie char, int, or float). The second argument (&number) specifies the variable into which the typed response will be placed. In this case the response will be placed into the memory location associated with the variable number. This explains the special significance of ).

Format Modifiers 

• Can be used to specify the required width of decimal integers and text strings. 

  1.  %d , %i - Print as decimal integer 
  2.  %6d - Print as decimal integer, at least six characters wide. 
  3. %f - Print as floating point 
  4. %6f  - Print as floating point, at least six characters wide. 
  5. %.2f - Print as floating point, 2 characters after decimal point. 
  6.  %6.2f % - Print as floating point, at least 6 wide and 2 characters after decimal point. 
  7. -4s - Print as four character string with left justified.
  8.  %4s - Print as four character string with right justified. 





 



















Comments