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

Example of Structure in C

The document discusses several C programming concepts including structures, strings, functions for string manipulation like strupr() and strlwr(), pointers, and copying strings with strcpy(). It provides code examples for each concept to demonstrate their usage.

Uploaded by

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

Example of Structure in C

The document discusses several C programming concepts including structures, strings, functions for string manipulation like strupr() and strlwr(), pointers, and copying strings with strcpy(). It provides code examples for each concept to demonstrate their usage.

Uploaded by

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

Example of Structure in C

#include <stdio.h>

struct StudentData
{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;

/*Assigning the values of each struct member here*/


student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;

/* Displaying the values of struct members */


printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
getch();
}

#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Strings in C
Strings are defined as an array of characters. The difference between a
character array and a string is the string is terminated with a special
character ‘\0’.

Declaration of strings: Declaring a string is as simple as declaring a one


dimensional array. Below is the basic syntax for declaring a string.

char str_name[size];

C – strupr() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strupr(str));
return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strlwr(str));
return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strrev(str));
return 0;
}

#include <stdio.h>
#include <string.h>

int main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
return 0;
}

#include <stdio.h>
#include <string.h>

int main( )
{
int len;
char array[20]="fresh2refresh.com" ;

len = strlen(array) ;

printf ( "\string length = %d \n" , len ) ;


return 0;
}
Pointers
A pointer is a variable that stores the address of another variable. Unlike other
variables that hold values of a certain type, pointer holds the address of a
variable. For example, an integer variable holds (or you can say stores) an integer
value, however an integer pointer holds the address of a integer variable. In this
guide, we will discuss pointers in C programming with the help of examples.

#include <stdio.h>
int main()
{
int num = 10;
printf("Value of variable num is: %d", num);
printf("\nAddress of variable num is: %p", &num);
return 0;
}
Output:

Value of variable num is: 10


Address of variable num is: 0x7fff5694dc58

You might also like