Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

C Programming Session 3 Ip Op Var Data Operator

The document discusses printf() and scanf() functions in C language. - printf() is used for output and prints a formatted string to the console. scanf() is used for input and reads data from the console. - It provides examples of using printf() and scanf() to get user input, perform calculations, and print outputs. - It also discusses variables, data types, keywords and identifiers in C language.

Uploaded by

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

C Programming Session 3 Ip Op Var Data Operator

The document discusses printf() and scanf() functions in C language. - printf() is used for output and prints a formatted string to the console. scanf() is used for input and reads data from the console. - It provides examples of using printf() and scanf() to get user input, perform calculations, and print outputs. - It also discusses variables, data types, keywords and identifiers in C language.

Uploaded by

Kiran Kardile
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

printf() and scanf() in C

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).

printf() function
The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

1. printf("format string",argument_list);  

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function
The scanf() function is used for input. It reads the input data from the console.

1. scanf("format string",argument_list);  

Program to print cube of given number


Let's see a simple example of c language that gets input from the user and prints the
cube of the given number.

1. #include<stdio.h>    
2. int main(){    
3. int number;    
4. printf("enter a number:");    
5. scanf("%d",&number);    
6. printf("cube of number is:%d ",number*number*number);    
7. return 0;  
8. }    

Output

enter a number:5
cube of number is:125
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 ",number*number*number) statement prints


the cube of number on the console.

Program to print sum of 2 numbers


Let's see a simple example of input and output in C language that prints addition of 2
numbers.

1. #include<stdio.h>    
2. int main(){    
3. int x=0,y=0,result=0;  
4.   
5. printf("enter first number:");  
6. scanf("%d",&x);  
7. printf("enter second number:");  
8. scanf("%d",&y);  
9.   
10.result=x+y;  
11.printf("sum of 2 numbers:%d ",result);  
12.  
13.return 0;  
14.}    

Output

enter first number:9


enter second number:9
sum of 2 numbers:18
Variables in C
A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily


identified.

Let's see the syntax to declare a variable:

1. type variable_list;  

The example of declaring the variable is given below:

1. int a;  
2. float b;  
3. char c;  

Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below:

1. int a=10,b=20;//declaring 2 variable of integer type  
2. float f=20.8;  
3. char c='A';  

Rules for defining variables


o A variable can have alphabets, digits, and underscore.

o A variable name can start with the alphabet, and underscore only. It can't start
with a digit.

o No whitespace is allowed within the variable name.

o A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid variable names:

1. int a;  
2. int _ab;  
3. int a30;  
Invalid variable names:

1. int 2;  
2. int a b;  
3. int long;  

Types of Variables in C
There are many types of variables in c:

1. local variable

2. global variable

3. static variable

4. automatic variable

5. external variable

Local Variable
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.

1. void function1()
2. {  
3. int x=10;//local variable  
4. }  

You must have to initialize the local variable before it is used.

Global Variable
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.

1. int value=20;//global variable  
2. void function1(){  
3. int x=10;//local variable  
4. }  

Static Variable
A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

1. void function1(){  
2. int x=10;//local variable  
3. static int y=10;//static variable  
4. x=x+1;  
5. y=y+1;  
6. printf("%d,%d",x,y);  
7. }  

If you call this function many times, the local variable will print the same value for
each function call, e.g, 11,11,11 and so on. But the static variable will print the
incremented value in each function call, e.g. 11, 12, 13 and so on.

Automatic Variable
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.

1. void main(){  
2. int x=10;//local variable (also automatic)  
3. auto int y=20;//automatic variable  
4. }  

External Variable
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.

myfile.h

1. extern int x=10;//external variable (also global)  
program1.c

1. #include "myfile.h"  
2. #include <stdio.h>  
3. void printValue(){  
4.     printf("Global variable: %d", x);  
5. }  

Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.

There are the following data types in C language.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union


Enumeration Data Type enum

