C – Program Code – Basics Part II

Submitted by Karthikeyan on

Some important points to keep rembers (contd..)

  1. Constants => 1. Numeric Constants, 2.Character constants
  2. Numeric constants => 1. Integer, 2. Real
  3. Character constants => 1. Single character, 2. String constants
  4. Integer Constants
  1. Decimal => 0 to 9 with optional + or –
  2. Octal => 0 to 7 with leading 0
  3. Hexadecimal – 0 to 9, A to F
  1. The largest integer value that can be stored is machine-dependent
    For 16 bit machines => 32767

For 32 bit machines => 2,147,483,647

 

  1. It is also possible to store larger integer constants by appending qualifiers such as U(unsigned), L (long), UL(unsigned long)
  2. Real (floating point) constants – to store numbers with fractional parts e.g. 3.14, -5.023
    Real number may also be expressed in exponential (or scientific) form e.g., 3.142148e5
  3. Single character constants enclosed with a pair of single quotes

e.g. ‘A’, ‘c’

 

  1. String constants – enclosed with double quotes
    e.g . “Hello!”
  2. Backslash character constants

\a – audibl alert

\b – back space

\f – form feed

\n – new line

\r – carriage return

\t – horizontal tab

\v – vertical tab

\” – double quote

\? - Question mark

\\ - backslash

\0 – null

 

  1. Variables are data names to store a data value
  2. Rules for variables are same as identifiers
  3. ANSI standard recongnizes a length of 31 characters, however in C99, at least 63 characters are significant
  4. Data Types
  1. Primary (or fundamental) data types
  2. Derived data types
  3. User-defined data types
  1. Five fundamental data types
    integer (int), character (char), floating point (float), double-precision floating point (double) and void
  2. Extended data types : long int, long double
  3. C99 – Additional data types:
    _Bool, _Complex, _Imaginary
  4. Data type ranges in 16-bit machines

char               -128 to 127

int                   -32,768 to 32,767

float               3.4e-38 to 3.4e+e38

double          1.7e-308 to 1.7e+308

 

  1. Integer types : short int, int, long int
  2. Floating point types : float, double, long double
  3. User-defined type declaration based on existing type:

            typedef type indentifier
e.g. :
typedef int marks

 

  1. Enumerated data types

enum identifier{value1, value2, … valuen}

e.g. enum day{Monday, Tuesday,…. Sunday}

  1. Storage Classes – information about their location and visibility

auto – Local variable known only to the function in which it is declared (default)

static – retains its value even after the control is transferred to the calling function

extern – Global variable known to all functions in a file

register – Local variable which is stored in register

 

  1. C Output format specifies

%i or %d

int

%c

char

%f

float

%lf

double

%s

string

 

 

 

 

 

 

e.g.
printf(“%3d”,n)

Here, the number (3) tells the compiler that we want to reserve 3 digit spaces for output.

 

printf(“%4.2lf”,n)

Here, totally 4 digit spaces with 2 digit decimal places.

 

  1. Reading data from keyboard

scanf(“control string”, &variable1, &variable2,…);

 

e.g., scanf(“%f %f %d”, &amunt, &intrate, &years);

 

(For reading data use “&”(It specifies address of the variable), for printf no need.)

 

  1. Some compilers permit using prompt message with scanf statement. e.g. scanf(“Enter age %d”, &age);
  2. Symbolic constants are defined using #define directive.

e.g. #define PI 3.14159

  • No ‘=’ sign
  • No space between define and ‘#’
  • No semicolon (;)
  • No multiple definitions in single statement (line)
  • ‘define’ should be in lowercase
  • No symbol ($) permitted
  1. A variable can be defined as constant using ‘const’ data type qualifier. It tells the compiler that the value of the variable must not e modified by the program.

e.g. cons tint class_size = 40;

 

  1. In contra with ‘const’, ‘volatile’ qualifier can be used to tell explicitly the compiler that a variable’s value may be changed at any time by some external sources.
    e.g. volatile int date;
  2. To avoid changing of a variable by its own program (but external resources allowed to modify),  the variable can be defined as follows:

e.g volatile const int location = 100;

 

  1. The new type qualifier ‘restrict’ is introduced in C99, that is applied only to pointers

e.g. int *restrict p1;  void *restrict p2;

A pointer declared “restricted” is the only means of accessing the object it point to.