C Programming Notes - 21759696

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

ROYAL ACADEMY OF TECHNOLOGY

C Programming Notes

For More Inquiry about C, C++, DS, DB , M1, M2 Contact :7020715325/9689369507


OPERATORS
• Operators are special symbols that are used to perform certain action or operations using
operands(operand means variables).
• In C++ there are various types of Operators as given below :

1) Arithmetic Operators :
Arithmetical Operators are used to perform mathematical operations, such as
addition , subtraction, multiplication , division and modulus.
Operator Use

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
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\namespace", a/b);
printf("a%b=%d\n", a%b);
}
2) Increment /Decrement Operators :
• These operators are used to increment / decrement value of variable by one.
• In post increment/decrement , Statement get executed before
increment/decrement
• In pre increment/decrement , Statement get executed After increment/decrement.
Operator Use

Post Pre

i++ ++i Increment operand value by one

i-- --i Decrement Operand value by one

Example :
/increment decrement
#include <stdio.h>
int main()
{
int var;
printf("enter value for var :");
scanf("%d", &var); // 6
printf("var++ = %d\n", var++); //6 //post incr
printf("++var= %d\n", var--); // 7 pre inc
printf("var-- = %d\n", ++var); // 7 post dec
printf("--var= %d\n", --var); // 6 pre dnc
return 0;

Note :
//post incre/decr -> statement first and then incr/decr
//pre incr/decr -> incr/decr first and statement

3) Relational Operators :
• Relational operators are simply used to check the relationship between the two
entities .
• It compares the operands beside operator and give result by comparing it .
• Result given by these operators is always 0 (false) or 1 (true).
Operator Use

< Less than


> Greater than
<= Less than or equal
>= Greater than or equal
== Equals to
!= Not equals to

Example:
//relational operator
#include <stdio.h>
void main()
{
int a=5, b=2;
printf("%d \n", (a<b));
printf("%d \n", (a>b));
printf("%d \n", (a==b));
printf("%d \n", (a!=b));
printf("%d \n", (a<=b));
printf("%d", (a>=b));

4) Assignment Operators :
• Assignment operators are used to assign the value of one operand to another
operand.
• Just like relational operator , assignment operators are also used in almost every
program.
Operator Use

= Assign right operand value to left operand

+= Assign sum of right &left operand value to left operand

-= Assign difference of right &left operand value to left operand

*= Assign Multiplication of right &left operand value to left operand

/= Assign division of right &left operand value to left operand

%= Assign Modulus of right &left operand value to left operand

Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
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);
}
5) Logical Operators :
• Logical operators are used to compare between two test expressions (conditions).
• It compares two Boolean values and give result in true or false , but mostly it is used
to compare expressions.
&& ( Logical AND) : returns one only if both conditions are true.
|| (Logical OR) : returns one when any one of both condition is true.
! (Logical NOT) : It returns the opposite state of result , for ex. If output is true then
return false
Operator Use

&& If all conditions are true then answer is true

|| If any one condition is true then answer is true

! convert answer to opposite

Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
printf("(a>b)&&(b>a)=%d\n",(a>b)&&(b<a));
printf("(a>b)||(b>a)=%d\n",(a>b)||(b<a));
printf("!(a>b)=%d", !(a>b));
}
6) Bitwise Operators :
• These operations are performed at bit level.
• For output, they convert our input values(can be decimal values) to binary values
first and then process it with given respective operator used with it .
• At last again gives result by again converting binary values into decimal.
Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

~ Bitwise Complement

>> Shift Right Operator

<< Shift Left Operator


