Posts

Pointers to Structures in c

#include <stdio.h> #include <string.h> struct Books {    char  title[50];    char  author[50];    char  subject[100];    int   book_id; }; /* function declaration */ void printBook( struct Books book ); int main( ) {    struct Books Book1;        /* Declare Book1 of type Book */    struct Books Book2;        /* Declare Book2 of type Book */    /* book 1 specification */    strcpy( Book1.title, "C Programming");    strcpy( Book1.author, "ramratan bissoo");    strcpy( Book1.subject, "C Programming Tutorial");    Book1.book_id = 654123;    /* book 2 specification */    strcpy( Book2.title, "Miracle Billing");    strcpy( Book2.author, "DEEPAK SHARMA");    strcpy( Book2.subject, "Miracle Billing Tutorial");    Book2.book_id = 420420;    /* print Book1 info */    printBook( Book1 );    /* Print Book2 info */    printBook( Book2 );    return 0; } void printBook( struct Books *book )

Structures as Function Arguments

#include <stdio.h> #include <string.h> struct Books {    char  title[50];    char  author[50];    char  subject[100];    int   book_id; }; /* function declaration */ void printBook( struct Books book ); int main( ) {    struct Books Book1;        /* Declare Book1 of type Book */    struct Books Book2;        /* Declare Book2 of type Book */    /* book 1 specification */    strcpy( Book1.title, "C Programming");    strcpy( Book1.author, "ramratan bissoo");    strcpy( Book1.subject, "C Programming Tutorial");    Book1.book_id = 654123;    /* book 2 specification */    strcpy( Book2.title, "Miracle Billing");    strcpy( Book2.author, "DEEPAK SHARMA");    strcpy( Book2.subject, "Miracle Billing Tutorial");    Book2.book_id = 420420;    /* print Book1 info */    printBook( Book1 );    /* Print Book2 info */    printBook( Book2 );    return 0; } void printBook( struct Books book )

string copy in c

#include <stdio.h> #include <string.h> int main () {    char str1[12] = "Hello";    char str2[12] = "World";    char str3[12];    int  len ;    /* copy str1 into str3 */    strcpy(str3, str1);    printf("strcpy( str3, str1) :  %s\n", str3 );    /* concatenates str1 and str2 */    strcat( str1, str2);    printf("strcat( str1, str2):   %s\n", str1 );    /* total lenghth of str1 after concatenation */    len = strlen(str1);    printf("strlen(str1) :  %d\n", len );    return 0; } output:-- strcpy( str3, str1) :  Hello strcat( str1, str2):   HelloWorld strlen(str1) :  10

The static Storage Class

#include <stdio.h> /* function declaration */ void func(void); static int count = 5; /* global variable */ main() {    while(count--) {       func();    }    return 0; } /* function definition */ void func( void ) {    static int i = 5; /* local static variable */    i++;    printf("i is %d and count is %d\n", i, count); } output is i is 6 and count is 4 i is 7 and count is 3 i is 8 and count is 2 i is 9 and count is 1 i is 10 and count is 0

Global Variables

#include <stdio.h> /* global variable declaration */ int g; int main () {   /* local variable declaration */   int a, b;   a = 10;   b = 20;   g = a + b;   printf ("value of a = %d, b = %d and g = %d\n", a, b, g);   return 0; } OUTPUT:- value of a = 10, b = 20 and g = 30

Local Variables

#include <stdio.h> int main () {   /* local variable declaration */   int a, b;   int c;   /* actual initialization */   a = 10;   b = 20;   c = a + b;   printf ("value of a = %d, b = %d and c = %d\n", a, b, c);   return 0; } OUTPUT:-- value of a = 10, b = 20 and c = 30

Calling a Function

#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () {    /* local variable definition */    int a = 100;    int b = 200;    int ret;    /* calling a function to get max value */    ret = max(a, b);    printf( "Max value is : %d\n", ret );    return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) {    /* local variable declaration */    int result;    if (num1 > num2)       result = num1;    else       result = num2;    return result; } output:-- Max value is : 200