2Q) What are data types and constants? Explain.
A)Computers are useful because of their ability to store and manipulate huge amounts of information. This information may be numbers, such as a financial report, or alphabetic characters like names and addresses. Managing this information requires the usage of programming languages. One of the important task of a programming language is identifying the type of data it is manipulating.
Data is stored in a computer's memory. The memory system comprises of uniquely numbered cells called memory addresses. We need to know the address where something is stored in order to retrieve it and work on it. A programming language frees us from keeping track of these memory addresses by substituting names for them. These names are called variables. Variables are descriptive names for the memory addresses.
Before we use a variable in C we must declare it. We must identify what kind of information will be stored in it. This is called defining a variable.Variables must be declared at the start of any block of code, but most are found at the start of each function. A variable must be defined to be one of the legal C data types. When a variable is defined it is not automatically initialized, it is the responsibility of the programmer to initialize this to a start value.
Variable names
All variable definitions must include two things variable name and its data type. Some rules to be followed in naming a variable in C are, it must start with an alphabet and can contain letter, underscores and digits. Both uppercase and lowercase letters can be used. Spaces should not be used in a variable name. Certain keywords like int, float, struct, if, while cannot be used as variable names. The variable names should not be very long and one should refer to the documentation of the C compiler to know the limitation. Generally the first eight characters of a variable name are significant.Some of the valid variable names are :
fAmount
iTotal
iCounter
Note : The variable names should be meaningful and denote what the variable stores. As far as possible single letter variable names should be avoided.
C provides a number of data types, some of the basic types used are :
|
Table 3.1There are variants to the above, such as
|
Table 3.2When declaring variables to be of type long int, short int and unsigned int it is permissible to omit the keyword int. Since these variables must always be integers. Thus we can declare an unsigned integer counter as
unsigned counter;
Unlike in programming languages like Visual Basic, there is no datatype such as string in C. However you can declare an array of the datatype char and write functions to manipulate this.
The main difference among these data types is the amount of memory allocated for storing these. The maximum and minimum values that can be stored in these variables depends on the version of C and the computer system being used. Some of the typical values are :
|
No comments:
Post a Comment