0% found this document useful (0 votes)
15 views40 pages

C Programming: Variables and Data Types

Uploaded by

s60276005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views40 pages

C Programming: Variables and Data Types

Uploaded by

s60276005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming

Fundamentals
C Variables

• A variable in C is a named piece of memory which is used to store data


and access it whenever required. It allows us to use the memory
without having to memorize the exact memory address.
• To create a variable in C, we have to specify a name and the type of
data it is going to store in the syntax.
• C provides different data types that can store almost all kinds of data.
For example, int, char, float, double, etc.
• In C, every variable must be declared before it is used. We can also
declare multiple variables of same data type in a single statement by
separating them using comma as shown:
• data_type name1, name2, name3, ...;
Rules for Naming Variables in C

• We can assign any name to a C variable as long as it follows the


following rules:
• A variable name must only contain letters, digits,
and underscores.
• It must start with an alphabet or an underscore only. It cannot
start with a digit.
• No white space is allowed within the variable name.
• A variable name must not be any reserved word or keyword.
• The name must be unique in the program.
• C Variable Initialization
• Once the variable is declared, we can store useful values in it. The first value we
store is called initial value and the process is called Initialization. It is done
using assignment operator (=).
• int num;
• num = 3;
• It is important to initialize a variable because a C variable only
contains garbage value when it is declared. We can also initialize
a variable along with declaration.
• Note: It is compulsory that the values assigned to the variables
should be of the same data type as specified in the declaration.
Memory Allocation of C Variables

• When a variable is declared, the compiler is told that the variable


with the given name and type exists in the program. But no
memory is allocated to it yet. Memory is allocated when the
variable is defined.
• Most programming languages like C generally declare and define a
variable in the single step. For example, in the above part where
we create a variable, variable is declared and defined in a single
statement.
• The size of memory assigned for variables depends on the type of
variable. We can check the size of the variables using size of
operator.
• Example:
• #include <stdio.h>
• int main()
• { int num = 22;
• printf("%dbytes", sizeof(num));
• return 0;} output 4 bytes
Scope of Variables in C

• We have told that a variable can be accessed anywhere once it is


declared, but it is partially true. A variable can be accessed using
its name anywhere in a specific region of the program called
its scope. It is the region of the program where the name assigned
to the variable is valid.
• A scope is generally the area inside the {} curly braces.
Keywords in C
• Keywords are predefined or reserved words that have special meanings to the compiler.

• These are part of the syntax and cannot be used as identifiers in the program.
• these keywords as identifiers (such as variable names, function names, or struct names).
• These keywords can not be
used as identifiers (such as
variable names, function
names, or struct names).
Difference Between Keywords and Identifiers
Constants in C

• In C programming, const is a keyword used to declare a variable as constant,


meaning its value cannot be changed after it is initialized.
• It is mainly used to protect variables from being accidentally modified,
making the program safer and easier to understand.
• These constants can be of various types, such as integer, floating-point,
string, or character constants.
• #include <stdio.h>
• int main() { // Defining constant variable
• const int a = 10;
• printf("%d", a);
• return 0;
• }
Syntax

• We define a constant in C using the const keyword.


• Also known as a const type qualifier, the const keyword is placed
at the start of the variable declaration to declare that variable as a
constant.
• const data_type var_name = value
Properties of Constant

• The important properties of constant variables in C defined using


the const keyword are as follows:
• 1. Initialization with Declaration
• We can only initialize the constant variable in C at the time of its
declaration.
• If we do not initialize it at the time of declaration, it will store the
garbage value that was previously stored in the same memory.
• #include <stdio.h>
• int main() {
// Not initializing a constant variable
• const int a;
• // printing value
• printf("%d", a);
• return 0;
• } output any garbage value.
Immutability
• The constant variables in c are immutable after its definition, i.e.,
they can be initialized only once in the whole program.
• After that, we cannot modify the value stored inside that variable.
• #include <stdio.h>
• int main()
{ const int a; // Initializing constant variable var after declaration
a = 20;
• printf("%d", a);
return 0;
}
Constants Using #define

• In C, the #define directive can also be used to define symbolic


constants that do not require a data type.
• They are called macros and are replaced by their values at compile
time.
• Syntax:
• #define CONSTANT_NAME value
• Example:
• #include <stdio.h>
• #define PI 3.14
• int main()
• { printf("%.2f", PI);
• return 0;}
Real world examples of Const

• ATM Daily Withdrawal Limit


