0% found this document useful (0 votes)
13 views26 pages

C Language Notes

ccbv bhfjhv hbhjfhvjh hgvhgvjhgj .

Uploaded by

ayushpctech5
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)
13 views26 pages

C Language Notes

ccbv bhfjhv hbhjfhvjh hgvhgvjhgj .

Uploaded by

ayushpctech5
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
You are on page 1/ 26

What is C?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.
It is a very popular language, despite being old. The main reason for its popularity is because it is a
fundamental language in the field of computer science.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular programming languages such as Java, Python,
C++, C#, etc, as the syntax is similar
If you know C, you will understand how computer memory works
C is very fast, compared to other programming languages, like Java and Python
C is very versatile; it can be used in both applications and technologies

Difference between C and C++


C++ was developed as an extension of C, and both languages have almost the same syntax
The main difference between C and C++ is that C++ supports classes and objects, while C does not.
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}

Example explained
Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions, such
as printf() (used in line 4). Header files add functionality to C programs.
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
Line 3: Another thing that always appear in a C program is main(). This is called a function. Any code inside its
curly brackets {} will be executed.
Line 4: printf() is a function used to output/print text to the screen. In our example, it will output "Hello
World!".
Note: Every C statement ends with a semicolon ;
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.
Line 5: return 0 ends the main() function.
Line 6: Do not forget to add the closing curly bracket } to actually end the main function.

C Statements
Statements
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
The following statement "instructs" the compiler to print the text "Hello World" to the screen:
printf("Hello World!");
It is important that you end the statement with a semicolon ;
If you forget the semicolon (;), an error will occur and the program will not run:
Many printf Functions
You can use as many printf() functions as you want. However, note that it does not insert a new line at the
end of the output:
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}

C New Lines
To insert a new line, you can use the \n character:
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
You can also output multiple lines with a single printf() function. However, this could make the code harder
to read:
Example
#include <stdio.h>
int main() {
printf("Hello World!\nI am learning C.\nAnd it is awesome!");
return 0;
}
Tip: Two \n characters after each other will create a blank line:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}

What is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to the
beginning of the next line on the screen. This results in a new line.
Examples of other valid escape sequences are:
Escape Sequence Description Try it
\t Creates a horizontal tab
\\ Inserts a backslash character (\)
\" Inserts a double quote character
Comments in C
Comments can be used to explain code, and to make it more readable. It can also be used to prevent
execution when testing alternative code.
Comments can be singled-lined or multi-lined.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not be executed).
This example uses a single-line comment before a line of code:
// This is a comment
printf("Hello World!");

C Variables
Variables
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Characters are surrounded by single quotes
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
Where type is one of C types (such as int), and variableName is the name of the variable (such
as x or myName). The equal sign is used to assign a value to the variable.
So, to create a variable that should store a number, look at the following example:
Create a variable called myNum of type int and assign the value 15 to it:
int myNum = 15;
You can also declare a variable without assigning the value, and assign the value later:
Example
// Declare a variable
int myNum;

// Assign a value to the variable


myNum = 15;

C Format Specifiers
Format Specifiers
Format specifiers are used together with the printf() function to tell the compiler what type of data the
variable is storing. It is basically a placeholder for the variable value.
A format specifier starts with a percentage sign %, followed by a character.
For example, to output the value of an int variable, use the format specifier %d surrounded by double quotes
(""), inside the printf() function:
Example
int myNum = 15;
printf("%d", myNum); // Outputs 15
To print other types, use %c for char and %f for float
// Create variables
int myNum = 15; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
To combine both text and a variable, separate them with a comma inside the printf() function:
Example
int myNum = 15;
printf("My favorite number is: %d", myNum);
To print different types in a single printf() function, you can use the following:
Example
int myNum = 15;
char myLetter = 'D';
printf("My number is %d and my letter is %c", myNum, myLetter);
Print Values Without Variables
You can also just print a value without storing it in a variable, as long as you use the correct format specifier:
Example
printf("My favorite number is: %d", 15);
printf("My favorite letter is: %c", 'D');
However, it is more sustainable to use variables as they are saved for later and can be re-used whenever.

C Variable Values
Change Variable Values
If you assign a new value to an existing variable, it will overwrite the previous value:
Example
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
You can also assign the value of one variable to another:
Example
int myNum = 15;

int myOtherNum = 23;

// Assign the value of myOtherNum (23) to myNum


myNum = myOtherNum;

// myNum is now 23, instead of 15


