Showing posts with label C-Programming. Show all posts
Showing posts with label C-Programming. Show all posts
Showing posts with label C-Programming. Show all posts
Showing posts with label C-Programming. Show all posts

Monday, August 12, 2019

C- Programming Data Type |Part-#5|

C- Programming Data Type |Part-#3|

Symbolic constants - Symbolic Constant is a name that substitutes for a sequence of characters or a numeric constant, a character constant or a string constant. When program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence. 
The syntax is as follows:
#define name text

where name implies symbolic name in caps. text implies value or the text.

For example:

  • #define printf print
  • #define MAX 100
  • #define TRUE 1
  • #define FALSE 0
  • #define SIZE 10

The # character is used for pre-processor commands. A pre-processor is a system program, which comes into action prior to Compiler, and it replaces the replacement text by the actual text. This will allow correct use of the statement printf.

Advantages of using Symbolic Constants are:
  • They can be used to assign names to values
  • Replacement of value has to be done at one place and wherever the name appears in the text it gets the value by execution of the pre-processor. This saves time. if the Symbolic Constant appears 20 times in the program; it needs to be changed at one place only.

Variables: - A variable is an identifier or a name which is used to refer a value and this value varies or changes during the program execution. A variable is written with a combination of letters, numbers and special character _ (underscore) with the first letter being an alphabet. Maximum of 31 letters can be used to write a variable.

Ex: - c, fact, h34, total_amount etc

All variables have three essential attributes:
  • the name
  • the value
  • the memory, where the value is stored.

For example, in the following C program a, b, c, d is the variables but variable e is not declared and is used before declaration. After compiling the source code and look what gives?

main ( )
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = ‘a’;

After compiling the code, this will generate the message that variable e not defined.

Note the following points while writing a variable in C language: -
  • Upper- and Lower-case alphabets are taken differently, so the variable SUM and sum are referring to different value.

  • No special characters other than underscore (_) are permitted.

  • Some C compilers will not accept more than 8 characters.

  • All variables used in a C program are declared with appropriate data types before the variable is assigned any value.

  • Reserved words cannot be used as variables.

Variable Declaration - Before any data can be stored in the memory, we must assign a name to these locations of memory. For this we make declarations. Declaration associates a group of identifiers with a specific data type. All of them need to be declared before they appear in program statements, else accessing the variables results in junk values or a diagnostic error. The syntax for declaring variables is as follows:

data- type variable-name(s);

For example,
int a;
short int a, b;
int c, d;
long c, f;
float r1, r2;

Initializing variables - When variables are declared initial, values can be assigned to them in two ways:

1. Within a Type declaration

The value is assigned at the declaration time.

For example,
int a = 10;
float b = 0.4 e –5;
char c = ‘a’;

2. Using Assignment statement

The values are assigned just after the declarations are made.

For example,
a = 10;
b = 0.4 e –5;
c = ‘a’;

Basic Data Type – There are four basic data types in C language. They are given below along with number of bytes occupied by them in the computer memory (RAM).

char – It refers to character. It can hold one letter/symbol. Char in C language is associated with integers to refer a letter/symbol as per ASCII which has assigned integer value for all letters/symbols used in programming.

int – It refers integer. It can hold a signed or unsigned or unsigned whole number within the specified range.

float – It refers floating point or real number. It can hold a real number like 3.245453 or 5.87e3 with six decimal digits in decimal or exponential form. A float number using 6 decimal digits is called a single precision number.

double – It also refers to floating point or real number. It can hold a real number in double precision. A double precision number uses 12 decimal digits like 4.657835876582 or 3.67823574825e14.

Additional Data Types – There are some additional data type which is used in advance programming.

Note – unsigned is used when positive values are expected for a variable like age of a person.

Consider the following examples to declare variables in C program: -

  • unsigned short int marks, age;
  • unsigned int total, years;
  • int m, amount;
  • float x, y, z;
  • char sex, sname [20], address [50];

In this declaration, sname [20] refers to the variable sname to hold a string of maximum 20 characters; address [50] holds string of 50 characters; and sex holds a single character such as ‘m’ for male and ‘f’ for female.

Continue Reading

C- Programming Identifier, Constant |Part-#4|

C- Programming Identifier, Constant |Part-#4|

Identifier – An identifier is a name having a few letters, numbers and special character _ (Underscore). It is used to identify a variable, function, symbolic constant and so on. An identifier can be written with a maximum 31 characters. It is a good practice to have identifiers with few letters; less than 8 letters is commonly followed with the first letter being an alphabet.


Rules for Forming Identifiers

Identifiers are defined according to the following rules:

  • It consists of letters and digits.
  • First character must be an alphabet or underscore.
  • Both upper and lower cases are allowed. Same text of different case is not equivalent, for example: TEXT is not same as text.
  • Except the special character underscore ( _ ), no other special symbols can be used.

Keywords or Reserved words – C language uses the following keywords which are not available to users to use them as variables/function names. Generally, all keywords are in lower case although uppercase of same names can be used as identifiers.

Tokens – There are six types of tokens are used in C language.

Constants - A constant is an identifier whose value cannot be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:

  • Numeric Constant:

  1. Integer constants
  2. Floating point constants

  • String or Character Constants:

  1. Single character string constant / Character constants
  2. String of characters constant / String constants

Numeric constants

1. Integer Constant – An integer constant is signed or unsigned whole number. 

Ex: - 34, -48, +45 etc.

2. Real or Floating-Point Constant – A signed or unsigned number with fractional part is called real or floating-point constant.

