Skip to main content

Posts

Math library

This post lists the important functions available in the “math.h” library of C with their examples. Before continuing, you must know that we need to include the “math.h” library while compiling our program by using -lm ($cc -c filename.c  $cc -o filename filename.c -lm). Function     Description                                 Example sqrt(x)       square root of x                              sqrt(4.0) is 2.0                                                                          sqrt(10.0) is 3.162278 exp(x)        exponential (ex)                            exp(1.0) is 2.718282                                                                          exp(4.0) is 54.598150 log(x)        natural logarithm of x (base e)       log(2.0) is 0.693147                                                                          log(4.0) is 1.386294 log10(x)     logarithm of x (base 10)                log10(10.0) is 1.0                                                                

Character Processing

Character Processing (<ctype.h>) The <ctype.h> header file declares several functions for testing characters. For each function, the argument is an int whose value must be EOF or representable as an unsigned char , and the return value is an integer.   Functions   int isalnum(int c); Returns a nonzero integer if the character passed to it is an alphanumeric ASCII character. Otherwise, isalnum returns 0.   int isalpha(int c); Returns a nonzero integer if the character passed to it is an alphabetic ASCII character. Otherwise, isalpha returns 0.   int iscntrl(int c); Returns a nonzero integer if the character passed to it is an ASCII DEL character (177 octal, 0x7F hex) or any nonprinting ASCII character (a code less than 40 octal, 0x20 hex). Otherwise, iscntrl returns 0.   int isdigit(int c); Returns a nonzero integer if the character passed to it is a decimal digit character (0 to 9). Otherwise, isdigit returns 0.   int isgraph(int c); Returns a nonzero integer

Debug C Program using gdb

Debugging  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. Eg:         $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 Syntax:     break [file_name]:line_number Eg:         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} Example:     print i Step 6. Continue, stepping over and in – gdb comm

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

LIST OF LAB EXPERIMENTS 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 t

Programming in C using gcc in 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 https://gcc.gnu.org/install/ Install GCC The following linux command will install gcc compiler on Ubuntu 18.04 Bionic Beaver( Latest version is 20.04 Focal Fossa) .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 prog

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