0% found this document useful (0 votes)
18 views5 pages

Introductionto CProgramming

The document covers programming fundamentals focusing on data types and variables in C. It explains variable declaration, valid identifiers, assignment operations, and fundamental data types such as integers, floating-point numbers, and characters. Additionally, it discusses input and output operations using printf and scanf functions, along with examples demonstrating their usage.

Uploaded by

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

Introductionto CProgramming

The document covers programming fundamentals focusing on data types and variables in C. It explains variable declaration, valid identifiers, assignment operations, and fundamental data types such as integers, floating-point numbers, and characters. Additionally, it discusses input and output operations using printf and scanf functions, along with examples demonstrating their usage.

Uploaded by

kumarach48
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Fundamentals: Data Types and

Variables
Variables
A variable is a named location in memory used to store data for a program. Before a variable can
be used, it must be declared.

Variable Declaration

To declare a variable, you specify its data type followed by its name, ending with a semicolon.

Format: <data type> <Name of the variable>;

Examples:

int age; // Declares an integer variable named 'age'.


float weight; // Declares a floating-point variable named 'weight'.
double number; // Declares a double-precision floating-point variable named 'number'.
char letter; // Declares a character variable named 'letter'.

Variable Names (Identifiers)

Variable names in C must be valid identifiers, following these rules:

Can consist of letters (a-z, A-Z), digits (0-9), and underscores (_).
Cannot begin with a digit.
Cannot contain embedded blank spaces.
Cannot be a reserved keyword (e.g., int, return, if, while, for).
C is case-sensitive, meaning total, Total, and TOTAL are considered three different variable
names.

Storing Values into Variables

The assignment operation stores a value in a variable or changes its existing value. The
assignment operator is the equal sign (=).

Format: variable = expression;

This assigns the value of the expression (rvalue) to the variable (lvalue).
Examples:

age = 23;
weight = 65.3;
number = 12.345;
letter = 'A';

Data Types
Data types define the kind of data a variable can hold and the operations that can be performed
on it.

Fundamental Data Types


Integers:
Whole numbers without a decimal point.
int: Standard integer type.
short int (or short): Shorter integer type, typically uses less memory.
long int (or long): Longer integer type, typically can store larger values.

Real Numbers (Floating-Point):


Numbers that contain a decimal point and a fractional part.
float: Single-precision floating-point type.
double: Double-precision floating-point type, typically offers more precision and a larger
range than float.
long double: Extended-precision floating-point type.

Characters:
Single letters, symbols, or digits.
char: Stores a single character.

Data Type Examples


Data Type Example Values Description
int 10, -5, 0 Whole numbers.
float 3.14, -0.5 Numbers with a decimal point (single precision).
double 123.456789 Numbers with a decimal point (double precision).
char 'A', 'z', '!' Single characters enclosed in single quotes.

Input and Output


Input and output operations in C are performed using streams, which are sequences of bytes.
Input Stream: Bytes flow from a device (like the keyboard) to the program's main memory.
The standard input stream is typically connected to the keyboard.
Output Stream: Bytes flow from main memory to a device (like the screen). The standard
output stream is typically connected to the screen.

Output with printf

The printf function is used to display data to the standard output stream (the screen). It takes a
format control string and optional arguments.

Format: printf(format-control-string, other-arguments);

The format-control-string describes how the other-arguments should be displayed.

Conversion Specifications: These are special character sequences that tell printf how to format
and display data.

Type Conversion Description


Specification
Integer %d or %i Displays a signed decimal integer.
Floating- %f Displays floating-point values in fixed-point notation (for
Point float or double).
Floating- %.2f Displays a floating-point value with exactly 2 digits after
Point the decimal point.
Character %c Displays a single character.
String %s Displays a string (a sequence of characters).

Example:

#include <stdio.h>

int main(void) {
printf("%d\n", 455); // Output: 455
printf("%f\n", 1234567.89); // Output: 1234567.890000 (default precision)
printf("%.2f\n", 3.446); // Output: 3.45 (rounds to 2 decimal places)
printf("%c \n", 'A'); // Output: A
return 0;
}

Input with scanf

The scanf function reads data from the standard input stream (the keyboard). It requires a
format control string to specify the type of data to read and pointers to the variables where the
input should be stored.

Format: scanf(format-control-string, other-arguments);

The other-arguments must be the addresses of the variables where the input will be stored. This
is indicated by using the address-of operator (&).

Conversion Specifications for scanf:

Type Conversion Description


Specification
Integer %d or %i Reads a signed decimal integer. The argument must be a
pointer to an int.
Floating- %f Reads a floating-point value. The argument must be a
Point pointer to a float.
Floating- %lf Reads a floating-point value. The argument must be a
Point pointer to a double.
Character %c Reads a single character. The argument must be a
pointer to a char.
String %s Reads a string. The argument must be a pointer to an
array of char.

Example: To read two numbers from the keyboard and display their sum:

#include <stdio.h>

int main(void) {
int num1, num2, sum;

printf("Enter the first number: ");


scanf("%d", &num1); // Read the first integer into num1

printf("Enter the second number: ");


scanf("%d", &num2); // Read the second integer into num2

sum = num1 + num2; // Calculate the sum

printf("The sum of %d and %d is %d\n", num1, num2, sum); // Display the result

return 0;
}

The char Data Type


The char data type is used to store single characters. Characters are typically enclosed in single
quotes (e.g., 'A', '%', '7').

Storage: Characters are often stored internally as their numerical representation according to
a character set like ASCII.
Reading Characters:
scanf("%c", &variable); can be used to read a single character.
getchar(); is another function that reads a single character from the keyboard.

Character as Integer: A char variable can also be treated as a small integer. This allows you
to perform arithmetic operations on character codes or check their numerical values.

Example:

#include <stdio.h>

int main(void) {
char id;
int score;

printf("Enter a character for ID: ");


// scanf("%c", &id); // Using scanf to read a character
id = getchar(); // Using getchar() to read a character

printf("Enter score: ");


scanf("%d", &score); // Read an integer score

if (score > 10) {


printf("Score is high.\n");
}

// Demonstrating character's numerical value (ASCII)


printf("The character '%c' has the ASCII value %d.\n", id, id);

return 0;
}

ASCII Example Output: If the user enters 'a' for the character ID: The character 'a' has the ASCII
value 97. This shows that the character 'a' is represented internally by the integer value 97 in the
ASCII standard. You can use %c to display it as a character and %d to display its numerical
(ASCII) value.

You might also like