0% found this document useful (0 votes)
24 views

Data Types

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Data Types

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Scanf in C

In C programming language, scanf is a function that stands for Scan Formatted String. It
reads data from stdin (standard input stream i.e. usually keyboard) and then writes the result
into the given arguments.
• It accepts character, string, and numeric data from the user using standard input.
• Scanf also uses format specifiers like printf.
While scanning the input, scanf needs to store that input data somewhere. To store this
input data, scanf needs to known the memory location of a variable. And here comes the
ampersand to rescue.
• & is also called as address of the operator.
• For example, &var is the address of var.

#include <stdio.h>
int main()
{
int a, b;

printf("Enter first number: ");


scanf("%d", &a);

printf("Enter second number: ");


scanf("%d", &b);

printf("A : %d \t B : %d" , a , b);

return 0;
}
DATA TYPES IN C: Each data type requires different amounts of memory and has some
specific operations which can be performed over it.
• It specifies the type of data that the variable can store like integer, character, floating,
double, etc.
• Also determines the space that variable occupies in the storage.

Primary Integer, character, float, double, void


Derived Pointer, Array, functions
User Defined Enum, Structure, Union, Typedef

Primary Data Types


The size of any given data type in a program depends a lot on the type of processor, as well as
the compiler. In simpler words, the size of the data type depends entirely on the computer on
which we run C language along with the version of the C program compiler that we installed
in the computer.

The int data type can be 4 bytes/ 2 bytes.


Int will be 2 bytes or 16 bits in the case of an environment that is 16-bit.
However, int will be 4 bytes or 32 bits in case of an environment that is 32-bit and 64 bit.

The char data type is 1 byte.


The size of the char data type is basically 8 bits or 1 byte.
No variation would occur with different compilers and interpreters. It means that the type of
compiler or processor used will have no effect on its size whatsoever.

The double data type is 8 bytes.


The size of the double data type is basically 64 bits or 8 bytes.
It is capable of storing values that are comparatively double the size of the bytes that the float
data type can store. This is the reason why it is known as the double.
This data type is capable of holding about 15-17 digits, both after and before the decimal of the
data type.

The float data type is 4 bytes.


The size of the float data type is basically 32 bits or 4 bytes.
The float data type is single-precision in nature, and we use it for holding the decimal values.
It helps in the storage of various large values, but the float is comparatively faster than double.
It is because double works with comparatively much larger data values. Thus, it is slower
comparatively.
The void data type is 0 bytes.
Since the void data type has no meaning, it has no size at all.

There is a range of values that come under all of these data types. But before we look into that,
let us take a look at the modifiers that are used in the data types.

Modifiers in C

These are keywords in C to modify the default properties of int and char data types. There are
4 modifiers in C as follows.

1. short It limits user to store small integer values from -32768 to 32767. It can be used
only on int data type.
2. long It allows user to stores very large number (something like 9 Million Trillion)
from -9223372036854775808 to 9223372036854775807.
3. signed It is default modifier of int and char data type if no modifier is specified. It
says that user can store negative and positive values.
4. unsigned When user intends to store only positive values in the given data type (int
and char).

#include<stdio.h>
void main()
{
int a;
char c;
float f;
printf("size of int %d\n", sizeof(int));
printf("size of char %d\n",sizeof(char));
printf("size of float %d",sizeof(float));
}
Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler.
Data Type Memory Range Format
(bytes) Specifier
(32 Bit)
short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to %d
2,147,483,647

long int 4 -2,147,483,648 to %ld


2,147,483,647

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long 8 0 to %llu


int 18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 1.2E-38 to 3.4E+38 %f

double 8 1.7E-308 to 1.7E+308 %lf

long double 16 3.4E-4932 to 1.1E+4932 %Lf


User Defined Datatypes
The User-Defined Data Types are basically defined by a user according to their will.
1. Typedef
We use the keyword typedef for creating an alias (a new name) for a data type that already
exists. The typedef won’t create any new form of data type.
When using the typedef data type, the syntax would be:
typedef existing_data_type new_type;
Let us consider the example as follows:

#include <stdio.h>

typedef int Lessons;

