






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide to variables and data types in c programming. It covers the fundamentals of variable declaration, definition, and initialization, along with different types of variables like local, global, static, and automatic. The document also explains the various data types in c, including basic types like int, char, float, and double, as well as derived types like arrays, pointers, structures, and unions. It emphasizes the importance of understanding data types for efficient memory allocation, data manipulation, and code readability.
Typology: Lecture notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).
The printf() function is used for output. It prints the given statement to the console. The syntax of printf() function is given below:
The scanf() function is used for input. It reads the input data from the console.
enter a number: cube of number is: The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable. The printf("cube of number is:%d ",numbernumbernumber)** statement prints the cube of number on the console.
Let's see a simple example of input and output in C language tha t prints addition of 2 numbers.
Output enter first number: enter second number: sum of 2 numbers:
A variable is the name of the memory location. It is used to store information. Its value can be altered and reused several times. It is a way to represent memory location through symbols so that it can be easily identified. Variables are key building elements of the C programming language used to store and modify data in computer programs. A variable is a designated memory region that stores a specified data type value. Each variable has a unique identifier , its name , and a data type describing the type of data it may hold.
The syntax for defining a variable in C is as follows:
Let us see the syntax to declare a variable:
The general syntax for variable declaration is
The process of reserving memory space for the variable to keep its contents during program execution is known as a variable definition. It is based on the data type and connects the variable name with a particular memory address of sufficient size. A variable in C can be declared and defined in the same statement, although they can also be separated if necessary.
Variable declaration is the act of informing the compiler about the existence and data type of a variable. It informs the compiler that a variable with a specific name and data type will be used in the program, but that memory for the variable still needs to be allocated. Not explicitly initialized variables will contain garbage/random data that may result in unexpected program behavior.
There are many types of variables in c:
A variable that is declared inside the function or block is called a local variable. It must be declared at the start of the block.
You must have to initialize the local variable before it is used.
A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions. It must be declared at the start of the block.
A variable that is declared with the static keyword is called static variable. It retains its value between multiple function calls.
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.
We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.
The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals. The memory size of the basic data types may change according to 32 or 64-bit operating system. Let's see the basic data types. Its size is given according to 32-bit architecture. Data Types Memory S ize Range char 1 byte −128 to 127 signed char 1 byte −128 to 127 unsigned char 1 byte 0 to 255 short 2 byte −32,768 to 32, signed short 2 byte −32,768 to 32, unsigned short 2 byte 0 to 65, int 2 byte −32,768 to 32, signed int 2 byte −32,768 to 32, unsigned int 2 byte 0 to 65, short int 2 byte −32,768 to 32, signed short int 2 byte −32,768 to 32, unsigned short int 2 byte 0 to 65, long int 4 byte - 2,147,483,648 to 2,147,483, signed long int 4 byte - 2,147,483,648 to 2,147,483, unsigned long int 4 byte 0 to 4,294,967, float 4 byte double 8 byte long double 10 byte
Integers are entire numbers without any fractional or decimal parts, and the int data type is used to represent them. It is frequently applied to variables that include values , such as counts, indices , or other numerical numbers. The int data type may represent both positive and negative numbers because it is signed by default. An int takes up 4 bytes of memory on most devices, allowing it to store values between around - 2 billion and +2 billion.
Individual characters are represented by the char data type. Typically used to hold ASCII or UTF- 8 encoding scheme characters , such as letters, numbers, symbols , or commas. There are 256 characters that can be represented by a single char, which takes up one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in single quotes.
To represent integers, use the floating data type. Floating numbers can be used to represent fractional units or numbers with decimal places. The float type is usually used for variables that require very good precision but may not be very precise. It can store values with an accuracy of about 6 decimal places and a range of about 3.4 x 1038 in 4 bytes of memory.
Use two data types to represent two floating integers. When additional precision is needed, such as in scientific calculations or financial applications, it provides greater accuracy compared to float. Double type , which uses 8 bytes of memory and has an accuracy of about 15 decimal places, yields larger values. C treats floating point numbers as doubles by default if no explicit type is supplied.
Beyond the fundamental data types, C also supports derived data types, including arrays, pointers, structures, and unions. These data types give programmers the ability to handle heterogeneous data, directly modify memory, and build complicated data structures.
An array, a derived data type , lets you store a sequence of fixed-size elements of the same type. It provides a mechanism for joining multiple targets of the same data under the same name. The index is used to access the elements of the array, with a 0 index for the first entry. The size of the array is fixed at declaration time and cannot be changed during program execution. The array components are placed in adjacent memory regions. Here is an example of declaring and utilizing an array:
Values in the array: 10 20 30 40 50
A derived data type called a union enables you to store various data types in the samememory address. In contrast to structures, where each member has a separate memory space, members of a union all share a single memory space. A value can only be held by one member of a union at any given moment. When you need to represent many data types interchangeably, unions come in handy. Like structures, you can access the members of a union by using the dot (.) operator. Here is an example of a union being declared and used:
A set of named constants or enumerators that represent a collection of connected values can be defined in C using the enumeration data type (enum). Enumerations give you the means to give names that make sense to a group of integral values, which makes your code easier to read and maintain. Here is an example of how to define and use an enumeration in C:
Output: Today is 2
The void data type in the C language is used to denote the lack of a particular type. Function return types, function parameters , and pointers are three situations where it is frequently utilized.
A void return type function does not produce a value. A void function executes a task or action and ends rather than returning a value. Example:
The parameter void can be used to indicate that a function accepts no arguments. Example:
Any address can be stored in a pointer of type _void_* , making it a universal pointer. It offers a method for working with pointers to ambiguous or atypical types. Example:
1. void dataPtr;* The void data type is helpful for defining functions that don't accept any arguments when working with generic pointers or when you wish to signal that a function doesn't return a value. It is significant to note that while _void_* can be used to build generic pointers, void itself cannot be declared as a variable type. Here is a sample of code that shows how to utilize void in various situations: