Chapter 1 Getting Started
1.ASIMPLE EXAMPLE
This program prints out the message “Thisis a C program”
Example1:
#include <stdio.h>
int main() { printf( "This is a C Program\n" );
return 0; } |
Every C program contains a function calledmain , This is the start point of the program.
#include<stdio.h> allows the programto interact with the screen, keyboard and file system of your computer. Youwill find it at the beginning of almost every C program.
Main() declares the start of the function. Whilethe two curly brackets show the start and finish of the function.
Printf(“This is a C program\n”)
Prints the words on the screen . The textto be printed is enclosed in double quotes, The \n at the end of the text tellsthe program to print a newline as part of the output.
C is case sensitive, that is, it recognizesa lower case letter and it’s upper case equivalent as being different.
Compilingin Linux
Gcc is the compiler from Gnu
$ gcc hello.c
$ gcc –o sample hello.c
The compilation will proceed silently, andmake an executable file called a.out
Runningin Linux
./a.out
Result:
This is a C program
2.Comments
Emample2:
#include <stdio.h>
int main() { int Counter=0; /* Initalise Counter */ /* a comment */ /* *Another comment */ return 0; } |
A comment starts with a /*and ends with*/
Summary:
The basic structure of a one-functionprogram i:
#include <stdio.h> /************************************************* *...Heading comments...* *************************************************/ /*Data declaractions*/
int main() { /*...Executable statements...*/ return 0; } |