C Language

C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language.

Why to Learn C Programming?

C programming language is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Software Development Domain. I will list down some of the key advantages of learning C Programming:

  • Easy to learn
  • Structured language
  • It produces efficient programs
  • It can handle low-level activities

Facts about C

  • C was invented to write an operating system called UNIX.
  • The language was formalized in 1988 by the American National Standard Institute (ANSI).
  • The UNIX OS was totally written in C.
  • Today C is the most widely used and popular System Programming Language.

Hello World using C Programming.

Just to give you a little excitement about C programming, I’m going to give you a small conventional C Programming Hello World program, You can try it using Demo link.

#include <stdio.h>

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

Applications of C Programming

C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C are –

  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • Databases
  • Language Interpreters
  • Utilities

Semicolons

In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.

Given below are two different statements −

printf("Hello, World! \n");
return 0;

Comments

Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminate with the characters */ as shown below −

/* my first program in C */

You cannot have comments within comments and they do not occur within a string or character literals.

Identifiers

A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).

C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers −

mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      retVal

Keywords

The following list shows the reserved words in C. These reserved words may not be used as constants or variables or any other identifier names.

autoelselongswitch
breakenumregistertypedef
caseexternreturnunion
charfloatshortunsigned
constforsignedvoid
continuegotosizeofvolatile
defaultifstaticwhile
dointstruct_Packed
double

Whitespace in C

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.

Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement −

int age;

there must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them. On the other hand, in the following statement −

fruit = apples + oranges;   // get the total fruit

no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish to increase readability.

Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

The types in C can be classified as follows −

Sr.No.Types & Description
1Basic TypesThey are arithmetic types and are further classified into: (a) integer types and (b) floating-point types.
2Enumerated typesThey are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
3The type voidThe type specifier void indicates that no value is available.
4Derived typesThey include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

The array types and structure types are referred collectively as the aggregate types. The type of a function specifies the type of the function’s return value. We will see the basic types in the following section, where as other types will be covered in the upcoming chapters.

Integer Types

The following table provides the details of standard integer types with their storage sizes and value ranges −

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long8 bytes or (4bytes for 32 bit OS)-9223372036854775808 to 9223372036854775807
unsigned long8 bytes0 to 18446744073709551615

To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Given below is an example to get the size of various type on a machine using different constant defined in limits.h header file

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

    printf("CHAR_BIT    :   %d\n", CHAR_BIT);
    printf("CHAR_MAX    :   %d\n", CHAR_MAX);
    printf("CHAR_MIN    :   %d\n", CHAR_MIN);
    printf("INT_MAX     :   %d\n", INT_MAX);
    printf("INT_MIN     :   %d\n", INT_MIN);
    printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
    printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
    printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
    printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
    printf("SHRT_MAX    :   %d\n", SHRT_MAX);
    printf("SHRT_MIN    :   %d\n", SHRT_MIN);
    printf("UCHAR_MAX   :   %d\n", UCHAR_MAX);
    printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
    printf("ULONG_MAX   :   %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX   :   %d\n", (unsigned short) USHRT_MAX);

    return 0;
}

When you compile and execute the above program, it produces the following result on Linux −

CHAR_BIT    :   8
CHAR_MAX    :   127
CHAR_MIN    :   -128
INT_MAX     :   2147483647
INT_MIN     :   -2147483648
LONG_MAX    :   9223372036854775807
LONG_MIN    :   -9223372036854775808
SCHAR_MAX   :   127
SCHAR_MIN   :   -128
SHRT_MAX    :   32767
SHRT_MIN    :   -32768
UCHAR_MAX   :   255
UINT_MAX    :   4294967295
ULONG_MAX   :   18446744073709551615
USHRT_MAX   :   65535

Floating-Point Types

The following table provide the details of standard floating-point types with storage sizes and value ranges and their precision −

TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places

The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. The following example prints the storage space taken by a float type and its range values

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

    printf("Storage size for float : %d \n", sizeof(float));
    printf("FLT_MAX     :   %g\n", (float) FLT_MAX);
    printf("FLT_MIN     :   %g\n", (float) FLT_MIN);
    printf("-FLT_MAX    :   %g\n", (float) -FLT_MAX);
    printf("-FLT_MIN    :   %g\n", (float) -FLT_MIN);
    printf("DBL_MAX     :   %g\n", (double) DBL_MAX);
    printf("DBL_MIN     :   %g\n", (double) DBL_MIN);
    printf("-DBL_MAX     :  %g\n", (double) -DBL_MAX);
    printf("Precision value: %d\n", FLT_DIG );

    return 0;
}

