Showing posts with label Contents. Show all posts
Showing posts with label Contents. Show all posts
C Programming In Linux

C Programming In Linux

C Programming in Linux : In this tutorial we are going to study how to write, compile and run c program in Linux. Linux Wall command and make command  is also explained.

Contents


HOW TO WRITE C PROGRAM IN LINUX

Back to content

A c program is written in a text editor and is saved with ".c" extension. ".c" helps Unix or Linux OS(operating system) to identify that it is a C program.
I am assuming that all of you can have an access to a text editor and you know how to write in it and save it.

Lets write our first c program :

#include<stdio.h>
int main()
{
   printf("Yepeeeeeee this is my first C program\n"); // Displays the string as output
   return 0; // tell the OS that the program exited without errors
}

Type the above code in any editor and save it with ".c" extension say "firstprogram.c"


Explanation :

The execution of a C program starts with main() function.

// ( Double slash ) is a single line comment and is ignored by the compiler. It is used by the programmer to explain the working of the programs. so that other programmer who reads the program written by him would understand what the program is exactly doing.

/*

Written anything
.......

*/

The above syntax is for multiline comment i.e any text written in between is ignored by the compiler.

Now what does first line #include<stdio.h> mean :

The first line may be confusing, but it’s just a C syntax that tells the compiler to include headers for a standard input/output (I/O) library named "stdio.h". This header file is added to the program when it is compiled. It is located at /usr/include/stdio.h and it defines several constants and function prototypes for corresponding functions in the standard I/O library. Since the main() function uses the printf() function from the standard I/O library, a function prototype is needed for printf() before it can be used. This function prototype (along with many others) is included in the "stdio.h" header file. A lot of the power of C comes from its extensibility and libraries.

In  printf("Yepeeeeeee this is my first C program\n") we have used "\n" this is nothing but a escape sequence and is known as newline character and it causes the cursor to move to the start of the next line.

You can read more about escape sequence from : ESCAPE SEQUENCE

HOW TO COMPILE C PROGRAM IN LINUX

Back to content

Linux has a compiler named CC ( The C Compiler ) for compiling the C programs written in a text editor. Most unix or linux version also contain The GNU Compiler Collection (GCC) which is a free C compiler that translates C into machine language that a processor can understand.A compiler converts a high-level language into machine language.

How to compile  C program in Linux:


Note : The followed command should be issued carefully in terminal and keep in mind of the directory in which you are working. i.e if you have saved your file in media directory then in terminal you should type cd /media to go to that directory and then issue the following command. If you launch terminal from your taskbar or menu then the working directory is by default /home/username. It would be a better idea to open your terminal from the same directory in which your file is saved.

For compiling a C program you can simply type :

cc firstprogram.c or
gcc firstprogram.c

Now what does the above two statement does :

This will try to compile firstprogram.c and if successful will produce a runnable file called "a.out" which is a default name. If you want to give the runnable file a better name you can type :

cc firstprogram.c -o firstprogram ( or cc firstprogram.c -o testprog.out )
gcc firstprogram.c -o firstprogram ( or gcc firstprogram.c -o firstprogram.out )

This will compile firstprogram.c and  create a  runnable file firstprogram
If you notice we have excluded the extension ".out" but not to worry with extension or without extension your runnable or executable file will work.

LINUX MAKE COMMAND

Back to content

UNIX or Linux also includes a very useful program called "make". Make allows very complicated programs to be compiled quickly, by reference to a configuration file (usually called Makefile). If your C program is a single file, you can usually use "make" by simply typing :

make firstprogram

This will compile firstprogram.c and put the executable code in firstprogram
"make" does the same task what cc firstprogram.c -o firstprogram or gcc firstprogram.c -o firstprogram does.

LINUX WALL COMMAND

Back to content

The -Wall option causes the compiler to warn you about legal but dubious code constructs, and will help you catch a lot of bugs very early. To use -Wall option you can type :

gcc -Wall -c firstprogram.c or
cc -Wall -c firstprogram.c

If you want to be even more specific you can type :

gcc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c or
cc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c

The -Wstrict-prototypes option means that the compiler will warn you if you haven't written correct prototypes for all your functions. The -ansi and -pedantic options cause the compiler to warn about any non-portable construct (i.e  constructs that may be legal in gcc but not in other standard C compilers. We should try to avoid such features).

Note - Running the commands :

gcc -Wall -c firstprogram.c or
cc -Wall -c firstprogram.c

gcc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c or
cc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c

produces object file named firstprogram.o

So the next step is to make a runnable or executable file from the object file. To make an runnable or executale file type :

