Writing Your First C Program

Submitted by: 
Language: 

Writing Your First C Program

In this part you will learn:
1. C syntax
2. Headers
3. Declaring Variable
4. Brackets and tabs
5. Input/output
In this tutorial I will teach you about how to write a C Program first time. In the first program we will learn about printing on the screen and variable. In the second program we will learn about taking input from the user and displaying It on the screen.

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 two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.

  1. int main()
  2. {
  3. printf(“Hello World”);

This is Our main function which is the heart of a program. In this function is a print statement that will print “Hello World” on the screen. Whatever we write in those quotation marks is printed on the screen.

  1. int i;
  2. char c;
  3. float f;
  4. i=20;
  5. c=’A’;
  6. f=20.2;
  7. printf(“\n i=%d , c=%c , f=%f”,i,c,f);
  8.  
  9. getch();
  10. }
  11.  

Now we declared some variables. An integer variable ‘i’ , a character variable ‘c’ , and a float variable ‘f’.
Then we assigned them value. An interger can contain a interger , a charchter can contain a single character and a floating point variable can contain a floating number.
Then using a printf() statement we output the values of our variables. To output and integer we use %d for character we use %c and for float we use %f. The ‘\n’ in print function takes us to a new line in output. The getch() function in the end is used to pause the output screen. It stays there until the user press any button.
Execute > compile
then Execute > run
Output
picture viewer

Writing Your Second C Program

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 two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.

  1. int main()
  2. {
  3. printf("My second program");
  4.  
  5. int a;
  6. char c,d;
  7.  
  8. printf("\nEnter your age.");
  9. scanf("%d",&a);
  10. printf("\nEnter the initals of your first name.");
  11. scanf(" %c",&c);
  12. printf("\nEnter the initals of your last name.");
  13. scanf(" %c",&d);

At the start we wrote a statement using printf() function. Then we declared some variables. An integer variable and two character variables. We take a input in our integer variable from the user. This is done using a scanf() function. We write the format specifier first in quotation marks in the scanf() function which is ‘%d’ in this case. Then we place a comma and put a ‘&’ sign to give the compiler the address of the variable. At this stage you just write it you will understand it later in the course. And then with the ‘&’ sign we write the name of the variable in which we want to take the input.
Then similarly for characters input. We placed the format specifier , then the ‘&’ sign and name of the variable.
This code will ask us the input for all these variables line by line.

  1. printf("\nYour Entered age is=%d",a);
  2. printf("\nThe initials of your name are=%c%c",c,d);
  3.  
  4. getch();
  5. }

At last we are printing all these values with the help of printf() function.

Execute > compile
then Execute > run
Output

picture viewer


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • 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.