printf("%d", myNum);
Or copy values to empty variables:
Example
// Create a variable and assign the value 15 to it
int myNum = 15;

// Declare a variable without assigning it a value


int myOtherNum;

// Assign the value of myNum to myOtherNum


myOtherNum = myNum;

// myOtherNum now has 15 as a value


printf("%d", myOtherNum);
Add Variables Together
To add a variable to another variable, you can use the + operator:
Example
int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);

C Declare Multiple Variables


Declare Multiple Variables
To declare more than one variable of the same type, use a comma-separated list:
Example
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
You can also assign the same value to multiple variables of the same type:
Example
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);

C Variable Names (Identifiers)


All C variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and maintainable code:
Example
// Good variable name
int minutesPerHour = 60;

// OK, but not


The general rules for naming variables are:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case-sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words (such as int) cannot be used as names
Real-Life Example
Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types,
myChar for char types, and so on). This is done to avoid confusion.
However, for a practical example of using variables, we have created a program that stores different data
about a college student:
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';

// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);

Data Types

As explained in the Variables chapter, a variable in C must be a specified data type, and you must use
a format specifier inside the printf() function to display it

Example
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);

Basic Data Types


The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:
Data Size Description Example
Type
int 2 or 4 Stores whole numbers, without decimals 1
bytes
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for 1.99
storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for 1.99
storing 15 decimal digits
char 1 byte Stores a single character/letter/number, or ASCII values 'A'
Basic Format Specifiers
There are different format specifiers for each data type. Here are some of them:
Format Specifier Data Type Try it
%d or %i int Try it »
%f or %F float Try it »
%lf double Try it »
%c char Try it »
%s Used for strings (text), which you will learn more about in a later Try it »
chapter
Note: It is important that you use the correct format specifier for the specified data type. If not, the program
may produce errors or even crash.

The char Type


The char data type is used to store a single character.
The character must be surrounded by single quotes, like 'A' or 'c', and we use the %c format specifier to print
it:
Example
char myGrade = 'A';
printf("%c", myGrade);
Try it Yourself »
Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters. Note that
these values are not surrounded by quotes (''), as they are numbers:
Example
char a = 65, b = 66, c = 67;
printf("%c", a);
printf("%c", b);
printf("%c", c);
Try it Yourself »
Tip: A list of all ASCII values can be found in our ASCII Table Reference.

Notes on Characters
If you try to store more than a single character, it will only print the last character:
Example
char myText = 'Hello';
printf("%c", myText);
Try it Yourself »
Note: Don't use the char type for storing multiple characters, as it may produce errors.
To store multiple characters (or whole words), use strings (which you will learn more about in a later
chapter):
Example
char myText[] = "Hello";
printf("%s", myText);
Try it Yourself »
For now, just know that we use strings for storing multiple characters/text, and the char type for single
characters.
C Numeric Data Types

Numeric Types
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when
you need a floating point number (with decimals), like 9.99 or 3.14515.
int
int myNum = 1000;
printf("%d", myNum);
float
float myNum = 5.75;
printf("%f", myNum);
double
double myNum = 19.99;
printf("%lf", myNum);
float vs. double
The precision of a floating point value indicates how many digits the value can have after the decimal point.
The precision of float is six or seven decimal digits, while double variables have a precision of about 15 digits.
Therefore, it is often safer to use double for most calculations - but note that it takes up twice as
much memory as float (8 bytes vs. 4 bytes).

Scientific Numbers
In C, you can write very large or very small floating-point numbers using scientific notation.
This is done using the letter e (or E), which stands for "times 10 to the power of".
For example, 35e3 means 35 × 10³ = 35000.
This is useful for writing numbers in a shorter way. Especially when working with scientific values or large-
scale data.
Example
float f1 = 35e3; // 35 * 10^3 = 35000
double d1 = 12E4; // 12 * 10^4 = 120000

printf("%f\n", f1);
printf("%lf", d1);

Set Decimal Precision


You have probably already noticed that if you print a floating point number, the output will show many digits
after the decimal point:
Example
float myFloatNum = 3.5;
double myDoubleNum = 19.99;

printf("%f\n", myFloatNum); // Outputs 3.500000


printf("%lf", myDoubleNum); // Outputs 19.990000
If you want to remove the extra zeros (set decimal precision), you can use a dot (.) followed by a number that
specifies how many digits that should be shown after the decimal point:
Example
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits

C The sizeof Operator


