by Dinesh Thakur

To store signed integers, we need to reserve one bit for the sign of the integer. It is usual convention to use a 0 bit to indicate positive sign and a 1 bit to indicate a negative sign.

 
by Dinesh Thakur

calculate a student's result based on two examinations, one sports event, and three activities conducted. The weightage of activities 30%, sports = 20%, and examination = 50%.

 
by Dinesh Thakur

Macros are small functions (generally single line functions) which may be dealt with the help of preprocessor directive #define. Here, we shall discuss only the directive #define which is also used to define constants. A macro may or may not have parameters. An advantage of using a macro is that if a program involves a large number of the function calls of a small function the overburden of function calls can make the program inefficient; in case of macro, the code is substituted wherever the macro occurs. Thus, a programmer does not have to repeat the code again and again in the source code of the program while the function call is eliminated. However, a disadvantage of using macro is that data types are not included in the macro nor are these checked by the compiler. A few illustrations of macros are given below.

 
by Dinesh Thakur

The field width and precision setting may be used for characters and strings as well. However, these have different meaning. See the following code:

 
by Dinesh Thakur

In general, the sign is not displayed with positive values. However, if we desire to display the + sign, we may add it in the control string. If the display is desired to be left justified as well as with+ sign, add + or ++between the % sign and the conversion character; for example, the following code for integers.

 
by Dinesh Thakur

Precision is specified by the number of digits after the decimal point for the outputs for float as well as double numbers. If precision is not specified, it would be according to the default setting in the computer which is generally 6 digits. The precision may be specified by a period(.) followed by a positive number equal to the number of digits desired. An illustration is given below.



 
by Dinesh Thakur

Type casting or type conversion refers to change of type of variables or pointers or user-defined objects from one type to another type. The need for type conversion arises in some operations involving two operands of different types, or even of same type. The example given below illustrates this concept.

 
by Dinesh Thakur

An object is a space in memory with a name or an identifier. The lifetime of an object is the storage duration of the object in a program, that is, lifetime is the portion of program execution during which the object exists with a constant address in the program and retains the last stored value. Three types of storage durations are defined in C, that is, automatic, static, and allocated.



 
by Dinesh Thakur

C language supports wide varieties of data types to accommodate any types of data manipulation the variety of data types available allow the programmer to select the type appropriate to the - needs for the program as well as the machine. Mainly the C language supports two types of data. type such as:



 
by Dinesh Thakur

The input into a program and output from a program is the basic requirements of any useful program. For input from standard input device, i.e., keyboard, the function scanf () is used and for output to the standard output device, i.e., monitor, the function printf () is used. Both these functions can take any number of arguments. The first argument is the formatting string enclosed between double quotes and consists of conversion characters. For printf () function, the formatting string may also contain any text that needs to be displayed on the monitor along with the values of variables. For example, if we want to display the value of an integer variable n, we may write the code as



 
by Dinesh Thakur

When we declare a variable name and its type, the compiler allocates a block of memory for placing its value. In fact, for the computer, this allocated block of memory is the variable and it recognizes it by its name. The sizes of memory blocks allocated for different types of data may vary on different computers depending on the hardware, the operating system, and the compiler used. The names of variables or identifiers should be carefully selected. The general guidelines are as follows:



 
by Dinesh Thakur

The typedef feature allows us to give an alternative (possibly short and more meaningful) name to an existing data type and improve program readability. For example, instead of using the int data type to declare variables to represent marks in three subjects, we can associate a more meaningful name (say Marks)for the int data type using typedef as:



 
by Dinesh Thakur

Consider that we need to work with the colours in a rainbow, e. g., to paint a rainbow on the screen. We thus have to work with seven colours, namely, violet, indigo, blue, green, yellow, orange and red. These colours can be represented using integer values starting with 0. This enables us to use various program constructs such as conditions or loops to process these colours. However, programs written using such code often become difficult to understand as can be seen from the statement given below.

 
by Dinesh Thakur

The printf function allows the values of argument expressions to be printed in various formats. A large number of conversion characters are provided for printing expressions of different types. Also, the possibility of using several optional fields along with these conversion characters makes printf a very powerful and complicated function.

 
by Dinesh Thakur

The scanf standard library function is used to read one or more values from the standard input (keyboard) and assign them to specified variables. A typical call to the scanf function takes the following form:

 
by Dinesh Thakur

The printf (print formatted) standard library function is used to print the values of expressions on standard output (i. e., display) in a specified format. A typical call to the printf function takes the form of a C statement as

 
by Dinesh Thakur

An arithmetic expression contains only arithmetic operators and operands. We know that the arithmetic operators in C language include unary operators (+ - ++ -- ), multiplicative operators (* / %) and additive operators (+ - ). The arithmetic operands include integral operands (various int and char types) and floating-type operands (float, double and long double).

 
by Dinesh Thakur

The field width is specified by an integer placed between the % sign and the conversion character. The default justification is right justification. For specifying left justification, we have to put a -ve sign before the integer which specifies field width. For example, for a field width of 10 the code is given below. Program provides an illustration of the concept.

 
by Dinesh Thakur

The simple interest on deposit is given as i= prt / 100,where p is the principle (i. e., amount deposited), r is the interest rate per annum and t is the number of years (i. e., duration of deposit). The total amount to be returned to the depositor is given as t = P + i.

Let us use the variable names deposit,rate,years,interest and tot_amt.The variable years is assumed to be of type int and all other variables to be of type float.The program given below first accepts the values of deposit,rate and years. Then it calculates the values of interest and tot_amt and prints them.

 
by Dinesh Thakur

This example defines the radius of a circle in parameter r of type int and prints its area and circumference. It declares a constant PI of type float with value 3.1415927 and two variables area and cir also of type float. Then we calculates the values of area and circum and prints them along with the radius of the circle.

 
by Dinesh Thakur

The general form of this function is similar to that of printf()but with one important difference: instead of arguments, it contains address of arguments. The code is given below.

 

Page 1 of 2