Truth Tables for binary operations of &, | , ^ :-

Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
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<<2 =%d\n",(a << 2));
printf("a>>2 =%d\n",(a >> 2));
}
Decision control and loop control
1.1 : Decision Making statements .
o If
o If - else
o If - else ladder
o Nested If - else
1.2 : Selection Statements.
o Switch statement
1.3 : Looping Statements .
o While
o Do-while
o For
1.4 : Break, continue, goto, Return.

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Decision Making Statements
• Decision making statements are used to decide flow of program on the basis of
condition.
• If the test expression (Condition) is true, statements inside the body of if are
executed.
• If the test expression (Condition) is false, statements inside the body of else are
executed.
1) if :
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
Flowchart :

Example:
#include<stdio.h>
void main()
{

int num ;
printf ( "Enter a number less than 10 " );
scanf ( "%d", &num );
if ( num <= 10 )
{
printf ( "Number is less than 10");

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
}
printf("Wrong Number !!! please enter number less than 10 ");

2) if -else:
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
else
{
// Execute the Statements if condition is FALSE;
}
Flowchart :

Example:
#include<stdio.h>
void main()
{

int num ;
printf ( "Enter a number =\t " );
scanf ( "%d", &num );
if ( num >0 )
{
printf ( "\t\t\t Its positive number");

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
}
else
{
printf("\t\t\t Its negative number ");
}
}

3) if -else ladder:
Syntax :
if(Condition 1)
{
// Execute the Statement 1 if condition 1 is TRUE;
}
else if(Condition 2)
{
// Execute the Statement 2 if condition 2 is TRUE;
}
else if(Condition 3)
{
// Execute the Statement 3 if condition 3 is TRUE;
}
else
{
// Execute the Statement if every condition is FALSE;
}
Flowchart :

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Example:
void main()
{
int age;
printf("Enter your age\n");
scanf("%d", &age);
if (age>=18)
{
printf("You can vote!");
}
else if(age>=10)
{
printf("You are between 10 to 18 and you can vote for
classroom election");
}

else if(age>=3)
{
printf("You are between 3 to 10 and you can vote for babies
election");
}

else{
printf("You cannot vote because your not born!");
}

4) Nested if -else :
Syntax :
if(Condition 1)
{
// Execute the Statements if condition 1 is TRUE;
if(Condition 2)
{
// Execute the Statements if condition 2 is TRUE;
if(Condition 3)
{
// Execute the Statements if condition 3 is
TRUE;
}
else

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
{
// Execute the Statement if condition 3 is
FALSE;
}
}
else
{
// Execute the Statement if condition 2 is FALSE;
}
}
else
{
// Execute the Statement if condition 1 is FALSE;
}
Flowchart :

Example:
#include<stdio.h>
void main()
{

int num;
printf("enter the number greater than 0 and less than 100 = \t");
scanf("%d \n",&num);
if(num>0)
{

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
if(num<100)
{
printf("\t\t\tnumber is in between 1 to 100 \n");
}
else
{
printf("\t\t\tnumber must be greater than 100 \n");
}

}
else
{

printf("\t\t\tnumber is zero or smaller than zero \n");


}
}

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Selection Statement
o Every time we execute the switch the value inside choice variable is
matched with case name/ case value.
o Switch executes that block of code, which matches the case value/ case-
name. If the value does not match with any of the cases, then the
default block is executed.
Switch Case:
Syntax :
switch(Choice variable)
{
case casename1: statement_1;
break;
case casename 2: statement_2;
..... break;
......
......
case casename n: statement_n;
break;
default: default statement;
}
NOTE: Even if you don’t write break it will not give you an error , because it is not
official part of syntax .

Example:

#include<stdio.h>
void main()
{
int order;
printf("\t\t\t\t\t\t\t\tWELCOME TO ROYAL HOTEL\n\n");
printf("\t\t\t\t\t\t************************ MENU
************************\t\t\t\t\t\t\n");
printf("\t\t\t\t\t\t\t\t\t 1. TEA\n");
printf("\t\t\t\t\t\t\t\t\t 2. COFFEE\n");
printf("\t\t\t\t\t\t\t\t\t 3. WATER\n");
printf("\n\n\t\t\t\t enter your order = ");
scanf("%d",&order);
switch(order)
{
case 1: printf("\t\t\t\t\t\t\t serve Tea\n");
break;
case 2: printf("\t\t\t\t\t\t\t serve Coffee\n");
break;

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
case 3: printf("\t\t\t\t\t\t\t serve Water\n");
break;

default: printf("Invalid order number");


}
}

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Looping Statement
o In a programming sometimes we need to perform some particular task
repetitively .Thus to perform some part of code again and again we use
loops in C.
o There are two types of loops in C:
▪ Entry controlled loops : Test condition is checked before
entering into loop body. Example : while and for.
▪ Exit controlled loops : Test condition is checked after evaluating
loop body. Example : do-while.
1) While loop:
Syntax :
while(Condition)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
int i = 1;
while (i<51)
{
printf("%d\n", i);
i++;
}
}
2) Do-while loop:
Syntax :
do
{
// Statement to execute if condition is TRUE;
}
while(Condition);

Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
int i = 1;
do

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
{
printf("%d\n", i);
i++;
}while (i<51);
}