Note that we use the %zu format specifier to print the result, instead of %d. This is because the compiler
expects the sizeof operator to return a value of type size_t, which is an unsigned integer type. On some
computers it might work with %d, but it is safer and more portable to use %zu, which is specifically designed
for printing size_t values.
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;

printf("%zu\n", sizeof(myInt));
printf("%zu\n", sizeof(myFloat));
printf("%zu\n", sizeof(myDouble));
printf("%zu\n", sizeof(myChar));

return 0;
}

Why Should I Know the Size of Data Types?


Knowing the size of data types helps you understand how much memory your program uses. This is
important when writing larger programs or working with limited memory, because it can affect both
performance and efficiency.
For example, the size of a char type is 1 byte. Which means if you have an array of 1000 char values, it will
occupy 1000 bytes (1 KB) of memory.
Using the right data type for the right purpose will save memory and improve the performance of your
program.
int main() {
// Create variables of different data types
int items = 50;
float cost_per_item = 9.99;
float total_cost = items * cost_per_item;
char currency = '$';

// Print variables
printf("Number of items: %d\n", items);
printf("Cost per item: %.2f %c\n", cost_per_item, currency);
printf("Total cost = %.2f %c\n", total_cost, currency);

return 0;
}
Type Conversion
Sometimes, you have to convert the value of one data type to another type. This is known as type
conversion.
For example, if you try to divide two integers, 5 by 2, you would expect the result to be 2.5. But since we are
working with integers (and not floating-point values), the following example will just output 2:
Example
int x = 5;
int y = 2;
int sum = 5 / 2;

printf("%d", sum); // Outputs 2


To get the right result, you need to know how type conversion works.
There are two types of conversion in C:
 Implicit Conversion (automatically)
 Explicit Conversion (manually)

Implicit Conversion
Implicit conversion is done automatically by the compiler when you assign a value of one type to another.
For example, if you assign an int value to a float type:
Example
// Automatic conversion: int to float
float myFloat = 9;

printf("%f", myFloat); // 9.000000


As you can see, the compiler automatically converts the int value 9 to a float value of 9.000000.
This can be risky, as you might lose control over specific values in certain situations.
Especially if it was the other way around - the following example automatically converts the float
value 9.99 to an int value of 9:
Example
// Automatic conversion: float to int
int myInt = 9.99;

printf("%d", myInt); // 9
What happened to .99? We might want that data in our program! So be careful. It is important that you
know how the compiler work in these situations, to avoid unexpected results.
As another example, if you divide two integers: 5 by 2, you know that the sum is 2.5. And as you know from
the beginning of this page, if you store the sum as an integer, the result will only display the number 2.
Therefore, it would be better to store the sum as a float or a double, right?
Example
float sum = 5 / 2;

printf("%f", sum); // 2.000000


Why is the result 2.00000 and not 2.5? Well, it is because 5 and 2 are still integers in the division. In this case,
you need to manually convert the integer values to floating-point values. (see below).
Explicit Conversion
Explicit conversion is done manually by placing the type in parentheses () in front of the value.
Considering our problem from the example above, we can now get the right result:
Example
// Manual conversion: int to float
float sum = (float) 5 / 2;

printf("%f", sum); // 2.500000

You can also place the type in front of a variable:


Example
int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;
printf("%f", sum); // 2.500000

And since you learned about "decimal precision" in the previous chapter, you could make the output even
cleaner by removing the extra zeros (if you like):
Example
int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;

printf("%.1f", sum); // 2.5

Real-Life Example
Here's a real-life example of data types and type conversion where we create a program to calculate the
percentage of a user's score in relation to the maximum score in a game:
Example
// Set the maximum possible score in the game to 500
int maxScore = 500;

// The actual score of the user


int userScore = 423;

/* Calculate the percantage of the user's score in relation to the maximum available score.
Convert userScore to float to make sure that the division is accurate */
float percentage = (float) userScore / maxScore * 100.0;

// Print the percentage


printf("User's percentage is %.2f", percentage);

C Constants
Constants
If you don't want others (or yourself) to change existing variable values, you can use the const keyword.
This will declare the variable as "constant", which means unchangeable and read-only:
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
You should always declare the variable as constant when you have values that are unlikely to change:
const int minutesPerHour = 60;
example
int main() {
const int BIRTHYEAR = 1980;

printf("%d", BIRTHYEAR);
return 0;
}

