Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

C Array Full Notes

This document provides an overview of key concepts in C programming including: 1. C is considered the mother language of many other programming languages due to influencing their development. 2. The main() function acts as the entry point for C programs. 3. Variables are used to store and manipulate data within a program. Common variable types include local, global, static, and automatic variables. 4. C programs use operators, comments, control statements like if/else conditions and loops, and data types to perform tasks.

Uploaded by

manoj1980pranesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C Array Full Notes

This document provides an overview of key concepts in C programming including: 1. C is considered the mother language of many other programming languages due to influencing their development. 2. The main() function acts as the entry point for C programs. 3. Variables are used to store and manipulate data within a program. Common variable types include local, global, static, and automatic variables. 4. C programs use operators, comments, control statements like if/else conditions and loops, and data types to perform tasks.

Uploaded by

manoj1980pranesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C Programming -

C programming is considered as the base for other programming languages, that is why it is
known as mother language.

1. Mother language
2. System programming language
3. Procedure-oriented programming language
4. Structured programming language
5. Mid-level programming language

Syntax –

#include <stdio.h>
int main() {
printf("Hello C Programming\n");
return 0;
}

#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .

int main() The main() function is the entry point of every program in c language.

Variable -

A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily


identified.

Syntax –

type variable_list;
int a;
float b;
char c;

Rule for defining variables-


o A variable can have alphabets, digits, and underscore.
o A variable name can start with the alphabet, and underscore only. It can't start
with a digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Type of variables –
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable

Local Variable –
A variable that is declared inside the function or block is called a local variable.

Example –

void function1(){
int x=10;//local variable
}

Global Variables –
A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all the functions.

Example –
int value=20;//global variable
void function1(){
int x=10;//local variable
}

Static Variable –
A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

Example –

void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}

If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13

Automatic Variable –
All variables in C that are declared inside the block, are automatic variables by default. We
can explicitly declare an automatic variable using auto keyword.

Example –

void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}

External Variable –
We can share a variable in multiple C source files by using an external variable. To declare an
external variable, you need to use extern keyword.

Example –

extern int x=10;//external variable (also global)

#include "myfile.h"
#include <stdio.h>
void printValue(){
printf("Global variable: %d", global_variable);
}

Data Type –
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.

Data Types Memory Range


Size

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned 2 byte 0 to 65,535


short

int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767


signed short 2 byte −32,768 to 32,767
int

unsigned 2 byte 0 to 65,535


short int

long int 4 byte -2,147,483,648 to


2,147,483,647

signed long 4 byte -2,147,483,648 to


int 2,147,483,647

unsigned long 4 byte 0 to 4,294,967,295


int

float 4 byte

double 8 byte

long double 10 byte

Example –

int age = 25;


char grade = 'A';
float temperature = 98.6;
double pi = 3.14159265359;

Operators –
An operator is simply a symbol that is used to perform operations.

Type –

o Arithmetic Operators - +,-,*,/,%


o Relational Operators - <,>,==,<=,>=
o Logical Operators - &&(and),||(or)
o Bitwise Operators - &,|
o Assignment Operator - = ,+=, -=, *=, /=,
Comment –

Type –

1. Single Line Comments


2. Multi-Line Comments

Single Line Comment –


Example –

#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}

Multi-line Comment –
Example –

/*
code
to be commented
*/

#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
return 0;
}

Control Statement –
1 Condition Statement

2 Loop

3 Jump Statement
Condition Statement –
o If statement
o If-else statement
o If else-if ladder
o Nested if

If Statement –
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions.

Synatx –

if(expression){
//code to be executed
}

Example –

#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}

If-else Statement –
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations,
Syntax –

if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

Example –

#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}

If else-if Statement –
The if-else-if ladder statement is an extension to the if-else statement. It is used in the
scenario where there are multiple cases to be performed for different conditions. In if-else-if
ladder statement, if a condition is true then the statements defined in the if block will be
executed, otherwise if some other condition is true then the statements defined in the else-if
block will be executed, at the last if none of the condition is true then the statements defined
in the else block will be executed.

Synatx –

if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

Example-

#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

Switch Statement –
For loop –
used to iterate the statements or a part of the program several times. It is frequently used to
traverse the data structures like the array and linked list.

Syntax –

for(Expression 1; Expression 2; Expression 3){


//code to be executed
}

Example –

#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}

Example –

