Advertisement

  • Topic 4: My First C Program

    Now lets go to our first program...
    to print 'Hello World' on the screen..!
    Type the following into the DevC++
























    1. Save it as helloworld.c
    • spaces are not allowed in the c program name
    • we can use ( _ ) underscore instead
    2. Compile and the execute the program


    Explaining Each Elements In The Program


    1. Header Files

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

    C contains several predefined operations in its header files.
    So when we use #include<>;

    It includes the suite of


    Header files that are already in the directory


    The preprocessor replaces the line #include with the system header file of that name. More precisely, the entire text of the file 'stdio.h' is replaced for #include

    don't confuse.. if it feel confusing skip...

    stdio.h - standard buffered input/output (used for printf(), cin, cout ..)

    conio.h - console input and output (used for clrscr(), getch()..)
    .

    2. Commenting

    Two methods..
    • // Your comment here....... (only single line)
    • /* Your Comment Here.....*/ (from starting /* to the closing */)
    The compiler just ignores the content typed like this...
    You can type anything here..



    3. Main Function

    Every C program has a primary (main) function that must be named main
    The main function serves as the starting point for program execution.
    A program usually stops executing at the end of main,
    although it can terminate at other points in the program for a variety of reasons.

    void main()

    void indicates that the method doesn't return any value (can discuss later..)


    4. The Printf Statement

    The printf is used to print text or other contents on the screen
    Let's look at some variations to understand printf completely. Here is the simplest printf statement:

    printf("hello..");

    This call to printf has a format string that tells printf to send the word "Hello" to standard out.

    The following line shows how to output the value of a variable using printf.

    printf("%d", b);

    The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is
    Often, you will want to embed the value within some other words.

    Lets see something for fun..

    printf("The temperature is ");
    printf("%d", b);
    printf(" degrees\n");


    An easier way is to say this:

    printf("The temperature is %d degrees\n", b);


    5. The getch()
    GETCH stand for Get Character. It holds on till it get a keystroke, otherwise the program will execute, produce results and exit at once.


    Output:

    The program should run and produce result like this..




















    Best Of Luck..

0 comments:

Leave a Reply

Advertisement

Advertisement