Tuesday, October 21, 2025

Storage Class in C

 Storage Class in C


            

The scope and lifetime of a variable or / and function within a C program is defined by storage class. There are four storage classes in C


auto -      It is the default storage class for all variables and / or   functions.


register - Allows for defining variables to store in CPU register  instead of RAM. Unary operator is not applied for register    variable.


static –   The static storage class allows the updated variable values  available  for the next time when the function, in which the  variable is defined,  is invoked for the next time.


extern -   It allows a global variable to be visible to all the program  files /  external files ( C programs).

     

Calculate Simple Interest

 Calculate Simple Interest with following detail. p=1000, n=5 and  r=2.5


#include<stdio.h>

main()

{

      int n=5;

      float p=1000,r=2.5,si=0.0;

      si=p*n*r/100;

      printf("\nSimple Interest %.2f",si);

} 

      Output





Perform Simple Arithmetic Operation Program

 

Perform Simple Arithmetic Operation Program

Where a is 5 and b is 2.    

 +(Add)      -(Subtraction)   *(Multiplication

 / (Division)   %(Remainder)


#include<stdio.h>

main()

{

      int a=5,b=2,c,d,e,f,g;

      c=d=e=f=g=0;

      c=a+b;

      d=a-b;

      e=a*b;

      f=a/b;

      g=a%b;

      printf("\nC=%d\nD=%d\nE=%d",c,d,e);

      printf("\nF=%d\nG=%d",f,g);

}


Output 



Note : if Both are Integer then result is 

       integer other wise Float.

   Ex. (5/2.0  or  5.0/2   or   5.0/2.0 -----          2.5)


Monday, October 20, 2025

Formatted Output

Formatted Output

printf():  The printf() function is used to print the data from the computers memory onto a standard output device.  This function can be used to output any combination of numerical values, single character and strings.

Syntax: 

printf(“Control String Control  Message Conversion String”, List of arg1 );


Where control string is a string that contains backslash characters, Control message that conations the user defined information and conversion string that contains formatted information, and list of arg.  are arguments that represent the output data items.


A simple format specification has the following form:

%w.p type-specifier

Where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point (of a real number) or the number of characters to be printed from a string. Both wad p are optional. Some examples of formatted printf statement are:

Example :  

int  a=6; float b=2.75; char d=’A’, city[10]=”KHAMMAM”;


Statement                 Output

printf(“Welcome To C Program”);         Welcome  To C Program

printf(“\nWelcome  To C Program”);  (skip line and Print next line)

                                                                         Welcome  To C Program

printf(“%d”,a);                        5

printf(“%f”,b);                 2.75

printf(“%c”,d);                         A

printf(“%s”,city);                 KHAMMAM

printf(“%d%f%c%s”,a,b,d,city);         52.75AKHAMMAM

printf(“%d\t%f\t%c\t%s”,a,b,d,city); 5   2.75   A    KHAMMAM

printf(“A=%d\tB=%f\tD=%c”,a,b,d); A=5    B=2.75    D=A


Where  %c %s %d %f are conversion characters.

 Output of Integer Numbers: The format specification for printing an integer number is

%wd

                Format   Output

printf("%d", 9876)                              9876

printf("%6d", 9876)                            _ _ 9876

                     printf("%2d", 9876)                            9876

          printf("%-6d", 9876)                           9876_ _

          printf("%06d", 9876)                          009876


Output of Real Numbers

The output of a real number may be displayed in decimal notation using the following format specification.

%w.pf

 The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point (precision). The value, when displayed, is rounded to p decimal places and printed right-justified in the field of w columns. Leading blanks and trailing zeros will appear as necessary. The default precision is 6 decimal places. The negative numbers will be printed with the minus sign. The number will be displayed in the form -.

            Ex. y=98.7654

Format                                                 Output

printf("% 7.4f",y)                                98.7654

printf("%7.2f",y)                                 _ _98.77

printf("%-7.2f",y)                                98.77_ _

printf("%f",y)                                      98.7654

printf("%10.2e",y)                               _ _9.88e+01

printf("%11.4e",-y)                             -9.8765e+01

printf("%-l0.2e",y)                              9.88e+01_ _

printf("%e",y)                                      9.876540e+01

Printing of a Single Character

A single character can be displayed in a desired position using the format

%wc

The character will be displayed right-justified the field of w columns. We can make the display left-Justified by placing a minus sign before the integer w. The default value for w is 1.

KHAMMAM 507001

Specification                   Output 

                        |1 |2 |3 | 4 |5 |6 |7 |8 |9 |0 |1 |2  |3 |4 |5 |6 |7 |8 |9 |0  |

%s                   |K|H|A|M|M|A|   |5 |0 |7 |0  |0 |1 |   |   |   |   |   |   |    |

%20s               |   |   |   |   |    |   |   |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |

%20.10s          |   |   |   |   |    |   |   |   |   |   |K|H|A|M|M|A|M|   |5 | 0 |

%.5s                |K|H|A|M|M||  |   |   |   |   |   |   |    |   |   |   |   |   |   |    |

%-20.10s         |K|H|A|M|M|A|   |5 |0 |   |   |   |    |   |   |   |   |   |   |    |

%5s                 |K|H|A|M|M|A|   |5 |0 |7 |0 |0 |1  |   |   |   |   |   |   |    |


Backslash Character (Only Output)

 Backslash Character  (Only Output)


C supports some special backslash character constants that are  used in output function.


"\a"     Alert or bell

"\b"     Back space      

"\f"     Form Feed

"\n"     New line

"\r"     Return

"\t"     Tab

"\v"     Vertical tab

"\\"     Print \ (back slash)

"\?"     Print ?

"\'"     Print ' (Single Quote)

"\""     Print " (Double Quote)

"\0"     Null

"%%"    Print %


Sunday, October 19, 2025

Operators in C Language

                                            Operators

An Operator is symbol that tells the computer to perform mathematical and other calculation. Operators are used in program to manipulate data and variable. C language support 8 types operators. Arithmetic Operators, Relational Operators, Logical Operators, Increment/Decrement Operators, Assignment Operators, Conditional Operators, Bitwise Operators, and Special Operators.

8 Types

-Arithmetic Operators               +  -   *   /    %

-Relational Operators         ==   >    <    !=    >=     <=

-Logical Operators      && (and)   ||(or)    ! (not)

-Increment/Decrement Operators ++   --

-Assignment Operators         =    +=    -=   *=   /=    %=

-Conditional Operators          ?  :

-Bitwise Operators                 &   |    ^    >>   <<   ~

-Special Operators                   ,  . ->    & *

 

-Arithmetic Operators     

    +    -     *          /           %

5+2 7

5-2 3

5*2 10       

5/2 2      (5/2.0  or  5.0/2   or   5.0/2.0 -----   2.5)

5%2 1


-Relational Operators

==   >    <    !=    >=     <=

5== 5      a==b

a>b

a<b

a!=b

a>=b         marks>=35

a<=b


-Logical Operators

&& (and)                         || (or)                       ! (not)

T && T  = T T || T  = T !T = F

F && T= F    F || T  = T !F = T

T && F= F  T || F  = T

F && F= F F || F  = F


-Increment/Decrement Operators ++   --

     ++                                     --

   int a=5, b=0;

    ++a               a++            --a                    a--

    6                  6                    4                    4

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

    Pre               Post            Pre                  Post

 b=++a               b=a++         b=--a              b=a--

 a = 6              a=6             a=4                 a=4

 b = 6              b=5             b=4                 b=5  


-Assignment Operators

=    +=    -=   *=   /=    %=

Var=Constant (or)  Var (or)  Expression 

Ex. a=5         b=2.5        x='A'         ( city="Khammam")

      a=d 

      c=(f-32)*5/9.0

 

   a=a+2                    a=a+n

   a+=2                      a+=n


   a=a-2                    a=a-n

   a-=2                      a-=n


    a=a*2                    a=a*n

    a*=2                      a*=n


   a=a/2                    a=a/n

   a/=2                      a/=n


   a=a%2                    a=a%n

   a%=2                      a%=n 


-Conditional Operators ?  :

Syntax  

       (Expression) ? True Statement; : False Statement;

  Eg.     (a>b) ? printf("A is Big"); : printf("B is Big");


-Bitwise Operators

&(and)   |(pipe/or)    ^(caret/XOR)    >>(right shift) <<(left shift) ~(tild/negation)

int a=7, b=54, c=0

       a=7   ---0000 0000 0000 0111

       b=54  ---0000 0000 0011 0110

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

       c=a&b ---0000 0000 0000 0110 

       c=6

       a=7   ---0000 0000 0000 0111

       b=54  ---0000 0000 0011 0110

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

       c=a|b ---0000 0000 0011 0111 

       c=55

       a=7   ---0000 0000 0000 0111

       b=54  ---0000 0000 0011 0110

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

       c=a^b ---0000 0000 0011 0001 

       c=49

       b=54  ---0000 0000 0011 0110

       c=b>>2   0000 0000 0000 1101         (13)

       b=54  ---0000 0000 0011 0110

       c=b<<2   0000 0000 1101 1000        (216)

       c=~b     1111 1111 1100 1001


-Special Operators

               ,      .  ->        &           *

x=(a=4,b=3,c=b-a*2)

x=-5

DATA TYPES

Data Types

Primary Data type           Secondary DT                    User defined DT            Null or Empty DT
                                           (or) Derived D

int float char   double      arrays, functions              sizeof, typedef, enum           void
                                             files, struct, union
                                             pointers   

Data Name                                     Size in Bits (Bytes)                                            Ranges
-----------------------------------------------------------------------------------------------------------------------
char (or) signed char                          8 (1)                                                 -128 To  127
unsigned char                                     8 (1)                                                         0 To 255
short int (or) signed short int             8 (1)                                                      -128 To 127
unsigned short int                    8 (1)                                                     0 To 255
int (or) signed int                               16 (2)                                                   -32768 To 32767
unsigned int                                       16 (2)                                                       0 To 65535
long int (or) signed long int               32 (4)                                     -2147483648  To 2147483647
unsigned long int                    32 (4)                                                       0 To 429497285
float                                    32 (4)                                               3.4E-38 To 3.4E+38
double                                  64 (8)                                             1.7E-308 To  1.7E+308
long double                             80(10)                                            3.4E-4932 To 1.1E+4932


Conversion String  (Input/Output)
"%c"                    char
"%d"                    int
"%e"                    floating point (Exponent)
"%f"                       float      
"%g"                    floating point
"%h"                    short int
"%i"           int, octal, Hexadecimal
"%o"           Octal
"%s"         String
"%u"         unsigned int
"%x"         Hexadecimal
"%[..]"         String words
"l" for long int or double                
"%ld"  Long int    
"%lf"  Double
"L" Long double                   
"%Lf"       long double