Void Data Type void

Basic Data Types


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 Size 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,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

Int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535


short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

Float 4 byte

Double 8 byte

long double 10 byte

Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant name,
etc. There are only 32 reserved words (keywords) in the C language.

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static


struct switch typedef union unsigned void volatile while

A list of 32 keywords in the c language is given below:

We will learn about all the C language keywords later.

C Identifiers
C identifiers represent the name in the C program, for example, variables, functions,
arrays, structures, unions, labels, etc. An identifier can be composed of letters such as
uppercase, lowercase letters, underscore, digits, but the starting letter should be either
an alphabet or an underscore. If the identifier is not used in the external linkage, then
it is called as an internal identifier. If the identifier is used in the external linkage, then
it is called as an external identifier.

We can say that an identifier is a collection of alphanumeric characters that begins


either with an alphabetical character or an underscore, which are used to represent
various programming elements such as variables, functions, arrays, structures, unions,
labels, etc. There are 52 alphabetical characters (uppercase and lowercase), underscore
character, and ten numerical digits (0-9) that represent the identifiers. There is a total
of 63 alphanumerical characters that represent the identifiers.

Rules for constructing C identifiers


o The first character of an identifier should be either an alphabet or an underscore,
and then it can be followed by any of the character, digit, or underscore.
o It should not begin with any numerical digit.

o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we


can say that identifiers are case sensitive.

o Commas or blank spaces cannot be specified within an identifier.

o Keywords cannot be represented as an identifier.

o The length of the identifiers should not be more than 31 characters.

o Identifiers should be written in such a way that it is meaningful, short, and easy
to read.
Example of valid identifiers

1. total, sum, average, _m _, sum_1, etc.  

Example of invalid identifiers

1. 2sum (starts with a numerical digit)  
2. int (reserved word)  
3. char (reserved word)  
4. m+n (special character, i.e., '+')  

Types of identifiers
o Internal identifier

o External identifier

Internal Identifier

If the identifier is not used in the external linkage, then it is known as an internal
identifier. The internal identifiers can be local variables.

External Identifier

If the identifier is used in the external linkage, then it is known as an external identifier.
The external identifiers can be function names, global variables.

Differences between Keyword and Identifier


Keyword Identifier

Keyword is a pre-defined word. The identifier is a user-defined word

It must be written in a lowercase letter. It can be written in both lowercase and

uppercase letters.

Its meaning is pre-defined in the c compiler. Its meaning is not defined in the c compiler.

It is a combination of alphabetical characters. It is a combination of alphanumeric characters


It does not contain the underscore character. It can contain the underscore character.

Let's understand through an example.

1. int main()  
2. {  
3.     int a=10;  
4.     int A=20;  
5.     printf("Value of a is : %d",a);  
6.     printf("\nValue of A is :%d",A);  
7.     return 0;  
8. }  

Output

Value of a is: 10
Value of A is: 20

The above output shows that the values of both the variables, 'a' and 'A' are different.
Therefore, we conclude that the identifiers are case sensitive.

C Operators
An operator is simply a symbol that is used to perform operations. There can be many
types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of operations in C


language.

o Arithmetic Operators

o Relational Operators

o Shift Operators

o Logical Operators

o Bitwise Operators
o Ternary or Conditional Operators

o Assignment Operator

o Misc Operator

Precedence of Operators in C
The precedence of operator species that which operator will be evaluated first and next.
The associativity specifies the operator direction to be evaluated; it may be left to right
or right to left.

Let's understand the precedence by the example given below:

1. int value=10+20*10;  

The value variable will contain 210 because * (multiplicative operator) is evaluated


before + (additive operator).

The precedence and associativity of C operators is given below:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* &sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift <<>> Left to right

Relational <<= >>= Left to right

Equality == != Left to right

Bitwise AND & Left to right


Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= Right to left


&= ^= |=

Comma , Left to right

You might also like