Languages – Shishir Kant Singh https://shishirkant.com Jada Sir जाड़ा सर :) Tue, 12 May 2020 07:01:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://shishirkant.com/wp-content/uploads/2020/05/cropped-shishir-32x32.jpg Languages – Shishir Kant Singh https://shishirkant.com 32 32 187312365 Tokens and Keywords Programming C https://shishirkant.com/tokens-and-keywords-programming-c/?utm_source=rss&utm_medium=rss&utm_campaign=tokens-and-keywords-programming-c Tue, 12 May 2020 07:01:27 +0000 http://shishirkant.com/?p=105 C tokens, Identifiers and Keywords are the basics in a C program. All are explained in this page with definition and simple example programs.

1. C TOKENS:

  • C tokensare the basic buildings blocks in C language which are constructed together to write a C program.
  • Each and every smallest individual units in a C program are known as C tokens.

C tokens are of six types. They are,

  1. Keywords               (eg: int, while),
  2. Identifiers               (eg: main, total),
  3. Constants              (eg: 10, 20),
  4. Strings                    (eg: “total”, “hello”),
  5. Special symbols  (eg: (), {}),
  6. Operators              (eg: +, /,-,*)

C TOKENS EXAMPLE PROGRAM:

int main()
{
int x, y, total;
x = 10, y = 20;
total = x + y;
printf ("Total = %d \n", total);
}

where,

  • main – identifier
  • {,}, (,) – delimiter
  • int – keyword
  • x, y, total – identifier
  • main, {, }, (, ), int, x, y, total – tokens

Do you know how to use C token in real time application programs? We have given simple real time application programs where C token is used. You can refer the below C programs to know how to use C token in real time program.

2. IDENTIFIERS IN C LANGUAGE:
  • Each program elements in a C program are given a name called identifiers.
  • Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name given to integer variable in above program.
RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:
  1. First character should be an alphabet or underscore.
  2. Succeeding characters might be digits or letter.
  3. Punctuation and special characters aren’t allowed except underscore.
  4. Identifiers should not be keywords.
3. KEYWORDS IN C LANGUAGE:
  • Keywords are pre-defined words in a C compiler.
  • Each keyword is meant to perform a specific function in a C program.
  • Since keywords are referred names for compiler, they can’t be used as variable name.

C language supports 32 keywords which are given below. Click on each keywords below for detail description and example programs.

