C language Introduction

In this chapter you will learn about:
1. Introduction to C Language.
2. Features of C language.
3. Disadvantages of C language.
4. C program structure.
5. C Comments.
6. Important header files.
7. C programming Rules.

1. Introduction to C Language.

1. C is a Procedural programming language. This means there will a series of statements inside a procedure (function) that needs to be carried out.
2. C was developed by Dennis Ritchie at Bell labs in 1973.
3. C11 is the latest stable version released in 2011 December. This is supported by all major C compilers.
4. Unix and Linux operating system are written using C language.
5. C is a middle level programming language.
6. Many other programming languages like Java, Go, C# have been heavily influenced by C.
7. C programming allows static and dynamic memory management.
8. During 1970 and 1980 many versions of C have been implemented. Hence in 1989 ANSI C was introduced and was later accepted by ISO in 1990.
9. All the C source file will be saved as .c file extension. Example “helloWorld.c”
10. All the C header file will be saved as .h file extension. Example “stdio.h”

2. Features of C language:

1. Robust
2. Portable
3. Fast
4. Simple and easy to learn
5. Extensible

3. Disadvantages of C language

1. Not an object oriented language.
2. Code needs to be recompiled when running on different machine.
3. No constructors and destructors
4. There is no runtime checking. An error is known only after execution of a c program.
5. There is no strict type checking. In C programming language we can send integer value for a float data type.
6. No data security is available.
C is able to access low level memory of hardware. This helps a programmer to develop efficient code. Hence C language is called as a middle level programming language.

4. C program structure:

A simple C program should contain following lines.
1. #include<stdio.h>
2.
3. int main()
4. {
5.
6. printf("Hello World \n");
7.
8. return 0;
9. }

The first line “#include” is called as pre-processor directive. “stdio.h” is called as header file. This header file has declarations about standard input and output functions.

Next line is “int main()”. main() is the starting point of any c program. There is a return type specifies “int”, that informs compiler that the function returns a value is of type int.

Next is opening brace, indicating starting of function main().

Next line is a “printf()” function. This is used to display the output to standard console. Note that every statements inside a function has ended with semicolon. This informs the compiler that the statement has ended. And this semi-colon is mandatory to terminate a statement.

Next is “return 0;”. This will return the program execution back to the called function. In this case compiler. In C, returning value 0 indicates program completed without any issues.

Last line is closing brace, indicating end of function main().

To compile a C program in Linux use the following command:

gcc hello.c –o hello.o

To run the executed program, use below command:

./hello.o

5. C Comments:

Comments provide effective ways of knowing the functionality of a function. It is always a good practice to include comments in beginning of a function with a short description of what it does.

The part written inside comments will be ignored by the compiler and the comments will be stripped off while compiling the program.

C supports 2 types of comments:

1. Single line comment:

// This is a single line comment

2. Multiline comment:

/* This is 
an example of
multiline comment
*/

Example:

#include<stdio.h>

int main()
{
  int num = 1; // integer variable

/* Multi line comment
	num = num + 1;
   Above code will not be executed,
   as it is in comment
*/

return 0;

}

6. Important header files.

stdio.h : Provides input, output functions like printf(), scanf().
conio.h : Console input and output, provides functions like clrscr(), getch().
alloc.h : Provides memory allocation functions like malloc(), calloc(), free().
math.h : Provides math related functions like abs(), squrt().
string.h : Provides string related functions like strcpy(), strcat().
assert.h : Provides macros like assert(int).

7. C programming Rules:

While writing any C program, we must follow below rules:
1. Every program should have a main() to run and generate an output.
2. All the statements should be terminated by semicolon.
3. If the programmer has written only semicolon in a line, then it is treated as an empty statement.
Ex:
;
4. All the function names, variable names should be written in lower case, Upper case is used for symbolic constants.
5. Every opening braces should have an closing braces.
Write a Comment

Leave a Comment

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