C Programming Fundamentals
C programming : Structure of C program, Tokens, Formatted and Unformatted I/O
Structure of C program
Documentation Section | It includes the statement specified at the beginning of a program,such as a program's name, date, description, and title. // Program for Addition of Two Numbers |
Preprocessor Directive | The preprocessor section contains all the header files used in a program. It informs the system to link the header files to the system libraries. It is given by #include"stdio.h" |
Definition Section | The define section comprises of different constants declared using the define keyword. It is given by: #define pi 3.14 |
Global Declaration | The global section comprises of all the global declarations in the program. It is given by: int a=5; |
main Function | main() is the first function to be executed by the computer. It is necessary for a code to include the main(). It is like any other function available in the C library. Parenthesis () are used for passing parameters (if any) to a function. The main function is normally declared as: void main() |
User Defined Functions | The user defined functions specified the functions specified as per the requirements of the user. The program follows the same sections as listed above.Return function is generally the last section of a code. Return type ends the execution of the function. It further returns control to the specified calling function. |
EXAMPLE
// Program for addition of two numbers <Documentation Section>
#include<stdio.h> <Preprocessor Directive>
#define a=5 <Definition Section>
Int b=6; <Global Declaration>
int add(int p,int q); <User Defined Function declaration>
main() <main function>
{
int x;
x=add(a,b);
printf(“%d”,x);
getch();
}
int add(int p,int q) <User Defined Function definition>
{
return(p+q);
}
C Tokens :
Tokens are some of the most important elements used in the C language for creating a program. One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler.
A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token. A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the compilation process.
Classification and Types of Tokens in C
Here are the categories in which we can divide the token in C language:
Identifiers in C
Keywords in C
Operators in C
Constant in C
Identifiers in C
These are used to name the arrays, functions, structures, variables, etc. The identifiers are user-defined words in the C language. These can consist of lowercase letters, uppercase letters, digits, or underscores, but the starting letter should always be either an alphabet or an underscore.
Here are the rules that we must follow when constructing the identifiers:
- The identifiers must not begin with a numerical digit.
- The first character used in an identifier should be either an underscore or an alphabet. After that, any of the characters, underscores, or digits can follow it. E.g. a, b, a123 valid 1a , @2a Invalid
- Both- the lowercase and uppercase letters are distinct in an identifier. Thus, we can safely say that an identifier is case-sensitive.
- We cannot use an identifier for representing the keywords.
- An identifier does not specify blank spaces or commas.
- The maximum length of an identifier is 31 characters.
- We must write identifiers in such a way that it is not only meaningful- but also easy to read and short.
Keywords in C :
Keywords in C can be defined as the pre-defined or the reserved words having its own importance, and each keyword has its own functionality. Since keywords are the pre-defined words used by the compiler, so they cannot be used as the variable names. If the keywords are used as the variable names, it means that we are assigning a different meaning to the keyword, which is not
allowed. C language supports 32 keywords given below:
Turbo C is an integrated development environment (IDE) and compiler for the C programming language. Here are some keywords in C that are recognized by Turbo C:
auto break case char const continue default do double else enum extern
float for goto if int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
These keywords are used in the syntax of the C language and are reserved, meaning they cannot be used as variable names or other identifiers in your program.
Operators in C :
Operator is a special symbol used to perform the functions. The data items on which the operators are applied are known as operands. Operators are applied between the operands. Depending on the number of operands, operators are classified as follows:
Unary Operators :
A unary operator is an operator applied to the single operand.
For example: increment operator (++), decrement operator (--), sizeof, (type)*.
Binary Operators :
The binary operator is an operator applied between two operands.
The following is the list of the binary operators:
- Arithmetic Operators : + , - , * , / , %
- Relational Operators : < , > , <= , >= , != , ==
- Shift Operators : << , >>
- Logical Operators : && , || , !
- Bitwise Operators : & , | , ^
- Conditional Operators : ? , :
Constants in C :
A constant is a value assigned to the variable which will remain the same throughout the program, i.e., the constant value cannot be changed.
Integer Constant : 12,15,192
Floating Point Constant : 12.5, 18.3
Octal Constant : 011, 033
Hexadecimal Constant : 0x1A, 0x2C
Character Constant : 'A', 'h'
String Constant : "cprogramming", "java"
Formatted I/O Statements :
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.
Following are the format specifiers used in C
%d: integer (decimal)
%f: floating point number
%c: character
%s: string
%o: octal number
%x or %X: hexadecimal number
%e or %E: exponential notation
%%: prints a single percent sign
Following are some formatted I/O Statements used in C
printf()
scanf()
sprintf()
sscanf()
printf() Statement:
printf statement is used to print the value of variable or message on to screen. This is output statement used in C.
Syntax
printf(“<Format Specifier/Message”, <var(s)(if any)>);
Example
1. printf(“Hello ! How are you”);
2. printf(“%d”,a);
3. printf(“Value of a is %d”,a);
scanf() Statement :
scanf statement is used to read the value of variable from user . This is input statement used in C.
Syntax
scanf(“<Format Specifier(s)”, <var(s)>);
Example
1. scanf(“%d”,&a);
2. scanf(“%d%d”,&a,&b);
sprintf() Statement :
This statement is almost same as printf statement but instead of displaying value onto screen it will store it in one string.
Syntax
sprintf(<string>,“<Format Specifier/Message”, <var(s)(if any)>);
Example
1. sprintf(str,“Hello ! How are you”);
2. sprintf(str,“Value of a is %d”,a);
Unformatted I/O statements
In Unformatted I/O statements you can use only char data type or char array/string and you cannot use any other data type. These statements are used to read single input from the user at the console and it allows to display the value at the console.
Why they are called unformatted I/O?
These statements are called unformatted I/O statements because we cannot use format specifiers in these functions.
There are following unformatted I/O functions used in C
getch()
getche()
getchar()
putchar()
gets()
puts()
putch()
getch() Statement :
It reads a single character from the keyboard by the user but doesn’t display that character on the screen and immediately returned when the key is pressed.
Syntax :
getch(); OR <var>=getch();
Example :
1. getch()
2. c=getch();
getche() Statement :
It reads a single character from the keyboard by the user and displays it on the console screen and immediately returns when a key is pressed.
Syntax :
getche(); OR <var>=getche();
Example:
1. getche()
2. c=getche();
getchar() Statement:
It read only a first single character from the keyboard whether multiple characters is typed by the user and this function reads one character at one time until and unless the enter key is pressed.
Syntax :
<var>=getchar();
Example:
c=getchar();
putchar() Statement:
This statement is used to display a single character at a time by passing that character directly to it or by passing a variable that has already stored a character. This function is declared in stdio.h(header file)
Syntax :
putchar(<var>);
Example:
putchar(c);
gets() Statement:
This statement reads a group of characters or strings from the keyboard by the user and these characters get stored in a character array. This function allows us to write space-separated texts or strings. This function is declared in stdio.h(header file).
Syntax :
gets(<string>);
Example:
gets(s); (Here s is declared as char array)
Related Youtube Video
Comments
Post a Comment