autodouble
intstruct
constfloat
shortunsigned
breakelse
longswitch
continuefor
signed void
caseenum
registertypedef
defaultgoto
sizeofvolatile
charextern
returnunion
doif
static while
]]>
105
Programming C- Data Types https://shishirkant.com/programming-c-data-types/?utm_source=rss&utm_medium=rss&utm_campaign=programming-c-data-types Tue, 12 May 2020 06:47:20 +0000 http://shishirkant.com/?p=99
  • C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
  • Data types are used to define a variable before to use in a program.
  • Size of variable, constant and array are determined by data types.
  • Programming C – DATA TYPES:

    There are four data types in C language. They are,

    TypesData Types
    Basic data typesint, char, float, double
    Enumeration data typeenum
    Derived data typepointer, array, structure, union
    Void data typevoid
    1. BASIC DATA TYPES IN C LANGUAGE:
    1.1. INTEGER DATA TYPE:
    • Integer data type allows a variable to store numeric values.
    • “int” keyword is used to refer integer data type.
    • The storage size of int data type is 2 or 4 or 8 byte.
    • It varies depend upon the processor in the CPU that we use.  If we are using 16 bit processor, 2 byte  (16 bit) of memory will be allocated for int data type.
    • Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
    • int (2 byte) can store values from -32,768 to +32,767
    • int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
    • If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for which the limits are very high.

    Note:

    • We can’t store decimal values using int data type.
    • If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
    • In this case, float data type can be used to store decimal values in a variable.
    1.2. CHARACTER DATA TYPE:
    • Character data type allows a variable to store only one character.
    • Storage size of character data type is 1. We can store only one character using character data type.
    • “char” keyword is used to refer character data type.
    • For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.
    • Please refer C – Strings topic to know how to store more than one characters in a variable.
    1.3. FLOATING POINT DATA TYPE:

    Floating point data type consists of 2 types. They are,

    1. float
    2. double
    1. FLOAT:
    • Float data type allows a variable to store decimal values.
    • Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
    • We can use up-to 6 digits after decimal using float data type.
    • For example, 10.456789 can be stored in a variable using float data type.
    2. DOUBLE:
    • Double data type is also same as float data type which allows up-to 10 digits after decimal.
    • The range for double datatype is from 1E–37 to 1E+37.
    1.3.1. SIZEOF() FUNCTION IN C LANGUAGE:

    sizeof() function is used to find the memory space allocated for each C data types.

    #include<stdio.h>
    #include<limits.h>
    int main()
    {
    int a;
    char b;
    float c;
    double d;
    printf("Storage size for int data type:%d \n",sizeof(a));
    printf("Storage size for char data type:%d \n",sizeof(b));
    printf("Storage size for float data type:%d \n",sizeof(c));
    printf("Storage size for double data type:%d\n",sizeof(d));
    return 0;
    }
    OUTPUT:
    Storage size for int data type:4
    Storage size for char data type:1
    Storage size for float data type:4
    Storage size for double data type:8
    1.3.2. MODIFIERS IN C LANGUAGE:
    • The amount of memory space to be allocated for a variable is derived by modifiers.
    • Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
    • For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.
    • There are 5 modifiers available in C language. They are,
      1. short
      2. long
      3. signed
      4. unsigned
      5. long long

    Below table gives the detail about the storage size of each C basic data type in 16 bit processor. Please keep in mind that storage size and range for int and float datatype will vary depend on the CPU processor (8,16, 32 and 64 bit)

    C Data types / storage SizeRange
    char / 1–127 to 127
    int / 2–32,767 to 32,767
    float / 41E–37 to 1E+37 with six digits of precision
    double / 81E–37 to 1E+37 with ten digits of precision
    long double / 101E–37 to 1E+37 with ten digits of precision
    long int / 4–2,147,483,647 to 2,147,483,647
    short int / 2–32,767 to 32,767
    unsigned short int / 20 to 65,535
    signed short int / 2–32,767 to 32,767
    long long int / 8–(2power(63) –1) to 2(power)63 –1
    signed long int / 4–2,147,483,647 to 2,147,483,647
    unsigned long int / 40 to 4,294,967,295
    unsigned long long int / 82(power)64 –1
    2. ENUMERATION DATA TYPE IN C LANGUAGE:
    • Enumeration data type consists of named integer constants as a list.
    • It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
    • Enum syntax in C:
      enum identifier [optional{ enumerator-list }];
    • Enum example in C:

    enum month { Jan, Feb, Mar }; or
    /* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
    enum month { Jan = 1, Feb, Mar };
    /* Feb and Mar variables will be assigned to 2 and 3 respectively by default */
    enum month { Jan = 20, Feb, Mar };
    /* Jan is assigned to 20. Feb and Mar variables will be assigned to 21 and 22 respectively by default */

    • The above enum functionality can also be implemented by “#define” preprocessor directive as given below. Above enum example is same as given below.

    #define Jan 20;
    #define Feb 21;
    #define Mar 22;

    C – ENUM EXAMPLE PROGRAM:

    #include<stdio.h>

    int main()
    {
    enum MONTH { Jan = 0, Feb, Mar };
    enum MONTH month = Mar;
    if(month == 0)
     printf("Value of Jan");
     else if(month == 1)
    printf("Month is Feb");
    if(month == 2)
    printf("Month is Mar");
    }
    OUTPUT:
    Month is March
    3. DERIVED DATA TYPE IN C LANGUAGE:
    • Array, pointer, structure and union are called derived data type in C language.
    4. VOID DATA TYPE IN C LANGUAGE:
    • Void is an empty data type that has no value.
    • This can be used in functions and pointers.
    ]]>
    99
    C- printf() and scanf() https://shishirkant.com/c-printf-and-scanf/?utm_source=rss&utm_medium=rss&utm_campaign=c-printf-and-scanf Tue, 12 May 2020 06:25:04 +0000 http://shishirkant.com/?p=95
  • printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.
  • We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language.
  • 1. PRINTF() FUNCTION IN C LANGUAGE:

    • In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
    • We use printf() function with %d format specifier to display the value of an integer variable.
    • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
    • To generate a newline,we use “\n” in C printf() statement.
    • C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

    EXAMPLE PROGRAM FOR C PRINTF() FUNCTION:

    #include<stdio.h>
    int main()
    {
    char ch = ‘A’;
    char str[20] = “shishirkantsingh”;
    float flt = 15.321;
    int no = 150;
    double dbl = 20.123456;
    printf(“Character is %c \n”, ch);
    printf(“String is %s \n” , str);
    printf(“Float value is %f \n”, flt);
    printf(“Integer value is %d\n” , no);
    printf(“Double value is %lf \n”, dbl);
    printf(“Octal value is %o \n”, no);
    printf(“Hexadecimal value is %x \n”, no);
    return 0;

    OUTPUT:
    Character is A
    String is shishirkantsingh
    Float value is 15.321000
    Integer value is 150
    Double value is 20.123456
    Octal value is 226
    Hexadecimal value is 96

    You can see the output with the same data which are placed within the double quotes of printf statement in the program except

    • %d got replaced by value of an integer variable  (no),
    • %c got replaced by value of a character variable  (ch),
    • %f got replaced by value of a float variable  (flt),
    • %lf got replaced by value of a double variable  (dbl),
    • %s got replaced by value of a string variable  (str),
    • %o got replaced by a octal value corresponding to integer variable  (no),
    • %x got replaced by a hexadecimal value corresponding to integer variable
    • \n got replaced by a newline.

    2. SCANF() FUNCTION IN C LANGUAGE:

    • In C programming language, scanf() function is used to read character, string, numeric data from keyboard
    • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
    • Then, user enters a string and this value is assigned to the variable “str” and then displayed.
    EXAMPLE PROGRAM FOR PRINTF() AND SCANF() FUNCTIONS IN

    #include<stdio.h>

    int main()
    {
    char ch;
    char str[100];
    printf(“Enter any character \n”);
    scanf(“%c”, &ch);
    printf(“Entered character is %c \n”, ch);
    printf(“Enter any string ( upto 100 character ) \n”);
    scanf(“%s”, &str);
    printf(“Entered string is %s \n”, str);
    }

    OUTPUT : 
    Enter any character
    a
    Entered character is a
    Enter any string ( upto 100 character )
    hai
    Entered string is hai
    • The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string.
    • Ampersand is used before variable name “ch” in scanf() statement as &ch.
    • It is just like in a pointer which is used to point to the variable. 
    KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():
    1. printf() is used to display the output and scanf() is used to read the inputs.
    2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
    3. All syntax in C language including printf() and scanf() functions are case sensitive.
    ]]>
    95
    C Language Basic https://shishirkant.com/c-language-basic/?utm_source=rss&utm_medium=rss&utm_campaign=c-language-basic Tue, 12 May 2020 05:36:20 +0000 http://shishirkant.com/?p=83 Classification of Programming Language

    Programming language can be classified into 2 types

    1.                         High Level Language and

    2.                         Low Level Language.

    High Level Language:- Those are more English like language and hence the programmers found them very easy to learn to convert the programs in high level language to machine language compilers and interpreters are used.

    Low Level Language:- All low level language called assembly language is designed in the beginning.  It has some simple instructions.  Those instructions are not binary codes.  But the computer can understand only the machine language.  Hence a converter or translator is developed to translate the low level language programs into machine language.  This translator is known as Assembler.

    Translators:- There are three types of translators available for the language.

    1. Assembler
    2. Compiler
    3. Interpreter
    1. Assembler:- This translator is used to convert the programs written in low level language(Assembly) into machine language.
    • Compiler:-  Compiler is used to convert high level language into machine level language.  It checks for error in the entire program and converts the program into machine language.
    • Interpreter:- This is also used to convert high level language into machine language.  It checks for errors statement by statement and converts the statement into machine level language.

    There are 256 characters by the micro computer. These values 0 to These can be divided operating system under.
    Character type Number of Character

    Capital letteres 26 ( A to Z)
    Small Letters 26 (a to z)
    Digits 10 ( 0 to 9)
    Special symbols 32
    Control Characters 34
    Graphics Characters 128

    Out of the 256 character set. First 128 are called ASCII Characters and the next 128 as extended ASCII Characters each ASCII character has a unique appearance.

    A to Z 65 to 90
    a to z 97 to 122
    0 to 9 48 to 57
    Enter 13
    Space 32
    Tab 9
    Back Space 8

    Algorithm:-A step by step procedure to solve the given problem is known as algorithm.

    To find the sum, product, and division of given two numbers.
    Steps:–
    Read any two numbers a,b
    sum=a+b
    product=a*b
    division=a/b
    print sum, product, division
    end

    To find the maximum value of given two numbers:
    Steps:-
    Read any two values a,b
    max=a
    if max<b then max=b
    print max
    end

    To find the maximum value of three numbers
    Steps:-
    Read any three values a,b,c
    max=a
    if max<b then max=b a=10 b=20 c=5
    if max<c then max=c
    print max
    end

    To check whether the given number is even or odd
    Steps:-
    Read n
    if n%2= = 0,
    print “n is even”
    else
    print “n is odd”
    end

    To display natural numbers from 1 to given number
    Steps:-
    Read n
    i=1
    print i
    i=i+1
    if i<=n then to to step3
    end

    To display factors of given numbers
    Steps:-
    Read n
    i=1
    if n%i= = 0 then print i
    i=i+1
    if i<=n then to to step 3
    end

    To display factorial of given number
    Steps:-
    Read n
    fact=1
    fact=fact*n
    n=n-1
    if n>=1 then go to step3
    print fact
    end

    To display how many digits in given number
    Steps:
    Read n
    nd=0
    nd=nd+1
    n=n/10
    if n>0 then go to step3
    print nd
    end

    To calculate and display sum of given digits in given number
    Steps:-
    Read n 25
    Sum=0
    Sum=Sum+(n%10) sum=5+2=7
    n=n/10 2/10=0
    If n>0 then go to step3
    Print sum
    end

    To check whether the given number is Palindrome or not
    Steps:-
    Read n 22
    rev=0,m=n m=22
    rev=rev10+(m%10) 210+(2%10)=22
    m=m/10 22/10 =2
    if n>0 then go to step3
    if n= = rev then print “n is palindrome”
    else
    “n is not palindrome”
    end

    ]]>
    83
    Introduction – Programming C https://shishirkant.com/introduction-programming-c/?utm_source=rss&utm_medium=rss&utm_campaign=introduction-programming-c Tue, 12 May 2020 05:12:38 +0000 http://shishirkant.com/?p=78 C is a programming language it is designed by DENNIS RITCHIE in 1972 at AT & T [American Telephone and telegraphs]  Bell labs in USA.  C is most popular general purpose language.

    BRIEF HISTORY OF C LANGUAGE:

                In 1960’s COBOL was being used for commercial applications and FORTRAN for scientific and engineering applications.  At this stage people started to develop a language which is suitable for all possible applications.  Therefore an international committee was setup to develop such a language ALGOL 60 was released.  It was not the generality a new language called CPL  [Combine Programming Language] was developed at Cambridge University then some other features were added to this language a new language called BCPL [Basic combine programming language] developed by MARTIN RICHARDS at Cambridge University.  Then B language was developed by KENTHOMSON at AT &T Bell labs.

                DENNIS RITCHIED inherited the features of B and BCPL, added some of its own feature and developed C language in 1972.

    Features of C Language:

    1. C is structured programming language with fundamental flow chart constructions.
    2. C is highly portable programming language.  The programs return for one computers can be run on another with or without any modifications.
    3. The programs return in C are efficient and fast.
    4. C improves by itself it has several predefined functions.
    5. C has only 32 keywords.
    6. C supports all data conversions and mixed mode operators.
    7. Dynamic memory storage allocation is possible with C.
    8. C is simple and versatile programming language.
    9. C easily manipulate with bytes, bits and address.
    10. C has a richest of operators.         
    11. Extensive verities of data types such as pointers, structures, Unions etc., available in C.
    12. Recursive function calls for algorithmic approach is possible with C.
    13. Mainly we are using the C language to implement the system software those are compilers, text editors, Network drives, data base utilities and finally the operating systems.
    14. C compiler combines the capabilities of assembly level language with the features of high level language.  So it is called as middle level language.

    Important Points

    1. C is a case sensitive programming language. C statements are entered in lower case letters only.
    2. Every C statement must be terminated by a semicolon ( ; ) except preprocessor statements and function definition.
    3. C is a function oriented language.  Any C program contains one or more functions.  Mainly one function is necessary by the name called main. Without main we cannot execute C Programs.
    4. A function is represented by a function name with a pair of parenthesis.

    COMPUTER: Computer is an electronic device, which has memory and performs arithmetic and logical operations.

    Input: The data entered in the computer is called input. Ex:- Keyboard, mouse, joy stick etc.,

    Output: The resultant information obtained by the computer is called output. Ex:- Through Screen, Printer etc.,

    Program: A sequence of instructions that can be executed by the computer is called program.

    Software: A group of program to operate and control the computer is called software

    Hardware: All the physical components or units of computer system connected to the computer circuit is called hardware.

    Operating System: It is an interface between user and the computer.  In other words, operating system is a complex set of programs,  that manage the resources of a computer. Resource include input, output, processor etc.,  So operating system is a Resource manager.

    Language: It consists of a set of executable instructions.

    Package: It is designed by any other language with limited resources.

    Various steps involved in program or Application development:

     The following steps are involved in program development.

    1. Problem definition
    2. Analysis and Design
    3. Algorithms
    4. Flow Chart
    5. Coding and Implementation
    6. Debugging ( errors) and testing
    7. Documentation
    1. Problem Definition: Problem definition phase is a clear understanding of exactly what is needed for creating a workable solution.  We must know exactly what we want to do before we do a problem.  It involves three specifications regarding a proper definition.
    • Input Specification
    • Output Specification
    • Processing
    • Analysis and Design:  Before going to make a final solution for the problem, the problem must be analyzed outline solution is prepared in the case of simple problems. But in the case of complex problems, the main problem is divided into sub problems called modules.  These modules can be handled and can be solved independently. When the task is too big, it is always better to analyze the task, such that, it can be divided into number of modules and seek solution for each.
    • Algorithms: Once the problem is divided into number of modules, the logic for solving each module can be developed.  The logic is expressed step by step.  A step by step procedure to solve the given problem is called algorithm. An algorithm is defined as a finite set of instructions which accomplish a particular task.  An algorithm can be described in a natural language like English.
    • Flow Chart: After completion of algorithm, the program can be visualized by drawing a flow chart. A flow chart is nothing but a graphical or symbolic representation of how instructions will be executed one after another.
    • Coding and Implementation: Coding is a process of converting the algorithm solution of flow chart in computer program.

    In this process each and every step of algorithm is converted to instructions of selected computer programming language. Before selecting

    the programming language we must follow three considerations.

    • Nature of problem
    • Programming language available.              
    • Limitations of computer
    • Debugging and Testing:-

    Debugging: Before loading the program into the computer we must correct all the errors.  This process is called debugging. There are three types of errors.

                *Syntax error

                *Runtime error

                *Logical error

    Testing:- It is very important to test the program written to achieve a specific task.  Testing is running the program with known data and known result.

    • Documentation:- It is the most important aspect of programming.  It is a continuous  process. To keep the copy of continuous process.  To keep the copy of all the phases (involving) in parts, definition, analysis and designing, algorithm, flow chart, coding and implementation, debugging and testing are the parts of the documentation.  This phase involves to produce written document for the user.
    ]]>
    78