C Programming Introduction
C Programming Introduction
ii. Initialization
Initialization means assigning an initial value to the variable. After
declaration, you can optionally initialize a variable with a specific value.
For example:
int age = 18; // Initialization of 'age' with the value 18
float height = 7.5; // Initialization of 'height' with the value 7.5
char grade = 'B'; // Initialization of 'grade' with the character 'B'
v. Constants
• In addition to variables, C allows the use of constants. Constants are
similar to variables, but their values cannot be changed once set.
They are often defined using the const keyword.
• Are the read-only variables whose values cannot be modified once
they are declared in the C program
Syntax
const data_type var_name = value;
Data types
As the name suggests, a Datatype defines the type of data being used. Whenever
we define a variable or use any data in the C programming, we have to specify
the type of the data, so that the compiler knows what type of data to expect.
The following are data types in C language.
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type Enum
Void Data Type Void
Example
int number;
• Float Data Type: Floating-point data type is used to store numbers
with decimal points.
Range : 1.2E-38 to 3.4E+38
Size : 4 bytes
Format Specifier : %f
Syntax of float
The float keyword is used to declare the variable as a floating point:
float var_name;
Example
float pi = 3.14;
• Double Data Type: Double-precision floating-point data type is
used for larger decimal numbers and provides more precision than
float.
Range : 1.7E-308 to 1.7E+308
Size : 8 bytes
Format Specifier : %lf
Syntax of Double
The variable can be declared as double precision floating point using
the double keyword:
double var_name;
Example
double bigSize = 123456.79;
char var_name;
Example
char grade = 'A';
• Bool: Boolean data type is used to represent true or false values (0
or 1).
Example
bool a = true;
B. Derived Data Type
• These data types give programmers the ability to handle
heterogeneous data, directly modify memory, and build complicated
data structures.
• C also supports derived data types, including arrays, pointers,
structures, and unions.
➢ Arrays: Arrays allow you to store multiple values of the same
data type in a contiguous memory block.
Example
int numbers[5] = {1, 2, 3, 4, 5};
➢ Pointers: Pointers store the memory address of a variable.
They allow dynamic memory allocation and manipulation.
Example
int *ptr = &number;
➢ Structures: Structures allow you to group variables of different
data types under a single name.
Example
struct Person {
char name[20];
int age;
};
➢ Unions: Unions are similar to structures, but they share the
same memory location for all their members.
Example
union Value {
int intValue;
float floatValue;
};
C operators
Meaning;
✓ An operator is a symbol that tells the compiler to perform a certain
operation (arithmetic, comparison, etc.) using the values provided along
with the operator.
✓ Operators are used in programs to manipulate data and variables.
✓ An operand
➢ Every operator works with some values.
➢ The value with which an operator works is called as Operand.
➢ Can be a constant, a variable or a function result
Type of C Operators
C operators can be classified into the following types
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Special operators
1. Arithmetic Operators
The C language supports all the basic arithmetic operators such as addition,
subtraction, multiplication, division, etc.
The following table shows all the basic arithmetic operators along with their
descriptions.
Example
(where a and b are
Operator Description variables with some
integer value)
+ Adds two operands (values) a+b
++ operands
This is as the
the result
Increment operator - a++ or ++a
increases the integer value by one. This
-- operatorisneeds
This theonly a single operand.
Decrement operator - --b or b--
decreases integer value by one. This
operator needs only a single operand.
Example: Basic Arithmetic Operators
#include <stdio.h>
int main() {
int a = 50, b = 25, result;
// addition
result = a+b;
printf("Addition of a & b = %d \n",result);
// subtraction
result = a-b;
printf("Subtraction of a & b = %d \n",result);
// multiplication
result = a*b;
printf("Multiplication of a & b = %d \n",result);
// division
result = a/b;
printf("Division of a & b = %d \n",result);
// Using Modulus operator
result = a%b;
printf("result = %d",result);
return 0;
}
2. Relational operators in C
➢ The relational operators (or comparison operators) are used to check
the relationship between two operands.
➢ It checks whether two operands are equal or not equal or less than or
greater than, etc.
➢ It returns 1 if the relationship checks pass, otherwise, it returns 0.
➢ The following table shows all relational operators supported in the C
language.
Example
Operator Description (a and b, where a = 10 and b = 11)
#include <stdio.h>
int main() {
int a = 10, b = 20, result;
// Equal
result = (a==b);
printf("(a == b) = %d \n",result);
// less than
result = (a<b);
printf("(a < b) = %d \n",result);
// greater than
result = (a>b);
printf("(a > b) = %d \n",result);
// less than equal to
result = (a<=b);
printf("(a <= b) = %d \n",result);
return 0;
}
3. Logical Operators
C language supports the following 3 logical operators.
&& (Logical AND): Returns true if both operands are true.
|| (Logical OR): Returns true if at least one operand is true.
! (Logical NOT): Returns true if the operand is false and vice versa.
4. Assignment Operators
The assignment operators are used to assign value to a variable.
You can see all the assignment operators in the table given below.
Example
(a and b are two variables,
Operator Description with where a=10 and b=5)
Example
#include <stdio.h>
int main ()
{
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is
1. In C Programming, bitwise OR operator is denoted by |.
Example for the bitwise OR operation of two integers 12 and 25.
Example
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a | b);
return 0;
}
Example
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a ^ b);
return 0;
}
Shift Operators
There are two shift operators in C programming:
Right shift operator.
Left shift operator.
#include <stdio.h>
int main()
{
int num=212, i;
for (i = 0; i <= 2; ++i)
{
printf("Right shift by %d: %d\n", i, num >> i);
}
printf("\n");
for (i = 0; i <= 2; ++i)
{
printf("Left shift by %d: %d\n", i, num << i);
}
return 0;
}
6. Conditional (Ternary) Operator:
➢ The conditional statements are the decision-making statements which
depends upon the output of the expression. It is represented by two
symbols, i.e., '?' and ':'.
➢ As conditional operator works on three operands, so it is also known as
the ternary operator.
➢ The behavior of the conditional operator is similar to the 'if-else'
statement as 'if-else' statement is also a decision-making statement.
➢ Syntax of a conditional operator
Expression1? expression2: expression3;
7. Special operators
The sizeof operator
The sizeof is a unary operator that returns the size of data (constants,
variables, array, structure, etc).
Comma Operator
Comma operators are used to link related expressions together.
In C, operators have precedence and associativity rules that determine the order
in which they are evaluated in an expression. Precedence defines the priority of
operators, and associativity defines the order in which operators of the same
precedence are evaluated. Understanding these rules is crucial for correctly
interpreting and writing expressions.
Precedence Rules:
The table below lists some of the major C operators in order of decreasing
precedence:
iii. All the operators that have a similar level of precedence have the same
associativity. It is very important, or else the compiler won’t be able to
decide what order of evaluation must an expression follow when it has two
operators with the same precedence but different associativity. For
example, – and + have the very same associativity.
iv. The associativity and precedence of prefix ++ and postfix ++ are very
different from each other. Here, the precedence of prefix ++ is less as
compared to the postfix ++. Thus, their associativity also turns out to be
different. Here, the associativity of prefix ++ is from right to left, while the
associativity of postfix ++ is from left to right.
v. We must use a comma (,) very carefully, as it has the least level of
precedence among all the other operators present in an expression.
int main()
{
int x = 5, y = 10;
int z;
z = x * 2 + y;
printf(“\n output = %d”, z);
return 0;
}
(type) expression
Here, (type) is the type cast operator, and expression is the value or
variable to be cast to the specified data type.
Float to Integer:
float num = 15.75;
int result = (int) num;
Character to Integer:
char ch = 'A';
int asciiValue = (int) ch;
Integer to Character:
int asciiValue = 65;
char ch = (char) asciiValue;
➢ Examples
1. printf()
Syntax 1:
To display any variable value
printf(“Format Specifiers”, var1, var2, …., varn);
Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
2. scanf()
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Example
char ch;
ch = getchar(); // Read a character
putchar(ch); // Write a character
Exercise 1:
#include <stdio.h>
#include<conio.h>
void main()
{
// Declare variables
float length, width, area;
getch();
}
Exercise 2:
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a ^ b);
printf("Output = %d", a & b);
printf("Output = %d", a | b);
return 0;
}