C Standards
Before standardization of c several compiler like turbo c or gcc compiler gives different output of same c program. Then what the solution is? The solution is the c standard that have to follow by all the compilers and produce same result. The latest C standard was released in June 2018 which is ISO/IEC 9899:2018 also known as the C11.
The main() function’s void return type −
See the following program −
void main() { Â Â //program code }
This program will run ok if we use the turbo c compiler but other compilers throw an error that the main cannot be void. So, which one is correct? The answer is mentioned in the standards.
Here are the standardization period of C language:
- K&R C
- ANSI C (C89)
- C99
- C11
Escape Sequence
Variables, Data types and constants/literal
In C programming, variables, data types, and constants are fundamental concepts used to store and manipulate data. Let’s take a closer look at each of them:
- Variables: A variable is a named location in memory used to store data. It allows programmers to assign values to it during the program’s execution and use those values in calculations, comparisons, and other operations. Before using a variable, it must be declared with a specific data type. The syntax for declaring a variable is:
data_type variable_name;
eg : int age;
2. Data Types: Data types in C specify the type of data that a variable can hold. The C language provides several built-in data types, which can be categorized into four primary categories:
a. Integer Types: Used for whole numbers.
int
: Integer type.char
: Character type (used to store individual characters).short
: Short integer type.long
: Long integer type.long long
: Long long integer type (C99 and later).
b. Floating-Point Types: Used for real numbers with fractional parts.
float
: Single-precision floating-point type.double
: Double-precision floating-point type.long double
: Extended-precision floating-point type (may provide higher precision thandouble
).
c. Character Type:
char
: Used to store individual characters (e.g., letters, digits, symbols)
3. Constants
In C, constants are fixed values that remain unchanged during the execution of a program. They are used to represent data that should not be modified or modified only under specific conditions.
The const
keyword is used to declare a constant variable, which means its value cannot be modified after initialization. It is often used to define numeric or character constants.
Example:
const int MAX_LENGTH = 100;
const float PI = 3.14159;
const char NEWLINE = '\n';
Comments
Comments in C are used to provide explanatory notes within the source code. They are entirely ignored by the compiler and have no effect on the program’s execution. There are mainly two types of comments in c and they are:
Single-line comments:
To create a single-line comment, you use two forward slashes //
followed by the comment text. Everything after the //
on that line is treated as a comment and is not processed by the compiler.
Example:
//bimstudies.com
Multi-line comments:
Multi-line comments, also known as block comments, are used to write comments that span multiple lines. You enclose the comment text between /*
and */
. Everything between these symbols, including line breaks, is treated as a comment and is ignored by the compiler.
Example:
/*bimstudies.com*/
Library Functions and Preprocessor Directives
Library functions and the preprocessor are essential components that aid in code organization, reusability, and efficiency.
They offer a wide range of functionality, such as mathematical operations, string manipulation, file handling, memory management, and more
Example of using a library function:
#include <stdio.h>
#include <conio.h>
void main()
{ int num; printf(“Enter a number: “);
scanf(“%d”, &num);
// Using the scanf() function from stdio.h to read input
printf(“The number you entered is: %d\n”, num);
// Using printf() to display output
getch();
}
Preprocessor Directives:
The preprocessor is a part of the C compiler that performs text processing before the actual compilation of the code begins. Preprocessor directives start with the #
symbol, and they are used to instruct the preprocessor to perform specific actions, such as including header files, defining macros, conditional compilation, and more.
Common preprocessor directives:
#include
: Used to include header files in the source code.#define
: Used to define macros or symbolic constants.#ifdef
,#ifndef
,#else
,#endif
: Used for conditional compilation.#pragma
: Used to provide additional instructions to the compiler.