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 -
- A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
- The first letter of an identifier should be either a letter or an underscore.
- However, it is discouraged to start an identifier name with an underscore.
- There is no rule on length of an identifier.
- However, the first 31 characters of identifiers are discriminated by the compiler.
- int – for storing integral numbers.
- float – for storing floating-point numbers.
- 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.
- Integer constants- -5 , 0, 100, oX50, oxfff, oxABF, 050
- Floating point constants - 125.8, –.0001,
- Character constants - ‘A’, ‘a’, ‘0’, ‘&’, ‘3’
- Character String Constants -A sequence of zero or more characters enclosed within double quotation marks represents a character string constant.
- Enumeration Constants
- Expression 128 + 7 – 17 is a constant expression because each of the terms of the expression is a constant value.
- 128 + 7 – i would not represent a constant expression beause i was represented as a variable.
1.Assign the value directly in the program.
Method 1: varible declaration and the assigning values as two separate statements;
data_typevariable; 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.
- %d , %i - Print as decimal integer
- %6d - Print as decimal integer, at least six characters wide.
- %f - Print as floating point
- %6f - Print as floating point, at least six characters wide.
- %.2f - Print as floating point, 2 characters after decimal point.
- %6.2f % - Print as floating point, at least 6 wide and 2 characters after decimal point.
- -4s - Print as four character string with left justified.
- %4s - Print as four character string with right justified.
Comments
Post a Comment