Note on Displaying Variables

  • Note
  • Things to remember

The output of the program is 10. The variable num is defined before it is used. A value 10 is assigned to the variable num and finally %d tells that the value is in integer format. The %d is the format string. It tells the printf that the output is an int. The second parameter num provides the actual value to be output.

Example:

#include <stdio.h>

void main ()

{

int num;

num = 10;

printf (“\n %d”, num);

}

Reading Variables

The scanf function reads the values of variables from the keyboard and stores them in variables.

Example: Reading values from the keyboard

#include <stdio.h>

void main ()

{

Float years, seconds;

Printf (“\n Input your age in years: “);

Scanf (“%f”,& years);

Printf(“\n You have Lived for %f seconds. “, seconds);

}

The output will be:

Input your age in years: 12

You have Lived for 378432000.000000 seconds.

Value Initialization Variables

We can declare an integer variable i by the statement:

int i;

This can be initialized in two other methods.

int i;

i = 10;

Or,

int i = 5;

If you want more than one variable to be initialized at the same time, use the following convention.

int i = 100, j = 15;

Now, i and j both are a same (int) type of data which have already assigned values.

Characters and Character String

A character variable %c can hold only one character which can be alphabet or symbol. Let use examine the following example.

Example:

#include <stdio.h>

main ()

{

/* declare a character */

Char c;

/* assigning a character to the variable c*/

c =” # “;

printf( “\n This is the hash symbol: %c “,c);

}

Example: Printing ASCII code.

#include <stdio.h>

void main ()

{

int code;

char symbol;

printf (“\n Input an ASCII code (0 to 127) : “);

scanf (“%d”,& code);

symbol = code;

printf ( “\n The symbol corresponding to %d is %c”, code, symbol );

}


In the above example, we have seen the single character that can be input to the computer and that can be displayed. Let us examine how the strings are accessed from the keyboard and displayed. A string in C is a sequence of consecutive characters in the memory, the last one being the null character. A null character has an ASCII code 0 and is called the end of string marker in C.

Example: Using String

#include <stdio.h>

void main ()

{

char mystring [20] =”My Nepal My pride”;

printf (“\n The string is : %s”, mystring);

}

In the above example, mystring [20] defines the number of characters that can be stored in the string variable. The %s sign is used for string formatted output. The characters of above string placed in sequence as follows

String

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Char

M

y

N

e

p

a

1

M

y

P

r

I

d

e

\0

ASCII Code

77

121

32

78

101

112

97

108

32

77

121

32

112

114

105

100

101

0

Now, let us examine how the string is scanned from the keyboard with the help of the following example.

Example:

#include <stdio.h>

void main ()

{

char mystring [20];

scanf (“%s”, & mystring);

printf (“\n The string is: %s”, mystring);

}

In this example, if you enter a string with spaces, the string before the first space only will be the output. This is suitable when a long string without blank spaces is used. All the characters after the first blank (space bar) will be truncated.

To print the entire sentence with more characters and blank spaces, we use gets () and puts (). This is illustrated with the following example.

Example:

#include <stdio.h>

void main ()

{

char mystring [20];

printf (“\n Enter a string: “);

gets (mystring);

printf(“\n The string is :”);

puts (mystring);

}

While running above program, as you enter a string within 20 characters long, it is accepted by gets () function prints the string on the screen.

Qualifiers

Qualifiers modify the behavior of the variable type to which they are applied. Some examples of qualifiers we discussed earlier are:

int i;

signed int i;

unsigned int i;

There are two size qualifiers that can be applied to integers: short and long. In any ANSI C complier, the size of short int, int and long int follow the restrictions given below:

  1. The maximum size of a short int is 16 bits.
  2. The size of an int must be greater than or equal to that of a short int.
  3. The size of a long int must be greater than or equal to that of an int.
  4. The maximum size of a long int is 32 bits.

The size of each type on your compiler can be tested by using size of operator. It gives the size of any data type in bytes.

Example:

#include <stdio.h>

void main ()

{

printf (“size of a short int is %d bytes\n”, size of (short int));

printf(“size of a long int is %d bytes\n”, size of (long int));

}

The output of the program is:

size of a short int is 2 bytes

size of an int is 2 bytes

size of a long int is 4 bytes

The following format specifies are used for displaying short and long integers.

Example:

#include <stdio.h>

Void main ()

{

short int shorti;

long int longi;

printf (“\n Input short int: “);

scanf (%hd”, &shorti);

printf (“\n Input long int: “);

scanf (“%d”, &long);

printf (“ shorti = %hd and longi = ”, shorti, longi);

}

A sample run output is given below:

input short int: 45

input long int: 600000000

shorti =45 longi = 600000000

The typedef Statement

The typedef statement is used to give new names to exiting types. For example,

Typedef unsigned long ulong;

This statement declares ulong a new type, equivalent to unsigned long. It can e used just any other type in program. Eg., ulong u;

Similarly, typef int integer;

Creates integer as a synonym for int. Then, you can use integer to define variables of type int, as;

integer count;

typedef Example typedef int age;

In this example, the declaration age is a user-defined data type, which is equivalent to type int. Hence, the varaiable declaration,

age male, female

Is equivalent to writing int male, female;

In other words, male and female are regarded as variables of type age, though they are actually integer-type variables.

The typedef feature is particularly convenient when defining structures, as it eliminates the need to repeatedly write struct tag whenever a structure is referred.

Promotion and Typecasting

Promotion is the type of data conversion when the variable of lower type is converted to a higher type i.e. the variable that holds lower range of values or has lower precision is converted to a variable that holds higher range of values or higher precision.

Example:

int i;

float f;

i = 5;

f = i;

Here, the last statement assigns i to f. Since, i is an integer, while f is floating point variable, the integer i is converted to float automatically. This is data type promotion. Another type of promotion is the conversion of a character to an integer. This particular type of promotion is called internal promotion.

Example:

int i;

char c;

c = ‘a’;

i = c;

The value present in the character c, i.e., the ASCII code of the character a is assigned to the integer i.

Assignments of variables of higher types to those of lower types result in truncation.

Example:

f = 7.8;

i = f;

Results in the fractional part (0.8) discarded. The value 7 is assigned to the integer i.

When we explicitly convert integer value i to floating point number, the explicit type of conversions are called typecasting.

Example:

int i;

Float f;

i = 12;

j =5;

f = i/j;

Printf(“%f\n”,f);

To obtain the floating point division, we have to convert either i or j into floating point of data. This can be done by using the following relation.

f = float (i/j);

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. -144.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

  1. The output of the program is 10. The variable num is defined before it is used. A value 10 is assigned to the variable num and finally %d tells that the value is in integer format.
  2. The scanf function reads the values of variables from the keyboard and stores them in variables. A character variable %c can hold only one character which can be alphabet or symbol.
  3. C is a sequence of consecutive characters in the memory, the last one being the null character. A null character has an ASCII code 0 and is called the end of string marker in C.
  4. Qualifiers modify the behavior of the variable type to which they are applied. The are two size qualifiers that can be applied to integers: short and long.
  5. The typedef feature is particularly convenient when defining structures since it eliminates the need to repeatedly write struct tag whenever a structure is referred.
  6. Promotion is the type of data conversion when the variable of lower type is converted to a higher type, i.e., the variable that holds a lower range of values or has lower precision is converted to a variable that holds a higher range of values or higher precision. 
.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

You must login to reply

Forum Time Replies Report

To calculate profit amount by asking value of sp and cp ?


You must login to reply