0% found this document useful (0 votes)
13 views12 pages

CSP 1

The document contains several C program code snippets that demonstrate different ways of defining and using functions, including: 1) Functions that return values and accept arguments, functions that return no value but accept arguments, functions that return values but accept no arguments, and functions that return no value and accept no arguments. 2) Examples of using structures and nested structures to store and print student name, roll number, and date of birth data. 3) Code showing how to get and print the address and value of variables, pointers, and pointer variables. 4) Programs that demonstrate reading from and writing to data files using functions like fopen(), fread(), fwrite(), fscanf(), fprintf(), etc.

Uploaded by

sujalchauhan946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

CSP 1

The document contains several C program code snippets that demonstrate different ways of defining and using functions, including: 1) Functions that return values and accept arguments, functions that return no value but accept arguments, functions that return values but accept no arguments, and functions that return no value and accept no arguments. 2) Examples of using structures and nested structures to store and print student name, roll number, and date of birth data. 3) Code showing how to get and print the address and value of variables, pointers, and pointer variables. 4) Programs that demonstrate reading from and writing to data files using functions like fopen(), fread(), fwrite(), fscanf(), fprintf(), etc.

Uploaded by

sujalchauhan946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Function returning value and passing arguments.

#include<stdio.h>
#include<conio.h>
int add(int,int);
int main()
{
int a,b,c;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
c=add(a,b);
printf("the sum is: %d",c);
getch();
}
int add(int a,int b)
{
int sum;
sum=a+b;
return(sum);
}
Function returning no value but passing arguments
#include<stdio.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
add(a,b),
getch();
}
void add(int a,int b)
{
int sum;
sum=a+b;
printf("The sum is %d:",sum);
}
Function returning value and passing no arguments
#include<stdio.h>
#include<conio.h>
int add();
int main()
{
int c;
c=add();
printf("The sum is: %d",c);
getch();
}
int add()
{
int sum,a,b;
printf("Enter two numbers:"); scanf("%d %d",&a,&b);
sum=a+b;
return(sum);
}
Function returning no value and passing no arguments
#include<stdio.h>
#include<conio.h>
void add();
int main()
{
add();
getch();
}
void add()
{
int sum,a,b;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
sum=a+b;
printf("the sum is %d",sum);
}
Write a program to print name, roll and date of birth by nested
structure
#include<stdio.h>
#include<conio.h>
int main()
{
struct date
{
int day;
int month;
int year;
};
struct student
{
char name[30];
int roll;
struct date d;
};
struct student s;
printf("Enter name:");
scanf("%s",s.name);
printf("Enter roll No:");
scanf("%d", &s.roll); printf("Enter day.month and year:");
scanf("%d %d %d",&s.d.day,&s.d.month,&s.d.year);
printf("\n name=%s\nRoll=%d\n",s.name,s.roll);
printf("\nDay=%d\n\t Month=%d\n\t Year-%d",s.d.day,s.d.month,s.d.year);
getch();
}
Write a program to display the address and the content of a variable
#include<stdio.h>
#include<conio.h>
int main()
{
int u=3;
int v;
int *pu;
int *pv;
pu=&u;
v=*pu;
pv=&v;
printf("\n u=%d &u=%u pu=%u *pu=%d",u,&u,pu,*pu);
printf("\n v=%d &v=%u pv=%u *pv=%d",v,&v,pv,*pv);
getch();
}
Write a program to display the address and the content of a pointer
variable and ordinary variable.
#include<stdio.h>
#include<conio.h>
int main()
{
int x = 33;
int*y;
y =&x;
printf("\n address of x=%u",&x);
printf("\n address of x=%u",y);
printf("\n address of y=%u", &y);
printf("\n value of y=%u",y);
printf("\n value of x=%d",x);
printf("\n value of x=%d",*(&x));
printf("\n value of x=%d",*y);
getch();
}
WAP to display contents from the file "student.dat".
#include<stdio.h>
#include<conio.h>
int main()
{
char name[22];
int roll;
float marks;
FILE *fp;
fp=fopen("student1.dat","r");
fscanf(fp,"%s%d%f", name, &roll,&marks);
printf("\n Name: %s", name);
printf("\n Roll: %d", roll);
printf("\n Marks: %f", marks);
fclose(fp);
getch();
}
WAP to enter person's name, address, rollno, and marks of 'n'
students.
#include<stdio.h>
#include<conio.h>
int main()
{
char name[55]; char address[44];
int roll;
float me;
FILE *fp;
int i,n;
fp=fopen("sad.txt","a");
printf("Enter how many records:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name:");
scanf("%s", name);
printf("Enter address:");
scanf("%s", address);
printf("Enter roll:");
scanf("%d", &roll);
printf("Enter marks in english:");
scanf("%f",&me);
fprintf(fp, "\n%s\n\n%s %d\n%f", name,
address,roll,me);
}
fclose(fp);
getch();
}
Write a C program to write all the members of an array of structures
to a file using fwrite(). Read the array from the file and display on the
screen.
#include <stdio.h>
struct s{
char name[50]; int height;
};
int main() {
struct s a[5],b[5];
FILE *fptr;
int i;
fptr=fopen("file.dat", "w"); for (i=0;i<3; ++i) {
fflush(stdin);
printf("Enter name: ");
gets(a[i].name);
printf("Enter height: ");
scanf("%d",&a[i].height);
}
fwrite(a,sizeof(a),1,fptr);
fclose(fptr);
fptr=fopen("file.dat","r");
fread(b,sizeof(b),1,fptr);
for (i=0;i<3; ++i) {
printf("\n Name: %s\n Height: %d\n",
b[i].name,b[i].height);
}
fclose(fptr);
}

You might also like