gcc firstprogram.o -o firstprogram or 
cc firstprogram.o -o firstprogram

This produces an executable file named firstprogram

To create an executable file directly without producing an object file. You can type :

gcc -g -Wall -o firstprogram firstprogram.c or
cc -g -Wall -o firstprogram firstprogram.c

This produces an executable file named firstprogram without producing an object file firstprogram.o


HOW TO RUN C PROGRAM IN LINUX

Back to content

Now that we are done with writing and compiling our code. Lets run it. You can run your code by simply typing the runnable or executable file name i.e type :

firstprogram

If in case the above command show error and does not execute your runnable file then you can type :

./firstprogram

This tells the compiler that the executable file is in the current working directory.

HOW DO YOU FEEL

Back to content

Now that you have executed your first C program. Tell us how do you feel by simply dropping a comment in the comment box.

More Informative post :
C PROGRAMS

C PROGRAMS

Here more than 100 basic c programs are listed for practice. I have divided these c programs according to the topics for easier navigation and understanding.

Request : Please Link to this post and share this on social networking sites if you find it useful.

CONTENTS

Back to contents
  1. A simple program for printing a line of text
  2. Program to input integer number and display it
  3. Program to add, subtract, multiply and divide two numbers
  4. Program to subtract two numbers without using arithmetic operator ( - )
  5. Program to find greatest between three numbers using conditional operator ( ? : )
  6. Program to find the average of three numbers with input values as integer and output value (avg) as float
  7. Program to find the area of a right angled triangle based on base and height values
  8. Program to find or obtain the sum of square of two floating point numbers
  9. Program to find the simple interest an total amount based on interest
  10. Program to enter 10 digit number and displaying it in the given format where first 3 digit refer to area code, next three digit to exchange code and remaining to number
  11. Program to input a number and find whether it is even or odd
  12. Program to find biggest of four no by using ternary operator
  13. Program to print smallest of four no by using ternary operators
  14. Program to accept a year and check the given year is leap or not by using ternary operator 
  15. Program to find area of a triangle when there sides are given

CONTROL STRUCTURES

Back to contents
  1. Program to multiply 2 number without using * operator
  2. Program to determine Armstrong number
  3. Program to input time in seconds and convert them into hours and seconds and display it
  4. Program to determine Palindrome number
  5. Program showing the use of 'goto' statement
  6. Program for finding the factorial of a number
  7. Program to find whether the entered character is vowel or not using if else
  8. Program to find whether the entered character is vowel or not using switch case
  9. Program to print the table of a given number till the user wishes
  10. Program to sum up all the integer numbers input at run time until zero is entered
  11. Program to find whether a given number is Prime or Not
  12. Program to print ( list ) all the prime number between 1 to n
  13. Program to print (list) Palindrome number from 10 to n
  14. Program to print (list) Armstrong Number between two given numbers
  15. Program to print the factor of a given number
  16. Program to print the fibonacii series till which user wishes
  17. Program to determine whether entered character is capital case letter, small case letter, a digit or a special symbol
  18. Program to find whether a number is Perfect Square or Not
  19. Program to print (list) Perfect Square Number between any two number
  20. Program for Bank account Handling
  21. Program to award Grades
  22. Grade Program without using if-else statement
  23. Program to calculate the sum of individual digits of a number
  24. Program to determine whether a given year is leap year or not
  25. Program to encode and decode ( cipher )

PATTERNS

