Learn C: Printf – Displaying Text Data on Monitor
In C language ‘printf’ is the most using built-in function. Like the name printf, it is using for printing or displaying a text content on your output monitor. It is very simple to use printf function in C, not a complicated one.I think you got an idea about C language from our previous introduction.
Let us learn printf with an example:
This is a simple C program to display “Hello World”:
#include<stdio.h> #include <conio.h> void main() { printf (“Hello World”); getch(); } |
Above program is to display a text ‘Hello World’ (without any quotes).
Let us look each line of above program:
=> #include<stdio.h> is a header; it contains many functions like printf. Without this header, you cannot run your program correctly.
=> #include <conio.h> is another header file using to run bottom line ‘getch()’, it is used to display output until you press a key.
Both #include <conio.h> and getch() lines are not necessary in compilers than ‘Turbo C’. If you use these lines in another compiler, you may get errors. So, no need to write these lines in other compiler than Turbo C.
=> void main() is necessary in each C program, program will start execution from void main(). You may use only main() instead of void main even there is no return value. In Quincy C IDE if you write void main(), then program will not work, you have to write only main(). But Turbo C needs void main().
=> { [curly bracket or braze] is needed after main() block. and you have to close the braze ( } ) at end.
=> printf(“Hello world”); is indicate to print Hello World. Here printf is an inbuilt function on compiler to display text on monitor. It should be in between a bracket, printing text should be in between double quotes, and statement should be end with a semicolon (;).
Let us compile and Run (build) your first C program:
Method for compilation is different in different IDE.
These are the steps for Compiling and Running a program in Turbo C:
- Go to ‘Compile’ menu, from list, click on ‘Compile’.
- Now Turbo C will open a compiling status window, there you can see the number of errors and warnings in your program.
- If there is no error, then go to ‘Run’ menu and click on ‘Run’.
- Now you can see the output of your program (Hello World).
- Press any key for back to code.
In C program these line are necessary in all program..
#include<stdio.h> #include <conio.h> void main() { getch(); } |
You have to write your C codes inside the void main block
#include<stdio.h> #include <conio.h> void main() { <your code here> getch(); } |
Now let us write another program to show your name and place:
#include<stdio.h> #include <conio.h> void main() { printf (“Aslam Kakkove”); getch(); } |
Here output should be ‘Aslam Kakkove’. Here output is in a single line, so let us see how to display name in first line and place in second name:
- Just add ‘n’ for next line: (/n is a constant used in C)
Then display code becomes: printf (“Aslam n Kakkove”);
Then output should be
Aslam Kakkove |
You can use many printf function in a single program:
Example: Displaying your Name and age:
#include<stdio.h> #include <conio.h> void main() { printf (“ My name is Aslam”); printf (“My age is 22”); getch(); } |
List of some constants and their meaning for printing:
Constant | Meaning |
---|---|
n
|
New line
|
t
|
Horizontal tab
|
b
|
Backspace
|
What happens when we apply these constants
printf line | Output |
---|---|
printf (“AslamKakkove”); |
AslamKakkove
|
printf (“Aslam Kakkove”);
|
Aslam Kakkove
|
printf (“AslamnKakkove”);
|
Aslam
Kakkove
|
printf (“AslamtKakkove”); | Aslam Kakkove |
printf (“Aslamb Kakkove”); | Asla Kakkove |
Test: Can you write a program to get output like this?
#include<stdio.h> #include <conio.h> void main() { clrscr(); printf (“n1t2t3n4t5t6n7t8t9”); getch(); } |
-Where clrscr(); used to clear your output screen.
Now you learnt how display any text string as output, now let us learn how to print (display) a declared variable,
If you got any error while compiling or running time, please inform us, we will solve immediately.
Next lesson describes how to declare variable with values and how to print (display or show) declared variable with printf function.
Be First to Comment