When you compile and execute the above program, it produces the following result on Linux −

Storage size for float : 4 
FLT_MAX      :   3.40282e+38
FLT_MIN      :   1.17549e-38
-FLT_MAX     :   -3.40282e+38
-FLT_MIN     :   -1.17549e-38
DBL_MAX      :   1.79769e+308
DBL_MIN      :   2.22507e-308
-DBL_MAX     :  -1.79769e+308
Precision value: 6

The void Type

The void type specifies that no value is available. It is used in three kinds of situations −

Sr.No.Types & Description
1Function returns as voidThere are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
2Function arguments as voidThere are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void);
3Pointers to voidA pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.

C Language”

C is a middle level functional programming language. Where functional means all work done in c in form of function .Middle level means c stands in between these two categories. Because it have good programming efficiency as compare to low level language and good machine efficiency as compare to high level language.

“Development of C”

It developed by Dennis Richie in 1972 in USA at AT&T Bell Laboratories. In C a character denotes any alphabet, digit or special symbol used to represent information. Ex. a to z, 0 to 9 and ―~ ! @ # % <>.

“Constant, variable and keyword” When alphabets, numbers and special symbols properly combined to form constant, variable and keywords.

“Constants”

A Constant is a quantity that does not change. This quantity can be stored at a location in the memory of a computer.

“Variables”

A quantity which may very during program execution is called a variable.

“Variable name”

A name given to locations in the memory of computer where different constants are stored. These locations can contain integer, real or character constants.

“Keywords”

Keywords are the words whose meaning has already been explained to the C Compiler .Keywords cannot be used as variable names keywords also called Reserved words there are 32 keywords available in C. Ex.auto, double, if, continue, do, goto , signed, while, void, uniox, switch etc.

“Types of C Constant”

We can divide C Constant into two categories.

I. Primary Constants

II. Secondary Constants

“Primary Constants”

” Integer”

” Real or Float”

“Character “Integer”

” Integer”

Have no decimal and fractional values It have at last one digit. No commas OR blanks are allowed in integer constant. It could be either positive OR Negative It have 2 Bytes in memory Range for integer constant is-32768 to 32767 declaration of integer constant is ―Int‖ and Conversion specified of integer is %d OR %I

“Real Constants”

Real constant are also called float constant real constant could be written in two forms. Fractional Form Exponential Form A real Constant must have at least one digit. It must have a decimal point. It could be either positive OR Negative default sign is positive. No commas OR Blanks are allowed within a real constant. They have 4 Bytes in memory. Range of real constants -3.4e38 to 3.4e38 declaration of real constant is float and conversion specifier of real constant is %f.

“Character Constant”

