Writing your first C program
In this part you will learn:
1. C syntax
2. Showing output
3. Tabs and new lines
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.
Open Dev C++ then File > new > source file and start writing the code below.
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.
printf("This is my first C program"); //to show an output on screen getch(); // pause the screen and wait for user to press any key
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.
Writing your first 2nd C program:
File > new > Source file
First of all writing the headers then writing the main function
// Declaring variables and setting their values
int number = 25;
char alphabet = 'a';
float average = 5.25;
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.
/* Printing values
printf("The value of number is : %d ",
number); //to print an integer we use %d
printf("\nThe alphabet is : %c ",
alphabet); //to print a charcter we use %c
printf("\nAverage is : %f \t\t Value of PI is %lf",
PI); // to print float we use %f for double %lf
printf("\n\n\n End of Program \n\n\n"); // for newline we user \n
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.