C Operators
Operators are used to perform operations on variables and values.
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
int main() {
int x = 10;
int y = 3;

printf("%d\n", x + y); // 13
printf("%d\n", x - y); // 7
printf("%d\n", x * y); // 30
printf("%d\n", x / y); // 3
printf("%d\n", x % y); // 1

int z = 5;
++z;
printf("%d\n", z); // 6
--z;
printf("%d\n", z); // 5

return 0;
}
Assignment operators can also be used in real-life scenarios. For example, you can use the += operator to
keep track of savings when you add money to an account:
int main() {
// Create an integer variable that will store the number we get from the user
int myNum;

// Ask the user to type a number


printf("Type a number and press enter: \n");

// Get and save the number the user types


scanf("%d", &myNum);

// Print the number the user typed


printf("Your number is: %d", myNum);
return 0;
}
int main() {
// Create an int and a char variable
int myNum;
char myChar;

// Ask the user to type a number AND a character


printf("Type a number AND a character and press enter: \n");

// Get and save the number AND character the user types
scanf("%d %c", &myNum, &myChar);

// Print the number


printf("Your number is: %d\n", myNum);

// Print the character


printf("Your character is: %c\n", myChar);

return 0;
}
#include <stdio.h>

int main() {
// Create a string
char firstName[30];

// Ask the user to input some text (name)


printf("Enter your first name and press enter: \n");

// Get and save the text


scanf("%s", firstName);

// Output the text


printf("Hello %s", firstName);

return 0;
}
#include <stdio.h>

int main() {
int length, width;
int area, perimeter;

// Prompt user for input


printf("Enter the length of the rectangle: ");
scanf("%d", &length);

printf("Enter the width of the rectangle: ");


scanf("%d", &width);

// Calculate area and perimeter


area = length * width;
perimeter = 2 * (length + width);

// Display the results


printf("The area of the rectangle is: %d\n", area);
printf("The perimeter of the rectangle is: %d\n", perimeter);

return 0;
}

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
#include <stdio.h>
int main() {

// Expression for getting sum


int sum = 10 + 20;

printf("%d", sum);
return 0;
}

Output
30

Unary, Binary and Ternary Operators


On the basis of the number of operands they work on, operators can be classified into three types :
1. Unary Operators: Operators that work on single operand.
Example: Increment( ++) , Decrement(--)
2. Binary Operators: Operators that work on two operands.
Example: Addition (+), Subtraction( -) , Multiplication (*)
3. Ternary Operators: Operators that work on three operands.
Example: Conditional Operator( ? : )

Types of Operators in C
C language provides a wide range of built in operators that can be classified into 6 types based on their
functionality:

Arithmetic Operators
The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There
are nine arithmetic operators in C language:
C
#include <stdio.h>

int main() {

int a = 25, b = 5;

// using operators and printing results


printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);

return 0;
}

Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 25
a-- = 26

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.
C
#include <stdio.h>
int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);

return 0;
}

Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Here, 0 means false and 1 means true.

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.
Symbol Operator Description Syntax

&& Logical AND Returns true if both the operands are true. a && b

|| Logical OR Returns true if both or any o f the operand is true. a || b

! Logical NOT Returns true if the operand is false. !a

C
#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);

return 0;
}

Output
a && b : 1
a || b : 1
!a: 0

Bitwise Operators
The Bitwise operators are used to perform bit-level operations on the operands. The operators are first
converted to bit-level and then the calculation is performed on the operands.
Note: Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit
level for faster processing.
Symbol Operator Description Syntax

Performs bit-by-bit AND operation and


& Bitwise AND a&b
returns the result.

Performs bit-by-bit OR operation and


| Bitwise OR a|b
returns the result.

Performs bit-by-bit XOR operation and


^ Bitwise XOR a^b
returns the result.

Flips all the set and unset bits on the


~ Bitwise First Complement ~a
number.

Shifts bits to the left by a given number


<< Bitwise Leftshift of positions; multiplies the number by 2 a << b
for each shift.

Shifts bits to the right by a given number


>> Bitwise Rightshilft of positions; divides the number by 2 for a >> b
each shift.

C
#include <stdio.h>

int main() {
int a = 25, b = 5;
// using operators and printing results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);

return 0;
}

Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800

Assignment Operators
Assignment operators are used to assign value to a variable. The left side operand of the assignment
operator is a variable and the right side operand of the assignment operator is a value. The value on the right
side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide multiple operations
using single operator. These operators are called compound operators.

Symbol Operator Description Syntax

Assign the value of the right operand to the left


= Simple Assignment a=b
operand.

Add the right operand and left operand and


+= Plus and assign a += b
assign this value to the left operand.

Subtract the right operand and left operand and


-= Minus and assign a -= b
assign this value to the left operand.

Multiply the right operand and left operand and


*= Multiply and assign a *= b
assign this value to the left operand.

Divide the left operand with the right operand


/= Divide and assign a /= b
and assign this value to the left operand.

%= Modulus and assign Assign the remainder in the division of left a %= b


Symbol Operator Description Syntax

operand with the right operand to the left


operand.

Performs bitwise AND and assigns this value to


&= AND and assign a &= b
the left operand.

Performs bitwise OR and assigns this value to


|= OR and assign a |= b
the left operand.

Performs bitwise XOR and assigns this value to


^= XOR and assign a ^= b
the left operand.

Performs bitwise Rightshift and assign this value


>>= Rightshift and assign a >>= b
to the left operand.

Performs bitwise Leftshift and assign this value


<<= Leftshift and assign a <<= b
to the left operand.

C
#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n", a |= b);
printf("a ^= b: %d\n", a ^= b);
printf("a >>= b: %d\n", a >>= b);
printf("a <<= b: %d\n", a <<= b);

return 0;
}

Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a ^= b: 0
a >>= b: 0
a <<= b: 0

Other Operators
Apart from the above operators, there are some other operators available in C used to perform some specific
tasks. Some of them are discussed here:

sizeof Operator
 sizeof is much used in the C programming language.
 It is a compile-time unary operator which can be used to compute the size of its operand.
 The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
 Basically, the sizeof the operator is used to compute the size of the variable or datatype.
Syntax
C
sizeof (operand)

Comma Operator ( , )
The comma operator (represented by the token) is a binary operator that evaluates its first operand and
discards the result, it then evaluates the second operand and returns this value (and type).
The comma operator has the lowest precedence of any C operator. It can act as both operator and
separator.
Syntax
C
operand1 , operand2

Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C++. It is a conditional operator that we can use in
place of if..else statements.
Syntax
C
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute
and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and
return the result of Expression3.
dot (.) and arrow (->) Operators
Member operators are used to reference individual members of classes, structures, and unions.
 The dot operator is applied to the actual object.
 The arrow operator is used with a pointer to an object.
Syntax
C
structure_variable . member;
structure_pointer -> member;

Cast Operators
Casting operators convert one data type to another. For example, int(2.2000) would return 2.
 A cast is a special operator that forces one data type to be converted into another.
Syntax
C
(new_type) operand;
addressof (&) and Dereference (*) Operators
Addressof operator & returns the address of a variable and the dereference operator * is a pointer to a
variable. For example *var; will pointer to a variable var.
Example of Other C Operators
C
// C Program to demonstrate the use of Misc operators
#include <stdio.h>

int main()
{
// integer variable
int num = 10;
int* add_of_num = &num;

printf("sizeof(num) = %d bytes\n", sizeof(num));


printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);

return 0;
}

Output
sizeof(num) = 4 bytes
&num = 0x7ffdb58c037c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
It is strongly recommended to learn Operator Precedence and Associativity after reading about operators.

int main() {
int x = 5;
++x; // Increment x by 1
printf("%d\n", x); // 6
--x; // Decrement x by 1
printf("%d\n", x); // 5
return 0;
}
In C programming, storage classes define the scope, lifetime, and storage location of variables and
functions. There are four primary storage classes:
auto
 Scope: Local to the block or function in which it is declared.
 Lifetime: Exists only while the block or function is active.
 Storage Location: Stored in the stack memory.
 Key Feature: This is the default storage class for local variables. They are temporary and reinitialized each
time their scope is entered.
register
 Scope: Local to the block or function in which it is declared.
 Lifetime: Exists only while the block or function is active.
 Storage Location: Stored in CPU registers for faster access, if available. Otherwise, stored in RAM.
 Key Feature: Used for variables that are frequently accessed, like loop counters, to potentially improve
performance. The & operator cannot be applied to register variables as they might not have a memory
address.
static
 Scope:
 Local static: Local to the function or block where it's declared.
 Global static: Limited to the file where it's declared.
 Lifetime: Persists throughout the entire program execution.
 Storage Location: Stored in the data segment of RAM.
 Key Feature:
 Local static: Retains its value across multiple function calls.
 Global static: Prevents the variable from being accessed by other files, ensuring file-level encapsulation.
