An Introduction To The C language

The C programming language is a popular and widely used programming language for creating computer programs. C is a general-purpose structured programming language that is powerful, efficient and compact.

It was developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by Dennis Ritchie. C was an offspring of the Basic Cambridge Programming Language (BCPL) called B.

what is C language

The main characteristics of C language are:

  1. It is reliable, simple and easy to use.
  2. Programs written in C are efficient and fast. This is due to its variety of data types and powerful operators.
  3. There are only 32 keywords and its strength lies in its built-in functions.
  4. C helps in developing structured programs.
  5. C is highly suitable for recursive programming.
  6. It is also suitable for recursive programming.
  7. C is highly portable.
  8. It is highly suitable for writing both system software and business packages.
  9. We can add our own functions to the C library. Therefore, an important feature of C is its ability to extend itself.
  10. C is a format free language. No line numbers are needed. Need not place statements on a specified location on a line.
  11. Standard libraries for I/O, String manipulation, Arithmetic operations etc.
  12. Pointer implementation is available.
  13. Bitwise manipulation is also possible.
  14. C stands in between the assembly languages and high Level Languages. C has all the advantages of assembly language and it has all the significant features of modem HLLs (High Level Languages). That’s why it is often called a Middle Level Language.

CHARACTER SET:-

Like any other language, it has its own character set. A character denotes any letter or alphabet, digit or special symbol used to form word, number and expression. 

KEYWORDS:-

C contains 32 keywords that have a standard, predefined meanings. These keywords can be used only for their intended purpose, they cannot be redefined by the programmer.

All keywords must be written in lowercase. The keywords are also known as Reserved words. The list of all keywords are given in the Table:-

autodoubleintstruct
break elselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

CONSTANTS:-

A constants is a quantity that remains unchanged during the execution of a program. C supports several types of constants.

INTEGER CONSTANT:-

An integer constant refers to a sequence of digits without decimal point.

Rules for constructing integer constants are:

  1. An integer constant must have at least one digit.
  2. An integer constant contains neither a decimal point nor an exponent.
  3. Commas and blank spaces cannot be included within the number.
  4. Sign (+or-) must precede the number.
  5. Default sign is positive.
  6. The allowable range for integer constants for a 16 bit computer is -32768 to +32767 

There are three types of integer constants, namely, decimal, octal and hexadecimal.

  1. Decimal integers consist of a set of digits 0 through 9.

Example:- The following are valid decimal integer constants

18               -11                    +39         0        8752

On the other hand, the following are not valid integer constants

28.0            contains a decimal point.

12, 250       contains a comma.

5 –               Sign does not precede.

40  20         Blank spaces are not allowed.

  1. An octal integer constant consists of any combination of digits from 0 through 7, with a leading 0.

Example:- The following are valid octal integer constants 

015       0375       0        0764          05  

     (c) A hexadecimal integer constant consists of any combination of digits from 0 through 9 or alphabets (or A) through f (or F), with a leading OX or OX.

Example:- The following are valid hexadecimal integer constants

0x5          0x7ab        0x6f         0xabc        0x975

We generally use decimal integer constant or simply integer constant in programming.

REAL (FLOATING POINT) CONSTANTS:-

Real Constants are also called Floating point constant is a number that contains either a decimal point or an exponent (or both). The real constants could be written in two forms- Fractional form and Exponential form

  1. Fractional form real constants:-

Rules for Real Constants in fractional forms are:

  1. A real constant must have at least one digit.
  2. A real constant must contain a decimal point.
  3. Commas and blank spaces cannot be included within the number.
  4. Sign (+or-) must precede the number.
  5. It could be either (+ve) or (-ve).
  6. Default sign is positive.

Example:- The following are valid Real constants

35.5           0.045           0.0             2345.575

(b) Exponential form real constants:

The general form of real constants expressed in exponential form is mantissa a exponent 

mantissa a exponent