A Character constant is either a single alphabet,digit,special symbol enclosed within single inverted commas. Declaration of character constant is Character and Conversion specifier of character constant is %c Range of character Constant is -128 to 127. Ex. a = `A‘;

“C Variables”

A quantity which may vary during program execution is called variable.

“Rules for Constructing variable names”

1. A Variable Name is any Combination of 1 to 8 Alphabets, digits and underscore.

2. First character in the variable must be an alphabets.

3. No Commas and Blanks are allows in the variable Name.

4. No special symbols other them underscore can be used.

“C Instructions”

DEF.:- different types of constant ,variables and keywords combined to each from instructions There are 4type of instruction in c language

1. type declaration instruction

2. input/out put instruction

3. Arithmetic instruction

4. Control instruction

“Purpose of instructions

1. type declaration instruction are used to declare the type of variables

2. Input/ output instruction are used to perform the function of supplying input data to a program and obtaining the output results

3. Arithmetic instruction to perform arithmetic operation between constants and variables

4. Control instruction are used to control the sequence of execution of various statements Type declaration instructions are written at the beginning of the program In arithmetic instruction consists variable name on the left hand side and constants placed on the right hand side of ―=‖.

“Operands”

The variables and constants together are called operands which are operations in an arithmetic statement are performed is called the hierarchy of operations . Priority Operators 1 st *,/,% 2 nd +,- 3 rd = Or “ Bodmas “ is the hierarchy of operations .(Brackets ,open ,divvied , multiplictication , addition , subtraction comparison with English language. Alphabets -> word-> sentence -> paragraph “C language “ Alphabets —> constants Digits —> variables —> instruction—> program Symbols —> keywords

“Conversion specifies

Conversion specifies indicates the computer a number is in the from of character , float, integer. Some specifies given below %d for integer % f for float % s for string % c for character % p for hexadecimal number

“/* Comments .*/”

we may type more than one argument in the between these two sets of characters and compiler will ignore them

“# Include < stdio .h >

Where # include is called directive and the standard input output header file .It is a good habit to include this directive at the beginning of any C program ― ; (Semicolon at the end of a statement semicolon indicates the compiler that a statement is finished .All the programs are written in lower case in C program “\n” To go to next line “\T” To give tab of 5 spaces

“Oh shell”

Temporary exit from C or to go to the dos prompt. Again go to the program give exit

“ Clrscr();”

To clear the screen

“Declaration of variables”

When we use variables in a program then first of all beginning of a C program Variable must be in float and character type .

“Assignment”

After declaration of variables we assign values for these variables multiple assignment is possible in c we can assign the given value for more than one variables Ex x=y 2=24;

“operators”

we have three type of operator in C language

1. Arithmetic operators

2. Relational operators

3. Logical operators

“ Arithmetic operators”

-,+,*,/,%,- -,+ +,=

“Relational operators”

> > >= <= = = != Not equal to

“Logical operators”

$$ and :: or ! Not ―=‖ is used in assignment a value ―= =‖ is used in loops for conditions

“Long integer”

:- range –2147483648 to +2147483647 Declaration :- long int. Variable name Conversion spacifier = % ld

“Unsigned integer”

range :- +65535 it does not except –xe value declaration :- unsigned Int. variable name Conversion specifies = % u

“ Float or real”

Float :- Range –3.4e38 to +3.4e38 Declaration :- float variable name Conversion specifier :- %f “Double float” range :- -1.7e308 to +1.7e308 Declaration :- double variable name Spacifier :- %lf

“Character”

character :- range –128 to 127 declaration :- character variable name Conversion Specifies :- %c

“unsigned character”

range:- 0 to +225 Declaration :- unsigned character variable name conversion Spacifier :- %C

When we save our program the computer or file with different Extension

1. Program file .C

2. Save program .bak

3. Compile program .obj

4. Run program .exe These file make in out directory “#Include < conio.h>” Consol Input out put header file. “#Include < math.h>” Mathematical function header file “Lib directory” Lib directory have library functions Included directory have header, file “Purpose” \N new line \B back space \F from file \T tab

“ Main( )”

It is the first function ( that) during the program execution every program must have one main function the function main always followed by ( ) parentheses which may be empty. Syntax: – main () { Statement; }

“Print f”

It is formatted out put function this function allowed us to obtained the output in a fixed format. Syntax: – print f (― conversion specified ―,Variable name)

“Scanf”

Scanf is a standard C library function which reads in key board input of the specified data and the$ operator is used to indicates the addresses of data item in the computer memory Syntax: – Scanf (― conversion specifies‖, & variable name)

“ If condition”

It condition is always enclosed with a pair of parentheses of the condition is true the statement is executed .If condition is false the statement are not executed If statement is not terminated at the end of the Parenthesis Syntax: – If (condition) { Statement1; Statement2; } All the statements written in if condition always were terminated

“ Multiple statements with in if “

If more than one statements to be executed if condition is statements are enclose with a pair of braces Syntax: – if (condition) { Statement; Statement; Statement } If a pair of braces is not used then C compiler assumes that (the) to executed only first statement of the program only

“The if – else statement”

If else statement execute one group of statement if condition is true if condition is false then execute the group of statement which are played after the Else statement Syntax: – If (condition) { Statement; Statement; Statement; Statement; } Else { Statements; Statement; } The group of statement after if and below Else is called an ―if block‖ and the statement after the else from the ―Else block‖

“Nested if –Else”

(f) It is perfectly alright if we write on entire if else construct within either the body of the if statement or the body of an else statement this is called nesting if else statement Syntax: – if (condition) { If (condition) { Do this } Else { Do this and this } Else { Do this; }

“Use of logical operators”

C allows only three logical operators 1. && ―And‖ 2. || ―Or ― 3.! ―Not‖ We do not use single symbols have their own meanings && And || operator allow two or more conditions to be combined in an if statement .And the third operator not operator makes a true part or expression false and a false expression in true . Hierarchy logical operators. * /% + – < > < =>= = =! = && || And = assignment ―Conditional operators‖ And | sometimes called ternary operators since they take three arguments Syntax: – If (condition)? Expression! : Expression If condition is true then expression will be executed other wise expression two will executed “Limitations” After the condition operators only one C statements can occur

“ While loop”

Syntax: – While (condition) { Do this and this Increment or decrement } The statement within the while loop would be executed till the condition remains true when. The condition being tested may use relational or logical operators Ex. While (! < = 10 &&j <=15) OR while (J>10&&(b<15 \\ c<20) It is not necessary that trop counter must only be an and it can even be a float

(“Increment and decrement operator “)

In C program we give increment and decrement in fallowing ways I+ + or + + I For one decrement in i If we want to increase no more than one them one then we give i+2 or I+ 2=i Difference between ++ i and i ++++ i:- First of all increment of i takes place and than the comparison with condition is performed i++:- First of all comparison of i with condition takes place and then increment of i takes place

“ Do while loop”

syntax :- main ( ) { Do { this and this } while ( condition); } ― Difference between while and Do-while loop‖ In while loop condition is tested before the execution of statements but in do-while loop the statements execute before the condition tested.

“Continue statement”

Continue is a keyword continue passes the control automatically to the beginning of the loop. A continue is usually associated with an of condition. Syntax:-Continue;

“Break statement”

Break is a keyword when Break encountered inside a c. program control automatically passes to the first statement after the loop. Break statement associated with an of condition. Syntax:-Break; Break statement passes the control to the firstly statement after the loop. But exit statement termination whole program. ―Difference Between Break and Exit‖

“Operators”

―Arithmetic operators‖ Expression:-Expression built up from literals, variable and operators. Operators:-The operators define how the variable and literals in the Expression will be manipulated.

“Arithmetic operators”

+,-,*,/,% are the Arithmetic operators which are used for Addition, subtraction, multiplication division and modulus modulus division operator(%) gives the remainder of the Integer division.

“ Relational operators”

Relational operators are used to compare values forming relational Expressions.  areater than  areater than equal to  less than  less than equal to  is equal to  not equal to

“Precedence of Relational logical operators”

>>=<<= ==!= && ‗‘ ‗‘

“ ASSIGNMENT operators”

The simple equal sign(=) is the Assignment operators. ―+=‖ It includes Both inereament and assignment so it could combined operators. Ex. *** x-=y; or x=x-y; x*=y; x=x*y; x/=y; x=x/y; x%=y; x=x%y;

“Size of operators”

The size of operators is used to return the size in bytes of its operand. Syntax:-Size of (n); Where n is a variable.

“Bitwise operators”

& AND | OR ^ XOR ~ ones complement. >> Right shift << Left shift. Precedence of Bitwise operators ~ << >> & ^ ! All of the Bitwise operators are evaluated from left to right. ―Precedence of c operators‖ ( ) [ ] – > Left to right /~ ++ — &* size of right to left */% left to right = – ― << >> ― < < =>= ― ==!= ― & ― ^ ― | ― && ― ?: ― = + = – =/= right to left , ―

“GETCHE”

getche means get character echa.The getche function assins the letter to the variable as soon as. It is enter without watching for the enter key to be press. Ex. Main( ) { char c; c=getchew ( ); printf(―%c‖,c); }

“ get char( )”

It is a c standred library function that gets a single character from key Borad.Get character does not gets the input untile after you press enter key. Ex:- Main( ) { int A; printf(―the character %c‖,a); printf(―correspends to the a%d‖,a);

“Put char( )”

Syntax:- put char(c ); The put character function will write its character argament to the screan.

“gets”

It gets a requance of characters from key Borad.Which are stored in the variable gets allows the user to use the back space key to corree any error whole Entering the letters Ex. Main ( ) { char c; gets ©;

“ Puts ©”

Puts(―in the print valu is‖,c) “puts” syntax:- puts © puts only outputs a string of characters on the screan but it canot out put no. or do format conversions these for when displaying strings puts taker up less space as com[aised to printf statement.

“Loop”

Loop:- Loop is a opeating some portion of the program either a specinfied number of times or until a porticular condition is being satisfied.There are three methods 1. for statement. 2. while statement. 3. do while statement.

Leave a Reply

Your email address will not be published. Required fields are marked *