Posts

Showing posts with the label C Language

Type Declaration Instruction in C

Type Declaration Instruction This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function. Ex.: int bas ; float rs, grosssal ; char name, code ; There are several subtle variations of the type declaration instruction. These are discussed below: While declaring the type of variable we can also initialize it as shown below. int i = 10, j = 25 ; float a = 1.5, b = 1.99 + 2.4 * 1.44 ; The order in which we define the variables is sometimes important sometimes not. For example, int i = 10, j = 25 ; is same as int j = 25, j = 10 ; However, float a = 1.5, b = a + 3.1 ; is alright, but float b = a + 3.1, a = 1.5 ;         is not. This is because here we are trying to use a even before defining it. The following statements would work int a, b, c, d ; a = b = ...

Instructions to write a C program

C Instructions Now that we have written a few programs let us look at the instructions that we used in these programs. There are basically three types of instructions in C: Type Declaration Instruction Arithmetic Instruction Control Instruction The purpose of each of these instructions is given below:       a)       Type declaration instruction To declare the type of variables used in a C program.       b)       Arithmetic instruction To perform arithmetic operations between constants and variables.       c)       Control instruction To control the sequence of execution of various statements in a C program. C programs would usually contain only the type declaration and the arithmetic instructions.

Instructions to write C Program

C Instructions Now that we have written a few programs let us look at the instructions that we used in these  programs. There are basically three types of instructions in C: Type Declaration Instruction Arithmetic Instruction Control Instruction The purpose of each of these instructions is given below:      Type declaration instruction To declare the type of variables used in a C program.             Arithmetic instruction To perform arithmetic operations between constants and variables.             Control instruction To control the sequence of execution of various statements in a C program. Since, the elementary C programs would usually contain only the type declaration and the arithmetic instructions; we would discuss only these two instructions at this stage. The other types of instructions would be discussed in det...

Receiving Input of C Program

Receiving Input In the program discussed below we assumed the values of p , n and r to be 1000, 3 and 8.5. Every time we run the program we would get the same value for simple interest. If we want to calculate simple interest for some other set of values then we are required to make the relevant change in the program, and again compile and execute it. Thus the program is not general enough to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able  to make changes in the program. Hence it is a good practice to create a program that is general enough to work for any set of values. To make the program general the program itself should ask the user to supply the values of p , n and r through the keyboard during execution. This can be achieved using a function called scanf( ) . This function is a counter-part of the printf( ) function....

Compilation and Execution of C Program

Compilation and Execution Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need ano ther program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems. For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them. If you are a beginner you would be better off using a simple compiler like...

The First C Program

The First C Program Armed with the knowledge about the types of variables, constants & keywords the next logical step is to combine them to form instructions. Before we begin with our first C program do remember the following rules that are applicable to all C programs: Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements. The statements in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence. Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword. All statements are entered in small case letters. C has no specific rules for the position at which a statement is to be written. That’s why it is often called a free-form ...

C Keywords

Image
C Keywords Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called ‘Reserved words’. There are only 32 keywords available in C.  Figure 1.5 gives a list of these keywords for your ready reference. A detailed discussion of each of these keywords would be taken up in later chapters wherever their use is relevant. Note that compiler vendors (like Microsoft, Borland, etc.) provide their own keywords apart from the ones mentioned above. These include extended keywords like near , far , asm , etc. Though it has been suggested by the ANSI committee that every suc...

Rules for Constructing Variable Names

Rules for Constructing Variable Names A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort. The first character in the variable name must be an alphabet or underscore . No commas or blanks are allowed within a variable name. No special symbol other than an underscore (as in gross_sal) can be used in a variable name. Ex.:  si_int m_hra pop_e_89 These rules remain same for all the types of primary and secondary variables. Naturally, the question follows... how is C able to differentiate between these variables? This is a rather simple matter. C compiler is able to distinguish between the variable names by making it compulsory for you to declare the type of any variable name that you wish to use in a program. This type ...

Types of C Variables

Types of C Variables As we saw earlier, an entity that may vary during program execution is called a variable . Variable names are names given to locations in memory . These locations can contain integer, real or character constants .  In any language, the types of variables that it can support depend on the types of constants that it can handle . This is because a particular type of variable can hold only the same type of  constant.  For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant. The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply. These rules are given below.

Rules for Constructing Character Constants

Rules for Constructing Character Constants A character constant is a single alphabet. A single digit or a single special symbol enclosed within single inverted commas .  Both the inverted commas should point to the left. For example:                      ’A’ is a valid character constant whereas ‘A’ is not. The maximum length of a character constant can be 1 character. Ex.:  'A' 'I' '5' '='

Rules for Constructing Real Constants

Rules for Constructing Real Constants Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.   The  Fractional form Following rules must be observed while constructing real constants expressed in fractional form: A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. Ex.:   +325.34 426.0 -32.76 -48.5792 The exponential form of representation of real constants is usually used if the value of the constant is either too small or too large. It however doesn’t restrict us in any way from using exponential  form of representation for other real constants. In exponential form of representation, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part follow...

Rules for Constructing Integer Constants

Rules for Constructing Integer Constants An integer constant must have at least one digit . It must not have a decimal point . It can be either positive or negative . If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant . The allowable range for integer constants is -32768 to 32767 . Truly speaking the range of an Integer constant depends upon the compiler . For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768 to 32767. For a 32-bit compiler the range would  be even greater. Question like what exactly do you mean by a 16-bit or a 32-bit compiler, what range of an Integer constant has to do with the type of compiler and such questions are discussed in detail in Chapter 16. Till that time it would be assumed that we are working with a 16-bit compiler.  Ex.:   426 +782 -8000 -7605

Types of C Constants

Image
Types of C Constants C constants can be divided into two major categories: Primary Constants Secondary Constants These constants are further categorized as shown in Figure 1.4. At this stage we would restrict our discussion to only Primary Constants, namely, Integer, Real and Character constants. Let us see the details of each of these constants. For constructing these different types of constants certain rules have been laid down. These rules are as under: Rules for Constructing Integer Constants   Rules for Constructing Real Constants   Rules for Constructing Character Constants

Constants, Variables and Keywords

Image
Constants, Variables and Keywords The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are ‘constants’ and ‘variables’ in C. A constant is an entity that doesn’t change whereas a variable is an entity that may change. In any program we typically do lots of calculations. The results of these calculations are stored in computers memory. Like human memory the computer memory also consists of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names. Consider the following example. Here 3 is stored in a memory location and a name x is given to it. Then we are assigning a new value 5 to the same memory location x . This would overwrite the earlier value 3, since ...

The C Character Set

Image
The C Character Set A character denotes any alphabet, digit or special symbol used to represent information. Figure 1.2 shows the valid alphabets, numbers and special symbols allowed in C.

Getting Started with C

Image
Getting Started with C There is a close analogy between learning English language and learning C language. The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which in turn are combined to form sentences and sentences are combined to form paragraphs. Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed , and finally how are these combined to form an instruction. A group of instructions would be combined later on to form a program. This is illustrated in the Figure 1.1.

What is C

B efore we can begin to write serious programs in C , Four important aspects of any language : ·          are the way it stores data, ·          the way it operates upon this data, ·          how it accomplishes input and output, ·          How it lets you control the sequence of execution of instructions in a program.  We would discuss the first three of these building blocks in this chapter. What is C C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie . In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. Possibly why C seems so popular is because it is reliable, simple and easy to use . An opinion that is often heard today...