C Programming Tutorial




C Programming Tutorial

What is C programming language

C is a programming language. It was developed at AT & T's Bell Laboratories of USA in 1972. It was designed and written by Dennis Ritchie.

(F2) : - To save the file
(.c): - It is the extension file name of C.
(.cpp) : - It is the extension file name of C++
(;) : - Line terminator
(ctrl + F9) : - To compile the program
(ctrl + F5) : - To run the program


#include<stdio.h> : It is the header file of printf() and scanf().
#include<conio.h> : It is the header file of clrscr().

Use of printf() : printf function is used to print the massage on the screen.

Use of scanf() : scanf function is used to input the data through  the keyboard.

Use of clrscr() : clrscr function is used to clear the screen.



A simple example of  c program
#include<stdio.h>
#inculde<conio.h>
main()
{
clrscr();
printf("Hello world");
}


Output :- Hello world



Second example of  c program
#include<stdio.h>
#inculde<conio.h>
main()
{
clrscr();
printf("Hello world");
printf("Hello India");
}


Output :- Hello worldHello India


Scope Sequence Character In C Language

\n :- New line
\t :- Horizontal tab
\v :- Vertical tab
\b :- Back space
\F :- Form feed


Example of  \n (back slash n) in c language
#include<stdio.h>
#inculde<conio.h>
main()
{
clrscr();
printf("Hello world");
printf("\nHello India");
}


Output :- Hello world
                 Hello India


Example of  \t (back slash t) in c language
#include<stdio.h>
#inculde<conio.h>
main()
{
clrscr();
printf("Hello world");
printf("\tHello India");
}


Output :- Hello world
                                     Hello India





Conversion Specifier

Conversion specifier is used by printf function to decide which data type of value is going to print on the screen.

   %d    int
%ffloat
%cchar
%cunsigned char
%uunsigned int
%dshort int
%uunsigned short int
%ldlong int
%luunsigned long int
%lfdouble
%Lflong double
%xhexadecimal
%ooctal

Data Type: 

There are three types of data type

  1. Basic data type
  2. Drive data type
  3. User define data type

Basic Data Type

Data typeRangeBytesFormat
signed char -128 to +127    1    %c
unsigned char 0 to 255   1    %c
short signed int -32768 to +32767    2    %d
short unsigned int 0 to 65535   2    %u
signed int -32768 to +32767    2       %d
unsigned int 0 to 65535   2    %u
long signed int -2147483648 to +2147483647    4    %ld
long unsigned int 0 to 4294967295   4    %lu
float -3.4e38 to +3.4e38   4    %f
double -1.7e308 to +1.7e308   8    %lf
long double -3.4e4932 to +3.4e4932   10    %Ld


Variable

A variable is a data name that may be used to store a data value. unlike constants that remain unchanged during the execution of a program. A variable make take different values at different times during execution.

Rules for variable declaration

  1. Variable name should be started with alphabet character or underscore.
  2. Special character like #, @ not allow to variable name.
  3. Variable should not contain any keyword.
  4. Variable name can be up to 32 character. 

Example 5 of c program

#include<stdio.h>
#include<conio.h>
main()
{
int x, y, z;
clrscr();
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number : ");
scanf("%d",&y);
printf("Enter third number : ");
scanf("%d",&z);
printf("Printing the numbers on the screen : ");
printf("\n%d",x);
printf("\n%d",y);
printf("\n%d",z);
}
getch();
}


Output: Enter first number : 4
               Enter second number : 8
               Enter third number : 6
               Printing the numbers on the screen
               4
               8
               6

Or

#include<stdio.h>
#include<conio.h>
main()
{
int x, y, z;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&x,&y,&z);
printf("Printing the numbers on the screen : ");
printf("\n%d\n%d\n%d",x,y,z);
}
getch();
}


Output: Enter three numbers : 4 3 8
               Printing the numbers on the screen
               4
               3
               8

There are 32 keywords available in c

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

Operators : 

Operators are the symbols that respect specific operations.
  1. Arithmetic operator :[ + , - , / , * and %(modules operator) ]
  2. Relational operator : [ < , > , <= , >= , == ]
  3. Assignment operator :[ = ]
  4. Conditional or Ternary operator : [ ?: ]
  5. Logical operator : [ &&(and) , ||(or) and !(not) ]
  6. Increment operator : [ + + ]
  7. Decrement operator : [ - - ]
Question 1: Write a program to input two integer number by keyboard and calculate sum of two numbers. 
Solution:
#include<stdio.h>
#include<conio.h>
main()
{
int num1, num2, sum;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
sum=num1+num2;
printf("Sum=%d",sum);
getch();
}

SHARE
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment