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

C - Program For Exam

Uploaded by

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

C - Program For Exam

Uploaded by

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

1.

write a program to create marksheet for students using self referential structure

#include <stdio.h>

struct student {

char firstName[50];

int roll;

float marks;

} s[5];

int main() {

int i;

printf("Enter information of students:\n");

// storing information

for (i = 0; i < 5; ++i) {

s[i].roll = i + 1;

printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

printf("Enter marks: ");

scanf("%f", &s[i].marks);

printf("Displaying Information:\n\n");

// displaying information

for (i = 0; i < 5; ++i) {

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");


puts(s[i].firstName);

printf("Marks: %.1f", s[i].marks);

printf("\n");

return 0;

Enter information of students:

For roll number1,


Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.
.

Command line arguments :


Command line arguments are the arguments specified after the program name in the
operating system's command line. These argument values are passed to your program during
execution from your operating system. To use this concept in your program, you have to
understand the complete declaration of how the main() function works with the command-
line argument to fetch values that earlier took no arguments with it (main() without any
argument).

We can Program the main() in such a way that it can essentially accept two arguments where
the first argument denotes the number of command line arguments and the second denotes the
complete list of every command line argument. It is how you can code your command line
argument within the parenthesis of main():

int main ( int argc, char *argv [ ] )

In the above statement, the command line arguments have been handled via
the main() function, and you have set the arguments where

 argc (ARGument Count) denotes the number of arguments to be passed and


 argv [ ] (ARGument Vector) denotes a pointer array pointing to every argument that
has been passed to your program.
You must make sure that in your command line argument, argv[0] stores the name of your
program, similarly, argv[1] gets the pointer to the 1st command line argument that the user
has supplied, and *argv[n] denotes the last argument of the list.

Program for Command Line Argument

Example:

#include <stdio.h>

int main( int argc, char *argv [] )

printf(" \n Name of my Program %s \t", argv[0]);

if( argc == 2 )
{

printf("\n Value given by user is: %s \t", argv[1]);

else if( argc > 2 )

printf("\n Many values given by users.\n");

else

printf(" \n Single value expected.\n");

Output:

F:\ >program.exe hello

Name of my program program.exe

Value given by user is Hello

3.write a c program to get the name and mark the n number of students from user and
store them in a file.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char name[50];
int marks, i, n;
printf("Enter number of students: ");
scanf("%d", &n);
FILE *fptr;
fptr = (fopen("student.txt", "w"));
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
for (i = 0; i < n; ++i)
{
printf("For student%d\nEnter name: ", i + 1);
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fptr, "\nName: %s \nMarks=%d \n", name, marks);

printf("\nDetails of Student %d\n", i+1);


fscanf(fptr, "%s %d", name, &marks);
printf("Name: %s\n", name);
printf("Marks %d\n", marks);
}
fclose(fptr);
return 0;
}

Output:

You might also like