Back to contents
  1. Program to print the given pattern :
    *
    * *
    * * *
    * * * *
    * * * * * 
  2. Program to print the given pattern :
    *
    * *
    * * *
    * * * *
    * * * * *
    * * * * * * ..... till n rows
  3. Program to print the given pattern :
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5 .... till n rows
  4. Program to print the given pattern :
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5 .... till n rows
  5. Program to print the given pattern :
    1 1 1 1 1
    2 2 2 2 2
    3 3 3 3 3
    4 4 4 4 4
    5 5 5 5 5
  6. Program to print the given pattern :
    5
    5 4
    5 4 3
    5 4 3 2
    5 4 3 2 1
  7. Program to print the given pattern :
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
  8. Program to print the given pattern :
    5 4 3 2 1
    5 4 3 2
    5 4 3
    5 4
    5
  9. Program to print the given pattern :
            *
          * *
        * * *
      * * * *
    * * * * *
    
  10. Program to print the given pattern :
            1
          1 2
        1 2 3
      1 2 3 4
    1 2 3 4 5
  11. Program to print the given pattern :
            5
          4 5
        3 4 5
      2 3 4 5
    1 2 3 4 5
    
  12. Program to print the given pattern :
    1
    * *
    1 2 3
    * * * *
    1 2 3 4 5 .... till n rows
  13. Program to print the given pattern :
    1                 1
    1 2             1 2
    1 2 3         1 2 3
    1 2 3 4     1 2 3 4
    1 2 3 4 5 1 2 3 4 5
    
  14. Program to print the given pattern :
    1                 1
    1 2             2 1
    1 2 3         3 2 1
    1 2 3 4     4 3 2 1
    1 2 3 4 5 5 4 3 2 1
    
  15. Program to print the given pattern :
    1                 5
    1 2             5 4
    1 2 3         5 4 3
    1 2 3 4     5 4 3 2
    1 2 3 4 5 5 4 3 2 1
    
  16. Program to print the given pattern :
    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15 ... till n rows
  17. Program to print the given pattern :
    ******** ********
    *******   *******
    ******     ****** 
    *****       *****
    ****         ****
    ***           ***
    **             **
    *               *
     
    *               *
    **             **
    ***           ***
    ****         ****
    *****       ***** 
    ******     ******
    *******   *******
    ******** ********
    

  18. Program to print the given pattern
     
          *
         ***
        *****
       *******
      *********
       *******  
        *****
         ***
          * 
    
  19. Program to print the given pattern :
    8 4 2 1
    4 2 1
    2 1
    1
  20. Program to print Pascal triangle :

FUNCTIONS

Back to contents
  1. Program to find sum of two numbers using function sum(int,int) that returns sum value
  2. Program to print the area of a rectangle
  3. Program to find the factorial of two numbers and add them and print the result
  4. Program to implement functions that returns HCF OR GCD and LCM of two numbers
  5. Write a function that receives a positive integer as input and returns the leading digit in its decimal representation
  6. Implement a function that receives an integer value and returns the number of prime factors of that number. If the number is itself a prime number then return 0
  7. Program to find maximum number between three numbers
  8. Program to accept a number and print the sum of given and Reverse number using function

RECURSION

Back to contents
  1. Program to accept elements in an array and display it
  2. Program to accept elements in an array and display sum of all the elements
  3. Program to insert element in between an array. ( INSERTION )
  4. Program to delete element from an array. ( DELETION )
  5. Program to perform Transpose of a Matrix
  6. Program to find Minimum element in an array
  7. Program to find highest minimum temperature and lowest maximum temperature
  8. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order
  9. Program showing Passing array to function. Program finds the average of 5 marks input by user
  10. Program to initialize a character array and display the initialized character array in reverse order
  11. Program to generate histogram of entered number
  12. Program to enter values into a two-dimensional integer array of size (4 X 3) and then display it in matrix form
  13. Write a program to enter values into a two-dimensional integer array of size (4 X 3) and then display the elements of the second row
  14. Program to find the sum of elements that are greater than 5 within a two-dimensional array through a function receiving the array as arguments
  15. Program To Accept 5 Student - Roll No, Marks in 3 Subjects of each student and Calculate Total, Average and Print it along with student roll Number
  16. Programs to multiply two Matrices
  17. Program to print a diagonal matrix with diagonal value enter by user
  18. Program to print a anti diagonal matrix with diagonal value enter by user
  19. Program to print the sum of diagonal values and anti-diagonal values of a matrix
  1. Program to accept a string and print it
  2. Program to accept a character in the uppercase and print in lower case
  3. Program to accept a string in Lower case and print it in upper case
  4. Program to accept a character in any case and print in another case
  5. Program to accept a string and print it by using while loop
  6. Program to find length of a string using string function
  7. Program to find length of a string without using string function
  8. Program for Copy a String to another using String Function
  9. Program to find length of a string without using string function
  10. Program for Compare two String using String Function
  11. Program for Compare two String without using String Function
  12. Program for String Reverse with String Function
  13. Program for String Reverse without String Function
  14. Program for String Concatenation with String Function
  15. Program for String Concatenation without String Function
  16. Program to convert all characters in a string to lower case using string function
  17. Program to convert all characters in a string to lower case without using string function
  18. Program to convert all characters in a string to upper case using string function
  19. Program to convert all characters in a string to upper case without using string function
  20. Program to enter 5 string and print them with their length
  21. Program to accept a string and print each word of the string separately also print total number of words
  22. Program to accept a string and display vowels frequency( total number of vowels)
  23. Program to accept a string and display frequency of each vowel along with vowel
  24. Program to enter a word and check whether it is palindrome or not using string function
  25. Program to enter a word and check whether it is palindrome or not without using string function

POINTERS

Back to contents

DYNAMIC MEMORY ALLOCATION

Back to contents