Saturday 17 March 2018

Structure in c programming


Structure in c programming:-

C supports following program structure

  1. Preprocessor Commands
  2. Functions
  3. Variables
  4. Statements & Expressions
  5. Comments

Lets start with simple code :-


        #include<stdio.h>

int main()
{
    printf("Hello, Devesh\n");
    return 0;
}

Note:-

#include<stdio.h> :-It is  a standard for input / output, which allows us to use some commands which includes a file called stdio.h


int/void main():-It is the main function where program execution begins.Each C program must contain only one main function.

Curly braces:-Two curly brackets "{.......}" are used to group all statements in the program.

printf():- It is a function in C, which displays text on the screen.


return 0:- It terminates the main() function and returns the value 0.

For more info click here


Wednesday 14 March 2018

Storage Classes in Cprogramming

A storage class repsents  the scope  and life-time of variables which helps programmers to define a particular variable during program's runtime.
These storage classes are preceded by the data type  There are 4 types of  storage class.

  1. auto
  2. register
  3. static
  4. extern
1 Auto:-  By default it  is associated  with all local variables as its storage class
eg:-
int  number;
or
auto int number;

2. Register:- It  is used to define local variables which will  be stored in a register instead of RAM. This means that the variable has a maximum size which is equivalent to the register size .
 it is used  to make program execution faster.
eg:-
register int  counter;

3.Static::- The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program. Static storage class has its scope local to the function in which it is define,
The keyword used to define this storage class is static.
eg:-
static int var = 9

4.Extern:- Variables that are declared outside of all functions are known as external variables. External or global variables are accessible to any function.

    It can be said that an extern variable is a global variable which is assigned with a value that can be accessed and used within the entire program.
eg:-
#include <stdio.h>

   int val;
  extern void funcGlobal();

main() 
{
   val = 15;
   funcGlobal();
}
For more info click here



Friday 9 March 2018

Arguments in c programming

 Function Arguments :-

1.Call by value method:  In call by value method the actual arguments are copied to the formal arguments, so any operation performed by function on arguments doesn’t affect actual parameters.

eg:-


#include<stdio.h>

int addition(int num1, int num2);   /* function declaration */

int main()
{
      int result;   ;       /* local variable definition */ 
    int num1 = 14;
    int num2 = 6;      
   result = addition(num1,num2);   /* calling a function to get addition value */ 


    printf("The addition of two numbers is: %d\n",result);   
    return 0;
}

int addition(int a,int b)    /* function returning the addition of two numbers */
{
    return a + b;

}

Output:The addition of two numbers is:20

2. Call by reference method: In this method, address of actual arguments  is passed to the formal arguments, hence any operation performed on formal parameters will affects the value of actual arguments.

eg:-

#include<stdio.h>

int addition(int *num1, int *num2);  /* function declaration */

int main()
{
   int result ;     /* local variable definition */ 
    int num1 = 14;
    int num2 = 6;
    
      result = addition(&num1,&num2);  /* calling a function to get addition value */  

    printf("The addition of two numbers is: %d\n",result);
    return 0;
}

int addition(int *a,int *b)   /* function returning the addition of two numbers */
{
    return *a + *b;
}

output: The addition of two numbers is:20


For more info click here

Thursday 8 March 2018

function or method in c programming

Functions:-

A function is a set of statements that together perform some specific task. Every C program has at least one function, which is main(),
A function can also be known as a method
There are some built in function available in c programming such as printf() is a standard library function to display output to the screen . This function is defined in "stdio.h" header file.
There are other  library functions defined under "stdio.h", such as scanf(), fprintf(), getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.

User-defined functions

C allow programmers to define functions. Such functions created by the user are called user-defined functions.

eg:-

 #include <stdio.h>
void functionName()
{
    ... .. ...
    ... .. ...
}

int main()
{
    ... .. ...
    ... .. ...

    functionName();
 
    ... .. ...
    ... .. ...
}

The execution of a C program begins from the main() function.
When the compiler encounters functionName(); inside the main function, control of the program jumps to

 void functionName()
And, the compiler starts executing the codes inside the user-defined function.

Advantages of user-defined function
It is easy to understand ,debug and maintain.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules.Hence it is easy to understand

For more info click here

Variables in C programming with demo

Variables:-

Variables:-  It is to store data in memory,which determines the size and layout of the                       variable's memory. The value of variables  may change during execution.


Rules of Variables:-

A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
The first character must be a letter or underscore.
Blank spaces cannot be used in variable names.
Special characters  are not allowed.
C keywords cannot be used as variable names.
Variable names are case sensitive.
Values of the variables can be numeric or alphabetic.

Variable type can be char, int, float, double or void.


1.Int :- It is used to store integer values

 eg:- int a=5;
         int b=6;

2.Float:- It is used to store real number 0r decimal number

  eg:- float a=10.3;
          float b=11.3;

3.char:-It is used to store char value,The size of character variable is 1 byte.This is an integer type.
  eg:-  char test='v';

4. Void:-Repsents the absent type value.
For more info click here

keywords in C programming

  Keywords in C Programming:-



auto                  break           case                 char
const               continue       default               do
double             else              enum              extern
float                for              goto                  if
int                  long             register            return
short              signed         sizeof                static
struct            switch          typedef             union
unsigned        void           volatile              while

 It  is  a reserved words in C library and used to perform an internal operation

For more info click here

Data types in C programming

  Data types in c:-

                                                       
  C supports three types of data :-
  • Primary Data Types (Built-in):-
Integer types:-  
It  used to decleare both positive and negative values but no decimal v     alues. Example: 0, -6, 10
For example:
int a=3;

Floating type:-


Floating type variables can hold real numbers such as: 5.34, -8.382, 9.0 etc. 

You can declare a floating point variable  by using float or double keyword. 



For example:

float amount=10.5;
double  amount= 58.9;

Character type


Keyword char is used for declaring character type variables.

For example:

char demo = 'm';

Here, demo  is a character variable. The value of demo is 'm'.

The size of character variable is 1 byte.


  •  Derived types:-
         Arrays
           Pointers
          Structures
          Enumeration



Difference between float and double


The size of float  is 4 bytes. And the size of double is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.









For more info click here