To write c program you have to need a compiler such as borland, Turbo C etc. In this post I will discuss about what view of C programs mainly basic c program. How can we write? How C programs look? How to write c program?
We will see some examples to learn it. The main aim of this post is to explain basic idea of C programming.
This is c programming structure.
Yes. This is C program, if we write this line the compiler will not show error. Because there is no error. This is the main structure of C programming language. It is called main function of programming C and entry point of program. Originally a C program is the summation of one or many functions. But main function must have in programming C and other function must be written within the second bracket “{ }”. This is the rule of C. We can write above program as below also
Important sight is that every letters should be written on small letter. Because there is huge difference between small and capital letter in C language. So, C is known as case sensitive language. We will not write main to Main or MAIN or mAin, the compiler will show error. Now let we will write a program which output screen will
I am a new programmer.
For this work we have to use c print function
printf (“I am a new programmer”);
To show any statement we have to write the statement with double quotation (“ ”) within printf( ) function’s first bracket ‘( )’. C compiler can not work without main( ). So, we should write printf( ) function within main( ) function’s second bracket ‘{ }’.
If we type above program, may be we will get three error at the time of compiling.
Example
Compiling NONAMEOO.CPP:
Error NONAMEOO.CPP 3: Function ‘printf’ should have a prototype
Error NONAMEOO.CPP 4: Statement missing;
Error NONAMEOO.CPP 4: compound statement missing
C programming basics
(i) Cause of first error is
Using a function in C declaration of prototype is needed for that function. Printf ( ) is a library function and its prototype is written in a file named stdio.h. So, it is needed to write the name of stdio.h before and before main( ). It is the rule of C also. These types of files are called header file. The rule of writing header file is
(i) The cause of second error is missing semicolon (;). We must use semicolon at the end of instruction. Using semicolon in an instruction is called statement.(ii) Third error is the resultant error of first two errors. Now, if we write the program as below, compiler will not show any error.
If we run c programs we will see “I am a new programmer” on the output screen. But to see output you have to press Alt+F5 must. Look at given program below,
Two library functions named clrscr ( ) and getch ( ) are used in this program. And header file of these functions is conio.h. If you want to know the act of the two functions, just write the program and run. You will find easily yourself. There are many library functions for different sections. As a new learner you may desire to learn about library functions. You can learn about library function from the help of expert programmer or reading book.
There is a help option in TC editor you can learn from it also about library function pressing Ctrl+F1. Every instruction should be written within main ( ) function and every instruction should be ended by semicolon (;). Semicolon means the separation of instruction. So, we can write many instructions or statements besides using semicolon, compiler without find any error.
Example:
But writing separately is easier to understand. One important thing is if any function many statements, first statement will work first then seemed and it will finished until getting second bracket ‘}’ So, statements are written one by one below serially for working serially. To get clear concept see the program below
Run the program pressing Ctrl+F9. You will find the output
How to run c programs
What do we do to send writing to anyone? We use paper. Here paper is medium. Similarly in programming C language we need to write programs where the compiler can read the programs and realize it. So, an editor program is remained with every compiler, which works as paper. And the pen of this paper is keyboard.
Like other compiler Turbo C has an editor. We can give changing/editing/save command using c program editor.
We will write program on text editor in Turbo C. First start Turbo C; you will see a screen as below.
This is the IDE of Turbo C.
Now click File>New. Then a blue screen will be appeared as below image. Primarily the name of the window of Turbo C will be NONAMEOO.CPP.
We will type our program on the above window. Suppose we have written the program below on the window. This is small c program.
They are called source code also. I know that at a first glance learner will not understand the given program. But don’t worry I will discuss all about of C programming. If you read all the post of programming C in this blog, I believe you will have the ability of writing program.
However, write the program on the blue screen. Then click compile and compile the program. If error you have to correct it. There is no error in the program which I have written. Before running a program you have to save it by clicking File>Save. You can save the name of the program as you desire. Finally run the program. You will get an output as the image given below running c program
This is the output of a program. We can run and save a program in this way.
How does C printf runs on programming C
Printf in C:
This is an easy printf tutorial.
C language printf function is used to show output of program. C programming printf is the function which shows the output result of program on screen. Using printf we can show any statement on screen, the statement must be written in the bracket of printf( ) function with double quotation. It is written as string.
For example, now I am writing a program where user will see the statement
“I am an engineer.”
The sentence which is written in the bracket of printf( ) is called parameter or argument. Now look the program below
Everyone may think that the printf output will be
“I am an engineer.
This is engineers blogsite.”
It means the output of this program will two lines. But it is not true. The output of this programme will be only one line as
“I am an engineer this is engineers blogsite.”
Because printf( ) function shows output from the beginning of monitor screen and compiler starts showing next output on there where first output has finished.
But sometimes we need to show output by many lines. In this situation backslash character or escape sequence character, “n” is used. Using “n” compiler shows next output below next line from first output. If we rewrite the program as below
Here we have used “n” in first printf( ), so compiler has gone next line showing first output. Next output has shown as a new line below first output. “n” works as ENTER of keyboard. So, the output of above program will be shown by two separate lines.
Important matter is that we will not see “n” on the output screen. Because these types of backslash character (n, a, r etc) are used for a specific reason. The compiler knows the reason, it knows which character is used for which purpose. So, compiler does the work we need.