3) For loop :
Syntax :
for(initialization ; Condition ; increment/ decrement)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
for(int i=1;i<51;i++)
{
printf("%d\n", i);

}
}

1.4 Branching Statements :


1) Break :
It can be used inside switch and loops. It breaks the loop flow when
encountered.
Example :
#include<stdio.h>
int main()
{
int i,age;
for(i = 0 ; i < 5 ; i++) \
{

printf("\nEnter Your Age : " );


scanf("%d",&age);
if (age>10)
{
break;
}

printf("\nage is :%d", age);


} return 0;
}
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
2) continue :
It can be used inside switch and loops. It skips the current the loop iteration
in loop flow when encountered.
Example :
#include<stdio.h>
int main()
{
int i,age;
for(i = 0 ; i < 5 ; i++) \
{

printf("\nEnter Your Age : " );


scanf("%d",&age);
if (age>10)
{
continue;
}

printf("\nage is :%d", age);


} return 0;
}
3) go to :
The goto statement can be used to jump from anywhere to anywhere within
a function.
Example :
#include <stdio.h>
int main()
{
int x;
for (int i = 0;i <5; i++)
{
printf("\nHey Guys\n\n");
for (int j = ;j < 3; j++)
{
printf("Type any No. & To Exit : Press 1\n");
scanf("%d", &x);
if (x == 1)
{
goto end;
}
}
}
end:
printf("\'For\' loops are skipped as you pressed 1");
return 0;
}

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
4) Return :
The return statement is used to return the value of the expression back to the
main function.

Example :
#include<stdio.h>
void main()
{

printf(“%d”, add());

}
int add()
{
int a=4;
return a;
}

For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Array
- In C, An array is collection of similar type of data.
- In array elements are stored continuously.
- In C, there is no dimensions limit to an array.
- Array index always start from 0.
- Last index=size-1
- Declaration of array syntax :
Datatype arrayname[Column/Sizeofarray];
Example : int list[10];
float marks[5];
- Properties of array –
1) Data is stored continuously in the array.
2) Each element in array has same size as per its datatype.
3) Any elements or item of array can be accessed easily using its index and base
address.
- Advantages of array –
1) Array is used to represent multiple data items/elements (of same type) using
single name.
2) Accessing elements of array is fast.
- Most commonly used types of array –
1) One dimensional array :-
Syntax : datatype arrayname[column/size];
Example :
Output:

2) Two dimensional array :-


Syntax : datatype arrayname[no_of_rows][no_of_columns];
Example :

Output:
3) Three dimensional array :-
Syntax : datatype arrayname[no_of_array][no_of_rows][no_of_columns];
Example :

