0% found this document useful (0 votes)
6 views

Structure-of-a-Simple-C-Program1-Data-Types-and-Input-Output-Statement

The document provides an overview of the C programming language, focusing on the structure of a C program, comments, data types, variable declaration, and input/output statements. It explains the components of a simple C program, including preprocessor directives, the main function, and the use of constants and variables. Additionally, it covers the syntax for input and output functions, such as printf and scanf, along with format specifiers and escape sequences.

Uploaded by

mediinajess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Structure-of-a-Simple-C-Program1-Data-Types-and-Input-Output-Statement

The document provides an overview of the C programming language, focusing on the structure of a C program, comments, data types, variable declaration, and input/output statements. It explains the components of a simple C program, including preprocessor directives, the main function, and the use of constants and variables. Additionally, it covers the syntax for input and output functions, such as printf and scanf, along with format specifiers and escape sequences.

Uploaded by

mediinajess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

C

Language

Presented by: Marisol De Guzman


Structure of a
C Program
Objectives:

• Familiarized with the structure of


simple C program

• Run a simple program in C and


discuss its components
Structure of a C Program
<pre-processor
directive>
<declaration area>
main()
{
Statement1 /* body of
the program*/
:
:
Statement n
}
Comments in C programming

• are non-executable statements that are used


to provide explanatory comments or
documentation to the source code. The
compiler ignores comments, which have no
effect on how the program is executed. They
make the code easier to read, understand,
and maintain.
There are two types of
comments in C.
1. Single-line comments start with "//" and continue to the end of the
line. Anything that follows "//" on the same line is considered a comment.

• // This is a single-line comment


• int x = 10; // Assigning a value to variable x

2. Multi-Line Comments: Multi-line comments begin with "/" and end


with "/". They can span across multiple lines and are often used for longer
explanations or commenting out blocks of code.

• /* This is a

• multi-line comment */

• /* int y = 20;
• This code is commented out */
In C language, both #define and const are
used for defining constants, but they have
some differences:

• #define: This is a preprocessor directive


that defines a macro. When you use
#define, the preprocessor replaces all
occurrences of the defined macro with its
value before compiling the code. It's a
simple text substitution mechanism
Sample:
#include <stdio.h>

#define PI 3.14159
#define LENGTH 10
#define WIDTH 5

