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

0C Data Types_112316

The document explains data types in C programming, highlighting the necessity of specifying a data type for variables and using appropriate format specifiers in the printf() function. It provides examples of basic data types such as integers, floating point numbers, and characters, along with their corresponding format specifiers. Additionally, it includes exercises for practicing the assignment of data types and format specifiers.

Uploaded by

darrylyann5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

0C Data Types_112316

The document explains data types in C programming, highlighting the necessity of specifying a data type for variables and using appropriate format specifiers in the printf() function. It provides examples of basic data types such as integers, floating point numbers, and characters, along with their corresponding format specifiers. Additionally, it includes exercises for practicing the assignment of data types and format specifiers.

Uploaded by

darrylyann5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Data Types

Data Types
As explained in the Variables chapter, a variable in C must be a
specified data type, and you must use a format specifier inside
the printf() function to display it:

Example
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);

#include <stdio.h>

int main() {
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}

Basic Data Types


The data type specifies the size and type of information the variable will
store.

In this tutorial, we will focus on the most basic ones:


Basic Format Specifiers
There are different format specifiers for each data type. Here are some of
them:

Example 1 :

#include <stdio.h>

int main() {
int myNum = 5; // integer

printf("%d\n", myNum);
printf("%i\n", myNum);
return 0;
}

Example 2 :

#include <stdio.h>

int main() {
float myFloatNum = 5.99; // Floating point number

printf("%f", myFloatNum);
return 0;
}
Example 3 :

#include <stdio.h>

int main() {
double myDoubleNum = 19.99; // Double (floating point number)

printf("%lf", myDoubleNum);
return 0;
}

Example 4 :

#include <stdio.h>

int main() {
char myLetter = 'D'; // Character

printf("%c", myLetter);
return 0;
}

Example 5 :

#include <stdio.h>

int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);

return 0;
}

Exercise:
Add the correct data type for the following variables:

myNum = 5;
myFloatNum = 5.99;
myLetter = 'D';
Exercise:
Add the correct format specifier to print the value of the following variable:

char myLetter = 'D';


printf(" ", myLetter);

You might also like