Programming‎ > ‎

C Programming

C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. Like most imperative languages in the ALGOL tradition, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.

C is one of the most widely used programming languages of all time, and C compilers are available for the majority of available computer architectures and operating systems.

How to compile a C Program on Ubuntu Linux

Steps to run
sample "Hello World" program:

1.Open up a terminal on Ubuntu Linux (Alt+Clt+Tab) and install the build-essential package by typing the following command in the terminal.

  • Type/Copy/Paste: sudo apt-get install build-essential
  • This will install the necessary C development libraries for your Ubuntu Linux system to create C programs.
2.Create a directory and a sub directory to hold your C programs and your main HelloWorld program.

  • Type/Copy/Paste: mkdir -p CCPP/HelloWorld
  • We are using CCPP for the main directory to hold our created C programs which stands for C programs directory and we are using the sub directory HelloWorld to hold our main program.
3.Then we will change into our created directory by issuing the following command

  • Type/Copy/Paste: cd CCPP/HelloWorld

4.Next we will use a text editor such as gedit to create our C source code using the following command.

5.For example for a C source code file we would issue the following command

  • Type/Copy/Paste: gedit main.c

6.Sample Hello World Program. Enter the following C source code below:

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
    return(0);
    }

7.Save the file as main.c and exit. You can view your saved program in home folder:


 
8.Compiling your C program

  • Important: Make sure you are in the CCPP/HelloWorld directory before you compile your C programs.

9.If you are compiling the C program version of Hello World type in the terminal

  • Type/Copy/Paste: gcc -Wall -W -Werror main.c -o HelloWorldC
  • The first line will invoke the GNU C compiler to compile the file main.c and output (-o) it to an executable called HelloWorldC.
  • The options -Wall -W and -Werror instruct the compiler to check for warnings.

10.If you should happen to get permission errors, you need to make the file executable. You can do this by issuing the following commands below

  •  Type/Copy/Paste: chmod +x HelloWorldC


11.In order to execute your program you will have to type in the following commands.

    To execute the C program version of the program:
    Type/Copy/Paste: ./HelloWorldC

12. Finally the output will appear as:



How to compile C program in Ubuntu


-----------------------------------------------------------------------------------------------------------------------------------------------------------
Dealing with libraries

Some libraries like conio.h, graphics.h may not work on Linux with gcc compiler. Following link may be useful in such case.

http://blog.eternal-thinker.com/2010/09/how-to-use-graphicsh-in-ubuntu.html *

*blog owned by Rahul Anand

 
Ċ
C.pdf
(1117k)
Unknown user,
Dec 6, 2013, 10:37 PM
Ċ
Unknown user,
Dec 6, 2013, 10:36 PM
Comments