Rules:

  1. The mantissa is either a real number expressed in decimal notation or an integer.
  2. The exponent is always an integer number with an optional plus or minus sign.
  3. The mantissa part  may have a (+) or (-) sign. Default sign is (+) (positive).
  4. The mantissa part and the exponential part should be separated by a letter ‘e’.
  5. No commas or blank spaces are allowed.
  6. Range of real constants expressed in exponential form is -3.4 e -38 to 3.4 e 38

Example:- The following are the valid real constants expressed in exponential form.

0.5e3               -1.2e-7                 1.5e+9                   1.5e-3                  12e3

Example:- The following are not valid Real Constants.

25,000.0           ,is not allowed.

75                     Either a decimal point or an exponent must be present.

125 e 4.5          Exponent must be an integer.

$ 425 e -15       $ symbols are not permitted.

SINGLE CHARACTER CONSTANT:-

A single character constant is a single character, enclosed in apostrophes (single quotation marks). The character may be a letter, number, or special character.

Example:-

‘q’                 ‘a’                ‘:’                  ‘5’                ‘$’

STRING CONSTANTS:-

A string constant is a sequence of characters enclosed in double quotes. Total number of characters enclosed within the quotes will be called the length of string constant.

Example:-

“D.S.C”             “2001”             “4+8-6”                “X”

Read also: What is PHP? A complete PHP guide

Backslash Character Constants: Within character and string constants, the backslash character\ is special, and is used to represent characters not easily typed on the keyboard. The most common of these “character escapes” are given in the table:-

ConstantMeaning
\na “newline” character
\ba backspace
\ra carriage return (without a line feed)
\’A single quote (e.g. in a character constant)
\”a double quote (e.g. in a string constant)
\\a single backlash 
\0a null character

VARIABLES:-

A variable is a data name that may be used to store a data value. A variable may take different values at different times during the execution of a program. Variables must be defined before they are used in a program.

The Rules for variable name are:-

  1. A variable may consist of letters, digits and underscore(_).
  2. The first character must be a letter.
  3. Special symbols and blanks are not allowed.
  4. Keywords or Reserved words are not allowed.
  5. Both uppercase and lowercase letters are allowed and are considered to be distinguishable. e.g. the variable SUM is not the same as sum or Sum.
  6. A variable name may be as long as you wish, but the first 8 characters of a variable are recognized by C Compilers. The variable name accounts branch and accounts office would be regarded as identical variables in C.

Example:-

The Following are valid variable names

area                       sum                 Average                     A2

distance                 table1              Total1                         John

Example:-

The following are invalid variable names:-

Ram Kumar                    Blank spaces are not allowed

Char                               char is a keyword.

8th                                  The first character must be a letter.

price$                              $ sign is not allowed.

Read also: javascript functions in html

DATA TYPES:-

Various quantities such as constants, variables etc. occuring in a C program must have a type associated with them. There can be one and only one type associated with an entity establishes the following information about it.

  1. Its meaning
  2. Constraints applicable to it
  3. Possible value of the entity 
  4. Possible operations applicable on the entity
  5. function that can be used with it.

A data type is defined as a finite set of values along with a set of rules for permissible operations. Data in C belongs to one of the following types.

  1. Basic (or Simple) data types
  1. Integer (int.)
  2. Floating point (float)
  3. Character (char)

2. Structured Data types

  1. Arrays and Strings
  2. Structures
  3. Unions

3. Enumerated Data Types

4. Pointer Data Types

5. The Void Data Types

Structured data types, Enumerated data types and pointers data types are discussed as and when they are encountered. The void data set is discussed in the chapter on functions.

BASIC DATA TYPES:- 

Each type of data may be represented differently in the computer’s memory. Consequently, memory requirements of these data types will also be different. Various Basic Data Types with the size according to the 16-bits machine.

C supports a number of qualifiers that can be applied to the basic data types. They are

  • short
  • long
  • signed
  • unsigned

Also double stores double precision floating point numbers. An unsigned number is always+ve or zero.  With a signed modifier, the first bit is reserved for the sign(+ve) or (-ve) of the variable.

