C Programming 2
C Programming 2
C - Program Structure
C - Program Structure
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
C - Program Structure
/*Writes the words "Hello, World!" on the screen */
#include<stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Let’s look into various parts of the above c
program
/* Comments */
Comments are a way of explaining what makes a
program. The compiler ignores comments and used by
others to understand the code. or
This is a comment block, which is ignored by the
compiler. Comment can be used anywhere in the
program to add info about 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
return 0
At the end of the main function returns value 0.
C input and Output (I/O)
As we all know the three essential functions of a
computer are reading, processing and writing data.
Majority of the programs take data as input, and then
after processing the processed data is being displayed
which is called information. In C programming you
can use scanf() and printf() predefined function to
read and print data.
Example of input and output
#include<stdio.h>
int 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);
}
The above program scanf() is used to take input from
the user, and respectively printf() is used to display
output result on the screen.
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, there are two important streams that play their role.
These are:
Standard Input (stdin)
Standard Output (stdout)
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.
C - Basic Syntax
Tokens in C
A C program consists of various tokens and a token is
either a keyword, an identifier, a constant, a string
literal, or a symbol. For example, the following C
statement consists of five tokens −
printf("Hello, World! \n");
The individual tokens are −
Printf
(
"Hello, World! \n”
)
;
Semicolons
In a C program, the semicolon is a statement
terminator. That is, each individual statement must be
ended with a semicolon. It indicates the end of one
logical entity.
Given below are two different statements −
printf("Hello, World! \n");
return 0;
Comments
int age;
there must be at least one whitespace character
(usually a space) between int and age for the compiler
to be able to distinguish them. On the other hand, in
the following statement −
fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary between fruit
and =, or between = and apples, although you are free
to include some if you wish to increase readability.