Skip to main content


Debug C Program using gdb

Step 1. Compile the C program with debugging option -g
Compile your C program with -g option. This allows the compiler to collect the debugging information.
        $cc -g test.c  //test.c is the sample program to debug
Step 2. Launch gdb
Launch the C debugger (gdb) as shown below.
    $gdb a.out
Step 3. Set up a break point inside C program
    break [file_name]:line_number
        break test.c:3 // this will set a break point at line number3
Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.
Step 4. Execute the C program in gdb debugger
You can start running the program using the run command in the gdb debugger.
  Eg: run [args]  // args are optional
Step 5. Printing the variable values inside gdb debugger
Syntax: print {variable}
    print i
Step 6. Continue, stepping over and in – gdb commands
There are three kind of gdb operations you can choose when the prog…
Recent posts

Escape Sequences

In C programming language, there are 256 numbers of characters in character set. The entire character set is divided into 2 parts i.e. the ASCII characters set and the extended ASCII characters set. But apart from that, some other characters are also there which are not the part of any characters set, known as ESCAPE characters. List of Escape Sequences \a Alarm or Beep \b Backspace \f Form Feed \n New Line \r Carriage Return \t Tab (Horizontal) \v Vertical Tab \\ Backslash \' Single Quote \" Double Quote \? Question Mark \ooo octal number \xhh hexadecimal number \0 Null

KTU Mandatory C programs for Laboratory and Solutions

1. Familiarization of Hardware Components of a Computer
2. Familiarization of Linux environment – Programming in C with Linux
3. Familiarization of console I/O and operators in C
    i) Display “Hello World”
    ii) Read two numbers, add them and display their sum
    iii) Read the radius of a circle, calculate its area and display it
4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution.
Read the values of the variables from the user through console
5. Read 3 integer values, find the largest among them.
6. Read a Natural Number and check whether the number is prime or not
7. Read a Natural Number and check whether the number is Armstrong or not
8. Read n integers, store them in an array and find their sum and average
9. Read n integers, store them in an array and search for an element in the
   array using an algorithm for Linear Search
10.Read n integers, store them in an array and sort the elements in the array

Programming in C with Linux

If you are using Linux or UNIX, then check whether GCC is installed on your system by entering the following command from the command line − $ gcc -v If you have GNU compiler installed on your machine, then it should print a message If GCC is not installed, then you will have to install it yourself using the detailed instructions available at Install GCC
The following linux command will install gcc compiler on on Ubuntu 18.04
Bionic Beaver.Open up terminal and enter:
$sudo apt-get install gcc
Install build-essential
Another way to install gcc compiler is to install it as part of build-essential package. build-essential package will also install additional libraries as well as g++ compiler. In most cases or if unsure this is exactly what you need:
$sudo apt-get install build-essential
Check GCC version
Confirm your installation by checking for GCC version:
$ gcc --version
Writing first program:
Following is first program in C
/* my first program */
#include <…

Variables and Constants in C

Variables In programming, a variable is a container (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example: int playerScore = 95; Here, playerScore is a variable of integer type. The variable is assigned value: 95. The value of a variable can be changed, hence the name 'variable'. In C programming, you have to declare a variable before you can use it. Rules for naming a variable in C A variable name can have letters (both uppercase and lowercase letters), digits and underscore only. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error. There is no rule on how long a variable can be. However, only the first 31 characters of a variable a…

Keywords and Identifiers in C

C Keywords Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example: int money; Here, int is a keyword that indicates 'money' is a variable of type integer.  As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C. Keywords in C Language auto double int struct break else long switch case enum register  typedef char extern return union continue for signed void do if static  while default goto sizeof volatile const float

Bubble Sort and Selection Sort in C

Sorting is nothing but arranging the data in ascending or descending order. The term sorting came into picture, as humans realised the importance of searching quickly. sorting  allows everyone to arrange data in an order, hence making it easier to search. Bubble Sort Algorithm Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an array with n number of elements. Bubble Sort compares all the element one by one and sort them based on their values. If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on. If we have total n elements, then we need to repeat this process for n-1 times. But it is noted that when the array is sorted , comparing adjacent element does not produce any swap and…