In this program the user enters ten values, and they are arranged in ascending order.Here is my code
#include<stdio.h>
#include<conio.h>
int main()
{
int i,k,t;
int array1[10];
printf("type numbers\n");
for(i=0;i<=9;i++)
scanf("%d",&array1[i]);
Showing posts with label c tutorial. Show all posts
Showing posts with label c tutorial. Show all posts
Switch case syntax
Reference > Syntax of Switch - case statements
switch(variable)
{
case value:
your code here
case value:
your code here
default:
your code here
}
if first of the case is true all the cases bellow get executed to prevent this we use break;
Labels:
C Language,
c tutorial,
reference,
switch,
syntax
Do while loop
Reference > Syntax of do while loop
do{
your code
}while(condition);
Labels:
c tutorial,
cs injection,
loop,
reference,
syntax
While loop
Reference > Syntax of while loops in c and c++
while(condition)
{
code
}
while(condition)
{
code
}
Labels:
c tutorial,
loop,
reference,
syntax
Introduction to Functions in programming
What are functions ?
Functions are set of instructions combined in one placefor example we have been working with function main
void main()
{
}
like this we can create different kind of functions to make our code easy to understand, and we can call function as many times as required
Example:
#include<stdio.h>
#include<conio.h>
void testing(); /*function prototype declaration*/
void main()
{
printf("we are in main body\n");
testing(); /*calling function*/
printf("welcome back to main function");
}
void testing() /*defination of your function*/
{
printf("you are now in testing, see how control flows");
}
How it works
1. Basically We first tell that void testing(); just like int a2. then in main function we write testing(); this calls the fuction testing and controls goes to void testing() and statements in get executed. and you are now in testing is displayed on screen
3.after this the control gets back to the main body and you get on display welcome back to main.
Labels:
C Language,
c tutorial,
cs injection,
csinjection,
functions
what are escape sequences explaination with examples
\n newline
\t tab
\b backspace
use of \n is very common in c programming. let us see how it works
{
printf("hello");
printf("how are you");
}
when we run this the display will be as
hellohow are you
to overcome this we use format specifier \n which means newline
{
printf("hello\n");
printf("how are you\n");
}
when we run this the display will be as
hello
how are you
\t tab
\b backspace
use of \n is very common in c programming. let us see how it works
{
printf("hello");
printf("how are you");
}
when we run this the display will be as
hellohow are you
to overcome this we use format specifier \n which means newline
{
printf("hello\n");
printf("how are you\n");
}
when we run this the display will be as
hello
how are you
What is format specifier in C %d %c etc
%d %f %c are called format specifiers
%d is used in printf() and scanf() when we know that the value is integer ( int).
%f is used in printf() and scanf() when we know that the value is real (float) .
%c is used in printf() and scanf() when we know that the value is charcter (char)
let us look at this program
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
printf("hmm the value is a ");
getche();
}
the display will be
hmm the value is a
So to overcome this problem and display the real value stored in a ? here we use format specifier
as a is integer type (int) so we will use %d
in above code write this
printf("hmm the value is %d",a);
now run the program and you will fulfill you purpose of displaying the value stored in a
display will be hmm the value is 10
%d is used in printf() and scanf() when we know that the value is integer ( int).
%f is used in printf() and scanf() when we know that the value is real (float) .
%c is used in printf() and scanf() when we know that the value is charcter (char)
Why we use format specifiers ?
If we want to display some value stored in variable a how will we show it ?let us look at this program
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
printf("hmm the value is a ");
getche();
}
the display will be
hmm the value is a
So to overcome this problem and display the real value stored in a ? here we use format specifier
as a is integer type (int) so we will use %d
in above code write this
printf("hmm the value is %d",a);
now run the program and you will fulfill you purpose of displaying the value stored in a
display will be hmm the value is 10
Use of Scanf in C and a program to add two numbers
What is the use of scanf and how it is used ?
scanf is used to take input value from the user and store it in a variable.you will understand its use from this example.
#include<conio.h>
void main()
{
int num1;
int num2;
int ans;
printf("please enter the value of number 1\n");
scanf("%d",&num1);
printf("please enter the value of number 2\n");
scanf("%d",&num2);
ans=num1+num2;
printf("the answer is %d",ans);
getche();
} press ctrl+ F9 to get display
2: first printf will diplay on screen please enter the value of number 1 and \n is called escape sequence
3: in scanf %d tells that the type of value is integer type and &num1 means that the value entered by the user will be stored in variable num1. %d is known as format specifier
4.second printf and scanf acts same, &num2 means that the value entered by user will be stored in num2 variable.
5.ans=num1+num2 means that both values stored in num1 and num 2 will be added and it will be stored in ans
6 in last printf() %d will get its value which is stored in ans. and it will be displayed on screen
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the values of a,b\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("the answer is %d",c);
getche();
}
another way to write this program is
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter the values of a,b\n");
scanf("%d %d",&a,&b);
printf("the answer is %d",a+b);
getche();
}
Note:Program can be written in different styles, never copy code of anyone. Just understand the logic of program and then make it with your own mind.
scanf is used to take input value from the user and store it in a variable.you will understand its use from this example.
Q. Write a program that gets two value from the user and adds them
solution:
#include<stdio.h>#include<conio.h>
void main()
{
int num1;
int num2;
int ans;
printf("please enter the value of number 1\n");
scanf("%d",&num1);
printf("please enter the value of number 2\n");
scanf("%d",&num2);
ans=num1+num2;
printf("the answer is %d",ans);
getche();
} press ctrl+ F9 to get display
How this program works? lets see
1: we told that num1 ,num2 and ans are variables which will store integer constants by writing int behind them2: first printf will diplay on screen please enter the value of number 1 and \n is called escape sequence
3: in scanf %d tells that the type of value is integer type and &num1 means that the value entered by the user will be stored in variable num1. %d is known as format specifier
4.second printf and scanf acts same, &num2 means that the value entered by user will be stored in num2 variable.
5.ans=num1+num2 means that both values stored in num1 and num 2 will be added and it will be stored in ans
6 in last printf() %d will get its value which is stored in ans. and it will be displayed on screen
Variations in this program
this program can also be made like this#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the values of a,b\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("the answer is %d",c);
getche();
}
another way to write this program is
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter the values of a,b\n");
scanf("%d %d",&a,&b);
printf("the answer is %d",a+b);
getche();
}
Note:Program can be written in different styles, never copy code of anyone. Just understand the logic of program and then make it with your own mind.
Labels:
Basics in C programming,
C Language,
c tutorial,
cs injection,
csinjection,
scanf
What are Constants and Variables in C programming
Constants
in c language constants and variables are frequently used.lets suppose
x=10
here x is a variable and 10 is constant.
mean that x can be assigned different value and x is variable.
Real Life example:
Suppose you have a blank Page and you write your name on it, now here that Page is variable and your name is constant because your name John will always remain constant. Now if you write your best friends name AJ Jadoon on that page.Aj Jadoon will be Constant because it cannot be changed but page is Variable.Constant is that thing which cannot be changed 10 Will remain always 10.
Variable is that thing whose value can be changed. x can be assigned a value 1,2,3 or whatever you want.
Types of Constants in C programming
There are two types of constants in C language we will focus on Primary constants only1. Primary constants
Integer constants
In integer constants we cannot use the numbers which have DECIMAL ( . )
and in c language we use ( int ) to declare integer constant.
Example int x=10
its range is from -32768 to 32767
Real constants
Real constants are those which contain Decimals. in c programming we use word float to declare real constant.
Example float x=10.22
Character Constant
Character constants are those which can store different characters in them like + , - , H etc.
we use word char to declare character constants.
example char x='A'
or
char x='+'
Note: character constant can only contain single alphabet,digit or symbol and it must be enclosed in single inverted comma.for example '+' , 'h', '*' etc
You will get familiar with their use in next examples
Labels:
ajjadoon,
Basics in C programming,
C Language,
c tutorial,
constants,
csinjection,
variables
Make First Program in C language.Step by Step guide to use Turbo C++ for first time
Hi this is our first program In C Language and it is simple and easy.
now press ctrl+F9 and you are done .congratulations. you have made your first program successfully
for example: printf("hello")
and it will be displayed as it is. The basic function of Printf() is to display what you type between double quotes " "
Statement terminator: Semi colon ; terminates the statement of your program and it is known as statement terminator
Delimiters { }: Delimiters tells us that where the body of function begins and where it ends. example in above program this " { " tells that the body of function main is starting and this " } " tells us that here it ends.
Header Files #include<stdio.h> and #<conio.h> : stdio.h allows us to use important standard predefined functions such as printf() scanf() and conio.h allows us to use some other important functions like clrscr() to clear screen.
Step1.Open your turbo c++ compiler .
Step2. Click on window then click on close all.

Step3. On top Right Click on File. then click on New.
Step 4. You are ready to make your first program in C.
Type this in your Turbo c++
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“hi my name is AJ and this
is my first program”);
getche();
}
now press ctrl+F9 and you are done .congratulations. you have made your first program successfully
Variations in above program:
In above program whatever you want to display on screen just type it between printf(" ").for example: printf("hello")
and it will be displayed as it is. The basic function of Printf() is to display what you type between double quotes " "
Statement terminator: Semi colon ; terminates the statement of your program and it is known as statement terminator
Delimiters { }: Delimiters tells us that where the body of function begins and where it ends. example in above program this " { " tells that the body of function main is starting and this " } " tells us that here it ends.
Header Files #include<stdio.h> and #<conio.h> : stdio.h allows us to use important standard predefined functions such as printf() scanf() and conio.h allows us to use some other important functions like clrscr() to clear screen.
Note: C is case sensitive language if you type capital "P" in printf it will give error or if you forget to type semi colon ; in this case it will also give error. be careful when u do coding and follow its rules
Subscribe to:
Posts (Atom)