Wednesday, June 24, 2015

Comparing Username Stored in File in C

I got a request to read the username from file and compare the same .
Here is the code for the same :

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
char user_name[128];
char line[128];
char str[128];
char *ptr;
int i=0;
FILE *fp;

//Saving username in file
printf("Enter the username to write in a file:");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
user_name[i]='\0';
--i;
}
else
{
printf("%c",ch);
user_name[i++]=ch;
}
ch=getch();
}
user_name[i]='\0';
ptr=user_name;
fp=fopen("d:\\user.txt","w+");
while(*ptr!='\0')
{
fputc(*ptr,fp);
ptr++;
}
printf("\nUsername successfully saved in the file");

//Reading username from file and saving to string .
fseek( fp, 0, SEEK_SET );
while(fgets(line,sizeof(line),fp)!=NULL)
   {
   char *nwln=strchr(line,'\n');
   if(nwln!=NULL)
   *nwln='\0';
   }
printf("\nUsername in file is :%s",line);
fp=NULL;

//take username from user to compare.
printf("\nEnter username to confirm:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = '\0';
if(!strcmp(line,str))
{
    printf("\nyou are right");
}
else
{
    printf("\nyou are wrong");
}

getch();
}


Tuesday, January 27, 2015

Format Specifiers in C

Let us take a look at some format specifiers used in printf which are necessary to format the output.....

%x--> prints an int in hexadecimal.
%4x--> prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded by spaces. If it's more than 4 digits, you get the full number.
%04x--> prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded by zeroes. If it's more than 4 digits, you get the full number, but no leading zeroes.

example:
void main()
{
int i=98;
printf("%x\n",i);
printf("%4x\n",i);
printf("%04x",i);
getch();
}

output:
62
       62
00062

Similary with %d
void main()
{
int i=5;
printf("%d\n",i);
printf("%4d\n",i);
printf("%04d",i);
getch();
}

output:
5
         5
00005

Similary with %c..
 void main()
{
char c='a';
printf("%c\n",c);
printf("%4c\n",c);
printf("%04c",c);
getch();
}


output:
a
         a

0000a



Calling conventions in C

What is the calling convention?
When a function is called, the arguments are typically passed to it, and the return value is retrieved.

A calling convention describes:
-->how the arguments are passed and values returned by functions.

Two calling Conventions:
1.)cdecl
2.)pascal

1.)cdecl
-->This convention is the default for C/C++ programs..
-->Arguments are passed from right to left, and placed on the stack.
-->Only the code that calls these functions knows for certain what it has placed on the stack.
So,stack cleanup is performed by the caller...
-->Support the variable argument functionality.

2.)pascal
-->Arguments are passed form left to right ,and placed on stack.
-->Stack cleanup is performed by the called function.
-->Does not support variable argument functionality.

Example:
#include<stdio.h>
#include<conio.h>
void pascal f(int i,int j,int k)
{
printf(“i=%d j=%d k=%d”,i, j, k);
}
void cdecl f1(int i,int j,int k)
{
printf(“i=%d j=%d k=%d”,i, j, k);
}

int main()
{
int i=50;
f1(i++,i++,i++);
printf("-->%d\n",i);
i=50;
f(i++,i++,i++);
printf("-->%d",i);
getch();
return 0;
}

output:
i=52 j=51 k=50-->53
i=50 j=51 k=52-->53

Explanation:
Strictly speaking the result is undefined , Read about the Sequence Points and Undefined Behavior .
now if we talk about a particular compiler implementation ,if that compiler produces "i=52 j=51 k=50" for cdecl convention(default for C) then it willl certainly give the output "i=50 j=51 k=52" for the pascal convention because they are opposite to each other..