C Program Structure
C Program Structure
C Program Structure
A C program involves the following sections:
#include<stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
or in a different way
/* Author: C-programmer
Date: 05-06-2021
Description:
Writes the words "Hello, World!" on the screen */
#include<stdio.h>
#include<conio.h>
void main()
{
printf("Hello, World!\n");
return;
}
Program Output:
The above example has been used to print Hello, World! Text on the screen.
or
This is a comment block, which is ignored by the compiler. Comment
can be used anywhere in the program to add info about the program
or code block, which will be helpful for developers to understand the
existing code in the future easily.
#include<stdio.h> stdio is standard for input/output, this allows us to use some commands
which includes a file called stdio.h.
or
This is a preprocessor command. That notifies the compiler to include
the header file stdio.h in the program before compiling the source-
code.
int/void main() int/void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C
program must contain only one main function.
or
This is the main function, which is the default entry point for every C
program and the void in front of it indicates that it does not return a
value.
Braces Two curly brackets "{...}" are used to group all statements.
or
Curly braces which shows how much the main() function has its
scope.
printf() It is a function in C, which prints text on the screen.
or
This is another pre-defined function of C which is used to be
displayed text string in the screen.
return 0 At the end of the main function returns value 0.
• Declaration part
• Execution part
The declaration part is used to declare all variables that will be used within the program.
There needs to be at least one statement in the executable part, and these two parts
are declared within the opening and closing curly braces of the main(). The execution of
the program begins at the opening brace '{' and ends with the closing brace '}'. Also, it
has to be noted that all the statements of these two parts need to be terminated with a
semi-colon.
The sub-program section deals with all user-defined functions that are called from the
main(). These user-defined functions are declared and usually defined after the main()
function.
void main()
{
int a,b,c;
printf("Please enter any two numbers: \n");
scanf("%d %d", &a, &b);
c = a + b;
printf("The addition of two number is: %d", c);
}
Output:
Outline:
# Managing Input/Output
# Reading Character In C
# Writing Character In C
# Formatted Input
# Reading and Writing Strings in C
Managing Input/Output
I/O operations are useful for a program to interact with users. stdlib is the standard C
library for input-output operations. While dealing with input-output operations in C, two
important streams play their role. These are:
Standard input or stdin is used for taking input from devices such as the keyboard as a
data stream. Standard output or stdout is used for giving output to a device such as a
monitor. For using I/O functionality, programmers must include stdio header-file within
the program.
Reading Character In C
The easiest and simplest of all I/O operations are taking a character as input by reading
that character from standard input (keyboard). getchar() function can be used to read a
single character. This function is alternate to scanf() function.
Syntax:
var_name = getchar();
Example:
#include<stdio.h>
void main()
{
char title;
title = getchar();
}
There is another function to do that task for files: getc which is used to accept a
character from standard input.
Syntax:
Writing Character In C
Similar to getchar() there is another function which is used to write characters, but one
at a time.
Syntax:
putchar(var_name);
Example:
#include<stdio.h>
void main()
{
char result = 'P';
putchar(result);
putchar('\n');
}
Similarly, there is another function putc which is used for sending a single character to
the standard output.
Syntax:
Formatted Input
It refers to an input data which has been arranged in a specific format. This is possible
in C using scanf(). We have already encountered this and familiar with this function.
Syntax:
void main()
{
int var1= 60;
int var2= 1234;
scanf("%2d %5d", &var1, &var2);
}
Input data items should have to be separated by spaces, tabs or new-line and the
punctuation marks are not counted as separators.
When a value is stored in a particular variable, then you cannot print the value stored in
the variable straightforwardly without using the format specifiers. You can retrieve the
data that are stored in the variables and can print them onto the console screen by
implementing these format specifiers in a printf() function.
There are mostly six types of format specifiers that are available in C.
Outline:
Specifying Precision
We can specify the precision by using '.' (Dot) operator which is followed by integer and
format specifier.
int main()
{
float x=12.2;
printf("%.2f", x);
return 0;
}
Output
Character Format Specifier %c
The %c format specifier is implemented for representing characters. This is used with
printf() function for printing the character stored in a variable. When you want to print a
character data, you should incorporate the %c format specifier.
Syntax:
printf("%c",<variable name>);
Escape Sequence in C
An escape sequence in C language is a sequence of characters that doesn't represent
itself when used inside string literal or character.
#include<stdio.h>
int main(){
int number=50;
printf("You\n are \n learning \n\'c\' language\n\"Do you know C language\"");
return 0;
}
Output:
You
are
learning
'c' language
"Do you know C language"
Comments in C
Comments in C language are used to provide information about lines of code. It is
widely used for documenting code. There are 2 types of comments in the C language.
2. Multi-Line Comments
Single line comments are represented by double slash \\. Let's see an example of a
single line comment in C.
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Even you can place the comment after the statement. For example:
/*
code
to be commented
*/
#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
return 0;
}
Output:
Hello C