Basic IO:
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
char password[15];
printf("Enter your password:");
fgets(password, 15, stdin);
//
scanf("%s", password);
printf("The password is :");
puts(password);
return 0;
}
Random operator:
#include <stdio.h>
#include <stdlib.h>
int main()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r;
srand((unsigned)time(NULL));
r = rand();
printf("%d is a random
number.\n",r);
return 0;
}
Do …. While:
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
char a, b;
puts("Enter letter: ");
scanf("%c", &b);
a = 'a';
do{
putchar(a);
if(a==b) break;
a++;
} while(a!='z'+1);
return 0;
}
Work with char:
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
int main()
{
int ch;
do{
ch=getchar();
ch = toupper(ch);
putchar(ch);
} while (ch!='\n');
return 0;
}
Working with char:
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
int main()
{
int ch;
char st[100];
int len;
fgets(st, 100, stdin);
len = strlen(st);
printf("%d", len);
return 0;
}
Working with array:
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
int main()
{
char first[] = "I would like to go
USA";
int n=0;
while(first[n]!='\0')
{
putchar(first[n]);
n++;
}
return 0;
}
Get value press enter:
#include <stdio.h>Get value press enter:
int main ( void ){
int a[100], i=0;
int j;
char again;
while (again != '\n') {
scanf("%d", &a[i++]);
again=getchar();
}
for(j = 0; j<i; j++) printf("%d ", a[j]);
return 0;
}
Struct Example:
#include <stdio.h>
int main ( void ){
struct date{
int year;
int month;
int day;
};
struct record {
char name[32];
struct date birthday;
};
struct record president;
strcpy(president.name,"Brad Pit");
president.birthday.year = 1991;
president.birthday.month = 6;
president.birthday.day = 1;
printf("My friend %s was born on %d/%d/%d\n", president.name, president.birthday.year,
president.birthday.month, president.birthday.day);
return 0;
}
Time:
#include <stdio.h>
#include <time.h>
int main ( void ){
time_t now;
struct tm *right_now;
time(&now);
right_now=localtime(&now);
printf("Today is %d/%d at %d:%d\n", right_now->tm_mon+1,
right_now->tm_mday, right_now->tm_hour, right_now->tm_min);
return 0;
}
Pointer and Array:
example 1: #include <stdio.h>
#include <time.h>
int main ( void ){
int array[]={11, 12, 13, 14};
int x;
int *aptr;
aptr = array;
for(x = 0; x<4; x++)
printf("Element %d: %d\n", x+1, *aptr++);
aptr = array;
*(aptr +2) = 0;
for(x = 0; x<4; x++)
printf("Element %d: %d\n", x+1, array[x]);
return 0;
}
example 2:
#include <stdio.h>
#include <time.h>
int main ( void ){
char *string="I'm just a normal string.\n";
int x =0;
while(*string)
{
putchar(*string++);
}
return 0;
}
Pointer Function:
example 1:
example 1: #include <stdio.h>
#include <time.h>
int main ( void ){
int array[]={11, 12, 13, 14};
int x;
int *aptr;
aptr = array;
for(x = 0; x<4; x++)
printf("Element %d: %d\n", x+1, *aptr++);
aptr = array;
*(aptr +2) = 0;
for(x = 0; x<4; x++)
printf("Element %d: %d\n", x+1, array[x]);
return 0;
}
example 2:
#include <stdio.h>
#include <time.h>
int main ( void ){
char *string="I'm just a normal string.\n";
int x =0;
while(*string)
{
putchar(*string++);
}
return 0;
}
Pointer Function:
example 1:
#include <stdio.h>
#include <time.h>
void minus10(int *v){
*v = *v - 10;
}
int main ( void ){
int v = 100;
printf("Value is %d\n", v);
minus10(&v);
printf("Value is %d\n", v);
return 0;
}
example 2:
#include <stdio.h>
#include <string.h>
char *longer(char *s1){
while(*s1){
*s1 = toupper(*s1);
*s1++;
}
}
int main ( void ){
char string[64];
printf("Type some text:");
fgets(string, 64, stdin);
printf("You typed:\n%s\n", string);
longer(string);
printf("If you were shouting, you'd typed: \n%s\n", string);
return 0;
}
example 3:
#include <stdio.h>
#include <string.h>
char *encrypt(char *input){
static char output[64];
int x=0;
while(*input){
if(isalpha(*input))
output[x]=*input+1;
else output[x]=*input;
x++;
input++;
}
return output;
}
int main ( void ){
char *instructions = "Deliver the package to Berlin";
printf("Here are your secret instructions:\n%s\n",encrypt(instructions));
return 0;
}
#include <stdio.h>
#include <string.h>
char *encrypt(char *input){
static char output[64];
int x=0;
while(*input){
if(isalpha(*input))
output[x]=*input+1;
else output[x]=*input;
x++;
input++;
}
return output;
}
int main ( void ){
char *instructions = "Deliver the package to Berlin";
printf("Here are your secret instructions:\n%s\n",encrypt(instructions));
return 0;
}
Linked List Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stats{
int account;
float balance;
struct stats *next;
};
void fill_structure(struct stats *s);
struct stats *create(void);
int main ( void ){
struct stats *first;
struct stats *current;
struct stats *new;
int x;
first = create();
current = first;
for(x = 0; x<5; x++){
if(x==0){
first=create();
current=first;
}
else {
new = create();
current->next = new;
current = new;
}
fill_structure(current);
}
current->next=NULL;
current = first;
while(current){
printf("Account %05d:\t$%0.2f\n",current->account, current->balance);
current=current->next;
}
return 0;
}
void fill_structure(struct stats *s){
printf("Account number:");
scanf("%d", &s->account);
printf("Balance: $");
scanf("%f", &s->balance);
s->next=NULL;
}
struct stats *create(void){
struct stats *baby;
baby = (struct stats *)malloc(sizeof(struct stats));
if(baby ==NULL){
puts("Memory error");
exit(1);
}
return baby;
};
#include <stdlib.h>
#include <string.h>
struct stats{
int account;
float balance;
struct stats *next;
};
void fill_structure(struct stats *s);
struct stats *create(void);
int main ( void ){
struct stats *first;
struct stats *current;
struct stats *new;
int x;
first = create();
current = first;
for(x = 0; x<5; x++){
if(x==0){
first=create();
current=first;
}
else {
new = create();
current->next = new;
current = new;
}
fill_structure(current);
}
current->next=NULL;
current = first;
while(current){
printf("Account %05d:\t$%0.2f\n",current->account, current->balance);
current=current->next;
}
return 0;
}
void fill_structure(struct stats *s){
printf("Account number:");
scanf("%d", &s->account);
printf("Balance: $");
scanf("%f", &s->balance);
s->next=NULL;
}
struct stats *create(void){
struct stats *baby;
baby = (struct stats *)malloc(sizeof(struct stats));
if(baby ==NULL){
puts("Memory error");
exit(1);
}
return baby;
};
No comments:
Post a Comment