Have a Read!

Hi Everyone.
The idea for this page is to provide new c users simple c tutorials to learn how to develop the logic and more importantly correct way to write syntax.
ENJOY !!!

Sunday, February 6, 2011

How to use sscanf

#include<stdio.h>
int main()
{
char s[]="123 45.32 4";
int x;
double y;
int c;
puts(s);
sscanf(s,"%d%lf%d",&x,&y,&c);
printf("%d\n%lf\n%d\n",x,y,c);
}

How to use sprintf

#include<stdio.h>
int main ()
{ int x;
  char s[80];
  double y;
  printf("ENTER\n");
  scanf("%d%lf",&x,&y);
  sprintf(s,"integer:%6d\ndouble:%.2f\n",x,y);
  puts(s);
}

How to get string input way2

#include<stdio.h>
int main ()
{ int i=0;
  char c;   
  char a[80];
  printf("ENTER\n");
  while ((c=getchar())!='\n') {a[i++]=c;}
  a[i]='\0';
  puts(a); 
}

How to get a string input way1

#include<stdio.h>
int main()
{
char s[50];
printf("enter\n");
fgets(s,50,stdin);
printf("%s",s);
}

How to use strtol

#include<stdio.h>
#include<stdlib.h>
int main () {
const char *string="21.3% are good" ;
long d;
char *stringptr;
d=strtol(string,&stringptr,0);
printf("%s\n%ld\n%s\n",string,d,stringptr);
}

How to use strod

#include<stdio.h>
#include<stdlib.h>
int main () {
const char *string="21.3% are good" ;
double d;
char *stringptr;
d=strtod(string,&stringptr);
printf("%s\n%.2f\n%s\n",string,d,stringptr);
}

How to use atol

#include<stdio.h>
#include<stdlib.h>
int main() {
long i;
char s[20]={"23.56 this is"};
i=atol(s);
printf("%ld\n",i);
}

How to use toupper and tolower

#include<stdio.h>
#include<ctype.h>
int main() { char c,a;
printf("ENTER\n");
scanf("%c",&c);
a=toupper(c);
printf("%c\n",a);
a=tolower(c);
printf("%c\n",a);
}

How to use ctype.h functions

The following codes shows how to use isdigit(), isalpha(), isalnum(), isxdigit(), isspace(), iscntrl(), ispunct(), isprint(), isgraph(),
#include<stdio.h>
#include<ctype.h>
int main()
{int i;
char f;
printf("enter character\n");
scanf("%c",&f);
i=isdigit(f);
if(i==0)printf("not digit\n");else printf("is digit\n");
i=isalpha(f);
if(i==0)printf("not alpha\n");else printf("is alpha\n");
i=isalnum(f);
if(i==0)printf("not digit or aplha\n");else printf("is digit or alpha\n");
i=isxdigit(f);
if(i==0)printf("not xdigit\n");else printf("is xdigit\n");
i=isspace(f);
if(i==0)printf("not space\n");else printf("is space\n");
i=iscntrl(f);
if(i==0)printf("not cntrl\n");else printf("is cntrl\n");
i=ispunct(f);
if(i==0)printf("not punct\n");else printf("is punct\n");
i=isprint(f);
if(i==0)printf("not print\n");else printf("is print\n");
i=isgraph(f);
if(i==0)printf("not graph\n");else printf("is graph\n");
}

How to use strlen

#include<stdio.h>
#include<string.h>
int main ()
{
  const char *s="abcdefghijklmnopqrstuvwxyz";
   int y;
   y=strlen(s);
   printf("%d\n",y);
}

How to use memset

#include<stdio.h>
#include<string.h>
int main()
{ char s[]="ggggggggggg";
  memset(s,'G',5);
puts(s);
}

How to use memmove

#include<stdio.h>
#include<string.h>
int main ()
{
char x[]="home sweet home";
memmove(x,&x[5],10);
puts(x);
}

How to use memcpy

#include<stdio.h>
#include<string.h>
int main()
{char s[17];
char x[]="Thats how it is done";
memcpy(s,x,17);
puts(s);
}

How to use strstr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="scorecard";
const char *y="e";
printf("%s\n",strstr(s,y));
}

How to use strrchr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="the camera is zooming";
int c='a';
printf("%s\n",strrchr(s,c));
}

How to use strpbrk

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="best of luck";
const char *x="o";
printf("%c\n",*strpbrk(s,x));
}

How to use strchr

#include<stdio.h>
#include<string.h>
int main()
{
const char *s="Thats how it is done";
char c='u';
if (strchr(s,c)!=NULL)
{printf ("found\n");}
else
{printf("not found\n");}
}

How to use strcmp