extern
 Scope: Global, accessible throughout the entire program (potentially across multiple files).
 Lifetime: Persists throughout the entire program execution.
 Storage Location: Stored in the data segment of RAM.
 Key Feature: Used to declare a variable or function that is defined in another file or later in the current
file. It informs the compiler that the definition exists elsewhere, enabling sharing of global variables and
functions.
Example Program Illustrating Storage Classes:
C
#include <stdio.h>

// Global static variable (file-scope)


static int globalStaticVar = 10;

// Global variable (externally linkable)


int globalVar = 20;

void func() {
// Local auto variable
auto int autoVar = 1;

// Local static variable (retains value across calls)


static int staticFuncVar = 5;
// Register variable (suggestion for CPU register storage)
register int regVar = 3;

printf("Inside func():\n");
printf(" autoVar: %d\n", autoVar);
printf(" staticFuncVar (before increment): %d\n", staticFuncVar);
printf(" regVar: %d\n", regVar);

autoVar++;
staticFuncVar++;

printf(" staticFuncVar (after increment): %d\n", staticFuncVar);


printf("\n");
}

int main() {
extern int globalVar; // Declaration of external globalVar

printf("In main():\n");
printf(" globalStaticVar: %d\n", globalStaticVar);
printf(" globalVar: %d\n", globalVar);
printf("\n");

func(); // First call


func(); // Second call, staticFuncVar retains its value

return 0;
}

In C programming, functions can be categorized based on whether they accept arguments and whether they
return a value.

1. Functions with Arguments and with Return Values


These functions take one or more input values (arguments) and compute a result, which they then return to
the calling function.
C
#include <stdio.h>

// Function declaration
int add(int a, int b);
int main() {
int num1 = 5, num2 = 10;
int sum = add(num1, num2); // Function call with arguments, storing return value
printf("Sum: %d\n", sum);
return 0;
}

// Function definition
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}

2. Functions with Arguments and without Return Values


These functions accept input values (arguments) to perform a specific task but do not return any value to the
calling function. Their return type is void.
C
#include <stdio.h>

// Function declaration
void printMessage(char message[]);

int main() {
char greeting[] = "Hello from a function!";
printMessage(greeting); // Function call with argument
return 0;
}

// Function definition
void printMessage(char message[]) {
printf("%s\n", message); // Prints the message, no value returned
}

3. Functions without Arguments and with Return Values


These functions do not take any input values (arguments) but perform a task and return a computed result to
the calling function.
C
#include <stdio.h>

// Function declaration
int generateRandomNumber();

int main() {
int randomNum = generateRandomNumber(); // Function call, storing return value
printf("Random Number: %d\n", randomNum);
return 0;
}

// Function definition
int generateRandomNumber() {
// In a real scenario, you'd use a proper random number generator
return 42; // Returns a fixed value for demonstration
}
C Storage Classes
Storage classes define the lifetime, visibility, and memory location of variables.
There are four main storage class specifiers in C:
 auto
 static
 register
 extern
Difference Between Scope and Storage Classes
Scope defines where a variable can be used, and storage classes define how long it lasts and where it's
stored. This chapter continues from the C Scope chapter.

auto
The auto keyword is used for local variables. It is default for variables declared inside functions, so it's rarely
used explicitly.
Example
int main() {
auto int x = 50; // Same as just: int x = 50;
printf("%d\n", x);
return 0;
}

static
The static keyword changes how a variable or function behaves in terms of lifetime and visibility:
 Static local variables keep their value between function calls.
 Static global variables/functions are not visible outside their file.
Example
void count() {
static int myNum = 0; // Keeps its value between calls
myNum++;
printf("num = %d\n", myNum);
}

int main() {
count();
count();
count();
return 0;
}
Result:
num = 1
num = 2
num = 3
Try to remove the static keyword from the example to see the difference.

register
The register keyword suggests that the variable should be stored in a CPU register (for faster access).
You cannot take the address of a register variable using &.
Note: The register keyword is mostly obsolete - modern compilers automatically choose the best variables to
keep in registers, so you usually don't need to use it.
Example

int main() {
register int counter = 0;
printf("Counter: %d\n", counter);
return 0;
}

extern

The extern keyword tells the compiler that a variable or function is defined in another file.
It is commonly used when working with multiple source files.
File 1: main.c

#include <stdio.h>

extern int shared; // Declared here, defined in another file

int main() {
printf("shared = %d\n", shared);
return 0;
}

File 2: data.c

int shared = 50; // Definition of the variable

Compile both files together:

gcc main.c data.c -o program

You might also like