void main() {

typedef int Hi;

Hi x = 17;

printf("Given value =%d\n", x);

2. Enum

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign
names to integral constants, the names make a program easy to read and maintain.
Here is the format that we use for creating the enum type:
enum enum_variable (value_a, value_b, …. , value_z);
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()

enum week day;

day = Wed;

printf("%d",day);

return 0;

Character Set In C
In the C programming language, the character set refers to a set of all the valid characters that
we can use in the source program for forming words, expressions, and numbers.

Types of Characters in C
The C programming language provides support for the following types of characters. In other
words, these are the valid characters that we can use in the C language:

• Digits
• Alphabets
• Special characters
• Whitespaces

Alphabets
The C programming language provides support for all the alphabets that we use in the
English language. Thus, in simpler words, a C program would easily support a total of 52
different characters- 26 uppercase and 26 lowercase.

Type of Character Description Characters

Lowercase a to z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
Alphabets

Uppercase A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W,
Alphabets X, Y, Z
Digits
The C programming language provides the support for all the digits that help in constructing/
supporting the numeric values or expressions in a program. These range from 0 to 9, and also
help in defining an identifier. Thus, the C language supports a total of 10 digits for
constructing the numeric values or expressions in any program.

Type of Character Description Characters

Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special Characters
We use some special characters in the C language for some special purposes, such as logical
operations, mathematical operations, checking of conditions, backspaces, white spaces, etc.
We can also use these characters for defining the identifiers in a much better way. For
instance, we use underscores for constructing a longer name for a variable, etc.
The C programming language provides support for the following types of special characters:

Type of Character Examples

Special Characters `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.?

White Spaces
The white spaces in the C programming language contain the following:

• Blank Spaces
• Tab
• New Line
Keywords
Keywords are generally called as pre-defined or reserved words in a programming language.
Every keyword in C language performs a specific function in a program.
• Keywords cannot be used as variable names.
• Keywords have fixed meanings, and that meaning cannot be changed.
• They are the building block of a 'C' program.
• C supports 32 keywords.
• All the keywords are written in lowercase letters.
The different types of keywords are as follows −

auto double int struct

break else long switch

case enum register typedef

char extern return union

const short float unsigned

continue for signed void

default goto sizeof volatile

do if static while
Identifiers
An identifier is used for any variable, function, data definition, labels in your program etc.
In C language, an identifier is a combination of alphanumeric characters, i.e. first begin with a
letter of the alphabet or an underline, and the remaining are letter of an alphabet, any numeric
digit, or the underline.

Rules for naming identifiers

The rules that must be followed while naming the identifiers are as follows −
• The case of alphabetic characters is significant.
• There is no rule on how long an identifier can be. We can run into problems in
some compilers, if an identifier is longer than 31 characters. This is different for
the different compilers.
• A valid identifier can have letters (both uppercase and lowercase letters), digits
and underscores.
• The first letter of an identifier should be either a letter or an underscore.
• You cannot use keywords like int, while etc. as identifiers.
Identifiers must be unique
For example,
int a;
int a();
{
//
}

Variables
A variable in simple terms is a storage place that has some memory allocated to it.
Basically, a variable is used to store some form of data. Different types of variables require
different amounts of memory, different type of memory locations, and some specific set of
operations that can be applied to them.
Variable Declaration: A typical variable declaration is of the form:
type variable_name;
Rules for defining variables
1. A variable can have alphabets, digits, and underscore.
2. A variable name can start with the alphabet, and underscore only. It can’t start
with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword, e.g. int, goto, et
Types of Variables in C
1. Local Variable: A variable that is declared and used inside the function or block is
called a local variable. It is scope is limited to function or block. It cannot be used outside
the block. Local variables need to be initialized before use.
2.Global Variable: A variable that is declared outside the function or block is called a global
variable. It is declared at the start of the program. It is available for all functions.

3.Static Variable: A variable that retains its value between multiple function calls is known
as a static variable. It is declared with the static keyword.

4.Automatic Variable: All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable using the auto
keyword. Automatic variables are similar to local variables.

Difference between Variable and identifier

The fundamental difference between an identifier and variable is that an identifier


is a “name given to entity” in a program whereas, a variable is a “name given to
memory location”, that is used to hold value, which may get modified during
program execution.
BASIS FOR
IDENTIFIER VARIABLE
COMPARISON

Use Identifier is used to name a Variable is used to name a

variable, function, class, memory location, which

structure, union etc. holds a value.

Purpose Created to give a unique Allots a unique name to a

name to an entity. particular memory

location.

Range All identifiers are not All variables names are

variable. identifier.

Example int a; int a;

or or

int a(){ float a;

// //

}
Ques 1: WAP to print whether a number entered by user is positive or negative.

int main()
{
int A;

printf("Enter the number A: ");


scanf("%d", &A);

if (A > 0)
printf("%d is positive.", A);
else if (A < 0)
printf("%d is negative.", A);
else if (A == 0)
printf("%d is zero.", A);

return 0;
}

Constants in C

Constants in c refers to fixed values that do not change during the execution of
a program.

1. Integer Constants
It can be an octal integer or a decimal integer or even a hexadecimal integer. 55
—> Decimal Integer Constant
0x5B —> Hexa Decimal Integer Constant
O23 —> Octal Integer Constant

• It must have at least one digit.


• There must be no decimal point.
• It does not allow any blanks or commas.
• An integer constant can be both negative or positive.
• We assume an integer constant to be positive if there is no sign in front of
that constant.
• The allowable range for this type of constant is from -32768 to 32767.

2. Real Constants
This type of constant must contain both the parts- decimal as well as integers.
We represent the floating-point value 3.14 as 3E-14 in its exponent form.

• This type of constant must consist of one digit at least.


• There must not be any decimal point.
• This type of constant can be both negative or positive.
• It does not allow any blanks or commas within.
• We assume an integer constant to be positive if there is no sign in front of
that constant.

3. Single Character Constants


The character constants are symbols that are enclosed in one single
quotation. The maximum length of a character quotation is of one
character only. Example,
‘B’
‘5’
‘+’

4. String Constant
• The string constants are a collection of various special symbols, digits,
characters, and escape sequences that get enclosed in double quotations.
• The definition of a string constant occurs in a single line:
• “This is Cookie”
Symbolic Constants: A symbolic constant is a name given to some numeric
constant, or a character constant or string constant, or any other constants.

Symbolic constant names are also known as constant identifiers. Pre-processor


directive #define is used for defining symbolic constants.

#define PI 3.14

#define MAX 500

You might also like