What is c programming? all you need to know

By | May 14, 2019

Introduction to c:

C programming is a high-level programming language where Instructions consist of algebraic expressions, English keywords such as if, else, for, do, while etc. In C programming, program can be divided down into several small modules. Hence, it can be also called a structured programming language.

  • C programming is flexible to use as system programming as well as application programming.
  • Huge collection of operators and library functions.
  • User can easily create own user-defined functions.
  • C compilers are easily available for all types of computers and program developed in C, can be executed in any computer with or without some alteration, hence it’s portable.
  • A program developed in C programming language is compact and highly efficient.
  • Because of modularity, it is easy to debug (to find the error).
  • it can also be used as a low-level language.

History of C:

C programming language was developed in the 1970s by Dennis Ritchie at Bell Laboratory. It was developed from earlier languages, called BCPL and B which were also developed at Bell Laboratory. It was confined within Bell Lab till 1978. later further Dennis Ritchie and Brian Kernighan developed the language. By the mid-’80s, it became more popular Later on, it was standardized by ANSI (American National Standard Institute).

Structure of a C program:

Every C program consists of one or more functions, one of which must be the main function. The program begins by executing the main function however it contains other many functions.

Each function must contain:

  • A function heading, which consists of function name and arguments enclosed in parenthesis.
  • A pair of a compound statement (curly braces).
  • It may consist of the number of input/output statements.
  • Library file access
  • Comments

program1:

/* A program to print Hello*/Comment
#include<stdio.h>Library file access
main( )Call of main function
{Compound statement start
printf(“\n Hello”);Output statement
}      Compound statement end    

program2:

/* A Program to find sum of two integer numbers 12, & 17 */

#include<stdio.h>

void main( )

{

      int x=12, y= 17;

      z= x + y;

      printf(” sum is %d”, z);

}

C fundamentals:

The fundamental elements of C programming includes  C character set, identifiers, keywords, data types, constants, variables, expressions and statements.

The c character set:

C uses uppercase (A to Z), the lowercase letters 
(a to z), the digits (0 to 9) and certain special characters such as:

!           ^          #          %         ^          &         *          (           )           ~ _       –

=          +          \           |           [           ]           {          }          ;           :           ‘

”           ,           <          .           >          /           ?          (blank)

Most of the versions of C also allow using @ $.

It can be a combination of certain characters such as \n, \t to represent non-printable characters new line, horizontal tab respectively. Such a character combination to print non-printable character is known as an escape sequence.

Identifiers:

Identifiers are the names given to the various elements of programs such as variables, functions and arrays. Identifiers consist of letters and digits. Rules are to be followed for identifiers:

  • It may consist of character, digits and others but the first character must be a letter.
  • It permits uppercase and lowercase but they not interchangeable.
  • It may begin with an underscore (_) too.
  • Most of C allows 31 chars.
  • Space and some special character are not allowed.

eg.       x1, sum, _temp, Table etc.

Some invalid identifiers are

1x, “x”, -temp, error flag etc.

Keywords:

There are certain reserved words in C programming, which are called keywords and such words have their predefined meaning. Such words are only used for their intended purpose.

The standard keywords are:

autoexternsizeofbreakfloat
staticcaseforstructchar
gotoswitchconstiftypedef
intuniondefaultlongcontinue
signedunsigneddoregistervoid
doublereturnvolatileelseshort
whileenum

Some compilers may also include:

ada                        far                          near                       asm                     fortran

pascal                    entry                      huge

Note: keywords must be in lowercase.

Constants:

Constant is a basic element of C programming which doesn’t change its value during execution of the program. Four basic types of constants in C programming are:

constant typeexampleillegal
integer200, -512,200; 3.0; 10 20; 090; 1-2
floating-point20.5; -2.5; 1.6e+81; 1,00.0; 2e+10.2
character‘a’; ‘3’; ‘ ‘; ‘\n’3
string“anuj”‘st xavier’s’

Variables:

It is an identifier that is used to represent some specified type of information within a designated portion of the program. A variable represents a single data item that can be a numerical quantity, or a character constant. Such data item can be accessed later in any portion of the program by referring the name of a variable.

Array:

An array is an identifier that refers to a collection of data items which all have the same name with different subscript but they must be the same data type (i.e. integer, floating point or character). Individual data item in an array is known as an array element.

e.g.

            int  a=4, b=5, c=2, d= -5, e=0;

            In the form of array, it can be expressed as follows:

            int  x[5] = {4, 5, 2, -5, 0};

where,

            x[0] = 4

            x[1] = 5

            x[2] = 2

            x[3] = -5

            x[4] = 0

Data types:

C program supports different types of data, each of which may be represented differently within the computer’s memory. But memory requirement for each data type may vary from one compiler to another.

Data typeDescriptionMemory in bytes
intinteger quantity2
charsingle character1
floatfloating point number4
doubledouble precision floating point number8

Declaration

All variables should be pre-declared before they appear in a program in order to reserve memory space. A declaration may consist of one or more variables of the same data type. A declaration begins with data type following with the desired number of variables and finally ends with a semicolon.

e.g.

            int   x=6, y=7, z;

            float  a=3.0, b=1.5e+5, c;

            char section=’a’, name[20] = “Xavier”;

/* The Program to find out area of a circle */

#include<stdio.h>

void main( )

{

      float a, r;

      printf(“\n Enter radius”);

      scanf(” %d”,&r);

      a = 3.1415 * r * r;       

      printf(” \n area of circle is %f”, a);

}

Expression

An expression is used to represents a single data item, such as a number or a character. The expression can consist of a single entity, such as a variable, a constant, an array element or a reference to a function. It may also consist of some combination of entities interconnected by one or more operators.

            a > b

           c = a + b

compound statement:

The compound statement consists of individual statements enclosed within a pair of braces ( { and }).

e.g.

            {

                        int  x=3;

                        printf (“%d”, x);

            }

Control statement :

           The control statement is a statement which controls the execution of other statements.

e.g.

            if(x>0)

                        printf(” x is positive”);

Symbolic Constant:

The symbolic constant is a name that substitutes for a sequence of characters. The substituted characters may be numeric, character or string constant. It replaces the instance of numeric, character or string constant in a program. In compiling the program with symbolic constant, each occurrence of a symbolic constant is replaced with its corresponding character sequence.

A symbolic constant is defined by writing

# define   name    text

e.g.

# define              PI          3.1415

#define               NAME     “Kathmandu”

program:

/* A Program to find area and perimeter of a circle */

# include <stdio.h>

# define  PI  3.1415

void main( )

{

      float a, r, p;

      printf(“\n Enter radius of circle”);

      scanf(“%f”,&r);

      a = PI * r * r;

      p = 2*PI*r;

      printf(” \n area of circle is %f”, a);

      printf(” \n perimeter of circle is %f”, p);

}

Leave a Reply

Your email address will not be published. Required fields are marked *