C – DATA TYPES:
There are four data types in C language. They are,
Basic data types: int, char, float, double
Enumeration data type: enum
Derived data type: pointer, array, structure, union
Void data type: void
1. BASIC DATA TYPES IN C LANGUAGE:
1.1. INTEGER DATA TYPE:
a. Integer data type allows a variable to store numeric values.
b. “int” keyword is used to refer integer data type.
c. The storage size of int data type is 2 or 4 or 8 byte.
d. It varies depend upon the processor in the CPU that we use.
e. If we are using 16 bit processor, 2 byte (16 bit) of memory will be allocated for int data type.
f. Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
g. int (2 byte) can store values from -32,768 to +32,767
h. int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
i. If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for which the limits are very high.
Note:
a. We can’t store decimal values using int data type.
b. If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
c. In this case, float data type can be used to store decimal values in a variable.
1.2. CHARACTER DATA TYPE:
a. Character data type allows a variable to store only one character.
b. Storage size of character data type is 1. We can store only one character using character data type.
c. “char” keyword is used to refer character data type.
d. For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.
1.3. FLOATING POINT DATA TYPE:
The floating-point data type consists of 2 types. They are,
a. float
b. double
1. FLOAT:
a. Float data type allows a variable to store decimal values.
b. Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
c. We can use up-to 6 digits after decimal using float data type.
d. For example, 10.456789 can be stored in a variable using float data type.
2. DOUBLE:
a. Double data type is also same as float data type which allows up-to 10 digits after decimal.
b.The range for double datatype is from 1E–37 to 1E+37.
1.3.1. SIZEOF() FUNCTION IN C LANGUAGE:
sizeof() function is used to find the memory space allocated for each C data types.
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
1.3.2. MODIFIERS IN C LANGUAGE:
a. The amount of memory space to be allocated for a variable is derived by modifiers.
b. Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
c. For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.
There are 5 modifiers available in C language. They are,
a. short
b. long
c. signed
d. unsigned
e. long long
2. ENUMERATION DATA TYPE IN C LANGUAGE:
a. Enumeration data type consists of named integer constants as a list.
b. It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
#include <stdio.h>
int main()
{
enum MONTH { Jan = 0, Feb, Mar };
enum MONTH month = Mar;
if(month == 0)
printf("Value of Jan");
else if(month == 1)
printf("Month is Feb");
if(month == 2)
printf("Month is Mar");
}
3. DERIVED DATA TYPE IN C LANGUAGE:
Array, pointer, structure and union are called derived data type in C language.
4. VOID DATA TYPE IN C LANGUAGE:
a. Void is an empty data type that has no value.
b. This can be used in functions and pointers.
C VARIABLE:
C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.The value of the C variable may get change in the program.C variable might be belonging to any of the data type like int, float, char etc.
RULES FOR NAMING C VARIABLE:
Variable name must begin with letter or underscore.
Variables are case sensitive
They can be constructed with digits, letters.
No special symbols are allowed other than underscore.
sum, height, _value are some examples for variable name
DECLARING & INITIALIZING C VARIABLE:
Variables should be declared in the C program before to use.
Memory space is not allocated for a variable while declaration. It happens only on variable definitions.
Variable initialization means assigning a value to the variable.
Variable declaration:
data_type variable_name; Example: int x, y, z; char flat, ch;
Variable initialization:
data_type variable_name = value;
Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;
THERE ARE THREE TYPES OF VARIABLES IN C PROGRAM THEY ARE,
Local variable
Global variable
Environment variable
1. EXAMPLE PROGRAM FOR LOCAL VARIABLE IN C:
a.The scope of local variables will be within the function only.
b.These variables are declared within the function and can’t be accessed outside the function.
c. In the below example, m and n variables are having scope within the main function only.
These are not visible to test function.
d. Likewise, a and b variables are having scope within the test function only. These are not visible to the main function.
#include<stdio.h>
void test();
int main()
{
int m = 22, n = 44;
// m, n are local variables of main function
/*m and n variables are having scope
within this main function only.
These are not visible to test funtion.*/
/* If you try to access a and b in this function,
you will get 'a' undeclared and 'b' undeclared error */
printf("\nvalues : m = %d and n = %d", m, n);
test();
return 0;
}
void test()
{
int a = 50, b = 80;
// a, b are local variables of test function
/*a and b variables are having scope
within this test function only.
These are not visible to main function.*/
/* If you try to access m and n in this function,
you will get 'm' undeclared and 'n' undeclared
error */
printf("\nvalues : a = %d and b = %d", a, b);
}
2. EXAMPLE PROGRAM FOR GLOBAL VARIABLE IN C:
The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.
#include<stdio.h>
void test();
int m = 22, n = 44;
int a = 50, b = 80;
int main()
{
printf("All variables are accessed from main function");
printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
test();
return 0;
}
void test()
{
printf("\n\nAll variables are accessed from" \
" test function");
printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
}
3. ENVIRONMENT VARIABLES IN C:
Environment variable is a variable that will be available for all C applications and C programs.
We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.
The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.
There are 3 functions which are used to access, modify and assign an environment variable in C. They are,
a. setenv() b. getenv() c. putenv()
EXAMPLE PROGRAM FOR GETENV() FUNCTION IN C:
This function gets the current value of the environment variable. Let us assume that environment variable DIR is assigned to “/usr/bin/test/”.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Directory = %s\n",getenv("DIR"));
return 0;
}
EXAMPLE PROGRAM FOR SETENV() FUNCTION IN C:
This function sets the value for environment variable. Let us assume that environment variable “FILE” is to be assigned “/usr/bin/example.c”
#include <stdio.h>
#include <stdlib.h>
int main()
{
setenv("FILE", "/usr/bin/example.c",50);
printf("File = %s\n", getenv("FILE"));
return 0;
}
EXAMPLE PROGRAM FOR PUTENV() FUNCTION IN C:
This function modifies the value for environment variable. Below example program shows that how to modify an existing environment variable value.
#include <stdio.h>
#include <stdlib.h>
int main()
{
setenv("DIR", "/usr/bin/example/",50);
printf("Directory name before modifying = " \"%s\n", getenv("DIR"));
putenv("DIR=/usr/home/");
printf("Directory name after modifying = " \"%s\n", getenv("DIR"));
return 0;
}
Questions on Data Types and Sizes
1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
a) The compiler will flag an error
b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
2. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
3. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
5. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
6. What will be the output of the following C code?
#include <stdio.h>
int main()
{
signed char chr;
chr = 128;
printf("%d\n", chr);
return 0;
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
7. What will be the output of the following C code?
#include <stdio.h>
int main()
{
char c;
int i = 0;
FILE *file;
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'a');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'b');
fclose(file);
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
8. What is short int in C programming? a) The basic data type of C b) Qualifier c) Short is the qualifier and int is the basic data type d) All of the mentioned
1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1)
printf("equal\n");
else
printf("not equal\n");
}
a) equal
b) not equal
c) output depends on the compiler
d) error
2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
a) equal
b) not equal
c) output depends on compiler
d) error
3. What will be the output of the following C code on a 32-bit machine?
#include <stdio.h>
int main()
{
int x = 10000;
double y = 56;
int *p = &x;
double *q = &y;
printf("p and q are %d and %d", sizeof(p), sizeof(q));
return 0;
}
a) p and q are 4 and 4
b) p and q are 4 and 8
c) compiler error
d) p and q are 2 and 8
4. Which is correct with respect to the size of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
5. What will be the output of the following C code on a 64 bit machine?
#include <stdio.h>
union Sti
{
int nu;
char m;
};
int main()
{
union Sti s;
printf("%d", sizeof(s));
return 0;
}
a) 8
b) 5
c) 9
d) 4
6. What will be the output of the following C code?
#include <stdio.h>
int main()
{
float x = 'a';
printf("%f", x);
return 0;
}
a) a
b) run time error
c) a.0000000
d) 97.000000
7. Which of the data types has the size that is variable? a) int b) struct c) float d) double
ความคิดเห็น