Showing posts with label First program in C. Show all posts
Showing posts with label First program in C. Show all posts

Monday, 2 January 2012

Structure of C-program

C program basically has the following form:
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments
The following program is written in the C programming language. Open a text file hello.c using vi editor and put the following lines inside that file.

#include <stdio.h>

int main()
{
   /* My first program */
   printf("Hello, World! \n");
   
   return 0;
}

Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation.

Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured using return statement.
The C Programming language provides a set of built-in functions. In the above example printf()is a C built-in function which is used to print anything on the screen.

Variables: are used to hold numbers, strings and complex data for manipulation.


Statements & Expressions: Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.


Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment can span through multiple lines.


Note the followings
  • C is a case sensitive programming language. It means in C printf and Printf will have different meanings.
  • C has a free-form line structure. End of each C statement must be marked with a semicolon.
  • Multiple statements can be one the same line.
  • White Spaces (ie tab space and space bar ) are ignored.
  • Statements can continue over multiple lines.

C Program Compilation

To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and program file name is hello.c, give following command at Unix prompt.

$cc hello.c

This will produce a binary file called a.out and an object file hello.o in your current directory. Here a.out is your first program which you will run at Unix prompt like any other system program. If you don't like the name a.out then you can produce a binary file with your own name by using -o option while compiling C program. See an example below

$cc -o hello hello.c

Now you will get a binary with name hello. Execute this program at Unix prompt but before executing / running this program make sure that it has execute permission set. If you don't know what is execute permission then just follow these two steps

$chmod 755 hello
$./hello

This will produce following result
Hello, World

                                             (OR)
                           In ANSI C (or) TURBO C
Alt+f9 to compile

Ctrl+f9 to Run

Alt+f5 to see the Output

Congratulations!! you have written your first program in "C". Now believe me its not difficult to learn "C".

Sunday, 1 January 2012

First program in C

Here is your first c program. Write carefully because C Language is a case sensative language.
#include <>
void main()
{
printf("Hello World\n");
}
Press Ctrl +F9 to compile,ALT+F5 to execute your program. If you have any error in your program, you will get the error message, remove your errors and then execute your program you will get the out put.
Hello World
printf()
The printf() function prints output to stdout, according to format and other arguments passed to printf(). The string format consists of two types of items - characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed.
printf( "Hello World’ );
scanf()
The scanf() function reads input from stdin, according to the given format, and stores the data in the other arguments. It works a lot like printf(). The format string consists of control characters, whitespace characters, and non-whitespace characters.
void main(void)
{
int i;
scanf(“%d”,&i);
printf(“%d”,i);
}
C Program File
All the C programs are writen into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write programming instructions inside a program file.
C Compilers
When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a language understandable by a computer. This is called machine language (ie. binary format). So before proceeding, make sure you have C Compiler available at your computer. It comes alongwith all flavors of Unix and Linux.
If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result. You can ask your system administrator or you can take help from anyone to identify an available C Compiler at your computer.


About Compilation Stages
Compiling a small C program requires at least a single .c file, with .h files as appropriate. Although the command to perform this task is simply cc file.c, there are 3 steps to obtain the final executable program, as shown:
  1. Compiler Stage: All C language code in the .c file is converted into a lower-level language called Assembly language; making .s files.

  2. Assembler Stage: The assembly language code made by the previous stage is then converted into object code which are fragments of code which the computer understands directly. An object code file ends with .o.

  3. Linker Stage: The final stage in compiling a program involves linking the object code to code libraries which contain certain "built-in" functions, such as printf. This stage produces an executable program, which is named a.out by default.
host gator coupon