Posts

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...

C# Fundamental: Stack vs. Heap

Image
Memory Allocation (Stack and Heap) Operating system and language runtimes such as .NET CLR divides the memory into two chunks called stack ad heap. Stack and heap act differently while allocating memory. When we call a method, the memory required for its parameters ad variable is accommodated by the stack. When a method finishes its job, the memory is automatically released by the garbage collector for the purpose of reuse. Stack Memory Stack is organized like stack of boxes sitting on top of one another. When a method is called, each parameter is put into a box, first box goes first into stack then second on top of it, and so on. As in the following diagram: Stack uses Last In First Out (LIFO) mechanism for allocating memory. Heap Memory Heap memory is an unorganized pile of boxes sitting in a room randomly, not like stack which stored boxes on top of each other in an order. The reference to the object is stored on stack but the actual value is stored on heap....

C# Fundamental: Value and Reference Types

Image
Data Types There are following data types in C#: Value type Reference type Value Type Value types directly contain data Allocated on stack It cannot be  null Data types such as integers, numeric, bytes, Booleans,  char  are called value type. Value types are derived from the  ValueType  class in  System namespace . There are two categories of value types which are built in and user defined. Built in value types  are  int ,  char ,  decimal ,  float ,  long , etc. and User defined  value types are structures ( DateTime  is a value type, a structure) and  enum . For example, when we declare the  int a  compiler allocates 4 bytes of memory (32bit). When we give a value such as 7 to the variable, it copies this value to that block of memory. We can declare a data type by writing the data type name like  int string , etc. then write any name of a variable by using name conve...

C# Fundamental: Data Types and Variable

Image
Data Types In C#, data types are used to store values in the memory. When data type is declared, it allocates a block of memory to hold the value. All the data types are directly or indirectly derived from the  System.object  class. Before moving, we need to understand the variables. Variables A variable is used to store the value (data). It is an address given to a memory location. Example: Take an example of a bag here bag is a variable we can put books into a bag and we can take them out again but we can put books according to bag size, a bigger bag can hold more books. Similarly, we can store and retrieve data from a variable. Here bag is the variable name the size of bag is the data type, an  int  can hold 32 bit value and  long  can hold 64 bit value. Different data types have different sizes we use them according to our requirement for example if we want to store only two values  true  or  false  we...