C & C++

WHAT IS C

C is a programming language developed at AT&T’s Bell Laboratories of USA in 1972. Dennis Ritchie was developed and written the C language in late seventies. At the time C was the replacement like PL/I, ALGOL but no one is competitor for C.

Later C was very popular because of its reliable, simple and easy to use. Out of the dozens of languages available , the prize of purity is often given to PASCAL, C’s pretty sister.

C was so popular because it is reliable, simple and easy to use . Basic Combined programming Language (BCPL), Developed by Martin Richards at Cambridge university aimed to solve this problem by bringing CPL down to its basic good features . But unfortunately it turned out to be too less powerful and too specific. Around same time a language called B was written by Ken Thompson at AT&T’s Bell Labs, as a further simplification of CPL. But like BCPL, B too turned out to be very specific.

Ritchie inherited the features of B and BCPL, added some of his own and developed C.

C’s compactness and coherence is mainly due to the fact it’s a one man language.

The purpose of developing C language is for creating system applications that direct interacts to the hardware devices such as drivers, kernals etc.

C programming is considered as the base for other programming languages, that is why it is known as mother language.

Evolution of C:

C Evolution
Evolution of C

 

WHERE is C

Let us now see how does C compare with other programming languages, All the programming languages can be divided into two categories.

I. Problem oriented languages or High level languages;

Higher level or problem oriented languages have been designed to give better programming efficiency. Examples of languages falling in this category are FORTRAN, BASIC, PASCAL.Etc.

II. Machine oriented languages or low level languages;

Machine oriented or low level languages have been designed to give a better machine efficiency, i.e faster program execution. Examples of languages falling in this category are Assembly language and machine language.

C stands in between these two categories. That’s why it is often called a middle level language, since it was designed to have both; a relatively good programming efficiency (as compared to machine oriented languages) and a relatively good machine efficiency (as compared to problem oriented languages.)

Features of C Language:

  1. Simple and easy to use – c programming language is simple because of its structured approach.
  2. Portable –  C programs can be run in many platforms/machines without changes or with change.
  3. Structured programming language – C divides the tasks into simple and design.
  4. Pointers – C supports pointers, pointers has direct access to memory.
  5. Rich functional libraries – C provides a lot of inbuilt functions that supports many features.
  6. Mid level programming language – . It is used to develop system applications such as kernel, driver etc. It also supports the feature of high level language. That is why it is known as mid-level language.
  7. Extensible –  It adopts new features quickly.
  8. Powerful –  C uses Wide verity of ‘Data Types‘, functions and loops and control statements.

Download from here and install C in your PC.

For installation follow the below steps

  1. Extract the rar file.
  2. Execute the  setup.exe  file by double click on it
  3. Choose the destination folder where to install ( I was installed in C drive)

   4. Click on complete

    5. Go to the folder where you installed C in your PC( in my system I was installed C in C:\TC folder)

     6. To enter into the c programming screen see the below screen shots.

         C:\cd  tc

         C:\TC>cd bin

         C:\TC\BIN>tc  …………….press enter here.

Then you will enter c programming area.

After press enter key you will enter into C programming screen like below.

 

Type the program and save like with a name.

To save program go to file and save or press ctrl+S.

      7.To run the program press ctrl+f9 or go to Run tab and press run.

You can view the user screen any time by pressing the alt+f5 keys.

Now press Esc to return to the turbo c++ console.

 

 

Tokens in C:

A Token is nothing a keyword, identifier, a constant, a string literal, or a symbol. C program have five these tokens.

  1. Every line in c program ends with semicolon; it is the terminator of the statement. (Compiler understands statement ends here)

       printf(“Hai C”);

    return 0;

     2. Keywords: C has 32 key words every key word has a meaning in c library so these are not used as variable or constants.

See the below list of keywords.

Auto else long switch
Break enum register typedef
Case extern return union
Char float short unsigned
Const for signed void
continue goto sizeof volatile
default if static while
Do int struct double

These keywords called as reserve words.

      3. Identifiers: C has identifiers, an identifier starts with a letter alphabets A to Z, a to z, or and an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9). C identifiers are used to identify variable or function.

C is case sensitive and it does not allow special characters like @,$,% .  for example cpp and CPP is not same.

        4.Comments :  Comments are used for helping text in the program. The comment is given in /* and */ these two symbols. The compiler will ignore the when it recognizes the symbols.

         /* my first program in C */

