A simple program that uses assert() function.
MAIN.C
/*
* main.c
*
* Created on: 15 Mar 2014
* Author: Aadit
*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SUCCESS "Operation success!\n"
#include "main.h"
#define USAGE printf("usage:<program name> command.\n");
struct userbase user1;
#include <assert.h>
int main(int argc,char *argv[])
{
if(check(argc,argv,&user1) == 2)
{
printf(SUCCESS);
}
else if(check(argc,argv,&user1) == CHECK_ERROR)
{
printf("Command not matched!\n");
}
else
USAGE;
assert(user1.password != NULL && user1.username != NULL);
printf("Username =%s\nPassword = %s\n",user1.username,user1.password);
}
MAIN.H
/*
* main.h
*
* Created on: 15 Mar 2014
* Author: Aadit
*/
#ifndef MAIN_H_
#define MAIN_H_
struct userbase
{
char *username;
char *password;
};
// return 2 if success
#define CHECK_ERROR 4
int check(int a,char *x[],struct userbase *u)
{
if(a == 2)
{
if(!strcmp(x[1],"start"))
{
u->username = "aadit";
u->password = "password";
return 2;
}
return CHECK_ERROR; // 4
}
else
return 1;
}
#endif /* MAIN_H_ */
For those who has not used assert function in c.
0