Output:
FUNCTION
• Functions are block of statements that perform some given task to it.
• Functions are used to divide large amount of code into smaller pieces.
• Functions can be called multiple times to perform its task repetitively .
Advantages : 1) Provides Modularity
2) Provides Reusability.
3) Make code easy to debug & understand.
Types of function :
1) System Defined Function/Library Function :
- Are also called as Pre-defined function.
- These functions are defined inside system libraries in C.
- Example of system defines functions are : main(), printf(), getch(),
scanf() , abs(), clrscr() etc :
- Note : Every program must contain one library function that is main() ,
because execution of program start from main. Its also called as entry
point of program .

2) User Defined Function :


- Functions that are created by programmer are called as user defined
function .
How to create a function ?/ Aspects of function :
1) Declaration : Function is declared to indicate compiler that it exist in code.

Syntax : returntype Functionname(Argument1, Argument 2,…, argument n);


Example : void display(int n, int m);
int calculate();

2) Definition : Function id defined to perform some tasks using it.

Syntax : returntype Functionname(Argument1, Argument 2,…, argument n)


{
//task to perform ;
}
Example : 1) void display(int n, int m)
{
printf(“this is display function”);
}
2) int calculate()
{
int a=2,b=3;
return a+b;
}
3) Function Call : Function is called in order to use it .

Syntax : Functionname(Argument1, Argument 2,…, argument n);


Example : 1) display(a,b);
2) calculate();

Function has four types based on its return type and argumets :
1) Without arguments and without return value –
In this type of function we don’t need to pass any value to function and
also don’t need to take any return value from it.
Example :

Output:
2) Without arguments and with return value -
In this type of function we don’t need to pass any arguments to function
but must get something in return from it.
Example :

Output:

3) With arguments and without return value:


In this type of function we don’t need to return any value but have to
pass arguments to function .
Example :
Output :

4) With arguments and with return value.

In this type of function we need to pass the arguments to function as


well as need to get some value in return also.
Example :

Output:

KEYS:
Actual parameters are the parameters that are passed to function while calling it.
Formal parameters are the parameters that are used in the function definition as
function arguments.

Call by value :
- In this method , Values of actual parameters are copied into Formal Parameters.
- In this type of function call, change in values of formal parameters in called
function does not affect the values of actual parameters.
Example :

Output:

Call by reference :
- In this method , address of actual parameters are copied into Formal Parameters.
- In this type of function call, change in values of formal parameters in called
function directly affect the values of actual parameters.
- Example :
Output:

Local and global variable :


- Local variables are the variables that are declared inside the function or block of
code .
- Local variables cannot be accessible outside the function. Scope of these variables
are only within the function it is created into .
- Global are basically declared outside the function definition.
- Global variables can be accessed from any function defined in the program .
- Example :

Output:

Recursive Function :
- The process of function calling itself to work on smaller problems is called
as recursion . And the function that calls itself directly or indirectly get called
as recursive function.
Example :

Output :
Structure
__________________________________________________________
• It is a collection of different type of data in same location .
• Structures are used when we want to store dissimilar data together.
• Syntax :
struct structure_name
{
//element 1;
//element 2;
.
.
.
//element n;
};
• Structure variable syntax:
struct structure_name var_name= value;
• Example :

Output
Union

• It is a collection of different type of data in same location .


• Unions are used when we want to store dissimilar data together.
• Syntax :
union structure_name
{
//element 1;
//element 2;
.
.
.
//element n;
};
• Union variable syntax:
union union_name var_name= value;

Example :

Output:
Pointer
• In c , Pointer is a special variable that stores the address of another variable .

fffh
fffg
fffg
50
P
(pointer variable) A
(normal variable)

• Here, * (asterisk ) sign is used to declare pointers


• Syntax : datatype *pointervariable_name = &address_var;
• In above syntax , pointervariable_name is name given to the pointer and address_var is a
variable whom address we store into pointer variable .
• Example :

Output :

Note :
Call by reference and Call by value notes are included in chapter function .

You might also like