Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Basic Structure of C Program

BASIC STRUCTURE OF C PROGRAM

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.

 Ex: Comment lines

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.

Global Declaration Section


The global variables that can be used anywhere in the program are declared in global declaration section.
This section also declares the user defined functions.

 
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

used to write any complex program.

 The C compiler combines the capabilities of an assembly language with features of a

high-level language.

 Programs Written in C are efficient and fast. This is due to its variety of data type and

powerful operators.

 It is many time faster than BASIC.

 C is highly portable this means that programs once written can be run on another

machines with little or no modification.

 Another important feature of C program, is its ability to extend itself.

 A C program is basically a collection of functions that are supported by C library. We

can also create our own functions and add it to C library.

 C language is the most widely used language in operating systems and embedded

system development today.


C CHARACTER SET

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:

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

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 Keywords cannot be represented as an identifier.

o The length of the identifiers should not be more than 31 characters.

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.

Now, we describe the strings in different ways:

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.

char a[10] = {'j','a','v','a','t','p','o','i','n','t','\0'}; // String is represented in the form of characters.

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.

There are two ways of declaring constant:

o Using const keyword

o Using #define pre-processor

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,

Example, 111, 1234

0-9 A- F
a- 10 B -11 15-F

1. Decimal constant contains digits from 0-9 such as,

Example, 111, 1234

2. Octal constant contains digits from 0-7, and these types of constants
are always preceded by 0.

Example, 012, 065

3. Hexadecimal constant contains a digit from 0-9 as well as characters


from A-F. Hexadecimal constants are always preceded by 0X.

Example, 0X2, 0Xbcd


Character constants
A character constant contains only a single character enclosed within a
single quote (''). We can also represent character constant by providing
ASCII value of it.

Example, 'A', '9'


Above are the examples of valid character constants.

String constants
A string constant contains a sequence of characters enclosed within double
quotes ("").

Example, "Hello", "Programming"

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.

Example, 202.15, 300.00

These are the valid real constants in 'C'.

A real constant can also be written as,

Mantissa e Exponent

Variables 
 A variable in simple terms is a storage place which has some memory
allocated to it.

 Basically, a variable used to store some form of data.

 Different types of variables require different amounts of memory, and have


some specific set of operations which can be applied on them.

 The value of the C variable may get change in the program.


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;

RULES FOR NAMING C VARIABLE:


1. Variable name must begin with letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name

 Type  Syntax

Variable declaration data_type variable_name;


Example: int x, y, z; char flat, ch;

Variable initialization data_type variable_name = value;


Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;

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);

printf( "The character entered is %c\n",ch);


printf( "The ASCII CODe of character entered is %d",ch);
return 0;
}
1. WAP to swap 2 integers using third variable

2. WAP to swap 2 integers without using third variable

3. WAP that converts temperature from Fahrenheit to Celsius

C= 5.0/9.0 * (F-32)

4. WAP that performs all arithmetic operations with 2 inputs

( %- remainder)

5. WAP to find the area of a triangle given 3 sides (a, b, c)

#include<math.h>

sqrt(s *(s-a)*(s-b)*(s-c))

A b

declare variables a=5 b=6


inputs of a and b a= a+b a 11
print the values of a and b before swapping b= a-b 11-6 = 5
c= b a = a-b 11-5 = 6
b=a b= 5 and a =6
a=c
print values of a b after swapping
INPUT AND OUTPUT FUNCTIONS

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.

syntax of printf( ) function:


 printf(“format-specifiers”, list of variables );

printf( “TEXT” );

printf(“%d, %f, %c”, a,b,c);


printf(“hi hello welcome”);
printf(“ The sum of 2 numbers is %d”, a);
include <stdio.h>
int main()
{
   char ch = 'A';
   char str[20] = "WELCOME";
   float flt = 10.234;
   int no = 150;
   double dbl = 20.123456;
   printf("Character is %c \n", ch);
   printf("String is %s \n" , str);
   printf("Float value is %f \n", flt);
   printf("Integer value is %d\n" , no);
   printf("Double value is %lf \n", dbl);
   printf("Octal value is %o \n", no);
   printf("Hexadecimal value is %x \n", no);
   return 0;
}

SCANF FUNCTION IN C LANGUAGE:


 In C programming language, scanf() function is used to read character, string, numeric data from
keyboard
 Consider below example program where user enters a character. This value is assigned to the variable
“ch” and then displayed.
 Then, user enters a string and this value is assigned to the variable “str” and then displayed.

syntax of scanf( ) function:


 scanf( “format-specifiers”, address of variables );

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:

Enter any character


a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai

#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);

printf("enter a float number\n");


scanf("%f",&f);
printf("enter an integer\n");
scanf("%d",&i);
printf("enter a double(long float)\n");
scanf("%lf",&d);

printf("%c, %f, %d, %lf",ch,f,i,d);


return 0;
}

#include <stdio.h>
int main()
{

int a,b,c;

printf("enter the values of a b and c\n");


scanf("%d-%d-%d",&a,&b,&c);
printf(" The values are %d, %d, %d", 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;

printf( "Enter a value :");


c = getchar( );

printf( "\nYou entered: ");


putchar( c );

return 0;
}
Enter a value : t
You entered: t

The gets() and puts() Functions


The char *gets(char *s) function reads a line from stdin into the buffer pointed to
by s until either a terminating newline or EOF (End of File).
The int puts(const char *s) function writes the string 's' and 'a' trailing newline
to stdout.
NOTE: Though it has been deprecated to use gets() function, Instead of using gets,
you want to use fgets().
#include <stdio.h>
int main( ) {

char str[100];
printf( "Enter a value :");
gets( str );

printf( "\nYou entered: ");


puts( 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];

printf( "Enter a character :");


gets(ch);

printf( "\nYou entered: ");


puts(ch);

printf( "Enter a character :");


scanf("%s",ch);

printf( "\nYou entered: ");


printf("%s",ch);
return 0;
}
Enter a character :hi hello welcome                                                                                             
                                                                                                                                
You entered: hi hello welcome                                                                                                   
Enter a character :hi hello welcome                                                                                             
                                                                                                                                
You entered: hi 
SRIVANI P

You might also like