0% found this document useful (0 votes)
28 views15 pages

C Program Structure

c notes

Uploaded by

asenathderlene27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views15 pages

C Program Structure

c notes

Uploaded by

asenathderlene27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Data Input and Output

C Program Structure
A C program involves the following sections:

• Documentations (Documentation Section)


• Preprocessor Statements (Link Section)
• Global Declarations (Definition Section)
• The main() function
o Local Declarations
o Program Statements & Expressions
• User Defined Functions

Let's begin with a simple C program code.


Outline:

# Sample Code of C "Hello World" Program


# Let's look into various parts of the above C program.
# Comments in C - Video Tutorial
# Basic Structure of C Program

Sample Code of C "Hello World" Program


Example:
/* Author: C-programmer
Date: 05-06-2021
Description:
Writes the words "Hello, World!" on the screen */

#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.

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 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.

Basic Structure of C Program


The example discussed above illustrates how a simple C program looks like and how
the program segment works. A C program may contain one or more sections which are
figured above.
The Documentation section usually contains the collection of comment lines giving the
name of the program, author's or programmer's name and few other details. The
second part is the link-section which instructs the compiler to connect to the various
functions from the system library. The Definition section describes all the symbolic-
constants. The global declaration section is used to define those variables that are used
globally within the entire program and is used in more than one function. This section
also declares all the user-defined functions. Then comes the main(). All C programs
must have a main() which contains two parts:

• 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.

C Input and Output


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:
#include<stdio.h>

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:

Please enter any two numbers:


12
3
The addition of two number is:15
The above program scanf() is used to take input from the user, and
respectively printf() is used to display output result on the screen.

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:

1. Standard Input (stdin)


2. 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.

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:

int getc(FILE *stream);

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:

int putc(int c, FILE *stream);

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:

scanf("control string", arg1, arg2, ..., argn);

The field specification for reading integer inputted number is:


%w sd
Here the % sign denotes the conversion specification; w signifies the integer number
that defines the field width of the number to be read. d defines the number to be read in
integer format.
Example:
#include<stdio.h>

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.

Reading and Writing Strings in C


There are two popular library functions gets() and puts() provides to deal with strings in
C.
gets: The char *gets(char *str) reads a line from stdin and keeps the string pointed to
by the str and is terminated when the new line is read or EOF is reached. The
declaration of gets() function is:
Syntax:

char *gets(char *str);

Where str is a pointer to an array of characters where C strings are stored.


puts: The function - int puts(const char *str) is used to write a string to stdout, but it
does not include null characters. A new line character needs to be appended to the
output. The declaration is:
Syntax:

int puts(const char *str)

where str is the string to be written in C.


C Format Specifiers
Format specifiers can be defined as the operators which are used in association with
printf() function for printing the data that is referred by any object or any variable.

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.

Format specifiers start with a percentage % operator and followed by a special


character for identifying the type of data.

There are mostly six types of format specifiers that are available in C.

Outline:

# List of format specifiers in C


# Integer Format Specifier %d
# Float Format Specifier %f
# Character Format Specifier %c
# String Format Specifier %s
# Unsigned Integer Format Specifier %u
# Long Int Format Specifier %ld

List of format specifiers in C


Format Specifier Description

%d Integer Format Specifier

%f Float Format Specifier

%c Character Format Specifier

%s String Format Specifier


%u Unsigned Integer Format Specifier

%ld Long Int Format Specifier

Integer Format Specifier %d


The %d format specifier is implemented for representing integer values. This is used
with printf() function for printing the integer value stored in the variable.
Syntax:
printf("%d",<variable name>);

Float Format Specifier %f


The %f format specifier is implemented for representing fractional values. This is
implemented within printf() function for printing the fractional or floating value stored in
the variable. Whenever you need to print any fractional or floating data, you have to
use %f format specifier.
Syntax:
printf("%f", <variable name>);

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

String Format Specifier %s


The %s format specifier is implemented for representing strings. This is used in printf()
function for printing a string stored in the character array variable. When you have to
print a string, you should implement the %s format specifier.
Syntax:
printf("%s",<variable name>);

Unsigned Integer Format Specifier %u


The %u format specifier is implemented for fetching values from the address of a
variable having unsigned decimal integer stored in the memory. This is used within
printf() function for printing the unsigned integer variable.
Syntax:
printf("%u",<variable name>);
Long Int Format Specifier %ld
The %ld format specifier is implemented for representing long integer values. This is
implemented with printf() function for printing the long integer value stored in the
variable.
Syntax:
printf("%ld",<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.

It is composed of two or more characters starting with backslash \. For example: \n


represents new line.
List of Escape Sequences in C

Escape Sequence Example

#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.

1. Single Line Comments

2. Multi-Line Comments

Single 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:

printf("Hello C");//printing information

Multi Line Comments


Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many
lines of code, but it can't be nested. Syntax:

/*
code
to be commented
*/

Let's see an example of a multi-Line comment in C.

#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
return 0;
}

Output:

Hello C

You might also like