Data type
Fri, 08 Nov 2024
printf
function. Think of printf
as your way to communicate with the user through messages or results.""
.printf("Welcome to C programming!"); // Text wrapped in double quotes
Welcome to C programming!
printf("This line works!"); //This is correct
printf(This line will cause an error.); // Missing double quotes
printf("Welcome to C programming!");
printf("Let's learn step-by-step.");
printf("Keep coding!");
Welcome to C programming!Let's learn step-by-step.Keep coding!
\n
.\n
) is known as an escape sequence.printf("Welcome to C programming!\n");
printf("Let's learn step-by-step.\n");
printf("Keep coding!\n");
Welcome to C programming!
Let's learn step-by-step.
Keep coding!
printf
.printf("Welcome to C programming!\nLet's learn step-by-step.\nKeep coding!\n");
Welcome to C programming!
Let's learn step-by-step.
Keep coding!
\t
---- Creates a horizontal tab\\
---- Inserts a backslash character\”
---- Inserts a double quoteprintf("Hello\tWorld!\n"); // Inserts a tab between words
printf("Backslash: \\\n"); // Prints a single backslash
printf("He said, \"Keep learning!\"\n"); // Prints quotes
Hello World! Backslash: \
He said, "Keep learning!"
//
).// This line of code displays a greeting message
printf("Hello, C Programmers!");
printf("Welcome to C!"); // This prints a Welcome to C!
//
, so it only prints the message./*
and end with */
./*for all beginner programmers in C.
Keep up the great work! */
printf("You are doing great! Keep it up!");
You are doing great! Keep it up!
/*
and */
is ignored by the compiler.//
for short comments and /* */
for longer explanations.type variableName = value;
type
: Defines the kind of data the variable will hold (like int
for whole numbers, float
for decimal numbers, and char
for characters).variableName
: The name of the variable, which should follow naming rules.int age = 21; // Declares an integer variable and assigns a value of 21
int age; // Declares the variable
age = 21; // Assigns a value later
int
: For storing whole numbers, like 100, -45, or 0.int score = 85; // Stores the whole number 85
float
: For storing numbers with decimals, like 3.14 or -2.71.float price = 4.99; // Stores the decimal number 4.99
char
: For storing single characters like 'A', 'b', or '5'.char grade = 'A'; // Stores the character 'A'
int age; // Correct
int _score; // Correct
int 3rdValue; // Incorrect - starts with a number
Age
and age
are treated as different.int Age = 21; // Correct
int age = 18; // Correct but different from 'Age'
int first_name; // Correct
int first-name; // Incorrect - contains a dash
int
, char
, and float
, case
.int return; // Incorrect - 'return' is a reserved keyword
int num; // Correct
printf()
function to specify the type of data you want to print.%d
: For printing integers.%f
: For printing floating-point numbers (decimals).%c
: For printing characters.%s
: For printing strings (text).printf()
function with appropriate format specifiers:int score = 85;
char grade = 'B';
printf("Score: %d, Grade: %c", score, grade); // Outputs: Score: 85, Grade: B
printf("My lucky number is: %d", 25); // Outputs: My lucky number is: 25
printf("My favorite letter is: %c", 'M'); // Outputs: My favorite letter is: M
int x = 20, y=8, z=60;
printf("%d", x + y + z);
int x, y, z;
x = y = z = 100;
printf("%d", x + y + z);
int score = 90; // Initially, the score is 90
printf("Score=%d\n", score);
score = 95; // Now, the score is updated to 95
printf("New Score=%d", score);
int points = 30;
int bonus = 50;
points = bonus; // Now, points holds the value 50
printf("%d", points); // Outputs: 50
int
or return
.Int
and int
are different.int
, float
, double
, if
, else
, char
, return
, case
, default
etcIn C programming, every variable must have a specific data type.
This data type tells the program what kind of value the variable can hold.
For example, it could be a whole number, a number with decimals, or a single character.
Understanding data types is important because it helps the program use memory efficiently and perform calculations correctly.
int
%d
or %i
float
double
char
%c
int
, float
, and double
.int age = 25;
printf("%d", age); // Output: 25
float
and double
Typesfloat
and double
.float price = 4.99;
double largeValue = 123456789.123456;
printf("%f\n", price); // Output: 4.990000
printf("%lf", largeValue); // Output: 123456789.123456
float temperature = 36.5;
printf("%f\n", temperature); // Output: 36.500000 (Default)
printf("%.1f\n", temperature); // Output: 36.5 (1 decimal place)
printf("%.2f\n", temperature); // Output: 36.50 (2 decimal places)
printf("%.4f", temperature); // Output: 36.5000 (4 decimal places)
e
or E
to indicate the power of 10.float largeNumber = 3.5e2; // Equivalent to 3.5 * 10^2 (350)
double smallNumber = 1.2E-3; // Equivalent to 1.2 * 10^-3 (0.0012)
printf("%f\n", largeNumber); // Output: 350.000000
printf("%lf", smallNumber); // Output: 0.001200
char
Type for Single Characters.char
data type is used to store a single character, such as a letter, number, or symbol.char grade = 'B';
printf("%c", grade); // Output: B
char firstLetter = 65; // ASCII value for 'A'
char secondLetter = 66; // ASCII value for 'B'
char thirdLetter = 67; // ASCII value for 'C'
printf("%c", firstLetter); // Output: A
printf("%c", secondLetter); // Output: B
printf("%c", thirdLetter); // Output: C
char
Typechar
variable is intended to hold a single character, which occupies exactly 1 byte of memory.char myChar = 'Hello'; // Incorrect: only the last character 'o' is stored
printf("%c", myChar); // Output: o
char greeting[] = "Welcome"; // Correct way to define a string
printf("%s", greeting); // Output: Welcome
char
type. int a = 7;
int b = 3;
int result = a / b;
printf("%d", result); // Outputs: 2
float myFloat = 8; // Automatically converts to 8.000000
printf("%f", myFloat); // 8.000000
int myInt = 5.89; // The decimal part is cut off
printf("%d", myInt); // Outputs: 5
When conversions happen automatically, you might lose valuable data.
For example, if you perform calculations involving two integers but need a precise result with decimals, the output won’t be what you expect:
Example:
float result = 7 / 3;
printf("%f", result); // Outputs: 2.000000 (not 2.33)
The numbers being divided are still integers, so C only gives the whole number part in the result.
float result = (float) 7 / 3;
printf("%f", result); // Outputs: 2.333333
You can also use explicit conversion on variables:
Example:
int a = 7;
int b = 3;
float result = (float) a / b;
printf("%f", result); // Outputs: 2.333333
+
, -
, *
, /
, %
=
):X = 10
+=
):X += 5
is same as X = X + 5
-=
):X -= 5
is same as X = X - 5
*=
):X *= 5
is same as X = X * 5
/=
):X /= 5
is same as X = X / 5
%=
):X %= 5
is same as X =X % 5
&=
):X &= 5
is same as X = X & 5
.|=
):X |= 5
is same as X = X | 5
^=
):X ^= 5
is same as X = X ^ 5
>>=
):X >>= 5
is same as X = X >> 5
<<=
):X <<= 5
is same as X = X << 5
.==
)X == Y
Returns 1 if X is equal to Y!=
)X != Y
Returns 1 if X is not equal to Y>
)X > Y
Returns 1 if X is greater than Y<
):X < Y
Returns 1 if X is less than Y>=
):X >= Y
Returns if X is greater than or equal to Y<=
):X <= Y
Returns 1 if X is less than or equal to Y&&
):COND1 && COND2
Returns 1 if both conditions are true||
):COND1 || COND2
Returns 1 if one of the conditions are true!
):!COND
Reverses the result; returns 0 if the result was 1Bitwise AND (&
):
Bitwise OR (|
):
Bitwise NOT (~):
printf()
is used to display output in C.scanf()
function comes in handy.scanf()
:To take input from the user, the scanf()
function is used.
It reads the input based on format specifiers and assigns it to a variable.
It takes two arguments:
Both single and multiple input can be taken using it
// Create two variables
int score; char grade;
// Ask the user to enter a score and a grade
printf("Enter your score and grade (like 95 A): ");
// Get and save both inputs
scanf("%d %c", &score, &grade);
// Print the score and grade
printf("Your score is: %d\n", score);
printf("Your grade is: %c\n", grade);
Enter your score and grade (like 95 A): |
Enter your score and grade (like 95 A): 97 A
Your score is: 97 Your grade is: A
scanf()
can also take string input as well.
scanf()
reads until it encounters a whitespace character (like a space or tab). This means it can only read one word at a time.// Create a string to store the name
char Name [30];
// Prompt the user to input their name
printf("Enter your first name: ");
// Get and save the name
scanf("%s", Name);
// Output the name
printf("Hello %s", Name);
Enter your first name: Tasnim
Hello Tasnim
scanf()
with strings, you don’t need the &
operator.Limitations of scanf()
with Strings
scanf()
stops reading a string when it encounters a space, which means it only captures the first word entered.fgets()
to Take Multi-Word Inputchar fullName [30];
printf("Type your full name: ");
fgets (fullName, sizeof(fullName), stdin);
printf("Hello %s", fullName);
Type your full name: John Doe
Hello John Doe
fgets()
Works:stdin
), which refers to the standard input (keyboard).This is a comment 1.
This is a comment 2.