int main() {
// Calculating area of a rectangle
float rectangle = LENGTH * WIDTH;
const: This is a keyword used to declare
variables as constants. When you declare
a variable using const, its value cannot be
changed throughout the program. const
creates read-only variables.
Steps using Turbo C
1. Open Turbo C using the default directory c:\tc\bin
or double click the shortcut key of tc.exe on the
desktop.

2. On the edit window click File menu, then click


New. A window will appear on the screen where
the program can be typed.

3. Type the following:

#include <stdio.h>
#include <conio.h>

void main()
{
clrscr();
printf (“ Starting to learn C
Programming”);
Steps using Turbo C
4. To save the program, select Save command from the file
menu. A dialog box will appear asking for the path
and name of the file name (ex. Exer1.c). You can save
the program after compiling too but saving it before
compilation is more appropriate.

5. Press F9 to compile and link the program.

6. If the compiler recognizes some errors, correct them.


The errors are to be removed by returning to the edit
window. These errors might be a result of a typing
mistake. The compiler will point you to the exact place in
code where you made the mistake.

7. If the program is compiled and link without errors,


select Run from the Run menu or press the Ctr+F9 key
combination.

8. To exit from Turbo C IDE, select Exit from the File menu.
Understanding the
Program
From the previous discussion, you were asked to
code a simple program shown below.

#include <stdio.h>
#include <conio.h>

void main()
{
clrscr();
printf (“ Starting to learn C
Programming”);
getch();
}
Sample Simple Program
Let us discuss what each line of codes
means and does.

1. #include<stdio.h>
2. #include<conio.h>
These lines are called the preprocessors.

3. Void main()
main() stand for the main function in C program.

4. {
This signifies the beginning of the function main.

5. clrscr();
This instruct the computer to clear the screen.
Let us discuss what each line of codes
means and does.

6. printf(“Starting to Learn C Programming”);


This prints whatever inside the quotes inside the
parenthesis.

7. getch();
This allows you to read the output on the screen.

semicolon(;) at the end of each statement


ends. A statement is similar to one complete
sentence.

8. }
This indicates that the end of the function has
been reached.
DATA TYPES
Objectives:

• Identify the types of data to be


used in a program
• Determine the values and the
type of data that variable can
contain
• Declare variable with
appropriate data types
Data Types

Are used to define a variable


before its used. Data type simply
refer to the types of data
associated with variables and
functions.
Variables
● Storage Locations: Variables are named
storage locations that can hold data values.
They are used to represent changing or
dynamic information in a program.
● Declaration: Before using a variable, you
need to declare its type and name. The
declaration specifies the type of data the
variable can hold.
● Assignment: After declaration, you can
assign values to variables. The value of a
variable can change during the execution
of the program.
Example:
#include <stdio.h>
int main() {
int age; // Declaration
age = 25; // Assignment
printf("Age: %d\n", age);
return 0;
}
In the examples above, MAX_VALUE is a constant with a
fixed value, and age is a variable that can hold changing
data. Constants are typically declared using the const
keyword to indicate that their value should not be modified
during the program's execution.
In C programming, variables are classed as global
or local based on their scope and lifetime.

Global Variables:
A global variable is a variable declared outside of
any function, usually at the start of the program.
It can access any portion of the program,
including all functions. It has a lifetime that spans
the duration of the program's execution. It is only
initialized once, at the beginning of the
application.
In C programming, variables are classed as global
or local based on their scope and lifetime.

Global Variables:
A global variable is a variable declared outside of
any function, usually at the start of the program.
It can access any portion of the program,
including all functions. It has a lifetime that spans
the duration of the program's execution. It is only
initialized once, at the beginning of the
application.
// Global variable declaration
int globalVar = 10;

int main() {
// Accessing globalVar inside main function
printf("Value of globalVar inside main: %d\n", globalVar);
// Calling a function that uses globalVar
displayGlobal();
return 0;
}

// Function definition that uses globalVar


void displayGlobal() {
// Accessing globalVar inside displayGlobal function
printf("Value of globalVar inside displayGlobal: %d\n", globalVar);
}
Local Variables:

A local variable is a variable that is defined


within a function or block of code. It is only
available within the function or block that it
is declared in.
It has a limited lifetime that ends when the
function or block in which it is declared exits.
It is initialized every time the function or
block is invoked.
#include <stdio.h>

void displayLocal() {
// Local variable declaration
int localVar = 20;

// Accessing localVar inside displayLocal function


printf("Value of localVar: %d\n", localVar);
}

int main() {
// Calling a function that uses local variable
displayLocal();
return 0;
}
DATA TYPES in C Language

In the C programming language, a data type


is a classification that specifies the type of
data that a variable can store and the
actions that can be performed on that data.
C provides various built-in data types, which
can be classified into the following major
categories:
PRIMARY DATA TYPE
1. Integer (int, short, long, unsigned int, etc.):
Integers are used to represent whole numbers without any
fractional part.
a) int: Represents signed integers typically ranging from -
32,768 to 32,767 (on a 16-bit system).
b) short: Represents shorter signed integers, typically ranging
from -32,768 to 32,767.
c) long: Represents longer signed integers, typically ranging
from -2,147,483,648 to 2,147,483,647.
d) unsigned int: Represents unsigned integers, which do not
have a sign bit and can represent only non-negative
numbers.
2. Character (char):
Characters are used to represent
single characters such as letters,
digits, and special symbols.
char: Represents a single byte,
typically representing a character
from the ASCII character set.
3. float: Represents single-precision
floating-point numbers with 32 bits,
typically capable of representing
values with 6-7 significant digits.

4. double: Represents double-


precision floating-point numbers with
64 bits, typically capable of
representing values with 15-16
significant digits.
5. Void (void):
void is a special data type used to
indicate the absence of a specific type.
It is commonly used as the return type
of functions that do not return a value
or to specify an empty parameter list in
function declarations.
DERIVED DATA TYPE
● Function:
A function is a self-contained chunk of code that completes a
defined task. Functions arrange code into logical chunks,
making it easier to understand, debug, and maintain.

● Array:
An array is a collection of elements with the same data type
that are stored in contiguous memory regions. Arrays provide
efficient storage and retrieval of numerous values under a
single variable name.
● Pointer:
A pointer is a variable that contains the memory
address of another variable. Pointers enable
indirect access to variables and dynamic memory
allocation.

● Reference:
C, unlike C++, does not directly support
references. In C, references are frequently
emulated via pointers.
USER DEFINED DATA TYPE
● Struct (Structure):
A structure is a user-defined data type that allows you to
organize variables from several data types under a single
name. Each variable within a structure is referred to as a
member or field.

● Union:
A union is a user-defined data type, comparable to a
structure, in which all members share the same memory
region. Unions allow numerous variables to share the same
memory space, with only one member active at a time.
● Enum (Enumeration):
An enumeration is a user-defined data type that defines a
collection of named integer constants. Enumerations enable
you to provide symbolic names to a collection of linked
constants, making the code more legible and maintainable.

● Typedef:
Typedef is a C keyword that creates aliases or different
names for existing data types. Typedef enables you to create
custom names for complex data types, making your code
more understandable and portable.
BASIC DATA TYPES
DATA TYPES RANGE KEYWORD
Character 0 to 255 char
Integer -32768 to 32768 int
Short Integer -128 to 128 short int
Long Integer -2147483648 to long int
2147483648
Unsigned Integer 0 to 65535 unsigned int
Floating Pointing 6 digit precision Float
Double Float 12 digit precision double

Note: It is important to note that when declaring


variables , keyword of the data types are being
used.
Rules in constructing
variable name:
 Characters Allowed :
 Underscore ( _ )
 Capital Letters ( A – Z )
 Small Letters ( a-z )
 Digits ( 0-9 )
 Blanks and Commas are not allowed
 No special Symbols other than underscore (_)
are allowed
 First character should not be Reserved Word.
Keywords or Reserved Words are words which
has a special meaning or function in Turbo C.
(eg. int, char, printf, scanf, etc. )
 It should be meaningful and descriptive
Variable declaration
Syntax :
DataType
VariableName;
Example :
int num1;
int
x=10,y=5,z=3;
char ans;
float
INPUT AND
OUTPUT
STATEMENT
Objectives:

• Identify the input and output


statements in C
• Determine the functions of
different format specifiers
• Write program using the input
and output statements in C
The Output Statements
• The printf() function

- In C language, displaying output to the


console (or terminal) is commonly done using the printf
function from the standard input/output library (stdio.h).
The printf function allows you to format and print text and
values.

- Output statements in C programming


are done with the printf() function. The
printf() function displays formatted data and
text.
The Output Statements

Syntax:
printf (“format string”, argument list”);

• Format string
- Describes the output format and
optional arguments corresponds to each
conversion specification. This maybe a
collection of escape sequence or/and format
specifier or/and string constant. It directs the
printf function to display the entire text
enclosed within the double quotes without any
change.
In this example:

#include <stdio.h>: This line includes the standard input/output library,


which provides functions like printf.

int main(): This is the main function where the program execution begins.

printf("Hello, World!\n");: This line uses the printf function to display the
text "Hello, World!" followed by a newline (\n) character. The newline
character is used to move the cursor to the next line.

return 0;: This statement indicates that the program executed successfully.
The main function in C should return an integer, typically 0 to indicate
success.
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}
Escape Sequence
 Is a pair of character. The first letter is a slash
