Elements of C

By Bipin Tiwari

 Important Questions

Character Set

The set of characters that are used to words, numbers and expression in C is called c character set. The combination of these characters form words, numbers and expression in C. The characters in C are grouped into the following four categories.

  1. letters or alphabets
  2. Digits
  3. Special Characters
  4. White space

1. Letter or Alphabets

  1. Uppercase alphabets – A…Z
  2. Lowercase alphabets – a…z

2. Digits

All decimal digits – 0 1 2 3 4 5 6 7 8 9

3. Special Characters

Symbol Meaning Symbol Meaning
, comma & ampersand
. period ^ caret
; semicolon * asterisk

etc.

4. White Spaces

  1. Blank space
  2. Horizontal tab
  3. Vertical tab
  4. carriage return
  5. New line or line feed
  6. Form feed

Keywords

Keywords are predefined words for a C programming language . All keywords have fixed meaning and these   meanings cannot be changed . They serve as basic building blocks for program statements. ANSIC keywords are   listed below .

auto , double, int , struct, break, else, long switch, case, enum, register, typedef, char, extern, return,union, const, float, short, unsigned, continue, signed, void, for, default, goto, sizeof, volatile, do, if, static, while.

Thus, the keywords are also called reserved words.

Identifiers

Every word used in C program to identify the name of variables, functions, arrays, pointers and symbolic constants    are  known as  identifiers . They are names given by the user and consist of a sequence of letters and digits, with a        letters as the first character. The underscore character can also be used to link between two words in long                      identifiers .

Data Types

ANSIC supports three classes of data types.

  1. Primary data types
  2. User defined data types
  3. Derived data types

(A) Primary Data Types

Primary data types are categorized into five types:

  1. Integer type (int)
  2. Floating point type (float)
  3. Double precision floating point type (double)
  4. Character type (char)
  5. void type (void)

The words in parentheses indicate the keyword used in C to indicate the data type.

i. Integer Types

Integers are whole numbers, i.e. non fractional numbers. Generally integers require 16 bit of storage. C has further three classes of integer – integer (int), short integer (short int) and long integer (long int), in both signed and unsigned forms. The appropriate data type is used according to our requirement i.e. what type of number is to be used.

ii. Floating Point Types

Floating point types are fractional number (i.e. real numbers). They are defined in C by the keyword float. Floating numbers reserve 32 bits (i.e. 4 bytes) of storage, with 6 digits of precision.

iii. Double Precision Floating Point Type

When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A number data type number used 64 bits giving a precision of 14 digits. These are known as double precision numbers. to extend the precision further, long double can be used which uses 80 bits giving 18 digits of  precision.

iv. Character Type

A single character can be defined as a character type data. characters are stored in 8 bits . The qualifier is char. The qualifier signed or unsigned may be used with char. The unsigned char has values between 0 and 255. The signed char has values from -128 to 127. The conversion character for this type is c.

v. void Type

The void type has no values. This is usually to specify a type of function when it does not return any value to the calling function, This is discussed in more detail in Functions chapter.

 B. User-Defined Data Types

C supports a feature called type definition which allows users to define an identifier that would represent an existing data type. The typedef statement is used to give new name to an existing data type. It allows users to  define new data types are equivalent to existing data types . It takes the general form

typedef  existing_ data_ type new_ name_ for_ existing_ data_ type;

Here, existing_ data_ type  refers to any one of the fundamental or user defined data types; new_ for_ existing_  data_ type refers to a new identifier name.

 C. Derived Data Types

There are some data types which are derived from the existing primary data types. For example:

struct st1{
     int a;
     float b;
     char c;
}

Here ‘struct’ is a derived data types ‘structure’ . it consists of integer, float and character variables. structures,   unions and enumerations are the derived data types used in C.

Constants

A constants is a quantity that does not change during the execution of a program. C constants can be divided into different categories. constant is divided into numeric and character constants. Integer and real constants lie in numeric constants while character and string constants lie into character constant types.

Variables

A variable is a symbolic name which is used to store data item, i.e. a numerical quantity or a character constant. Variables are defined in computer program to represent an item of data input by the user, any intermediate calculation or the end results.

Tokens in C

The basic elements recognized by the C compiler are the “tokens”. A token is source program text that the compiler does not break down into component elements . The keywords , identifiers, constants, string literals, operators, the special symbols: brackets([ ]), braces ({ }) , parentheses (()), and commas(,) are examples of tokens.

Example

Write a program that will convert temperature in Centigrade into Fahrenheit.

#include<stdio.h>
int main(){
    float centi, fah;
    printf("Enter Temperature in Centigrade");
    scanf("%f", &centi);
    fah= (9 / 5 * centi) + 32;
    printf("The Equivalent Temperature in Fahernheit is:%.2f", fah);
    return 0;
}

OUTPUT
Enter Temperature in centigrade: 41
The Equivalent Tempreature in Fahrenheit is: 80

Example

Write a program to find interest for given principle amount ‘p’, time ‘t’ years and rate ‘r’%.

#include<stdio.h>
#include<conio.h>
int main(){
    float p, t, r, i;
    printf("Enter Principle in Rs");
    scanf("%f", &p);
    printf("Enter Time in Year");
    scanf("%f", &t);
    printf("Enter Rate in percent per anum");
    scanf("%f", &r);
    i=p*t*r/100;
    printf("The intrest is.%2f", i);
    return 0;
}

OUTPUT
Enter Principle in Rs: 1000
Enter Time in Year: 2
Enter Rate : 20
The intrest is :Rs400
Important Questions
Comments
Subscribe
Notify of
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments