C/C++ Programming

 What is c/c++ ?

                                 A high-level programming language developed by Dennis Ritchie at Bell Labs in the mid 1970s. Although originally designed as a systems programming language, C has proved to be a powerful and flexible language that can be used for a variety of applications, from business programs to engineering. C is a particularly popular language for personal computer programmers because it is relatively small -- it requires less memory than other languages.
The first major program written in C was the UNIX operating system, and for many years C was considered to be inextricably linked with UNIX. Now, however, C is an important language independent of UNIX.
Although it is a high-level language, C is much closer to assembly language than are most other high-level languages. This closeness to the underlying machine language allows C programmers to write very efficient code. The low-level nature of C, however, can make the language difficult to use for some types of applications.




 Learn C programing language by just downloading file from following links:-


1)PDF file 



  
                                                                                  Download

2) PPT file



3) Video links
    
                    video-1 (Theory)      video-2 (Theory)       video-3(Practical)



Examples



Example 1 - C hello world program
/* A very simple c program printing a string on screen*/


#include <stdio.h>
 
main()
{
    printf("Hello World\n");
    return 0;
}

Output of above program:
"Hello World"



 Example 2 - c program to take input from user using scanf

#include <stdio.h>
 
main()
{
   int number;
 
   printf("Enter an integer\n");
   scanf("%d",&number);
 
   printf("Integer entered by you is %d\n", number);
 
   return 0;
}

Output:
Enter a number
5
Number entered by you is 5


Example 3 - using if else control instructions

#include <stdio.h>
 
main()
{
   int x = 1;
 
   if ( x == 1 )
      printf("x is equal to one.\n");
   else
      printf("For comparison use == as = is the assignment operator.\n");
 
   return 0;
}

Output:
x is equal to one.


Example 4 - loop example
#include <stdio.h>
 
main()
{
   int value = 1;
 
   while(value<=3)
   {
      printf("Value is %d\n", value);
      value++;
   }
 
   return 0;
}
Output:
Value is 1
Value is 2
Value is 3



Example 5 - c program for prime number

#include <stdio.h>
 
main()
{
   int n, c;
 
   printf("Enter a number\n");
   scanf("%d", &n);
 
   if ( n == 2 )
      printf("Prime number.\n");
   else
   {
       for ( c = 2 ; c <= n - 1 ; c++ )
       {
           if ( n % c == 0 )
              break;
       }
       if ( c != n )
          printf("Not prime.\n");
       else
          printf("Prime number.\n");
   }
   return 0;
}


Example 6 - command line arguments

#include <stdio.h>
 
main(int argc, char *argv[])
{
   int c;
 
   printf("Number of command line arguments passed: %d\n", argc);
 
   for ( c = 0 ; c < argc ; c++)
      printf("%d. Command line argument passed is %s\n", c+1, argv[c]);
 
   return 0;
}

Above c program prints the number and all arguments which are passed to it.


Example 7 - Array program

#include <stdio.h>
 
main() 
{
    int array[100], n, c;
 
    printf("Enter the number of elements in array\n");
    scanf("%d", &n);
 
    printf("Enter %d elements\n", n);
 
    for ( c = 0 ; c < n ; c++ ) 
        scanf("%d", &array[c]);
 
    printf("Array elements entered by you are:\n");
 
    for ( c = 0 ; c < n ; c++ ) 
        printf("array[%d] = %d\n", c, array[c]);
 
    return 0;
}


Example 8 - function program

#include <stdio.h>
 
void my_function();
 
main()
{
   printf("Main function.\n");
 
   my_function();
 
   printf("Back in function main.\n");
 
   return 0;
}
 
void my_function()
{
   printf("Welcome to my function. Feel at home.\n");
}

Example 9 - Using comments in a program


#include <stdio.h>
 
main()
{
   // Single line comment in c source code
 
   printf("Writing comments is very useful.\n");
 
   /*
    * Multi line comment syntax
    * Comments help us to understand code later easily.
    * Will you write comments while developing programs ?
    */
 
   printf("Good luck c programmer.\n"); 
 
   return 0;
}


Example 10 - using structures in c programming

#include <stdio.h>
 
struct programming
{
    float constant;
    char *pointer;
};
 
main()
{
   struct programming variable;
   char string[] = "Programming in Software Development.";   
 
   variable.constant = 1.23;
   variable.pointer = string;
 
   printf("%f\n", variable.constant);
   printf("%s\n", variable.pointer);
 
   return 0;
}



Example 11 - c program for Fibonacci series

#include <stdio.h>
 
main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else              
      {
         next = first + second;
         first = second;
         second = next;
      }                                                                         
      printf("%d\n",next);
   }
 
   return 0;
}


Stack

