INTRODUCTION TO C PROGRAMMING:

 

 

C is a general-purpose, procedural computer programming language supporting structured programming. A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix. It was applied to re-implementing the kernel of the Unix operating system. During the 1980s, C gradually gained popularity. It has become one of the most widely used programming languages, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the ANSI since 1989 (ANSI C) and by the International Organization for Standardization (ISO). As of September 2020, C is the most popular programming language.

C is an imperative procedural language. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support. Despite its low-level capabilities, the language was designed to encourage cross-platform programming.

 Download Here  [PDF]












C Programming

Write a C program to input a no and check it negative or positive no.

void main()
{
int a;
printf("Enter a no.");
scanf("%d",&a);
if(a<0 br=""> printf("Negative No.");
}
else
{
printf("Positive No.");
}
getch();
}

Input: -5
Output: Negative No.

C Programming

C Programming Write a C program to input two nos and swap it without using third variable. void main()
{ int a,b;
printf("Enter two nos.");
scanf("%d%d",&a,&b);
printf("Before Swaped value a=%db=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("Swaped value a=%db=%d",a,b);
getch();
}

Input: 10 20

Output: Swaped value a=20 b=10

C Programming

C Programming  Write a C program to input two nos and swap it.

void main()
{
int a,b,c;
printf("Enter two nos.");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("Swaped value a=%db=%d",a,b);
getch();
}

Input:   10  20
Output  Swaped value a=20 b=10


Write a C program to input principal, rate and time(in year) and find simple interest and final amount.


void main()
{
float p,r,t,si,fa;
printf("Enter Principal amount, rate and time.");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
fa=p+si;
printf("Simple Interest=%f", si);
printf("Final Amount=%f", fa);
getch();
}

Input : 1200 2 3
Output: Simple interest=108
              Final Amount=1308

Program to input two nos and perform simple operation.

void main()
{
int a,b;
printf("Enter two nos.");
scanf("%d%d", &a,&b);
printf("\nAddition=%d",a+b);
printf("\nSubstraction=%d",a-b);
printf("\nMultiplication=%d",a*b);
printf("\nDivision=%d",a/b);
getch();
}

Input :   100   20
Output:  Addition=120
              Substraction=80
              Multiplication=2000
              Division=5

3. Write a C program to input two no and print the sum without using third variable

void main()
{
int a,b;      // variable declaration
printf("Enter two nos.");     // message to enter no.
scanf("%d%d",&a,&b);    // store no in separate variable a and b
printf("Sum=%d",a+b);  // add variable and also print with message
getch();                          // waits untill any key is pressed.
}

Input:   10  15
Output:  Sum=25


Write a C program to input two number and print the sum using third variable.

 void main()
{
int first,second,sum;   // Variable declaration

printf("Enter Two No.");
printf("%d%d",&first,&second);   // number stored in first and second variable
sum=first+second;                   // add two no
printf("Sum=%d",sum);          // print the addition with message
getch();                           // waits untill any key is pressed.
}


Input: 5    10
Output:   Sum=15 

                  C Programming
             Switch Statement in C

In switch statement we test the single constant variable or value with different case value and if matched value found then body of case is executed and terminated by break statement. As soon as break statement is executed the control return back from switch. And If no matched value found then default statement is executed and switch terminated.

Syntax

switch(expression) {

   case constant-expression  :
      statement(s);
      break; 
 
   case constant-expression  :
      statement(s);
      break; 
     default : 
   statement(s);
}
 
Example 
int main () {
   char vowel = 'b';
   switch(vowel) {
      case 'a' :
         printf("Vowel\n" );
         break;
      case 'e' : 
       printf("Vowel\n" );
         break;
         case 'i' :  
 printf("Vowel\n" );
         break; 
        case 'o' :
         printf("Vowel\n" );
         break;
      case 'u' :
         printf("Vowel\n" );
         break;
      default :
         printf("Consonant\n" );
   }
   }
 
Output: Consonant 
 
C Programming 

           C Programming

Nested if

If any statement is executed on the basis of multiple if condition is called nested if.

if((condition)&&(condition)&&(condition))
{
//body to execute
}


else if((condition)&&(condition)&&(condition))
{
//body to execute
}


else if((condition)&&(condition)&&(condition))
{
//body to execute
}

else if((condition)&&(condition)&&(condition))
{
//body to execute
}

......

else
{
//body to execute
}





















void main(){
int a,b,c;
printf("Enter three no.");
scanf("%d%d%d", &a,&b,&c);
if((a>b)&&(a>c))
{
printf("Greater no. is %d",a);
}
else if((b>a)&&(b>c))
{
printf("Greater no. is %d",b);
}
else
{
printf("Greater no. is %d", c);
}
getch();
}

Input:   20,15,30
Output: 30


               C Programming

If else-if ladder

The if else ladder statement in C programming language is used to test set of conditions in sequence. An if condition is tested only when all previous if conditions in if-else ladder is false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder. 

if(condition)
{
//body to execute
}


else if(condition)
{
//body to execute
}


else if(condition)
{
//body to execute
}

else if(condition)
{
//body to execute
}

......

else
{
// body to execute
}


Fig. If else if ladder



Example:

Void main()
{
int num;
printf("Enter a no.:");
scanf("%d",&num);

if(num<=100)
{
printf("No is smaller than or equal to 100");

}
else if(num<=200)
{
printf("No is smaller than or equal to 200");
}
else if(num<=300){
printf("No is smaller than or equal to 300");
}
else if(num<=400){
printf("No is  smaller than or equal to 400");
}
else if(num<=500)
{
printf("No is  smaller than or equal to 500");
}
else
{
 printf("No is greater than 500")
 }

getch();
 

}

Input:    Enter a no.:  600
Output : No is greater than 500

c programming


if else

If we want to execute the particular statement on the basis of certain condition, called if else statement.

Syntax

    if(condition)
{

//body to execute

}

else

{

//body to execute

}

Example

C program to input a no and check negative no. or positive no./find nature.

void  main()
{
int num;
printf("Enter a No.\n");
scanf("%d\n",&num);
if(num>0)
{
printf("Number is Positive");
}
else
{
printf("Number is Negative");
}

getch();
}

Input :       -5
Output:     Number is  Negative







                 C Programming
Simple if Statement
The statement which executes on the basis of certain condition is called simple if statement.













Syntax.

  if(conditon)
{
//body to execute
}

Example:

void main()
{
int a=10;
if (a%2==0)
printf("No is even");
}

O/p:  No is even


C Programming
Control Statement

The statement which control the flow of control is called control statement.

   C Provides following types of control statement-

1. Selective Statement or Conditional Statement.
2. Iterative Statement or Repetitive Statement or Loop.

3. Jumping Statement.

1. Conditional Statement

The statement which executes on the basis of certain condition is called conditional statement.

 C provides following types of conditional statement:-

1. Simple if
2. if else statement
3. Nested if
4. if else if ladder

1. Iterative Statement

The statement which executes no of time on the basis of certain condition is called iterative statement.


C Programming

Constants in C

A constant variable is such type of variable that's value can't be changed during execution of the program.

Constants in C

ConstantExample
Decimal Constant10, 20, 450 etc.
Real or Floating-point Constant10.3, 20.2, 450.6 etc.
Octal Constant021, 033, 046 etc.
Hexadecimal Constant0x2a, 0x7b, 0xaa etc.
Character Constant'a', 'b', 'x' etc.
String Constant"c", "c program", "c in javatpoint" etc.

 

Example

void main()

{

const int a=10;

printf("%d", a);

a=a+10;     // Value of "a" cannot be changed.

}

O/p.-   Compile Error.

We can define constant with two different mechanism-

1. Variable Constant

2. Symbolic Constant 

Variable Constant 

If any declare  any variable with const keyword is called variable constant. 

#include <stdio.h>   

#include <conio.h>

 

void main()

{

const int a=10;

printf("Constant value %d", a);

}

O/p:  Constant value 10

C Symbolic Constant or #define preprocessor 

The variable which is defined with the help of # define preprocessor is called symbolic constant.

#include <stdio.h>   

#define PI 3.14   

void main()

{

 printf("Value of PI is=%f",PI); 

 } 

O/p: Value of PI is= 3.14

 




Fig:- Precedence of Operators



Precedence of Operators in C

















































 



C Programming

C Operators

An operator is the symbol that take one or more than one operand and perform operation on it.

Ex.   int a,b, c;

     a=10,b=20;

      c=a+b;   //   Here a, b is operand and + is operator


C provides following types of operators to perform different types of operations:
  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

C Programming

Keywords in C

A keyword is a reserved word that has special meaning. We cannot redefine the keyword or cannot force to change the naute. Keywords cannot be used as the variable name, constant name etc. There are only 32 reserved words for keywords in C language.

Following are 32 keyword used in C:-

autobreakcasecharconstcontinuedefaultdo
double else enum extern floatforgotoif
int longregisterreturnshortsignedsizeofstatic
structswitchtypedefunionunsignedvoidvolatilewhile


C Programming
Simple Operations in C

void main()
{
int a,b;

clrscr();

printf("Enter Two No.");


scanf("%d%d",&a,&b);

printf("\nsum=%d", a+b);

printf("\nSubs=%d",a-b);

 printf("\nMulti=%d", a*b);

printf("\nDivision=%d",a/b);

getch();

}

Note:
  1. a, b is integer variable which will store integer value range -32768- +32767
  2. clrscr() is the function which wiil clear the screen and set the cursor at first position on the screen.
  3. printf() is the function that display :-  Enter Two No.
  4. scanf() store the value that we give on console in the respective variable i.e.- a,b
  5. printf("\nsum=%d", a+b) -> "\n" change the line and set cursor at new line. "sum=" is the message and a+b add the value of a and b and also print it.
  6. getch() waits untill we press.

C Programming

Variables in C

variable is a name of memory location that is used to store the data. We can change its value and reuse it.

 Example. int a;  // a is the variable of type int.
                 flaot b; // b is the variable of type float.

Rules to Declare  Variable

  1. Variable may be combination of digit(0-9), alphabet(a-z, A-Z) and under score(_).
  2. Variable name does not contain any blank space.
  3. Does not start with digit.
  4. Does not contain any reserve keyword.
Example:

    int abc;          // Right
    int a2bc;        // Right
    int 2abc;        // Wrong,   Does not start with digit
    int _abc;        // Right
    int a bc;         // Wrong,  Does not contain blank/white space
    int float;        //Wrong,    Variable name must not be reserve keyword

Types of Variable 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