Writing your first C program

Submitted by: 
Language: 

Writing your first C program

In this part you will learn:
1. C syntax
2. Showing output
3. Tabs and new lines
4. Headers
5. Variables and data types

In this tutorial I will teach you how to write your first c program.
Writing your first C program:
NOTE: Whenever I write something after // or in between /* */ it means I am writing comments. Comments are not read by the compiler they are just a way to write your own notes after some statement. Just like some people write notes on the side of a book.

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>

These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.

  1. int main(){
  2. printf("This is my first C program"); //to show an output on screen
  3. getch(); // pause the screen and wait for user to press any key
  4. }//end main

In our first program we write our main function which is the heart of the program. Then we write an output statement known as the printf statement which simply outputs the text written in quotes on the screen (REMEMBER to write semi colon after every statement it tells the compiler that the statement is complete it’s just like a fullstop ) . After that we write getch function to pause the screen otherwise the screen will just blink and won’t be able to see the output. We keep writing the comments after every line for explanation (Remember they are not necessary).

After writing this go to save select extension as .c source file and save.
Go to execute -> compile.
Then after compilation go to execute -> run
Here a console window will open and you can see the output.

Output:
outputWriting your first 2nd C program:

File > new > Source file

  1. #include<stdio.h>
  2. #include<conio.h>

First of all writing the headers then writing the main function

  1. int main(){
  2. // Declaring variables and setting their values
  3. int number = 25;
  4. char alphabet = 'a';
  5. float average = 5.25;
  6. double PI = 3.1428571428;

Declaring 4 different variables with 4 different data types. Remember a character is always written in single quotes except in print statement.

  1. /* Printing values
  2. of
  3. variables*/
  4. printf("The value of number is : %d ",number); //to print an integer we use %d
  5. printf("\nThe alphabet is : %c ",alphabet); //to print a charcter we use %c
  6. printf("\nAverage is : %f \t\t Value of PI is %lf",average,PI); // to print float we use %f for double %lf
  7. printf("\n\n\n End of Program \n\n\n"); // for newline we user \n
  8.  
  9. }//end main

First of all writing multiple line comments using /* */ and then writing the print statement for integer. ( Remember the integer is printed using %d a character with %c, float with %f and double with %lf ). Then we print the alphabet character variable, notice that we wrote \n in the next print statement that is for printing in a NEW LINE. Then we write the average and double variable with \t in between which means we want to give a tab space between them. In the end we simply write end of program after leaving three lines and close the main function bracket.
Then save, compile and execute our program.

Ouput:
output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.