First C Program
======================================================================
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
a=10;
b=20;
c=a+b;
printf("%d",c);
getch();
}
======================================================================
Program Explanation
#include <stdio.h>
#include <conio.h>
are used to add required header files.
void main()
main function which tells the compiler for entry point of program
{ }
start and end of a block
int a,b,c;
declares thee variables (a,b,c) of type signed integer each of 16 bytes.
clrscr()
a function being used to clear the output screen. it works only when conio.h header file is included using #include
a=10;
b=20;
stores 10 in a and 20 in b
c=a+b;
adds value of a and b and stores it in c
printf("%d",c);
prints the value of c on the output screen. it is available only if stdio.h header file is included.
getch();
A function used to read a single character from keyboard but at here it is used to make the output static. the output screen generally displayed only for a fraction of second after execution. and then program terminates and c IDE is displayed again back. To prevent getch() is used because it waits until user doesn't press a key and in results it prevent termination of protram.
How to use printf
printf is used for formatted output. The general syntax of printf is
printf("control string",list of variables);
Control string consists of
Type Specifiers %d %f %c etc
Control Characters \n \r \t etc
General Text anything other is considered as general text
Let us create printf statement for the output (for above program)
30 is obtained by adding 10 and 20
Step 1.
Replace each numeric or character value coming from variable with corresponding type specifier.
the result will be
%d is obtained by adding %d and %d
becaues 30 comes from c, c is integer, , %d is type specifier for int
similar is the case for 10 and 20
now list of the variables will be c,a,b because first %d is for c , second is for a and third is for a. thus the final printf statement for the required output will be
printf("%d is obtained by adding %d and %d",c,a,b);
and the complete required program will be
======================================================================
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
a=10;
b=20;
c=a+b;
printf("%d is obtained by adding %d and %d",c,a,b);
getch();
}
======================================================================
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
a=10;
b=20;
c=a+b;
printf("%d is obtained by adding %d and %d",c,a,b);
getch();
}
======================================================================
control characters are used to control the output behavior
ie \n i use to print remaining text in new line.
the complete escape sequence is
Escape sequence
|
Hex value
|
Connotation
|
\a
|
07
|
Alarm (Beep, Bell)
|
\b
|
08
|
Backspace
|
\f
|
0C
|
Formfeed
|
\n
|
0A
|
Newline (Line Feed); see notes below
|
\r
|
0D
|
Carriage Return
|
\t
|
09
|
Horizontal Tab
|
\v
|
0B
|
Vertical Tab
|
\\
|
5C
|
Backslash
|
\'
|
27
|
Single quotation mark
|
\"
|
22
|
Double quotation mark
|
\?
|
3F
|
Question mark
|
\0
|
00
|
Null (string terminator)
|
printf("Hello\nWorld"); // will print World in next line
will print
Hello
World
---------------------------------------------
No comments:
Post a Comment