Pages

Wednesday 31 March 2021

Basic structure of a “C” program:-

Documentation Section

/* Program for addition of 2 numbers */

Link Section (or) 
Preprocessor Directives
#include<stdio.h>
#include<conio.h>

Definition section

#define pf printf

Global Declaration Section

 

Main() Function Section

          

           main()

           {

              Declaration part;

              Execution part;

           }

Main() Function Section

 

    main()

     {

       int a,b,c;

       clrscr();

       printf(“Enter a and b values:”);

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

       c=a+b;

       printf(“sum is %d”,c);

    }

Sub Program Section (or)

User-defined function Section

Function 1

Function 2

-----------------

Function n

 


Write a “C” program to print a message.

Method 1:-
/*print a message 
  of your choice */
#include<stdio.h>
#define pf printf
main()
{
pf(“welcome to “C”);
}
Method 2:-
//print a message
#include<stdio.h>
#define pf printf
Void main()
{
pf (“welcome to “C”);
}
Method 3:-
//print a message
#include<stdio.h>
#define pf printf
int main()
{
Pf(“welcome to “C”);
return 0;
}


/* Program to illustrate the different data types and there number of bytes occupied */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=22;
 char b[]="raj kumar";
 char c='f';
 float d=143.5;
 double e=1234.54;
 clrscr();
 printf("\n the string is : %s",b);
 printf("\n the character is :%c",c);
 printf("\n the number of bytes occupied by char is :%d",sizeof(c));
 printf("\n");
 printf("\n the integer is %d",a);
 printf("\n the number of bytes occupied by int is :%d",sizeof(a));
 printf("\n");
 printf("\n the float value is %f",d);
 printf("\n the number of bytes occupied by flaot is :%d",sizeof(d));
 printf("\n");
 printf("\n the double value is %lf",e);
 printf("\n the number of bytes occupied by double is :%d",sizeof(e));
 getch();
}

output:


//Program to illustrate the Local and Global variables 

#include<stdio.h>
#include<conio.h>
int global;
void main()
{
 int local;
 clrscr();
 printf("\n the local variable is : %d",local);
 printf("\n the global variable is : %d",global);
 getch();
}

output:

Tuesday 30 March 2021

C -Tokens - Keywords, Identifiers ,Constants

C TOKENS:

The Smallest individual units are known as ‘C’ Tokens.‘C’ as six types of tokens:

1. KEY WORDS

2. IDENTIFIERS

3. CONSTANTS

4.VARIABLES

5.READING VARIABLES or INPUT VARIABLE

6.PRINTING VARIABLE or WRITING VARIABLES or OUTPUT VARIABLE 

1. KEY WORDS:

Key words are the pre-defined names and the main meaning of keywords can’t be changed during the execution of programs and all the keywords must be written in Lower Case.

The following list shows the reserved words in ‘C’. These reserved words may not be used as constant value or as other identifier.

auto

double

int

struct

break

else

long

switch

case

enum

register

type def

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

size of

volatile

do

if

static

while







2. IDENTIFIERS:

Identifiers refers to the names of the variables ,functions, arrays. These are user defined names and consists of a sequence letters and digits. Both upper and lower case letters are permitted.

NOTE: The Underscore(_) characters are also permitted in identifiers.

             Example:  Ram_charan123, Ram_123

3. CONSTANTS:

The term constant means that it does not change during execution of a program.



I)NUMERICAL CONSTANTS: They are of two types,

1 Integer constants

2.Floating point constants

1.INTEGER CONSTANTS:

These are three types of constants. Integer constants are allowed in ‘C’. They are

1.Decimalconstants

2.Octalconstants

3.HexaDecimalconstants

DECIMAL CONSTANTS:

The allowed digits in decimal constants are 0,1,2,3,4,5,6,7,8,9.

Example:(1459)10 whose base or radix is 10 

OCTAL CONSTANTS:

The allowed digits in octal constants are 0,1,2,3,4,5,6,7. The octal constants are preceded  by‘0’

Example: (147)8 whose base or radix is 8

HEXA DECIMAL CONSTANTS:

It is a sequence of digits from 0-9,10,11,12,13,14,15

                                                              A , B,  C,  D,  E,  F

Example: (9A4F)16 =(0x9A4F)16 whose base or radix is 16 

(14AF)16=(0x14AF)16

2.FLOATING POINT CONSTANTS:

These are represented in two ways. They are

1.Fractional Form

2.Exponential Form

FRACTIONAL FORM:

This must have atleast one decimal point to the right of decimal point.

Example:145.7,7.54

EXPONENTIAL FORM:

It consists of a Mantissa and an exponent. The Mantissa must have atleast one digit after the decimal .The Mantissa is followed by a letter ‘e’or‘E’ and the exponent. The exponent must be an integer with out a decimal point and must have atleast one digit.

Example:

1.4e3=(14/10)* 103=(14/10)*1000=1400.000000

15.67e4=(1567/10)*104=(1567/100)*10000=15670.000000

147.5e5=(1475/10)*105=(1475/10)*100000=14750000.000000

197.65e7=(19765/100)*107=19765*105=1976500000.000000


II) CHARACTER CONSTANTS: They are of 3 types. They are:

1.Single character constant

2.String constant

3. Back slash constant

1.SINGLE CHARACTER CONSTANT:

It contains a single character enclosed with in a pair of single quote marks.

Example:‘A’,’5’,‘H’

2.STRING CONSTANTS:

          It is a sequence of characters enclosed in double quotes. The character may be a number (or) a letter (or) a special character (or) blankspace.

Example: “A”, “Well Done”, “1943 Love Story”

3.BACK SLASH CHARACTER CONSTANTS :

'C' supports some special Back Slash Character Constants that are used in output functions. This character constants are known as ‘Escaped Sequences’.

Example:

\n–New line

\b–Back Space

\a–Bell Sound

\t–Tab

4.VARIABLES :

A variable is a data name that may be used to store a data value,unlike constants they do not remain unchanged during the execution of program.A variable may take different values at different times during execution

Example: sum,total etc

RULES FOR VARIABLES :

They must begin with a letter

ANSI standards recognizes a length of 31 characters However length should not be normally more then 8 characters since only the first 8 characters treated significant by many compilers

Upper case and lower case are significant 

Example: TOTAL & total are different

It should not be a keyword 

5.READING VARIABLES or INPUT VARIABLE :

A variable which reads the value from the keyboard with the function scanner is 

scanf("control string" ,argument list);

The control string parameter tells how many values and what type of values reading from the keyboard

The argument list parameter tells where to store the values of these variables ,here the address can be obtained by placing an am-percent symbol (&) before the variable name

6.PRINTING VARIABLE or WRITING VARIABLES or OUTPUT VARIABLE :

A variable which prints the values of the variables by using a function printf is 

printf("control string" ,argument list);

The control string parameter may include either of string of characters or format specifiers or both which tells the type of values to be printed  

The argument list parameter tells which variable to be printed

Example: printf("The answer to the above is %d",n);

Saturday 27 March 2021

How to find the ranges of Basic C Data types




