Section | Description |
---|---|
Documentation | Consists of the description of the program, programmer's name, and creation date. These are generally written in the form of comments. |
Link | All header files are included in this section which contains different functions from the libraries. A copy of these header files is inserted into your code before compilation. |
Definition | Includes preprocessor directive, which contains symbolic constants. E.g.: #define allows us to use constants in our code. It replaces all the constants with its value in the code. |
Global Declaration | Includes declaration of global variables, function declarations, static global variables, and functions. |
Main() Function | For every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program. |
Subprograms | Includes all user-defined functions (functions the user provides). They can contain the inbuilt functions, and the function definitions declared in the Global Declaration section. These are called in the main() function. |
Types
|
Data Types
|
Basic data types | int, char, float, double |
Enumeration data type | enum |
Derived data type | pointer, array, structure, union |
Void data type | void |
Basic types
Type | Size (bytes) | Format Specifier |
---|---|---|
int |
at least 2, usually 4 | %d , %i |
char |
1 | %c |
float |
4 | %f |
double |
8 | %lf |
short int |
2 usually | %hd |
unsigned int |
at least 2, usually 4 | %u |
long int |
at least 4, usually 8 | %ld , %li |
long long int |
at least 8 | %lld , %lli |
unsigned long int |
at least 4 | %lu |
unsigned long long int |
at least 8 | %llu |
signed char |
1 | %c |
unsigned char |
1 | %c |
long double |
at least 10, usually 12 or 16 | %Lf |
DERIVED DATA TYPE IN C LANGUAGE:
- Array, pointer, structure and union are called derived data type in C language.
ENUMERATION DATA TYPE IN C LANGUAGE:
- Enumeration data type consists of named integer constants as a list.
- It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
- Enum syntax in C:
enum identifier [optional{ enumerator-list }];
VOID DATA TYPE IN C LANGUAGE:
- Void is an empty data type that has no value.
- This can be used in functions and pointers.
Parameters of Comparison | Signed | Unsigned |
Values Included | Signed data categories include both positive and negative integers. | Unsigned data categories include only zero and other positive integers. They cannot include negative integers. |
Magnitude | Signed integers have a smaller magnitude than their unsigned counterparts of the same range. | Unsigned integers have a greater magnitude than their signed counterparts of the same range. |
Flag Sign | Signed data types use a flag sign before the negative numbers they represent. | Unsigned data types do not use a flag sign before numbers, as they only represent positive integers. |
Process of Identification | The leftover bit is used by the signed data containers. | The leading bit of a value is used by the unsigned data containers. |
Range in Char | Signed integers range from -128 to 127 in chars. | Unsigned integers range from 0 to 255 in chars. |
Representation Method | 1’s complement form, 2’s complement form, and the sign-magnitude form methods can be used to represent signed binary variables. | Unsigned binary variables do not have a preceding sign or symbol, and thus, there exists only one representation method for such binary variables. |
Unambiguous Method of Representation | 1 out of 3 possible methods of representations is unambiguous. | The only method of representation available is an unambiguous one. |
Type Conversion in C
A type cast is basically a conversion from one type to another. There are two types of type conversion:
- Implicit Type Conversion
Also known as ‘automatic type conversion’.
- Done by the compiler on its own, without any external trigger from the user.
- Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid loss of data.
- All the data types of the variables are upgraded to the data type of the variable with largest data type.
bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double
- It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
Example of Type Implicit Conversion:
Output:
x = 107, z = 108.000000
- Explicit Type Conversion–
This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.
The syntax in C:
(type) expression
Type indicated the data type to which the final result is converted.
Output:
sum = 2
Advantages of Type Conversion
- This is done to take advantage of certain features of type hierarchies or type representations.
- It helps us to compute expressions containing variables of different data types
Formatted I/O Functions
Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to display the output to the user in different formats using the format specifiers. These I/O supports all data types like int, float, char, and many more.
Why they are called formatted I/O?
These functions are called formatted I/O functions because we can use format specifiers in these functions and hence, we can format these functions according to our needs.
List of some format specifiers-
S NO. | Format Specifier | Type | Description |
1 | %d | int/signed int | used for I/O signed integer value |
2 | %c | char | Used for I/O character value |
3 | %f | float | Used for I/O decimal floating-point value |
4 | %s | string | Used for I/O string/group of characters |
5 | %ld | long int | Used for I/O long signed integer value |
6 | %u | unsigned int | Used for I/O unsigned integer value |
7 | %i | unsigned int | used for the I/O integer value |
8 | %lf | double | Used for I/O fractional or floating data |
9 | %n | prints | prints nothing |
The following formatted I/O functions will be discussed in this section-
- printf()
- scanf()
- sprintf()
- sscanf()
printf():
printf() function is used in a C program to display any value like float, integer, character, string, etc on the console screen. It is a pre-defined function that is already declared in the stdio.h(header file).
Syntax 1:
To display any variable value.
printf(“Format Specifier”, var1, var2, …., varn);
Example:
- C
20
Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
Example:
- C
This is a string
scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard by the user, these values can be of any data type like integer, float, character, string, and many more. This function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In scanf() function we use &(address-of operator) which is used to store the variable value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Example:
- C
Enter a integer number: You have entered 0
Output:
Enter a integer number: 56 You have entered 56
sprintf():
sprintf stands for “string print”. This function is similar to printf() function but this function prints the string into a character array instead of printing it on the console screen.
Syntax:
sprintf(array_name, “format specifier”, variable_name);
Example:
- C
2 and 8 are even number
sscanf():
sscanf stands for “string scanf”. This function is similar to scanf() function but this function reads data from the string or character array instead of the console screen.
Syntax:
sscanf(array_name, “format specifier”, &variable_name);
Example:
- C
c = 2 and d = 8
Basic Input and Output in C
C language has standard libraries that allow input and output in a program. The stdio.h or standard input output library in C that has methods for input and output.
scanf()
The scanf() method, in C, reads the value from the console as per the type specified. Syntax:
scanf(“%X”, &variableOfXType); where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.
printf()
The printf() method, in C, prints the value passed as the parameter to it, on the console screen. Syntax:
printf(“%X”, variableOfXType); where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.
How to take input and output of basic types in C?
The basic type in C includes types like int, float, char, etc. Inorder to input or output the specific type, the X in the above syntax is changed with the specific format specifier of that type. The Syntax for input and output for these are:
- Integer:
Input: scanf("%d", &intVariable); Output: printf("%d", intVariable);
- Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
- Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
Please refer Format specifiers in C for more examples.
- C
Enter the integer: 10 Entered integer is: 10 Enter the float: 2.5 Entered float is: 2.500000 Enter the Character: A Entered Character is: A
1 Comments