-> In banking software, a customer may have a fixed limit of
₹20,000 per day.
• const int dailyLimit = 20000;
• Mobile App – Max Login Attempts
-> Apps like WhatsApp or banking apps usually allow only 3 login
attempts.
Operators in C
Operators in C
• Operators are the basic components of C programming. They are symbols that
represent some kind of operation, such as mathematical, relational, bitwise,
conditional, or logical computations, which are to be performed on values or
variables.
• The values and variables used with operators are called operands.
• C language provides a wide range of built in operators that can be classified
• into 6 types based on their functionality:

• Arithmetic Operators
• Relational Operators
• Logical Operator
• Bitwise Operators
• Assignment Operators
• Other Operators
Arithmetic Operators
• Relational Operators

• The relational operators in C are used for the comparison of the


two operands.
• All these operators are binary operators that return true or false
values as the result of comparison.
• These are a total of 6 relational operators in C:
• Logical Operator

• Logical Operators are used to combine two or more


conditions/constraints or to complement the evaluation of the
original condition in consideration.
• The result of the operation of a logical operator is a Boolean value
either true or false.
• There are 3 logical operators in C:
Assignment Operators in C

• In C language, the assignment operator stores a certain value in


an already declared variable.
• A variable in C can be assigned the value in the form of a literal,
another variable, or an expression.
• The value to be assigned forms the right-hand operand, whereas
the variable to be assigned should be the operand to the left of the
"=" symbol, which is defined as a simple assignment operator in
C.
D Data Types
• Each variable in C has an associated data type. It specifies the
type of data that the variable can store like integer, character,
floating, double, etc.
• For example
• int number;
• The above statement declares a variable with name number that
can store integer values.
• C is a statically type language where each variable's type must be
specified at the declaration and once specified, it cannot be
changed.
Data Types
Integer Data Type
The integer datatype
in C is used to store
the integer numbers
(any number
including positive,
negative and zero
without decimal part).
Octal values,
hexadecimal values,
and decimal values
can also be stored in
int data type in C.
•Range: -
2,147,483,648 to
2,147,483,647
•Size: 4 bytes
•Format
Integer Data Types

• The integer data type can also be used as:


• unsigned int: It can store the data values from zero to positive
numbers, but it can’t store negative values
• short int: It is lesser in size than the int by 2 bytes so can only store
values from -32,768 to 32,767.
• long int: Larger version of the int datatype so can store values
greater than int.
• unsigned short int: Similar in relationship with short int as
unsigned int with int.
Character Data Type

• Character data type allows its variable to store only a single


character. The size of the character is 1 byte. It is the most basic
data type in C. It stores a single character and requires a single
byte of memory in almost all compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
Float Data Type

• In C programming, float data type is used to store single precision


floating-point values. These values are decimal and exponential
numbers.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f
Double Data Type

• The double data type in C is used to store decimal numbers


(numbers with floating point values) with double precision. It can
easily accommodate about 16 to 17 digits after or before a
decimal point.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf
Void Data Type

• The void data type in C is used to indicate the absence of a value.


Variables of void data type are not allowed. It can only be used
for pointers and function return type and parameters.
• void fun(int a, int b)
•{
• // function body
•}
Arithmetic Operators in C

• Arithmetic operators are the type of operators used to perform basic


math operations like addition, subtraction, and multiplication.
#include <stdio.h>
int main() {
// Calculate the area of the triangle
int sum = 10 + 20;
printf("%d", sum);
return 0;
}
• Binary Arithmetic Operators

• The binary arithmetic operators work on two operands. C


provides 5 such operators for performing arithmetic functions
which are as follows:
• C provides 9 arithmetic operators to work with numbers and
perform different mathematical operations.
• These can be classified into two types based on the number of
operands they work on:
• Binary Arithmetic Operators
• Unary Arithmetic Operators
Unary Arithmetic Operators

• The unary arithmetic operators work with a single operand. In C,


we have four such operators which are as follows:
• int main(){
• int a = 10, res;
• res = a++;
• printf("a is %d, res is %d\n", a,res);
• res = a--;
• printf("a is %d, res is %d\n", a, res);
• res = ++a;
• printf("a is %d, res is %d\n", a, res);
res = --a;
printf("a is %d, res is %d\n", a, res);
printf("+a is %d\n", +a);
printf("-a is %d", -a);
return 0;
}
a is 11, res is 10
a is 10, res is 11
a is 11, res is 11
a is 10, res is 10
+a is 10
-a is -10

You might also like