Getting Started with C

By: Baski Emailed: 1609 times Printed: 2077 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages:

 Print the words
 hello, world

This is a big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.

In C, the program to print ``hello, world'' is

   #include <stdio.h>

   main()
   {
     printf("hello, world\n");
   }
Just how to run this program depends on the system you are using. As a specific example, on the UNIX operating system you must create the program in a file whose name ends in ``.c'', such as hello.c, then compile it with the command
   cc hello.c
If you haven't botched anything, such as omitting a character or misspelling something, the compilation will proceed silently, and make an executable file called a.out. If you run a.out by typing the command
   a.out
it will print
   hello, world
On other systems, the rules will be different; check with a local expert.

Now, for some explanations about the program itself. A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation. C functions are like the subroutines and functions in Fortran or the procedures and functions of Pascal. Our example is a function named main. Normally you are at liberty to give functions whatever names you like, but ``main'' is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere.

main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you. The first line of the program,

   #include <stdio.h>
tells the compiler to include information about the standard input/output library; the line appears at the beginning of many C source files.

One method of communicating data between functions is for the calling function to provide a list of values, called arguments, to the function it calls. The parentheses after the function name surround the argument list. In this example, main is defined to be a function that expects no arguments, which is indicated by the empty list ( ).

 


#include <stdio.h>                 include information about standard library
main()                                          define a function called main
                                             that received no argument values
{                                   statements of main are enclosed in braces
    printf("hello, world\n");              main calls library function printf
                                         to print this sequence of characters
}                                         \n represents the newline character

The first C program

 


The statements of a function are enclosed in braces { }. The function main contains only one statement,

   printf("hello, world\n");
A function is called by naming it, followed by a parenthesized list of arguments, so this calls the function printf with the argument "hello, world\n". printf is a library function that prints output, in this case the string of characters between the quotes.

A sequence of characters in double quotes, like "hello, world\n", is called a character string or string constant. For the moment our only use of character strings will be as arguments for printf and other functions.

The sequence \n in the string is C notation for the newline character, which when printed advances the output to the left margin on the next line. If you leave out the \n (a worthwhile experiment), you will find that there is no line advance after the output is printed. You must use \n to include a newline character in the printf argument; if you try something like

   printf("hello, world
   ");
the C compiler will produce an error message.

printf never supplies a newline character automatically, so several calls may be used to build up an output line in stages. Our first program could just as well have been written

   #include <stdio.h>

   main()
   {
     printf("hello, ");
     printf("world");
     printf("\n");
   }
to produce identical output.

Notice that \n represents only a single character. An escape sequence like \n provides a general and extensible mechanism for representing hard-to-type or invisible characters. Among the others that C provides are \t for tab, \b for backspace, \" for the double quote and \\ for the backslash itself.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
UNIX read and write system calls sample program in C
lseek() sample program in C
Using memset(), memcpy(), and memmove() in C
Find square and square root for a given number in C
Listing Files and Directories sample program in C
Open, Creat, Close, Unlink system calls sample program in C
Calculator in C
Variables and Arithmetic Expressions in C
Conditional Expressions in C
goto and labels in C
Static Variables in C
Macro Substitution using #define in C
Binary Tree - (Self-referential Structures) example program in C
Standard Input and Output in C
Fopen and Getc implementation program in C
Most Emailed Articles (in C)
Macro Substitution using #define in C
Address Arithmetic and pointers in C
Printing a simple histogram in C
The for statement in C
Symbolic Constants using #define in C
register Variables in C
Pointers to Structures example program in C
Using realloc() Function in C
Using memset(), memcpy(), and memmove() in C
Formatting with printf in C
Find square and square root for a given number in C
Getting Started with C
Variables and Arithmetic Expressions in C
Character Counting sample program in C
Arguments - Call by Value in C