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.
#include<stdio.h> : It is the header file of printf() and scanf().
#include<conio.h> : It is the header file of clrscr().
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.
#inculde<conio.h>
main()
{
clrscr();
printf("Hello world");
}
Output :- Hello world
(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
(.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 |
---|---|
%f | float |
%c | char |
%c | unsigned char |
%u | unsigned int |
%d | short int |
%u | unsigned short int |
%ld | long int |
%lu | unsigned long int |
%lf | double |
%Lf | long double |
%x | hexadecimal |
%o | octal |
Data Type:
There are three types of data type- Basic data type
- Drive data type
- User define data type
Basic Data Type
Data type | Range | Bytes | Format |
---|---|---|---|
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
- Variable name should be started with alphabet character or underscore.
- Special character like #, @ not allow to variable name.
- Variable should not contain any keyword.
- 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.
- Arithmetic operator :[ + , - , / , * and %(modules operator) ]
- Relational operator : [ < , > , <= , >= , == ]
- Assignment operator :[ = ]
- Conditional or Ternary operator : [ ?: ]
- Logical operator : [ &&(and) , ||(or) and !(not) ]
- Increment operator : [ + + ]
- 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();
}
0 comments:
Post a Comment