The use of qualifiers signed on integers or characters assumes a signed data type. Hence int or signed int have the same meaning, size and range.

Read also:What is JavaScript and Difference between the JavaScript and Java

DECLARATION OF VARIABLES:-

A variable can be used to store a value of any data type. The declaration of variables must be done before they are used in the program. The syntax for it is as follows

          data type v1, v2, ….., vn;

Where v1, v2, ….., vn, are the names of variables, separated by commas.

Example:-

int basicpay;

float sum, mean;

char name;

Here basicpay is an integer variable; sum and mean are of type real and name is a character variable.

STRUCTURE OF C PROGRAMS:-

  1. The link section provides instructions to the computer to link functions from the system library.
  2. The definition section defines various symbolic constants.
  3. Global declaration section consists of all global variables. Global variables are the variables that are used in more than one function.
  4. Every C program must have one and only one main ( ) function section. The declaration part of this section declares all the variables used in the executable part. There is at least one statement in the executable part.
  5. The subprogram section contains all the user-defined functions that are called in the main function.

All sections, except the main function section are optional. Comments can be included between the section, before the sections or after the section for increased readability.

THE ASSIGNMENT STATEMENT:-

The assignment statement is used to assign values to variables. The format is 

variable-name= expression;

Where expression can be a single constant or variable or an arithmetic or logical expression.

Example:-

Count=1;

flag= ‘C’;

FIRST= 5.2;

SUM= 3*4;

MULTIPLE ASSIGNMENT:-

If two or more variables are to be assigned the same value, the assignments can be combined. The format is :

variable=variable= variable= … =expression;

Example:-

A = B = C = 0;

It is also possible to assign a value to a variable at the time the variable is declared. This takes the following form:

data- type variable_name= constant;

Example:-

int a = 10;

float x = 234.75;

char flag = ‘y’;

COMMENTS IN A PROGRAM:-

Comments in a program are specified with the use of / * */. Its form is 

/* text  */

The text does not have to be enclosed within quotes. The matter enclosed between /* */ is ignored by the C compiler. Comments help the reader in understanding the flow of the program and what is being calculated at various stages. For example, we can write a comment as

/* Program to calculate the area of the circle */

FIRST C PROGRAM:-

Study the following program:

preprocessor statement → # include <studio.h>

main function       → main( )

           start of program            → {

printf (“Hello World”);

end of program             → } 

This program when executed, with produce the following output

Hello world

Let us have a close look at the program 

  1. This C program starts with #include <stdio.h>. This line includes the “standard I/O library that lets you read input from the keyboard (called “standard in”) and write output to the screen (called “standard out”), an extremely useful library.
  2. The main( ) is a special function to tell the compiler where the program starts. Every program must have exactly one main( ) function.
  3. The opening brace ‘{‘ indicates the beginning of the function main and the closing brace ‘}’ in the last line indicates the end of the function.
  4. The statement printf (“Hello World”); causes the string in quotation marks to be displayed on the screen.

printf ( ) is a print function which sends data given to it to the standard display device. Note that the print line ends with a semicolon (;) Every statement in C should end with a semicolon mark.

Suppose we want to print the above output in two lines as

Hello 

World

Modify the program as

#include <stdio.h>

main ( )

{

printf( “Hello \ n”);

printf( “World”);

}

The symbol ‘\n’ stands for newline character.  Therefore, you get the output split over two lines.

printf( ) FUNCTION:-

The general form of printf function is 

printf( “<Control string>”, <list of variables>);

Control string consists of three types of lines

  1. Characters that will be printed on the screen as they appear.
  2. Escape sequence characters such as\n.
  3. Format specifications that defines the output format for display of each variable. Format string could be:
%dforinteger values
%uforunsigned integer values
%fforfloat in real values
%eforfloat with an exponent values
%cforcharacter values
%oforoctal values
%xforhexadecimal values
%sforstring values

Example:-

#include<stdio.h>

main ( )

{

int n = 7;

printf (“There are % d days in a week. \n”, n);

}

Run

There are 7 days in a week.

There format specification for printing an integer number is 

