Data Types
Data Types
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;
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.
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
int 4 -2,147,483,648 to %d
2,147,483,647
#include <stdio.h>
void main() {
Hi x = 17;
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>
int main()
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.
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.
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:
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 −
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.
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.
location.
variable. identifier.
or or
// //
}
Ques 1: WAP to print whether a number entered by user is positive or negative.
int main()
{
int 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
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.
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.
#define PI 3.14