#include <stdio.h>
int main()
{
int a,b,c;
for(a=0,b=12,c=23;a<2;a++)
{
printf("%d ",a+b+c);
}
}
Nested Loop -
Any number of loops can be defined inside another loop, i.e., there is no restriction for
defining any number of loops. The nesting level can be defined at n times. You can define
any type of loop inside another loop.

Syntax –

Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}

Nested for loop Syntax –

for (initialization; condition; update)


{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
Example –

#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j)); // printing the value.
}
printf("\n");
}

While loop -
a while loop allows a part of the code to be executed multiple times depending upon a given
boolean condition. It can be viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in advance.

Syntax –

while(condition){
//code to be executed
}

Example –

#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
}
return 0;
}

Example –

#include<stdio.h>
int main(){
int i=1,number=0,b=9;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d \n",(number*i));
i++;
}
return 0;
}

Example –

#include<stdio.h>
void main ()
{
int j = 1;
while(j+=2,j<=10)
{
printf("%d ",j);
}
printf("%d",j);
}

Nested While loop –


The nested while loop means any type of loop which is defined inside the 'while' loop.
Example –

Syntax –

while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}

Example –

#include <stdio.h>
int main()
{
int rows; // variable declaration
int columns; // variable declaration
int k=1; // variable initialization
printf("Enter the number of rows :"); // input the number of rows.
scanf("%d",&rows);
printf("\nEnter the number of columns :"); // input the number of columns.
scanf("%d",&columns);
int a[rows][columns]; //2d array declaration
int i=1;
while(i<=rows) // outer loop
{
int j=1;
while(j<=columns) // inner loop
{
printf("%d\t",k); // printing the value of k.
k++; // increment counter
j++;
}
i++;
printf("\n");
}
}

Do-while loop –
A loop is a programming control structure that allows you to execute a block of
code indefinitely if a specific condition is met. Loops are used to execute repeating activities
and boost programming performance. There are multiple loops in the C programming
language, one of which is the "do-while" loop.

Syntax –

do{
//code to be executed
}while(condition);

o The do keyword marks the beginning of the Loop.


o The code block within curly braces {} is the body of the loop, which contains
the code you want to repeat.
o The while keyword is followed by a condition enclosed in parentheses (). After
the code block has been run, this condition is verified. If the condition is true,
the loop continues else, the loop ends.

Example –

#include <stdio.h>
#include <string.h>
int main() {
char password[] = "secret";
char input[20];
do {
printf("Enter the password: ");
scanf("%s", input);
} while (strcmp(input, password) != 0);
printf("Access granted!\n");
return 0;
}
Example –

#include<stdio.h>
intmain(){
inti=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
}

Nested Do-while Loop –


The nested do..while loop means any type of loop which is defined inside the 'do..while'
loop.

Syntax –

do
{
do
{
// inner loop statements.
}while(condition);
// outer loop statements.
}while(condition);
Example –

#include <stdio.h>
int main()
{
/*printing the pattern
********
********
********
******** */
int i=1;
do // outer loop
{
int j=1;
do // inner loop
{
printf("*");
j++;
}while(j<=8);
printf("\n");
i++;
}while(i<=4);
}

Array -
An array is defined as the collection of similar type of data items stored at contiguous
memory locations.

Properties of Array –
o Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
o Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size
of the data element.

Defining Array –
Syntax –

data_type array_name[array_size];

Example –

int marks[5];

Initialization Array –
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;

Example –

#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}

Array Defining with initialization –


int marks[5]={20,30,40,50,60};

no requirement to define the size.

int marks[]={20,30,40,50,60};

Example –

#include<stdio.h>
int main(){
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}

Sorting an array

#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}

2-D Array –
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.

Syntax – Declare 2D array

data_type array_name[rows][columns];

Example –

int twodimen[4][3];

Initialization of 2D array –

Example -
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}

Example – Storing element in a Matrix and printing –

#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Array in function -
Passing array to a function

Syntax –

functionname(arrayname);//passing array

Ways to declare a function that receives an array as an argument –

One - return_type function(type arrayname[])

Two - return_type function(type arrayname[SIZE])

Three - return_type function(type *arrayname)

Example –

#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array

min=minarray(numbers,6);//passing array with size


printf("minimum number is %d \n",min);
return 0;
}
Returning Array –
Syntax –

int * Function_name() {
//some statements;
return array_type;
}

Example –

#include<stdio.h>
int* Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int *p = Bubble_Sort(arr), i;
printf("printing sorted elements ...\n");
for(i=0;i<10;i++)
{
printf("%d\n",*(p+i));
}
}
int* Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j,temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}

You might also like