followed by a character. It helps us to represent
within the format string and the invisible and non-
printed character.
Escape Sequence Meaning
\n New line
\t Tab
\b Back space
\a Bell
\o Null character
\? To print question mark
\\ To print slash
\’ To print single quote
\” To print double quote
Format Specifier
 There are several format specifiers. Below are the list
of format specifiers that are commonly used. The one
you use should depend on the type of the variable
you wish to print out.

Format Specifier Type


%d int
%c char
%f float
%lf double
%s string
%x hexadecimal
Example
#include<stdio.h>
#include<conio>
main()
{
int x, y, z;
x = 5;
y = 2;
z = x+y;
printf(“z is %d”, z);
getch();
return 0;
}

z is 7
Output:
• The putchar() function

- Display a single character on the screen

Syntax:
putchar(variable);

• The puts() function

- Is used to display a string on a standard


output device. The puts() function automatically
inserts a newline character at the end of each
string.

Syntax:
puts(str);
The Input Statements
• The scanf() function

- In C programming, scanf is a function used


to read formatted input from the standard input (usually
the keyboard) and store it into variables. The format
specifiers used in scanf tell the function the type of data it
should expect to read and how to interpret it.

- Data is entered by the user by using


the scanf() function. The scanf() function reads
the integers, floating-point variables, and
character. The format specifiers are also used
in scanf() function to determine the type and
amount of data to be used.
The Input Statements
Syntax:
printf (“format string”, argument
list”);

Where:

 Format string must be a text


enclosed in double quotes.
 Argument list contains a list of
variables each preceded by the
address list and separated by comma.
Here are some common format specifiers
used in scanf:
%d - Read an integer value.
%f - Read a floating-point value.
%lf - Read a double-precision floating-point value.
%c - Read a single character.
%s - Read a string of characters (stops reading at whitespace).
%u - Read an unsigned integer.
%x - Read an integer in hexadecimal format.
%o - Read an integer in octal format.
%p - Read a pointer address (void pointer).
%ld - Read a long integer.
%lu - Read an unsigned long integer.
%lld - Read a long long integer.
Here are some common format specifiers
used in scanf:

%llu - Read an unsigned long long integer.


%lf - Read a double-precision floating-point number.
%Lf - Read a long double-precision floating-point number.
%hx - Read a short integer in hexadecimal format.
%ho - Read a short integer in octal format.
%hi - Read a short integer.
%lu - Read an unsigned long integer.
%le - Read a floating-point number in scientific notation.
%n - Read the number of characters read so far.
scanf(“%d”, &num);

• The getchar() function

- Reads a single character from the


keyboard and displays on screen.

• The gets() function

- Is used to input sequence of


character from the keyboard including
spaces.
Example:

A C program that demonstrate the use of the following: (1)


integer, (2) float (3) char (4) string and (5) unsignedint.

#include <stdio.h>

int main() {
int integer;
float floating;
char character;
char string[100];
unsigned int unsignedInt;
printf("Enter an integer: ");
scanf("%d", &integer);
printf("Enter a floating-point number: ");
scanf("%f", &floating);
printf("Enter a character: ");
scanf(" %c", &character);
printf("Enter a string: ");
scanf("%s", string);
printf("Enter an unsigned integer: ");
scanf("%u", &unsignedInt);
// Displaying the values
printf("\nInteger: %d\n", integer);
printf("Floating-point number: %.2f\n", floating);
// Displaying with 2 decimal places

printf("Character: %c\n", character);


printf("String: %s\n", string);
printf("Unsigned integer: %u\n", unsignedInt);
return 0;
}
Output:
Example:
#include<stdio.h>
#include<conio.h>
main()
{
int x, y, z;
printf(“Enter values for x and y:”);
scanf (“%d %d”, &x, &y);
z= x+y;
printf (“z is %d”, z);
getch();
return o;
}

Note: when the values 5 and 7 are entered for x and y


respective, the Output :
z is 12
THANK YOU!

You might also like