#include<stdio.h>
#include<string.h>
int main()
{
const char *x="memm";
const char *y="mmea";
int c;
c=strcmp(x,y);
if(c==0) {printf("0\n");}
if(c<0)  {printf("-1\n");}
if (c>0) {printf("1\n");}
}

How to use strcpy & strncpy

#include<stdio.h>
#include<string.h>
int main()
{
char x[]="HOW TO USE THIS";
  char y[25];
  char z[15];
strcpy(y,x);
puts(y);
strncpy(z,x,6);
puts(z);
}

C Program for Finding maximum entry in data

#include<stdio.h>
void array(int a[4][3]);
int locater(int b[4][3],int c);
int main()
{
    int marks,i;
    int record[4][3]={{12,25,67},{32,30,65},{15,34,70},{20,40,79}};
    printf("\n\n\n");
    array(record);
    for(i=0;i<3;i++)
        {
            marks=locater(record,i);
            if(i==0)
            {
            printf("\nThe Maximum lowest marks in all sections are %d\n",marks);
            }
            if(i==1)
            {
            printf("The Maximum Average in all sections are %d\n",marks);
            }
            if(i==2)
            {
            printf("The Maximum Highest marks in all sections are %d\n\n\n\n",marks);
            }
        }
    return 0;
}
void array(int a[4][3])
{
            int i,j;
            printf("Sections    Lowest Average Highest\n");
            printf("        marks  marks    marks\n"   );
                for(i=0;i<4;i++)
                    {   
                    printf("Section %d\t",i+1);
                    for(j=0;j<3;j++)
                        {
                            printf("%d\t",a[i][j]);
                        }   
                    printf("\n");
                    }
}
int locater(int a[4][3],int c)
{
            int x;
            for(x=0;x<4;x++)
            {
                if(a[x][c]>=a[1][c] && a[x][c]>=a[2][c] && a[x][c]>=a[3][c])
                               {   
                return a[x][c];       
                            }   
            }
}

C Program for Searching and Sorting Data

#include<stdio.h>
#include<stdlib.h>
void sorter(int b[],int size);
void searcher(int b[],int size);
int main()
{
    int a[40],i;
    printf("\n\n\nTHE DATA SET CONTAINS 40 VALUES WHICH ARE AS FOLLOW:");
    printf("\n");
    for(i=0;i<40;i++)
    {a[i]=rand()%40;
    printf("%d ",a[i]);
    }
    sorter(a,40);
    searcher(a,40);
    return 0;
}
void sorter(int b[],int size)
{
    int i,j,k,temp;
    for(i=0;i<size;i++)
        {
        for(j=0;j<size-1;j++)
        {
            if(b[j]>b[j+1])
            {temp=b[j];
                 b[j]=b[j+1];
                 b[j+1]=temp;
            }
         }
        }
    printf("\n\n\nTHE DATA IS BEING SORTED AS FOLLOW:\n");
    for(k=0;k<40;k++)
    {printf("%d ",b[k]);}
    printf("\n\n");
    }
void searcher(int b[],int size)
{
    int key,found,low,mid,high;
    printf("\nWhat value you want to locate=");
    scanf("%d",&key);
    low=0;
    high=size-1;
    while(low<=high)
    {   
    mid=(low+high)/2;
    if(b[mid]==key)
    {
    found=1;
    break;
    }
    else if(b[mid]>key)
    {
    high=mid-1;
    }
    else
    {
    low=mid+1;
    }
    }
    if(found==1)
    printf("%d is in the data set\n",key);
    else printf("%d is not in the data set\n",key);
}

C Program for int type function

#include<stdio.h>
int function(int);
int main ()
{
int x=10,y;
y=function(x);
printf("%d\n",y);
return 0;
}
int function(int a)
{return a*a;}

C Code for void type fuction

#include<stdio.h>
void function(int);
int main()
{
int x=10;
function(x);
return 0;
}
void function(int a)
{printf("%d\n",a);}

C Program for array of characters/string

#include<stdio.h>
int main ()
{
char a[]="Hello";
int x;
for (x=0;x<6;x++)
    {printf("%c",a[x]);} //printing character wise
printf("\n");
//printing string directly
printf("%s\n",a);
return 0;
}

C Program for initializing 2d array

#include<stdio.h>
int main ()
{
int a[10][10]={0};
int x,y;
for (x=0;x<10;x++)
    for (y=0;y<10;y++)
        {printf("%d\n",a[x][y]);}
return 0;
}

C Program for initializing and printing array

#include<stdio.h>
int main()
{
int a[10]={0};
int x;
for (x=0;x<10;x++)
  {
    printf("%d\n",a[x]);
  }
return 0;
}

Related Posts with Thumbnails