Defination
C is a powerful and versatile general-purpose programming language with low-level system access that is used to write a wide variety of software, including operating systems, embedded systems, compilers, databases, and games.
Advantages of C Programming
- It is simple and easy to understand and use.
- It is a building block of many other programming language.
- C code can run on various platforms with minimal modifications.
- It is fast and efficient.
Disadvantages of C Programming
- It is case sensitive.
- The error or bugs aren’t detected after each line of code.
- There is no runtime checking in c language.
- As the program extends it is very difficult to fix the bugs.
Feature of C Programming
- It is structured programming language.
- It offers low-level memory manipulation and direct hardware interaction.
- C code can be compiled and run on various platforms with minor adjustments.
- C provides a rich library of functions for tasks like I/O, math, and string manipulation.
Why C Programming is called structured programming language?
C programming is called structured programming because it emphasizes organizing code into clear, logical structures using functions and control flow constructs like loops and conditionals, enhancing code readability and maintainability.
Structure of C Program
- The ‘#include’ in the first line of the program is called a preprocessor directive.
- The next statement is the main() function. This is the place where the execution of the C program begins.
- Each expression must end with semicolon (;).
- Next comes the opening brace ‘{” which indicates the beginning of the function.
- The closing brace ‘}’ indicates the end of the function.
- ‘//……………’ is used for single line remarks.
- ‘/*……………….*/’ is used for multiline remarks.
Preprocessor
The C preprocessor is a text processing tool that is used to modify the source code of a C program before it is compiled. It handle directives for the following in c program
- Source file inclusion (#include)
- Macro definition (#define)
- Conditional inclusion(#if, #ifdef, #elif, #endif)
- Miscellaneous directives (#,pragma, #undef, #error)
File Inclusion
#include<filename> ; in specific directory, if you use DevC++ then in C:/Dev-Cpp/include
#include “filename” ; in specific and current directory(cpp saved files location)
Macro define
The declaration of variable by using #define is called macro define
#define PI 3.1416
Header Files
A file that is defined to be included at the beginning of a program in C language that contains the definitions of data types and declarations of variables used by the functions in the program is called header file.
It is placed at top of the every program and it’s file extension is ‘.h’
Header Files | Description | Main functions |
stdio.h | Standard input and output | gets(), puts(), gstchar(), scanf(), printf(), rename (), etc |
conio.h | Console input output (for DOS complier) | getch(), getche() |
math.h | Mathematical calculation | sin(x), log(x), pow(x,2), srt(x), cbrt(x) |
Character Set = alphabet, number, spec
Keywords
The predefined reversed word of c program is called keyword which is directly recognized by compiler. The keywords are written in lowercase. There are 32 keyword in c programming. For example; char, while, do, int, if, else, case, printf, double, long, return, const, float, unsigned, for, void, goto, etc
Identifier
The name given to various elements such as constants, variables, function name, etc rather than keywords called identifier. Identifiers are defined according to the following rules.
- It consists of letter, digit and underscore ‘_’ only.
- First character must be an alphabet or underscore.
- Keywords can not be used as identifier
- Only one special character ‘_’ can use.
- For Example add, sum, num, c12, _at, s_m, etc
Basic Data Type in C
Data Type | Type | Memory |
void | nothing | 0 |
char | character | 1 byte |
int | Integer | 2 or 4 bytes |
float | Floating point | 4 bytes |
Constant Declaration → const int a= 5; //constant integer
Variable Declaration → int a= 5; //variable declartion
Local variable → Declared within the function
Global variable → Declared outside the function
Types of specifier
It is used to format input and output in screen. It is of two types; escape sequence(format nonprintable character) and format specifier(format printable character)
Escape Sequence
Escape Sequence | Meaning /normally used by printf() function |
\’ | Pint ‘ in output |
\” | Print ” in output |
\0 | Terminate string |
\n | New line in output display |
\t | Create tab or 8 space in print output |
Format Specifier
Format Sequence | Meaning /normally used by scanf() and printf() function |
%d, %i | signed integer with + or – sign |
%u | unsigned integer without + or – sign |
%f | float number |
%e | exponent number |
%o | octal integer |
%c | single character |
%s | string |
Uses of Format Specifier in C
Example | Output |
printf(“%d”, a); | 12345678 |
printf(“%10d”, a); | 12345678 |
printf(“%-10d”, a); | 12345678 |
printf(“%f”, a); | 1.2345678 |
printf(“%5.3f”, a); | 1.234 |
printf(“%x”, a); | BC614E |
printf(“%s”, c); | COMPUTER |
Operators
Operator | Function |
+ | Add |
– | Substract |
* | Multiply |
/ | Divide |
% | Reminder of Division |
Operator | Function |
== | Return true if both are equal |
!= | Return true if both are unequal |
< | Return true if first value is less |
> | Return true if first value is greater |
<= | Return true if first value is less or equal to second value |
>= | Return true if first value is greater or equal to second value |
Operator | Function |
&& | AND Logic |
|| | OR Logic |
! | NOT Logic |
Increment Operator
++ is used as increment operator, it increase the value of variable by 1. Example; i++, a++ (post increment), ++i, ++a (pre increment).
Decrement Operator
— is used as decrement operator, it decrease the value of variable by 1. Example; i–, a– (post decrement), –i, –a (pre decrement).
Input Output Function
Function | Uses | Type |
printf() | To print | Formatted Output |
scanf() | To assign input value | Formatted Input |
getch() | Get Character; hold output screen for next line until user press any key. It doesn’t echo(display) character during entry. Can be used instead of scanf() for single character | Unformatted Input |
getche() | Get Character; hold output screen for next line until user press any key. It echo(display) character during entry. Can be used instead of scanf() for single character | Unformatted Input |
putch() | Put Character; Print Single Character | Unformatted Output |
getchar() | To read character from keyboard | Unformatted Input |
putchar() | To print character on monitor | Unformatted Output |
gets() | To read single or multiple word from keyboard | Unformatted Input |
puts() | To print single or multiple word on monitor | Unformatted Output |
Some Starting C Code
//To Print Hello World in C program
#include<stdio.h>
main()
{
printf("Hello World");
return 0;
}
//To assign integer value to variable and print it.
#include<stdio.h>
main()
{
int x= 5; //variable declaration
printf("value of x=%d",x);
return 0;
}
//Ask two integer and find it's sum
#include<stdio.h>
main()
{
int a, b, sum;
printf("Enter first integer=");
scanf("%d",&a);
printf("Enter first integer=");
scanf("%d",&b);
sum = a+ b;
printf("%d+%d = %d",a,b,sum);
return 0;
}
//Write a c program to ask first and last name to print full name
#include <stdio.h>
#include <string.h>
int main()
{
char firstName[50], lastName[50];
printf("Enter the first name: ");
scanf("%s", firstName);
printf("Enter the last name: ");
scanf("%s", lastName);
// Concatenate the first and last names
char fullName[100];
strcpy(fullName, firstName); // Copy the first name into fullName
strcat(fullName, " "); // Add a space between the names
strcat(fullName, lastName); // Concatenate the last name
// Print the full name
printf("Full Name: %s\n", fullName);
return 0;
}
Uses of Control Statements
// If statement
#include<stdio.h>
int main()
{
int a;
printf("Enter a number:");
scanf("%d", &a);
if(a>0)
{
printf("The number %d is positive.", a);
}
else
{
printf("The number %d is negative.", a);
}
}