// WAP to implement stack with all operations using array(using Global scope that is Array & Top variable are outside the main.
#include<stdio.h>
#include<stdlib.h> #define MAX 15
int a[MAX],top=-1; main()
{
int ch, n;
void push(int); void display(); void pop(); void peep(); void change();
printf("---------------------Stack Implementation using Array:----------------\n"); printf("Enter size of the stack(Maximum 15):");
scanf("%d",&n);
do
{
printf("1. Push\n2. Pop\n3. Peep\n4. Change\n5. Display\n6. Exit\n"); printf("Enter your choice:");
scanf("%d", &ch); switch(ch)
{
case 1:
push(n);
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
change();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("Wrong choice\n");
}
}while(ch!=6);
}
void push(int n)
{
int x;
if(top >= n-1)
{
printf("-----------------Stack is overflow.-----------------------\n");
return;
}
top=top+1;
printf("Enter an element:");
scanf("%d",&x);
a[top]=x;
printf("-------------------The element %d is pushed successfully at a[%d].---------------\n",x,top);
}
void pop()
{
if(top == -1)
{
printf("---------------------Stack is underflow------------------------\n");
return;
}
top = top-1;
printf("Popped element is %d\n", a[top+1]);
}
void peep()
{
int pos;
printf("Enter pos:");
scanf("%d", &pos);
if(top-pos+1 < 0)
{
printf("--------------------Stack is underflow--------------------\n");
return;
}
printf("------------------a[%d] = %d from Top.--------------\n",top-pos+1,a[top-pos+1]);
}
void change()
{
int pos,x;
printf("Enter pos:");
scanf("%d", &pos);
if(top-pos+1 < 0)
{
printf("------------------Stack is underflow--------------------\n");
return;
}
printf("New value for element:");
scanf("%d", &x);
a[top-pos+1]=x;
printf("-----------------Value is changed: a[%d] = %d----------------\n",top-pos+1,a[top-pos+1);
}
void display()
{
int i;
if(top==-1)
{
printf("---------------Stack is empty----------------\n");
return;
}
printf("Stack contains:------------\n");
for(i=0;i<=top;i++)
printf("a[%d] = %d\n", i,a[i]);
printf("------------------\n");
}

Example 2:

Program to implement stack with all operations using array(using Local scope that is Array & Top variable are inside the main.
#include<stdio.h>
#include<stdlib.h> #define MAX 15 main()
{
int a[MAX],i,ch,top=-1,n;
void push(int temp[],int *,int); void display(int temp[], int *); void pop(int temp[], int *); void peep(int temp[], int *); void change(int temp[], int *);
printf("---------------------Stack Implementation using Array:----------------\n"); printf("Enter size of the stack(Maximum 15):");
scanf("%d",&n);
do
{
printf("1. Push\n2. Pop\n3. Peep\n4. Change\n5. Display\n6. Exit\n"); printf("Enter your choice:");
scanf("%d", &ch); switch(ch)
{
case 1:
push(a,&top,n);
break;
case 2:
pop(a,&top);
break;
case 3:
peep(a,&top);
break;
case 4:
change(a,&top);
break;
case 5:
display(a,&top);
break;
case 6:
exit(0);
default:
printf("Wrong choice\n");
}
}while(ch!=6); }
void push(int temp[], int *top, int n)
{
int x;
if(*top >= n-1)
{
printf("-----------------Stack is overflow.-----------------------\n");
return;
}
*top=*top+1;
printf("Enter an element:");
scanf("%d",&x);
temp[*top]=x;
printf("-------------------The element %d is pushed successfully at a[%d].---------------\n",x,*top);
}
void pop(int temp[], int *top)
{
if(*top == -1)
{
printf("---------------------Stack is underflow------------------------\n");
return;
}
*top = *top-1;
printf("Popped element is %d\n",temp[*top+1]);
}
void peep(int temp[], int *top)
{
int pos;
printf("Enter pos:");
scanf("%d", &pos);
if(*top-pos+1 < 0)
{
printf("--------------------Stack is underflow--------------------\n");return;
}
printf("------------------a[%d] = %d from Top.--------------\n",*top-pos+1,temp[*top-pos+1]);
}
void change(int temp[], int *top)
{
int pos,x; 
printf("Enter pos:"); 
scanf("%d", &pos);
if(*top-pos+1< 0)
{
printf("------------------Stack is underflow--------------------\n");
return;
}
printf("New value for element:");
scanf("%d",&x);
temp[*top-pos+1]=x;
printf("-----------------Value is changed: a[%d] = %d----------------\n",*top-pos+1,temp[*top-pos+1]);
}
void display(int temp[], int *top)
{
int i;
if(*top==-1)
{
printf("---------------Stack is empty----------------\n");
return;
}
printf("Stack contains:------------\n");
for(i=0;i<=*top;i++)
printf("a[%d] = %d\n", i,temp[i]);
printf("------------------\n");
}
                                              


No comments:

Post a Comment