%wd

Where w specifies the minimum field width for the output. The number written is right justified. It is possible to force the printing to be left justified by placing a minus sign directly after the % character.

The format specification for the real number is 

% w.pf

Where w specifies the minimum field width for the output and p indicates the number of digits to be displayed after the decimal point (precision). The default precision is 6 decimal places. The value when displayed is rounded to p decimal places.

Example:-

Study the following program

/* Printing of integer or real numbers */

#include<stdio.h>

main( )

{

int i = 1234;

float   x= 457.128;

printf  (“%2d %4d %6d \n”, i, i, i);

printf  (“%f    %7f  %7.2f”, x, x, x);

}

Run 

1234  1234   1234

457.128000       457.128000     457.13

SCANF( ) FUNCTION:-

scanf( ) function is used to read the values of variables through the keyboard. The format of scanf function is 

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

Control string consists of two types of items

  1. Escape sequence characters such as\n.
  2. Field (or format specifications), consisting of the conversion character %, a data type character (or type specifier), and an optional number, specifying the field width.

The ampersand symbol (&) before each variable name is an operator that specifies the variable name’s address. We must always use this operator, otherwise unexpected results may occur.

#include<stdio.h)

main( )

{

int a, b, ;

….

scanf (“ % d % d”, &a, &b);

…..

}

When the program is executed, two integer quantities will be entered from the standard input device (keyboard). Suppose the input data items are entered as 

44  88

Then the following assignments will result.

a = 44, b = 88

Example:-

Suppose the following statements are included in a program:

#include <stdio.h.>

main ( )

{

int a;

float b;

char c;

scanf (“ % 3 d % 5 f % c”, &a, &b, &c);

}

It the data items are entered as

40 856.875 F

When the program is executed, then 40 will be assigned to a, 856.8 will be assigned to b and character 7 will be assigned to c. The remaining two input characters (5 and F) will be ignored.

Example:-

Study the following program

#include <stdio.h>

main ( )

{

int n;

printf(“Enter an integer number \n”);

scanf (“%d”, &n);

printf (“Entered number is %d”, n);

}

Run

Enter an integer number

20

Entered number is 20

DEFINING SYMBOLIC CONSTANTS:-

Constants may be assigned names as well. Its format is 

#definesymbolicnameconstantvalue

Symbolic names have the same form as variable names. No blank space between the pound sign # and the word define is permitted. Example

#define F1 3.143

#define COUNT 10

#define AGE 20

Note that #define statements must not end with a semicolon. #define statement is known as a preprocessor.

Advantages of Constant declarations are:-

  1. The value of the constant can be changed in the program by making only modification. For example in order to increase the accuracy of the computed values, the value PI could be changed in the definition.
  2. The chance of error by typing in a wrong value is reduced.
  3. There is no need to repeat the value of the same constant in every expression.
  4. The readability of the program is also increased.

Example:- 

Study the following program

#include<stdio.h>

#define PASS_MARK 35

main ( )

{

printf ( “Pass Marks = % d”, PASS_MARK);

}

Run

Pass Marks = 35

TYPE CONVERSION ACROSS ASSIGNMENT:-

It is not necessary that the value being assigned to a variable be of the same type as the data type of variable. The integer, float and char value can be assigned to an integer variable. Study  the following programs.

Example:-

#include <stdio.h>

main( )

{

int a;

a=1;

printf (“\nwhen a=1 then output is %d\n”, a);

a=’ C’;

printf (“when a=’C’ then output is %d\n”, a);

a=5.2;

printf (“when a=5.2 then output is %d\n”, a)

}

Run

when a = 1 then output is 1

when a = ‘C’ then output is 67

when a = 5.2 then output is 5

Here if a= ‘C’; then ASCII value of ‘C’ is assigned to a and if a = 5.2; then truncated value (5) is assigned to a Note that type conversion takes place, automatically, at the time of assignment. Similarly, only char and int value can be assigned to char variables. Only float, double and int values can be assigned to float/double variables.

source: wikipedia

Leave a Comment