Structure-of-a-Simple-C-Program1-Data-Types-and-Input-Output-Statement
Structure-of-a-Simple-C-Program1-Data-Types-and-Input-Output-Statement
Language
• /* 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 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.
#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.
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.
7. getch();
This allows you to read the output on the screen.
8. }
This indicates that the end of the function has
been reached.
DATA TYPES
Objectives:
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;
}
void displayLocal() {
// Local variable declaration
int localVar = 20;
int main() {
// Calling a function that uses local variable
displayLocal();
return 0;
}
DATA TYPES in C Language
● 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
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:
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.
z is 7
Output:
• The putchar() function
Syntax:
putchar(variable);
Syntax:
puts(str);
The Input Statements
• The scanf() function
Where:
#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