C language notes
C language notes
By: RT Rockstar
What is c?
Fast Speed
Clean Syntax
These features make the C language suitable for system programming like an
operating system or compiler development.
Difference Between C and C++
C++ was created to add the OOPs concept into the C language so they both have
very similar syntax with a few differences. The following are some of the main
differences between C and C++ Programming languages.
C++ supports OOPs paradigm while C only has the procedural concept of
programming.
1. Header Files
4. Statement
5. Return Statement
Uses of c language
Operating systems: C is widely used for developing operating systems such as
Unix, Linux, and Windows.
variable is a name given to the memory location that helps us to store some
form of data and retrieves it when required. It allows us to refer to memory
location without having to memorize the memory address. A variable name can
be used in expressions as a substitute in place of the value it stores.
Syntax
data_type variable_name;
#include <stdio.h>
int main() {
// Declaring variables
int a;
// Storing data
a = 25;
printf("%d", a);
return 0;
}
Data Types in C
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. Each data type
requires different amounts of memory and has some specific operations which
can be performed over it.
Primitive Data Types
Primitive data types are the most basic data types that are used for representing
simple values such as integers, float, characters, etc.
The data types that are derived from the primitive or built-in datatypes are
referred to as Derived Data Types.
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 be stored in int data type in C.
Size: 4 bytes
Format Specifier: %d
Float datatypes
Float data type is used to store floating-point values. Float in C is used to store
decimal and exponential values. It is used to store decimal numbers (numbers
with floating point values) with single precision.
● Range: 1.2E-38 to 3.4E+38
● Size: 4 bytes
● Format Specifier: %f
Char datatypes
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
Double Data Type
Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers
with decimal values in C.
The double data type is basically a precision sort of data type that is capable of holding
64 bits of decimal numbers or floating points. Since double has more precision as
compared to that float then it is much more obvious that it occupies twice the memory
occupied by the floating-point type. 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 specify that no value is present. It does not
provide a result value to its caller. It has no values and no operations. It is
used to represent nothing. Void is used in multiple ways as function return
type, function arguments as void, and pointers to void.
Void data type example
#include <stdio.h>
int main(){
printf("%d", *(int*)ptr);
return 0;
}
Type Conversion in C
type conversion refers to the process of converting one data type to another. It
can be done automatically by the compiler or manually by the programmer. The
type conversion is only performed to those data types where conversion is
possible.
Implicit Type Conversion
Implicit type conversion, also known as type coercion, occurs when the C
compiler automatically converts one data type to another without the need for
explicit instructions from the programmer. This typically happens when a smaller
data type is assigned to a larger data type or when different data types are
involved in an arithmetic operation.
Explicit Type Conversion
Syntax:
(type) expression
Header Files in c
A header file is a source file that has the .h extension. Header files contain the
function prototypes or function declaration, whereas the source code contains
the constants, macros, system-wide global variables. Whenever we require the
definition of a function, then we simply include that header file in which
function is declared.
○ #include<file>
○ #include "file"
There is a difference between the header files given above. If the header file is
defined within the predefined source path, we can specify the header within
the angular brackets. If the header file is not defined within the predefined
source path then we can specify the full path of the header file within the
double-quotes.
Multiply.h
}
Header Files List
char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a' array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run time.
● Arithmetic Operators
● Relational Operators
● Shift Operators
● Logical Operators
● Bitwise Operators
● Conditional Operators
● Assignment Operator
● Misc Operator
Arithmetic Operator
Subtracts right
2 – Minus operand from a–b
left operand.
Used to specify
6 + Unary Plus the positive +a
values.
Increases the
8 ++ Increment value of the a++
operand by 1.
Decreases the
9 — Decrement value of the a–
operand by 1.
Relational Operator
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.
S. No. Symbol Operator Description Syntax
Returns true if
5 == both the
Equal to a == b
operands are
equal.
Returns true if
6 != both the
Not equal to a != b
operands are
NOT equal.
Logical Operator
Returns true if
1 && both the a && b
Logical AND
operands are
true.
Returns true if
2 || both or any of a || b
Logical OR
the operand is
true.
Returns true if
3 ! Logical NOT the operand is !a
false.
Bitwise Operator
Performs
bit-by-bit AND
1 & Bitwise AND operation and a&b
returns the
result.
Performs
bit-by-bit OR
2 | Bitwise OR operation and a|b
returns the
result.
Performs
bit-by-bit XOR
3 ^ Bitwise XOR operation and a^b
returns the
result.
Flips all the set
4 ~ Bitwise First
and unset bits ~a
Complement
on the number.
Shifts the
number in
binary form by
5 << Bitwise
one place in the a << b
Leftshift
operation and
returns the
result.
Shifts the
number in
binary form by
6 >> Bitwise
one place in the a >> b
Rightshilft
operation and
returns the
result.
Assignment Operator
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.
S. No. Symbol Operator Description Syntax
Assign the
remainder in the
6 %= Modulus and division of left a %= b
assign operand with the
right operand to
the left operand.
Performs bitwise
7 &= AND and assigns a &= b
AND and assign
this value to the
left operand.
Performs bitwise
8 |= OR and assigns a |= b
OR and assign
this value to the
left operand.
Performs bitwise
9 ^= XOR and assigns a ^= b
XOR and assign
this value to the
left operand.
Performs bitwise
Rightshift and
10 >>= Rightshift and a >>= b
assign this value
assign
to the left
operand.
Performs bitwise
Leftshift and
11 <<= Leftshift and a <<= b
assign this value
assign
to the left
operand.
Conditional or Ternary Operator (?:) in C
Syntax:
#include <stdio.h>
int main(){
int m = 5, n = 4;
return 0;
}
Constants in C
A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
● Some special characters are used in C, and they have a special meaning
which cannot be used for another purpose.
● Square brackets [ ]: The opening and closing brackets represent the single
and multidimensional subscripts.
● Simple brackets ( ): It is used in function declaration and function calling.
For example, printf() is a pre-defined function.
● Curly braces { }: It is used in the opening and closing of the code. It is used
in the opening and closing of the loops.
● Comma (,): It is used for separating for more than one statement and for
example, separating function parameters in a function call, separating the
variable when printing the value of more than one variable using a single
printf statement.
● Hash/pre-processor (#): It is used for pre-processor directive. It basically
denotes that we are using the header file.
● Asterisk (*): This symbol is used to represent pointers and also used as an
operator for multiplication.
● Tilde (~): It is used as a destructor to free memory.
● Period (.): It is used to access a member of a structure or a union.
Control Statements in C
Decision Making in C (if , if..else, Nested if, if-else-if ):
There come situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next. Similar situations
arise in programming also where we need to make some decisions and based on
these decisions we will execute the next block of code. For example, in C if x
occurs then execute y else execute z. There can also be multiple conditions like
in C if x occurs then execute p, else if condition y occurs execute q, else execute
r. This condition of C else-if is one of the many ways of importing multiple
conditions.
Types of Conditional Statements in C
Following are the decision-making statements available in C:
● if Statement
● if-else Statement
● Nested if Statement
● if-else-if Ladder
● switch Statement
● Conditional Operator
● Jump Statements:
● break
● continue
● goto
● return
1. if in C
if(condition)
// Statements to execute if
// condition is true
}
2. if-else in C
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do
something else when the condition is false? Here comes the C else statement.
We can use the else statement with the if statement to execute a block of code
when the condition is false. The if-else statement consists of two blocks, one for
false expression and one for true expression.
Syntax of if else in C
if (condition){
// condition is true
else{
// condition is false
}
3. Nested if-else in C
The if else if statements are used when the user has to decide among multiple
options. The C if statements are executed from the top down. As soon as one of
the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the C else-if ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed. if-else-if ladder
is similar to the switch statement.
Syntax of if-else-if Ladder
if (condition)
statement;
else if (condition)
Statement;
else
statement;
5. switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be
used to execute the conditional code based on the value of the variable specified
in the switch statement. The switch block consists of cases to be executed
based on the value of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
default:
statements;
Jump Statements in C
These statements are used in C for the unconditional flow of control throughout
the functions in a program. They support four types of jump statements:
A) break
This loop control statement is used to terminate the loop. As soon as the break
statement is encountered from within a loop, the loop iterations stop there, and
control returns from the loop immediately to the first statement after the loop.
Syntax of break:
break;
B) continue
This loop control statement is just like the break statement. The continue
statement is opposite to that of the break statement, instead of terminating the
loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the loop,
the code inside the loop following the continue statement will be skipped and the
next iteration of the loop will begin.
Syntax of continue:
continue;
C) goto
The return in C returns the flow of the execution to the function from where it is
called. This statement does not mandatorily need any conditional statements.
As soon as the statement is executed, the flow of the program stops
immediately and returns the control from where it was called. The return
statement may or may not return anything for a void function, but for a non-void
function, a return value must be returned.
Syntax of return:
return [expression];