Professional Documents
Culture Documents
Basics of C
Basics of C
Documentation Section
This section consists of comment lines which include the name of programmer, the author and other details
like time and date of writing the program. Documentation section helps anyone to get an overview of the
program.
Link Section
The link section consists of the header files of the functions that are used in the program. It provides
instructions to the compiler to link functions from the system library.
Definition Section
All the symbolic constants are written in definition section. Macros are known as symbolic constants.
main() Function Section
It is necessary have one main() function section in every C program. This section contains two parts,
declaration and executable part. The declaration part declares all the variables that are used in executable
part. These two parts must be written in between the opening and closing braces. Each statement in the
declaration and executable part must end with a semicolon (;). The execution of program starts at opening
braces and ends at closing braces.
Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific task.
These user defined functions are called in the main() function.
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Features of C Program
It is a robust language with rich set of built-in functions and operators that can be
high-level language.
Programs Written in C are efficient and fast. This is due to its variety of data type and
powerful operators.
C is highly portable this means that programs once written can be run on another
C language is the most widely used language in operating systems and embedded
C TOKENS:
C tokens are the basic buildings blocks in C language which are
constructed together to write a C program.
Each and every smallest individual units in a C program are known as C
tokens.
C tokens are of six types. They are,
KEYWORDS IN C LANGUAGE:
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. C language supports 32 keywords given below:
do if static while
Identifiers in C
Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in C are the
user-defined words. It can be composed of uppercase letters, lowercase letters, underscore, or digits, but
the starting letter should be either an underscore or an alphabet. Identifiers cannot be used as keywords.
Rules for constructing identifiers in C are given below:
o The first character of an identifier should be either an alphabet or an underscore, and then it can be
followed by any of the character, digit, or underscore.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that
identifiers are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Identifiers should be written in such a way that it is meaningful, short, and easy to read.
Strings in C
Strings in C are always represented as an array of characters having null character '\0' at the end of the
string. This null character denotes the end of the string. Strings in C are enclosed within double quotes,
while characters are enclosed within single characters. The size of a string is a number of characters that
the string contains.
char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a' array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run time.
Operators in C
Operators in C 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 Operator
A unary operator is an operator applied to the single operand. For example: increment operator (++),
decrement operator (--), sizeof, (type)*.
Binary Operator
The binary operator is an operator applied between two operands. The following is the list of the binary
operators:
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Conditional Operators
o Assignment Operator
o Misc Operator
Special characters in C
Some special characters are used in C, and they have a special meaning which cannot be used for another
purpose.
o Square brackets [ ]: The opening and closing brackets represent the single and multidimensional
subscripts.
o Simple brackets ( ): It is used in function declaration and function calling. For example, printf()
is a pre-defined function.
o Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and
closing of the loops.
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.
Constants
Constants are the fixed values that never change during the execution of a
program. Following are the various types of constants:
Integer constants
An integer constant is nothing but a value consisting of digits or numbers.
These values never change during the execution of a program. Integer
constants can be octal, decimal and hexadecimal.
1. Decimal constant contains digits from 0-9 such as,
0-9 A- F
a- 10 B -11 15-F
2. Octal constant contains digits from 0-7, and these types of constants
are always preceded by 0.
String constants
A string constant contains a sequence of characters enclosed within double
quotes ("").
Real Constants
Like integer constants that always contains an integer value. 'C' also
provides real constants that contain a decimal point or a fraction value. The
real constants are also called as floating point constants. The real constant
contains a decimal point and a fractional value.
Mantissa e Exponent
Variables
A variable in simple terms is a storage place which has some memory
allocated to it.
Variable Declaration:
A typical variable declaration is of the form:
type variable_name;
or for multiple variables:
type variable1_name, variable2_name, variable3_name;
Examples:
int i, j, k;
char c, ch;
float f, salary;
double d;
Type Syntax
Data Types in C
'C' provides various data types to make it easy for a programmer to select a
suitable data type as per the requirements of an application. Following are
the three data types:
Integer is nothing but a whole number. It uses int keyword.
The'float' keyword is used to represent the floating point data type. It can
hold a floating point value which means a number is having a fraction and
a decimal part. a float can hold up to 6 precision values.
Character data types are used to store a single character value enclosed
in single quotes.
A void data type doesn't contain or return any value. It is mostly used for
defining functions in 'C'.
// WAP TO FIND THE SIZE OF EACH DATA TYPE
#include <stdio.h>
int main()
{
int a;
short int b;
long int c;
float f;
double d;
long double ld;
printf("%d\n",sizeof(b));
printf("%d\n",sizeof(a));
printf("%d\n",sizeof(c));
printf("%d\n",sizeof(f));
printf("%d\n",sizeof(d));
printf("%d\n",sizeof(ld));
return 0;
}
//WAP to find the ASCII code
#include <stdio.h>
int main()
{
char ch;
printf(" Enter the character \n");
scanf("%c",&ch);
C= 5.0/9.0 * (F-32)
( %- remainder)
#include<math.h>
sqrt(s *(s-a)*(s-b)*(s-c))
A b
printf() and scanf() functions are inbuilt library functions in C programming language which are
available in C library by default. These functions are declared and related macros are defined in
“stdio.h” which is a header file in C language.
We have to include “stdio.h” file as shown in below C program to make use of these printf() and
scanf() library functions in C language.
PRINTF() FUNCTION IN C LANGUAGE:
In C programming language, printf() function is used to print the (“character, string, float, integer,
octal and hexadecimal values”) onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double
and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf() statement.
printf( “TEXT” );
scanf(“%d %f”,&a,&b);
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string \n");
scanf("%s", str);
printf("Entered string is %s \n", str);
}
Output:
#include <stdio.h>
int main()
{
char ch ;
float f;
int i;
double d;
int a,b,c;
printf("enter a character\n");
scanf("%c",&ch);
#include <stdio.h>
int main()
{
int a,b,c;
return 0;
}
enter the values of a b and c
25-01-2021
The values are 25, 1, 2021
The getchar() and putchar() Functions
The int getchar(void) function reads the next available character from the screen
and returns it as an integer. This function reads only single character at a time. You
can use this method in the loop in case you want to read more than one character
from the screen.
Syntax: ch = getchar()
The int putchar(int c) function puts the passed character on the screen and returns
the same character. This function puts only single character at a time. You can use
this method in the loop in case you want to display more than one character on the
screen.
Syntax: putchar(ch)
Check the following example
#include <stdio.h>
int main( ) {
int c;
return 0;
}
Enter a value : t
You entered: t
char str[100];
printf( "Enter a value :");
gets( str );
return 0;
}
When the above code is compiled and executed, it waits for you to input some text.
When you enter a text and press enter, then the program proceeds and reads the
complete line till end, and displays it as follows −
$./a.out
Enter a value : this is test
You entered: this is test
#include <stdio.h>
#include<conio.h>
int main( ) {
int ch[10];