In this chapter you will learn about:
- Introduction to C Language.
- Features of C language.
- Disadvantages of C language.
- C program structure.
- C Comments.
- Important header files.
- C programming Rules.
Introduction to C Language:
- C is a Procedural programming language. This means there will a series of statements inside a procedure (function) that needs to be carried out.
- C was developed by Dennis Ritchie at Bell labs in 1973.
- C11 is the latest stable version released in 2011 December. This is supported by all major C compilers.
- Unix and Linux operating system is written using C language.
- C is a middle level programming language.
- Many other programming languages like Java, Go, C# have been heavily influenced by C.
- C programming allows static and dynamic memory management.
- 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.
- All the C source file will be saved as .c file extension. Example “helloWorld.c”
- All the C header file will be saved as .h file extension. Example “stdio.h”
Features of C language:
- Robust
- Portable
- Fast
- Simple and easy to learn
- Extensible
Disadvantages of C language:
- Not an object-oriented language.
- Some code needs to be recompiled when running on a different machine.
- No constructors and destructors
- There is no runtime checking. An error is known only after execution of a c program.
- There is no strict type checking. In C programming language we can send integer value for a float data type.
- 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.
C program structure:
A simple C program should contain following lines.
#include<stdio.h> int main() { printf("Hello World \n"); return 0; }
Program Explanation:
The first line “#include” is called as pre-processor directive. “stdio.h” is called a 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 the compiler that the function returns a value is of type int.
Next is opening brace ‘{‘, indicating the starting of function main().
Next line is a “printf()” function. This is used to display the output to the standard console. Note that every statement inside a function has ended with a semicolon. This informs the compiler that the statement has ended. And semicolon 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 the program completed without any issues.
Last line is closing brace ‘}‘, indicating the 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
C Comments:
Comments provide effective ways of knowing the functionality of a function. It is always a good practice to include comments in the 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:
-
Single line comment:
// This is a single line comment
-
Multiline comment:
/* This is an example of multiline comment */
Example for comments:
#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; }
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).
C programming Rules:
While writing any C program, we must follow below rules:
-
- Every program should have a main() to run and generate an output.
- All the statements should be terminated by a semicolon.
- If the programmer has written only semicolon in a line, then it is treated as an empty statement.
Ex:
;
- All the function names, variable names should be written in lower case, Upper case is used for symbolic constants.
- Every opening braces should have an closing braces.
List Of Tutorials available in this website:
Nandini
Can i get C and C++ as a pdf format