5.White spaces in c: White space is a blank space i.e whitespace is the term used to display the blank space, new line, empty characters etc. c recognizes the blankspaces. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins.

First C Program:

  1. First every c program must have one main() function.
  2. C program execution always start from main().
  3. Every statement ends with semicolon(;).

#include <stdio.h>

#include<conio.h>

void main()

{

printf(“Hello, world\n”);

getch();

}

#include <stdio.h> includes the standard input output library functions. The printf() function was defined in stdio.h file.

#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.

void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.

printf() The printf() function is used to print data on the console.

getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.

 

Compilation of C Program:  

By pressing ctlr+f9, the c program will compile.

To see the output alt+f5. (if you don’t give getch() function in the program).

Data types in C:

C has different types of data types. The data type is determines how much space is occupied by variable in the storage disk.

Mainly there are two types those are Pre- defined and User Defined.

Basic data types: These are arithmetic data types Integer, Char, float and Double.

Enumerated Type: enum is the arithmetic type used to define variables used to assign certain discrete integer values throughout the program.

Void data type: Void means no value.

Derived Data types: Derived data type means these are defined by the programmer at the time of programming based on the requirement.

Arrays, pointers, structures and union.

 

Data Types in C:

 

Data Types Size Range
Char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 127
Short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 32,767
Int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 32,767
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 32,767
long int 4 byte
signed long int 4 byte
unsigned long int 4 byte
Float 4 byte
Double 8 byte
long double 10 byte

 

The above shows the basic data types in c.

Note: If you want to know the exact size of the variable or a type use sizeof() operator .

Below program demonstrates how to use sizeof() operator in c and know the values of basic data types.

This is basic interview question for fresher use of sizeof() operator.

#include<stdio.h>

#include<conio.h>

void main(){

printf(“size of an integer: %d \n”,sizeof(int));

printf(“size of float: %d \n”,sizeof(float));

printf(“size of Character: %d \n”,sizeof(char));

printf(“size of double: %d \n”,sizeof(double));

getch();

}

 

Output is :

size of an integer:2

size of float: 4

size of Character:1

size of double: 8

Press  Alt +f9 and then Ctrl+f9 to compile and run the program.

 

Out put of the above program is:

Variables in C:

A variable is name which can hold different values at different instances. Here in C a variable is name of memory location used to store data or value. Its value can be changed and it can be reused many times. It is one of the way to represent memory location through symbol or character so that it can be easily identified. Each variable in C has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

 

A variable may be a letter, character or combination of characters and digits with special symbol underscore(_).

We know c is case sensitive so there always difference between A and a.

 

Syntax for variable declaration:  

Data type variable_list;

For example

int  a;

float b;

char c;

double d;

See the below variable types in C:

  1. Local variable
  2. Global variable
  3. Static variable
  4. Automatic variable
  5. External variable

Local Variable:

A variable which is declared within the block pr function is called local variable. Its scope is limited to that function or block or within the block.

A local variable must be declared starting of the block/function.

Void function_name(){

Int abc=90; // local variable declared starting of the block/function.

…..stmts;

}

Global Variable: A variable is declared outside of the function is called is global variable. Its scope is throughout the program and it is available to alla other functions in the program.

Float xyz=0.999;//global variable declared starting of the block

Void function_name(){

Int abc=90; // local variable declared starting of the block/function.

…..stmts;

}

Stmts;

 

Static variable:  A static variable is declared by using static key word and the variable is called static variable.

Retains its value between multiple function calls.
Static is used in the C programming language with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory.

static data_type var_name = var_value;

#include<stdio.h>

int fun()

{

  static int count = 0;

  count++;

  return count;

}

  

int main()

{

  printf(“%d “, fun());

  printf(“%d “, fun());

  return 0;

}

Output is:  1 2

Note : If the static is not used here then the output is 1 1. Static variable will print the incremented value.

Automatic Variable: By using auto keyword we declare automatic variables. In c all variables is declared inside the block, are automatic variables by default.

int main(){

int x =19;

auto int y=199; // automatic variable.

}

External variable:  eextern is used to let other C files or external components know this variable is already defined somewhere. We can share a variable in multiple C source files by using external variable.

Crate a file named with extendemo.h.

In externdemo.h

extern int xyz=1009;//external variable 

create a c program to use this file.

#include “externdemo.h”

#include<stdio.h>

void demoExtern(){

printf((“Global variable: %d”,xyz”)

}

It prints:  Global Variable 1009.

 

 

 

 

 

 

 

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.