• We just launched and are currently in beta. Join us as we build and grow the community.

Writing Your First C Program

TiernanFoster21

Financial Growth Hacker
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
160
Likes
182
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. getch

    (

    )

    ;
  9. }


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
1st_output_1.png

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. int

    a;
  5. char

    c,

    d;

  6. printf

    (

    "\n

    Enter your age."

    )

    ;
  7. scanf

    (

    "%d"

    ,&

    a)

    ;
  8. printf

    (

    "\n

    Enter the initals of your first name."

    )

    ;
  9. scanf

    (

    " %c"

    ,&

    c)

    ;
  10. printf

    (

    "\n

    Enter the initals of your last name."

    )

    ;
  11. 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

    (

    "\n

    Your Entered age is=%d"

    ,

    a)

    ;
  2. printf

    (

    "\n

    The initials of your name are=%c%c"

    ,

    c,

    d)

    ;

  3. getch

    (

    )

    ;
  4. }

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

Execute > compile
then Execute > run
Output
2nd_output_1.png


Book traversal links for Writing Your First C Program

  • ‹ Fundamentals of C Language
  • Up
  • Conditional Statements ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top