0% found this document useful (0 votes)
3 views

C notes

The document provides an overview of key concepts in C programming, including structures, character arrays, functions, memory allocation functions, pointers, and string handling functions. It includes syntax examples for defining structures, declaring character arrays, and creating functions, as well as explanations of pointers and operators. Additionally, it features sample code snippets demonstrating array input, string length calculation, and pointer usage.

Uploaded by

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

C notes

The document provides an overview of key concepts in C programming, including structures, character arrays, functions, memory allocation functions, pointers, and string handling functions. It includes syntax examples for defining structures, declaring character arrays, and creating functions, as well as explanations of pointers and operators. Additionally, it features sample code snippets demonstrating array input, string length calculation, and pointer usage.

Uploaded by

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

Q.

a)
Definition:-

Structure is collection of elements with different name an different data types

Syntax to Define a Structure

The basic syntax to define a structure is as follows:

struct structure_name
{
data_type member1;
data_type member2;
// Other members...
}structure variable;

Where:

 struct is the keyword used to define a structure.


 structure_name is the name you give to the structure
 member1, member2, etc., are the names of the structure members, and each member has a
specified data_type.
 Structure variable is used to access structure members

b)
A character array is simply an array of characters. It is used to store a sequence of characters in
a contiguous block of memory.

Declaration of Character Array:

To declare a character array in C, you specify the type char, followed by the name of the array
and the number of elements

For example:

char name[10];

This declaration creates an array that can hold 10 characters.


d)
Function in C

 A function in C is a block of code that performs a specific task.


 A large program is divided into a multiple functions
 Each function perform specific task

Syntax

return_type function_name(parameter1, parameter2, ...)

// Body of the function

// Code to perform the task

return value; // If the return type is not void

Need of fuction

Functions allow for the modularization of code, making it more organized, reusable, and easier to
debug.

e)
 malloc(): Allocates a specified number of bytes of memory and returns a pointer to the first
byte, initializing the memory to garbage values.

 calloc(): Allocates memory for a specified number of elements, each of a specified size, and
initializes all the bytes to zero.

 getchar(): Reads a single character from the standard input (keyboard) and returns it as an
integer.

 putchar(): Writes a single character to the standard output (screen).


f)

A pointer is a variable that stores the memory address of another variable

Pointer Declaration:
data_type *pointer_name;

For example:

int *ptr; // Declares a pointer to an integer

Pointer Initialization:

To initialize a pointer, you assign it the address of a variable using the address-of operator (&).

int a = 10;
int *ptr = &a; // Initializes ptr to the address of variable a

Here:

 a is an integer variable.
 ptr is a pointer that holds the memory address of a.
 &a is the address of a.

Simple Example:
#include <stdio.h>

void main()

int num = 5;
int *ptr;
ptr = &num;

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", (void*)&num);

getch();
}
g)
1. & (Address-of operator):

The & operator is used to obtain the memory address of a variable. It returns the address where a
variable is stored.

Syntax

&variable_name

int a = 10;
int *ptr;
ptr = &a; // &a gives the address of 'a' and stores it in ptr

2. * (Dereference operator):

The * operator is used to access the value stored at the memory address a pointer is pointing to. It
is called dereferencing the pointer.

Syntax

*pointer_name

If ptr is a pointer to an integer, *ptr gives the value stored at the memory address pointed to by
ptr.
Q.2
a)
#include<stdio.h>

#include<conio.h>

void main()

int a[10],i;

clrscr();

printf("\nn Enter 10 Array Elements ");

for(i=0;i<10;i++)

scanf("%d",&a[i]);

printf("\n Array Elements are ");

for(i=0;i<10;i++)

printf("\n %d ",a[i]);

getch();

}
b)

c)
Here is a list of string handling functions in C:

1. strlen()
2. strcpy()
3. strcat()
4. strcmp()
5. strlwr()
6. struppr()
7. strrev()

strlen():- It is a standard predefined string handling function used to find length of a string

Syntax :- strlen(str)

strrev():- It is a standard predefined string handling function used to reverse a string

Syntax :- strrev(str)
#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[30];

clrscr();

printf(“Enter a String ”);

scanf(“%s”,&str);

printf(“\n Length of string is %d ”,strlen(str));

printf(“\n Reverse of string is %s ”,strrev(str));

e)

#include<stdio.h>

#include<conio.h>

void product();

void main()

int a,b;

clrscr();

printf("\n Enter Two Numbers ");


scanf("%d%d",&a,&b);

printf("\n Product is %d ",a*b);

getch();

f)

#include<stdio.h>

#include<conio.h>

void main()

int a=10,*ptr;

clrscr();

ptr=&a;

printf("\n Address of a is %u ",ptr);

printf("\n Value of a is %d ",*ptr);

getch();

You might also like