Ex: - 0.45, -5.64, 0.45e2 etc.
            where e = exponential.

Rules to form Integer and Floating-Point Constants

  • No comma or blank space is allowed in a constant.
  • It can be preceded by – (minus) sign if desired.
  • The value should lie within a minimum and maximum permissible range decided by the word size of the computer.

String or Character constants

1. Single character string constant / Character constants – Any letter or character enclosed in single apostrophe is called Single character string constant / Character constant.

Ex: - ‘y’ ‘$’ ‘T’  ‘3’

2. String of characters constant / String constants – Any string of characters consisting of letters, digits and symbols enclosed in double quotes is called String of characters constant / String constants.

Ex:- “Total Amount”  “Jail-420”  “Average=”

Continue Reading
No comments

Monday, August 05, 2019

C- Programming Features |Part-#3|

Features of C- Programming

C Language – The C language is one of the powerful programming languages used to write computer programs for variety of applications. It was developed by Dennis Richie at Bell Lab during 1970’s. C has flexible features to write programs for numerical, commercial and graphical applications. May operating system programs for the latest computers and compilers are written using C language.
                        This language was developed from B language which was modified from a language called Basic Combined Programming Language (BCPL).

Salient features of C:
C is a general purpose, structured programming language. Among the two types of programming languages discussed earlier, C lies in between these two categories. That’s why it is often called a middle level language. It means that it combines the elements of high-level languages with the functionality of assembly language. It provides relatively good programming efficiency (as compared to machine-oriented language) and relatively good machine efficiency as compared to high level languages). As a middle level language, C allows the manipulation of bits, bytes and addresses – the basic elements with which the computer executes the inbuilt and memory management functions. C code is very portable, that it allows the same C program to be run on machines with different hardware configurations. The flexibility of C allows it to be used for systems programming as well as for application programming.

C is commonly called a structured language because of structural similarities to ALGOL and Pascal. The distinguishing feature of a structured language is compartmentalization of code and data. Structured language is one that divides the entire program into modules using top-down approach where each module executes one job or task. It is easy for debugging, testing, and maintenance if a language is a structured one. C supports several control structures such as while, do-while and for and various data structures such as structs, files, arrays etc. as would be seen in the later units. The basic unit of a C program is a function - C’s standalone subroutine. The structural component of C makes the programming and maintenance easier.

Characters used in C –

  • Upper case letters A to Z
  • Lower case letter a to z

  • 0 to 9

Special Characters

Continue Reading
No comments

C- Programming Algoruthm, Flowchart |Part #2|

C- Programming |Algoruthm|, |Flowchart|

Algorithm – In order to write computer programs without any logical errors, it is recommended programmers prepare a rough writing showing the steps involved in the program. This is called an algorithm.
                        An algorithm presents step by step instructions required to solve any problem. These steps can be shown diagrammatically using a flowchart.

Flowchart – Flowchart is a symbolic or diagrammatic representation of an algorithm. It uses several geometrical figures to represent the operations, and arrows to show the direction of flow. Following are the commonly used symbols in flowcharts:-

Question – Answer

Q. 1 Write the algorithm and draw the flowchart to find the sum and product of given two numbers.


            In this problem, two numbers, Let A and B, are given (input) and the result, sum (A+B) of two numbers and product (A*B) of two numbers, are to be calculated.


Step-1: Start
Step-2: Read a, b
Step-3: Sum ← a+b
Step-4: Product ←a*b
Step-5: Print sum, product
Step-6: Stop

Image Note – We can use equal to (=) sign instead of arrow (←). E.g. sum = a+b

Continue Reading
No comments

C-Programming Introduction |Part #1|


Computer Programming – A program has a set of instructions written in correct order to get the desired result. The method of writing the instructions to solve the given problem is called programming.

Programming languages can be divided into two categories:

Low Level Languages or Machine Oriented Languages: The language whose design is governed by the circuitry and the structure of the machine is known as the Machine language. This language is difficult to learn and use. It is specific to a given computer and is different for different computers i.e. these languages are machine-dependent. These languages have been designed to give a better machine efficiency, i.e. faster program execution. Such languages are also known as Low Level Languages. Another type of Low-Level Language is the Assembly Language. We will code the assembly language program in the form of mnemonics. Every machine provides a different set of mnemonics to be used for that machine only depending upon the processor that the machine is using

High Level Languages or Problem Oriented Languages: These languages are particularly oriented towards describing the procedures for solving the problem in a concise, precise and unambiguous manner. Every high-level language follows a precise set of rules. They are developed to allow application programs to be run on a variety of computers. These languages are machine-independent. Languages falling in this category are FORTRAN, BASIC, PASCAL etc. They are easy to learn and programs may be written in these languages with much less effort. However, the computer cannot understand them and they need to be translated into machine language with the help of other programs known as Compilers or Translators.

Programming techniques: - There are two type of programming techniques commonly used:-
  1. Procedural Programming
  2. Object Oriented Programming (OOP)

Procedural Programming – In Procedural Programming, for a given problem, variables are identified and instructions are written using the variables in the correct sequence to get the required result.
                        The procedural Programming method is commonly used to solve scientific and engineering problem involving variables.

Object Oriented Programming (OOP) – In Object Oriented Programming, Objects which have data related to a person or item are used. The program can be written using many functional blocks. The functional block contains instructions similar to procedural programming.
                        Object Oriented Programming method is commonly used to develop software packages. C++ is one of the commonly used object-oriented programming languages.

Continue Reading
No comments