Signed : (2n-1) - 1
Min : - (2n-1
Max: + (2n-1) - 1

Unsigned:(2n) - 1
Min :0
Max: (2n) - 1

1.Example: 
Char=1Byte =8bits 
Signed Char: (2n-1) - 1
Min : - (28-1) = -128 
Max: + (28-1) - 1= +127

Unsigned char:(2n) - 1
Min :0
Max: (28) - 1=255

2.Example: 
Int=2Bytes=16bits
Signed int: (2n-1) - 1
Min : - (216-1) = -32768 
Max: + (216-1) - 1= +32767

Unsigned int:(2n) - 1
Min :0
Max: (216) - 1=65535

3.Example:
Float=4Bytes=32bits
Signed int: (2n-1) - 1
Min : - (232-1) = -2.14748365E9 
Max: + (232-1) - 1= +32767

Unsigned int:(2n) - 1
Min :0
Max: (216) - 1=65535

Float=4
Double=8

DATA TYPES RANGES :

Data Type

Memory (In Bytes)

Format Specifiers

Range

int

4

%d

-2,147,483,648 to 2,147,483,647

short int

2

%hd

-32,768 to 32,767

unsigned int

4

%u

0 to 4,294,967,295

unsigned short int

2

%hu

0 to 65,535

long int

4

%ld

-2,147,483,648 to 2,147,483,647

unsigned long int

4

%lu

0 to 4,294,967,295

long long int

8

%lld

-(263) to (263)-1

unsigned long long int

8

%llu

0 to 18,446,744,073,709,551,615

char

1

%c

-128 to 127

Signed char

1

%c

-128 to 127

Unsigned char

1

%c

0 to 255

float

4

%f

double

8

%lf

long double

12

%Lf

FORMAT SPECIFIERS 

Format Specifiers

Data Type

Elucidation

%d, %i

int
short
long
unsigned short

Used for integers

%c

char
signed char
unsigned char

Used for characters with any type modifier

%f

float

Used for decimal values

%e, %E, %g, %G

float
double

Used for scientific notation of decimal values

%hi

short

Used for signed short integers

%hu

unsigned short

Used for unsigned short integers

%l, %ld, %li

long

Used for signed integers

%lf

double

Used for floating-point

%Lf

long double

Used for floating-point

%lu

unsigned int
unsigned long

Used for unsigned integers

%lli, %lld

long long

Used for signed long integers

%llu

unsigned long long

Used for unsigned long long integers

%s

char *

Used for a string

%p

void *

Used when finding the address of the pointer to void *

%o

int
short
long
unsigned short
unsigned int

Used for the octal representation of Integer.

%u

unsigned int
unsigned long

Used for unsigned integers

%x, %X

int
short
long
unsigned short
unsigned int

Used for the hexadecimal representation of Unsigned Integer

%%

Used to print % character

%n

Used to print nothing


METHOD 1:

#include<stdio.h>

#include<limits.h>

int main()

 {

   printf("The number of bits in a byte = %d\n", CHAR_BIT);

   printf("\nThe minimum value of Signed CHAR is = %d\n", SCHAR_MIN);

   printf("The maximum value of Signed CHAR is = %d\n", SCHAR_MAX);

   printf("The minimum value of CHAR is = %d\n", CHAR_MIN);

   printf("The maximum value of CHAR is = %d\n", CHAR_MAX);

   printf("The maximum value of Unsigned CHAR is = %u\n", UCHAR_MAX);


   printf("\nThe minimum value of Signed Short is = %d\n", SHRT_MIN);

   printf("The maximum value of Signed Short is = %d\n", SHRT_MAX);

   printf("The maximum value of Unsigned Short is = %u\n", USHRT_MAX);


   printf("\nThe minimum value of Signed INT is = %d\n", INT_MIN);

   printf("The maximum value of Signed INT is = %d\n", INT_MAX);

   printf("The maximum value of Unsigned INT is = %u\n", UINT_MAX);


   printf("\nThe minimum value of Signed LONG is = %ld\n", LONG_MIN);

   printf("The maximum value of Signed LONG is = %ld\n", LONG_MAX);

   printf("The maximum value of Unsigned LONG is = %lu\n", ULONG_MAX);

   return 0;

 }

Output:

The number of bits in a byte = 8


The minimum value of Signed CHAR is = -128

The maximum value of Signed CHAR is = 127

The minimum value of CHAR is = -128

The maximum value of CHAR is = 127

The maximum value of Unsigned CHAR is = 255


The minimum value of Signed Short is = -32768

The maximum value of Signed Short is = 32767

The maximum value of Unsigned Short is = 65535


The minimum value of Signed INT is = -2147483648

The maximum value of Signed INT is = 2147483647

The maximum value of Unsigned INT is = 4294967295


The minimum value of Signed LONG is = -9223372036854775808

The maximum value of Signed LONG is = 9223372036854775807

The maximum value of Unsigned LONG is = 18446744073709551615


METHOD 2:

The following are the constants defined in the header file <float.h>

#include<stdio.h>

#include<float.h>

int main()

 {

   printf("The minimum value of float is = %.10e\n", FLT_MIN);

   printf("The maximum value of float is = %.10e\n", FLT_MAX);


   printf("\nThe minimum value of Double is = %.10e\n", DBL_MIN);

   printf("The maximum value of Double is = %.10e\n", DBL_MAX);


   printf("\nThe minimum value of LONG Double is = %.10Le\n",LDBL_MIN);

   printf("The maximum value of LONG Double is = %.10Le\n",LDBL_MAX);


   return 0;

 }

Output:-

The minimum value of float is = 1.1754943508e-38

The maximum value of float is = 3.4028234664e+38


The minimum value of Double is = 2.2250738585e-308

The maximum value of Double is = 1.7976931349e+308


The minimum value of LONG Double is = 3.3621031431e-4932

The maximum value of LONG Double is = 1.1897314954e+4932


METHOD 3:
PROGRAM TO FIND THE MIN AND MAX VALUES OFTHE GIVEN DATA TYPES:

# include <stdio.h>
# include <float.h>
# include <conio.h>
# include <limits.h>
void main()
{
  clrscr();
   printf(" The smallest value of signed char is %d\n", CHAR_MIN);
   printf(" The largest  value of signed char is %d\n", CHAR_MAX);
   printf(" The largest  value of unsigned char is %u\n", UCHAR_MAX);
   printf("\n");
   printf(" The smallest value of int is %d\n", INT_MIN);
   printf(" The largest  value of int is %d\n", INT_MAX);
   printf(" The smallest value of short int is %hd\n", SHRT_MIN);
   printf(" The largest  value of short int is %hd\n", SHRT_MAX);
   printf(" The smallest value of long int is %ld\n", LONG_MIN);
   printf(" The largest  value of long int is %ld\n", LONG_MAX);
   printf(" The largest  value of unsigned short int  is %u\n", USHRT_MAX);
   printf(" The largest  value of unsigned int  is %u\n", UINT_MAX);
   printf(" The largest  value of unsigned long int is %lu\n", ULONG_MAX);
   printf("\n");
   printf(" The smallest value of float is %e\n", FLT_MIN);
   printf(" The largest  value of float is %e\n", FLT_MAX);
   printf("\n");
   printf(" The smallest value of double is %le\n", DBL_MIN);
   printf(" Tthe largest  value of double is %le\n", DBL_MAX);
   printf(" The smallest value of long double is %Le\n", LDBL_MIN);
   printf(" Tthe largest  value of long double is %Le\n", LDBL_MAX);
   printf("\n");

  getch();
}

Output:


Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...