CP Notes
CP Notes
CP Notes
1.Introduction
2.Features:-
3.The C Character Set :-
4.Identifier in C :- Identifiers are unique names that are assigned to variables, structs, functions, and other
entities.
Rules to Name an Identifier in C
A programmer has to follow certain rules while naming variables. For the valid identifier, we must follow the given below
set of rules.
5.Variable in C Language :-
“The name associated with some memory location to store data of different types.”
“Variables are containers for storing data values, like numbers and characters.”
Syntax :-
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple variable
Rules for constructing variable name:-
1. A variable name must only contain alphabets, digits, and underscore.
2. A variable name must start with an alphabet or an underscore only. It cannot 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.
Example :-
// Create integer variables
int length = 4;
int width = 6;
int area;
// Calculate the area of a rectangle
area = length * width;
// Print the variables
printf("Length is: %d\n", length);
printf("Width is: %d\n", width);
printf("Area of the rectangle is: %d", area);
Output:-
Length is: 4
Width is: 6
Area of the rectangle is: 24
6.Data Types :-
“The data type specifies the size and type of information the variable will store.”
The data types in C can be classified as follows:
Types Description
Primitive Data Types Basic data types such as integers, float, characters, etc.
Derived Types Derived from the primitive or built-in datatypes are referred to as Derived
Data Types.
Escape Sequence in C :-
7.Constants in C :-
The constants are the read-only variables whose values cannot be modified once they are declared in the C program.
7.1 define constant in C
There are two ways to define constant in C programming.
1. const keyword
2. #define preprocessor
8.Keywords in c :-
Keywords are predefined or reserved words that have special meanings to the compiler.
9.Operators in C :-
An operator is defined as the symbol that helps us to perform some specific mathematical, relational, bitwise,
conditional, or logical computations on values and variables.
9.1 Arithmetic Operators
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
> Greater than x>y Returns 1 if the first value is greater than the second value
< Less than x<y Returns 1 if the first value is less than the second value
>= Greater than or equal to x >= y Returns 1 if the first value is greater than, or equal to, the second
value
<= Less than or equal to x <= y Returns 1 if the first value is less than, or equal to, the second value
&& Logical and x < 5 && x < 10 Returns 1 if both statements are true
|| Logical or x < 5 || x < 4 Returns 1 if one of the statements is true
! Logical not !(x < 5 && x < 10) Reverse the result, returns 0 if the result is 1
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
Operators Precedence in C :-
Operator precedence determines the order in which operators are evaluated when an expression contains
multiple operators.
Operator precedence in which operators will be given priority when there are multiple operators in the
expression. It is very common to have multiple operators in C language and the compiler first evaluates the
operater with higher precedence.
Associativity of Operators
The associativity of operators determines the direction in which an expression is evaluated.
int x = 5 - 17* 6;
10.1 Formatted I/O Functions :- Formatted I/O functions are used to take various inputs from
the user and display multiple outputs to the user. These types of I/O functions can help to display the
output to the user in different formats using the format specifiers.
1. printf():
printf() function is used in a C program to display any value like float, integer, character,
string, etc on the console screen. It is a pre-defined function that is already declared in the
stdio.h(header file).
Syntax: printf(“Format Specifier”, var1, var2, …., varn);
Example:
// C program to implement printf() function
#include <stdio.h>
int main()
{
int a; // Declaring an int type variable
a = 20; // Assigning a value in a variable
printf("%d", a); // Printing the value of a variable
return 0;
}
Output :-
20
2. scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard
by the user, these values can be of any data type like integer, float, character, string, and
many more. In scanf() function use &(address-of operator) which is used to store the
variable value on the memory location of that variable.
Example:
// C program to implement scanf() function
#include <stdio.h>
int main()
int num1;
return 0;
Output
Enter a integer number: You have entered 0
Unformatted Input/Output functions:- Unformatted I/O functions are used only for
character data type or character array/string and cannot be used for any other datatype. These
functions are used to read single input from the user at the console and it allows to display the value
at the console.
These functions are called unformatted I/O functions because we cannot use format specifiers in these
functions and hence, cannot format these functions according to needs.
1. getchar()
2. putchar()
3. gets()
4. puts()
1.getchar()
The getchar() function is used to read only a first single character from the keyboard whether multiple
characters is typed by the user and this function reads one character at one time until and unless the
enter key is pressed. This function is declared in stdio.h(header file)
Example:
#include <stdio.h>
int main()
char ch;
ch = getchar();
printf("%c", ch);
return 0;
Output:
2.putchar():
The putchar() function is used to display a single character at a time by passing that character directly
to it or by passing a variable that has already stored a character. This function is declared in
stdio.h(header file)
Syntax: putchar(variable_name);
Example:
#include <conio.h>
#include <stdio.h>
int main()
char ch;
// Reads a character
ch = getchar();
putchar(ch);
return 0;
Output:
Enter any character: Z
3.gets():
gets() function reads a group of characters or strings from the keyboard by the user and these
characters get stored in a character array.
Syntax: char str[length of string in number]; //Declare a char type variable of any length
gets(str);
Example:
#include <conio.h>
#include <stdio.h>
int main()
char name[50];
return 0;
}
Output:
Please enter some texts: geeks for geeks
4.puts():
In C programming puts() function is used to display a group of characters or strings which is already
stored in a character array.
Syntax: puts(identifier_name );
Example:
#include <stdio.h>
int main()
char name[50];
Output:
Enter your text: GeeksforGeeks
Documentation section :- The documentation section consists of a set of comment lines giving the
name of the program, the author and other details, which the programmer would like to use later.
Link section :- The link section provides instructions to the compiler to link functions from the system
library such as using the #include directive.
Definition section :- The definition section defines all symbolic constants such using the #define
directive.
Global declaration section:- There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global declaration section that is outside of
all the functions. This section also declares all the user-defined functions.
main () function section:-Every C program must have one main function section. This section
contains two parts; declaration part and executable part
Declaration part:-The declaration part declares all the variables used in the executable part.
Executable part:- There is at least one statement in the executable part. These two parts must
appear between the opening and closing braces. The program execution begins at the opening brace
and ends at the closing brace. The closing brace of the main function is the logical end of the program.
All statements in the declaration and executable part end with a semicolon.
Subprogram section:- If the program is a multi-function program then the subprogram section
contains all the user-defined functions that are called in the main () function. User-defined functions
are generally placed immediately after the main () function, although they may appear in any order.
All section, except the main () function section may be absent when they are not required.
○
2. Blocks:
○ A block is a construct that groups multiple statements together.
○ It begins with an opening curly brace { and ends with a closing curly brace }.
○ Inside a block, you can have a series of statements and declarations.
○ Another term for blocks is compound statements.
○ Here are some key points about blocks:
■ A block can contain any kind of statement.
■ Although it doesn’t look like a normal statement (since it lacks a semicolon), you
can use it as a statement wherever required.
■ A function body is also a block, but it is not considered a regular statement.
■ Nested blocks (internal blocks) can be placed inside other blocks.
1. If Statement:
● It is used to decide whether a certain statement or block of statements will be executed or not i.e if a
certain condition is true then a block of statements is executed otherwise not.
Syntax:-
if(condition)
// Statements to execute if
// condition is true
}
Flowchart of if Statement :-
Example of if in C
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
2. if...else Statement:
The if...else statement is used for decision-making. It allows you to execute different
code blocks based on a condition.
Syntax:-
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
Output
i is greater than 15
3. Nested if-else in C
Nested if statements mean an if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Else
{
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
4. if-else-if Ladder 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.
if (condition1)
statement1;
else
{
if (condition2)
Statement2;
else
{
if(condition 3)
Statement 3;
else
Statement 4;
}
}
Output
i is 20
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;
}
Note: The switch expression should evaluate to either integer or character. It cannot evaluate any other data type.
Flowchart of switch
Example of switch Statement
Output
Case 2 is executed
7. 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;
Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop or
we want to terminate the loop based on some condition.
Example of break
#include <stdio.h>
int main() {
printf("break in for loop\n");
for (int i = 1; i < 5; i++) {
if (i == 3) {
break;
} else {
printf("%d ", i);
}
}
return 0;
}
Output:
break in for loop
1 2
break in while loop
1 2
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;
Flowchart of Continue
Example of continue
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 7 8 9 10
C) goto
The goto statement in C is a jump statement that allows you to transfer control from one part of your code to another. It’s sometimes
referred to as an unconditional jump statement because it can be used to jump from anywhere to anywhere within a function.
Syntax of goto
Syntax1 | Syntax2
----------------------------
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a label. Here, a label is a
user-defined identifier that indicates the target statement. The statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’ statement in the above syntax.
Examples of goto
// C program to check if a number is
// even or not using goto statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}
int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
Output:
26 is even
D) return
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.
Flowchart of return
Syntax of return
return [expression];
Example of return
For Loop:-
The for loop allows you to repeatedly execute a block of code a specified number of
times.
Syntax:
for (initialization; condition; increment/decrement)
{
// Code to execute in each iteration
}
Flowchart :-
Example:-
while Loop:
The while loop repeatedly executes a block of code as long as a condition remains true.
Syntax:
while (condition)
{
// Code to execute while the condition is true
}
Example:
#include <stdio.h>
int main()
{
int i = 0;
while (i < 5)
{
printf("GeeksforGeeks\n");
i++;
}
return 0;
}
Output:-
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Do-while Loop:
Syntax:-
do {
// Body of the do...while loop
} while (condition);
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Geeks\n");
i++;
} while (i < 3);
return 0;
}
Output :-
Geeks
Geeks
Geeks