C Programming Example
C Programming Example
int total=0;
int score;
float avg;
int count=0;
main()
{ int x;
for (x=1;x<=10;x++){
if (x == 5)
continue;
printf("%d ",x);
}
printf("\n Used the continue statement to skip printing the value 5\n\n");
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to make a box of stars
#include<stdio.h>
main()
{
int length,width,num_stars;
printf("enter the length of the square side: \n");
scanf("%d",& length);
width=length;
for(length=0;length<10;length++)
{ for(width=0;width<10;width++)
{printf("*");
}
putchar('\n');
}
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of storing value into an array
#include<stdio.h>
main()
{
int A[5],i;
printf("Enter the values of an Array:");
for(i=0;i<5;i++)
scanf("%d",&A[i]);
for(i=0;i<5;i++)
printf("%d", A[i]);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Another example of storing values in
#include<stdio.h>
main()
{
int A[5]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
printf("%d,", A[i]);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of finding the maximum value in an array
#include<stdio.h>
main()
{
int A[5]={10,20,30,40,50};
int i,max;
max=A[0];
for(i=0;i<5;i++)
{
if(A[i]>max)
{
max=A[i];
printf("the max no: %d,\n", max);
}
}
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to find a required value in an array !
#include<stdio.h>
main()
{
int search_key, i;
int a[5]={10,20,30,40,50};
printf("\nEnter the Search key value:");
scanf("%d",&search_key);
for(i=0;i<5;i++)
{
if (a[i] == search_key)
{ printf("the location of the element %d is %d\n", search_key,i)
;
break;
}
}
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of string
#include<stdio.h>
main()
{ char s1[20];
int i;
printf("\nEnter a string :");
scanf("%s",s1);
printf("%s",s1);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A simple example of pointer
#include<stdio.h>
void main()
{
char var = 'X'; // var a character variable, is declared and initialized to X
char *charPtr; //charPtr a pointer to a character is declared
charPtr = &var; //charPtr now holds (points to) the address of var
*charPtr = 'Y'; //The value which charPtr points to, is now changed to Y .
//using * in this manner is called derefere
ncing a pointer
printf("\n Value of var now is : %c",var); // displays new value Y
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of Call by value
#include<stdio.h>
void main()
{
void callbyvalue(int); //function prototype
int num = 5;
callbyvalue(num); //Function call sending value of num
printf("\n Value of num is %d",num); //Displays original value of nu
m
}
void callbyvalue (int b) //Parameter b is a mirror image of num
{
b=b+100; //b is increased by 100
printf("the value of B is %d\n",b);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
//An example of Call by reference
#include<stdio.h>
void main()
{
void callbyref(int *); //function prototype
int num = 5; //Declaring integer variable num
callbyref(&num); //Function call sending address of num
printf("\n Value of num is %d",num); //Displays new value of num
}
void callbyref (int *b) //function header
{
*b=*b+100; //Value pointed to by b is increased by 100
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Another exapmle of pointer and its uses
#include<stdio.h>
void main() {
int * check (int); //function prototype
int *ptr, a=1000; //Declare ptr and a, assign 1000 to a
ptr=check(a); //Function call
printf( \n Value returned is %d\n ,*ptr); // *ptr is 0
}
int * check (int x) //function header
{
if(x<=1000)
x=0;
return (&x); //returns address of x
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Example of call by value and the changes in values
#include<stdio.h>
extern int a=1, b=2, c=3;
int f(void);
int main()
{ printf("%3d\n", f());
printf("%3d%3d%3d\n", a, b, c);
return 0;
}
int f(void)
{ int b, c;
a=b=c=4;
return (a + b + c);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Call by value
#include<stdio.h>
void f(void);
int main()
{
printf("The function is run for the first time\n");
f();
printf("The function is run for the second time\n");
f();
return 0;
}
void f(void)
{
static int b=5;
printf("The value of
b is %d\n", b);
b++;
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Playing with values !
#include<stdio.h>
int main()
{
int a=1, b=2, c=3;
printf("Values for a, b and c in outer loop are : \n");
printf("%3d %3d %3d\n", a, b, c);
{ int b=4;
float c=5.0;
printf("Values for a, b and c in inner loop are : \n");
printf("%3d %3d %5.1f\n", a, b, c);
a=b;
{
int c;
c=b;
printf("Values for a, b, c in inner loop are: \n");
printf("%3d %3d %3d\n", a, b, c);
}
printf("Values for a, b and c in inner loop are: \n");
printf("%3d %3d %5.1f\n", a, b, c);
}
printf("Values for a, b and c in the outer loop: \n");
printf("%3d %3d %3d\n", a, b, c);
return 0;
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------