Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Monday, December 18, 2017

c Program in file input & output

File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage.


Advantage of File
It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium.


Functions for file handling
There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
No.
Function
Description
1
fopen()
opens new or existing file
2
fprintf()
write data into file
3
fscanf()
reads data from file
4
fputc()
writes a character into file
5
fgetc()
reads a character from file
6
fclose()
closes the file
7
fseek()
sets the file pointer to given position
8
fputw()
writes an integer to file
9
fgetw()
reads an integer from file
10
ftell()
returns current position
11
rewind()
sets the file pointer to the beginning of the file
ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode
  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file.txt""w");//opening file  
  5.    fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
  6.    fclose(fp);//closing file  
  7. }  

Friday, December 8, 2017

C program

The C Language is developed for creating system applications that direct interacts to the hardware devices such as drivers, kernals etc.
printf() function
The printf() function is used for output. It prints the given statement to the console.
1.    printf("format string",argument_list);  
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
1.    scanf("format string",argument_list); 


 A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:

type variable_name;

Rules for defining variables

  • A variable can have alphabets, digits and underscore.
  • A variable name can start with alphabet and underscore only. It can't start with digit.
  • No white space is allowed within variable name.
  • A variable name must not be any reserved word or keyword e.g. int, float etc.

Types of Variables in C

There are many types of variables in c:
  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable
TypesData Types
Basic Data Typeint, char, float, double
Derived Data Typearray, pointer, structure, union
Enumeration Data Typeenum
Void Data Typevoid

Basic Data Types

The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals.
The memory size of basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. Its size is given according to 32 bit architecture.
Data TypesMemory SizeRange
char1 byte−128 to 127
signed char1 byte−128 to 127
unsigned char1 byte0 to 255
short2 byte−32,768 to 32,767
signed short2 byte−32,768 to 32,767
unsigned short2 byte0 to 65,535
int2 byte−32,768 to 32,767
signed int2 byte−32,768 to 32,767
unsigned int2 byte0 to 65,535
short int2 byte−32,768 to 32,767
signed short int2 byte−32,768 to 32,767
unsigned short int2 byte0 to 65,535
long int4 byte-2,147,483,648 to 2,147,483,647
signed long int4 byte-2,147,483,648 to 2,147,483,647
unsigned long int4 byte0 to 4,294,967,295
float4 byte
double8 byte
long double10 byte
An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

Friday, April 21, 2017

c program



1. Write a Program to check the biggest number between three given integers.
2. Write a Program to check a given character is vowel or consonant?
3. Write a Program to check whether given number is prime or not?
4. Write a Program to check whether given number is perfect number?
5. Write a Program to display all even numbers from 1 to 50.
6. Write a Program to display all prime number from 1 to 100.

7. Write a Program to display the sum of digits of a given number.
8. Write a Program to display whether a given number is palindrome?
9. Write a Program to check whether a given number is armstrong or not?
10. Write a Program to display all the perfect numbers from 1 to 100.
11. Write a Program to display all the armstrong number from 100 to 200?
12.Write a Program to display the maximum and minimum number from a given 10 numbers.

Friday, December 30, 2016

Series

Write a program print series.
1. 1 3 6 10 15
2. 2 4 7 11  16



void main()
{
int i,j=1;//same java,c,c++
for(int i=1;i<=5;i++)
{
j=j+i;
printf("%d",j);//c program
System.out.println(j);//java
cout<<j;//c++
}


}
2.
void main()
{
int i,j=0;//same java,c,c++
for(int i=1;i<=5;i++)
{
j=j+i;
printf("%d",j);//c program
System.out.println(j);//java
cout<<j;//c++
}


}

Thursday, July 21, 2016

tree in c

#include<stdlib.h>
#include<stdio.h>

struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;

void insert(node ** tree, int val)
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->data = val;
        *tree = temp;
        return;
    }

    if(val < (*tree)->data)
    {
        insert(&(*tree)->left, val);
    }
    else if(val > (*tree)->data)
    {
        insert(&(*tree)->right, val);
    }

}

void print_preorder(node * tree)
{
    if (tree)
    {
        printf("%d\n",tree->data);
        print_preorder(tree->left);
        print_preorder(tree->right);
    }

}

void print_inorder(node * tree)
{
    if (tree)
    {
        print_inorder(tree->left);
        printf("%d\n",tree->data);
        print_inorder(tree->right);
    }
}

void print_postorder(node * tree)
{
    if (tree)
    {
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d\n",tree->data);
    }
}

void deltree(node * tree)
{
    if (tree)
    {
        deltree(tree->left);
        deltree(tree->right);
        free(tree);
    }
}

node* search(node ** tree, int val)
{
    if(!(*tree))
    {
        return NULL;
    }

    if(val < (*tree)->data)
    {
        search(&((*tree)->left), val);
    }
    else if(val > (*tree)->data)
    {
        search(&((*tree)->right), val);
    }
    else if(val == (*tree)->data)
    {
        return *tree;
    }
}

void main()
{
    node *root;
    node *tmp;
    //int i;

    root = NULL;
    /* Inserting nodes into tree */
    insert(&root, 9);
    insert(&root, 4);
    insert(&root, 15);
    insert(&root, 6);
    insert(&root, 12);
    insert(&root, 17);
    insert(&root, 2);

    /* Printing nodes of tree */
    printf("Pre Order Display\n");
    print_preorder(root);

    printf("In Order Display\n");
    print_inorder(root);

    printf("Post Order Display\n");
    print_postorder(root);

    /* Search node into tree */
    tmp = search(&root, 4);
    if (tmp)
    {
        printf("Searched node=%d\n", tmp->data);
    }
    else
    {
        printf("Data Not found in tree.\n");
    }

    /* Deleting all nodes of tree */
    deltree(root);
}

Tuesday, December 8, 2015

Introduction to C Language


C is one of the most popular computer language developed at AT&T’S Bell laboratories of USA in 1972 .It was developed by Dennis Ritchie. It is also called mid level language.
Today when the computer technology is on it's highest era and the advance programming languages like C++,C# and JAVA is very popular among developers many of us may think that C is now become less important. But this is not true since, C is still very popular and important language for it's following features.

Advantage Associated with C Language

  1. Simple to use and understand. You can later on migrate to C++, C# or java. Hence, preferring two-step learning process.
  2. C++,C# or java using oops technology that actually require core C language element.
  3. Operating system like window, Linux, UNIX is still written in C. And so if you are to extend any of above operating system to work with new devices you must need to write device driver program. And are exclusively written in C.
  4. C has best performance record. Because of highest execution among all other language (due to its verity of data type and powerful operator).
  5. Enormous electronic gadgets like mobile phone, digital camera, I-pod, microwaves, ovens, washing machine are using a microprocessor (chip) which contains an operating system and a program which are embedded in these devices which must have fast execution within limited amount of memory. And it is C who comes to show its smartness here.
  6. Professional 3D computer game like spaceship and firing bullet need speed to match player’s inputs and C is again hero here.
  7. Its structured, high level, machine independent language that provide programmer a freedom to portability facility.(One computer to another).
  8. C compilers combine the facility of an assembly language plus high level language. And hence capable to write both operating system and business pack. Many C compilers are itself written in C that is available in market.
  9. Another very important feature of C is its ability to extend itself .We can continuously add our own function to its library is convents.

Disadvantages Associated with C Language

  1. There is no strict type checking (for ex: we can pass an integer value for the floating data type).
  2. As the program extends it is very difficult to fix the bugs.
  3. There is no runtime checking.

Basic Structure of C program

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. -----------------------------------------------------------------
  6. main function section
  7. {
  8. //Declaration part
  9. //Executable part
  10. }
  11. -----------------------------------------------------------------
  12. Sub program section
  13. Function 1
  14. Function 2
  15. --
  16. -- //user defined function
  17. --
  18. Function n