0%
Loading ...

C Language Basics

This category contains tutorials related to C language basics.

Pointer in C

In this tutorial, we will learn about Pointer in C. C Pointer are variables which store the address of another variable. We can create a Pointer variable of any type such as int, char, array, function, or any other pointer. Before going further it is important to know that how we can declare a pointer variable in C. Declareing a Pointer in C: To declare a Pointer variable in C. We have used a * (asterisk symbol). Please check out the following example program. #include <stdio.h> int main() { int num = 20; int* p = &num; } In the above example, you can clearly see we have created a normal variable class num and initialised this variable with the value 20. After that, we have created a pointer variable called p with the help of *(asterisk symbol) and stored the address of num variable(operator ‘&’ returns the address of a variable) into this pointer variable. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Pointer in C Read More »

String in C

In this tutorial, we will learn about the String in C. String is collection of character but in the background, the string is One-dimensional array is used to which terminates with null character ‘\0’. The null character at the end of the string tells the compiler about the end of the string Initializing a String in C: #include <stdio.h> int main() { char name[] = “Owlbuddy”; //Intilizing string char new_name[] = {‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’} ; //another way to Intilize string return 0; } Showing String output: There is %s format specifier in C to show string output using printf function in c. Please check out the following example program to understand. #include <stdio.h> int main() { char name[] = “Owlbuddy”; printf(“Hello %s”,name); return 0; } Output: Hello Owlbuddy Taking String Input: There are many situations when we need to get String input from user. For example, asking a user for his/her name. Please check out the following program to understand how we can take string input. #include <stdio.h> int main() { char name[20]; printf(“Enter your name: “); scanf(“%s”, name); printf(“Hello %s”, name); return 0; } Output: Enter your name: Owlbuddy Hello Owlbuddy In C there are many functions which makes it really easy to work with strings in C. Function Description strcpy(s1, s2); This function is used to copy string1 (s1) into string2 (s2). strcat(s1, s2); This function is used to concatenate string2 (s2) at the end of the string1 (s1). strlen(s1); This function is used to get the length of the string. strcmp(s1, s2); This function return 0 in case both (s1 and s2) strings are same. retrun < 0 in case  s1<s2 and return > 0 if s1>s2. strchr(s1, ch); This function return the pointer of the first occurrence of the character(ch) in given string(s1). strstr(s1, s2); This function returns the pointer of the first occurrence of the string(s2) in a given string(s1). Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

String in C Read More »

Array in C

In this tutorial, we will learn about Array in C. Array is a collection of the same type(data type) of elements which stored at contiguous memory locations. We can create an array using ant primitive data type such as int, float, double, char, etc of any particular type. To store elements in an array and to fetch elements from an array we use index numbers. Index numbers start from 0. To understand this index system please check out the following illustration. Declaring an Array in C: It is really simple to declare an Array in C. You just have to mention data-type following with identifier and size of the array. To understand it check out the following Syntax to declare in C. dataType arrayName[arraySize]; As in the above syntax you can see first of we defined data-type then identifier(array-name). After the identifier, we have to mention the size of the array(The number of elements an array can store). Initializing an Array in C: Initializing an array means providing elements to the array during declaration. Please check out the following example code to understand this. Example Code: int myArray[5] = {10, 5, 2, 9, 7}; //another way //Here compiler will automatically get size of array according to number of elements int myArray[] = {10, 5, 2, 9, 7}; Inserting values in an Array: we can insert values in an array using an index number. we can mention on which index which value we want to insert. Please check out the following Example Code: int myArray[5]; //declaring an array in c myArray[0]=10; //inseting value at 0 index myArray[1]=20; //inseting value at 1 index myArray[2]=40; //inseting value at 2 index myArray[3]=35; //inseting value at 3 index myArray[4]=25; //inseting value at 4 index Fetching elements from an Array in C: We know in array elements are stored at continues memory locations and we can access and write elements in an array using index number. To understand it we will create a example program. In which first of all we will create an array then we will fetch values from that array using index number. Example Program: #include <stdio.h> int main() { int myArray[]={10,5,2,15,17,3}; printf(“%d\n”,myArray[0]); printf(“%d\n”,myArray[1]); printf(“%d\n”,myArray[2]); printf(“%d\n”,myArray[3]); printf(“%d\n”,myArray[4]); printf(“%d\n”,myArray[5]); } Output: 10 5 2 15 17 3 Using loop to fetch array elements: In the last example, you can see we are using the index number to fetch value at a particular index in an array. But suppose you have an array with 50 elements and you want to fetch all the elements from an array. In this kind of situation, you can use the looping statement to fetch elements from an array. Please check out the following example program to understand it. #include <stdio.h> int main() { int myArray[]={10,5,2,15,17,3}; // printing elements of an array for(int i = 0; i < 6; ++i) { printf(“%d\n”, myArray[i]); } } Output: 10 5 2 15 17 3 Changing the value of an array element: Same as fetching we can use the index number to update the value of an array element. Please check out the following example program. Example Program: #include <stdio.h> int main() { int myArray[]={10,5,2,15,17,3}; myArray[1]=20; //changing value of element at index 1 myArray[3]=12; //changing value of element at index 3 printf(“%d\n”,myArray[0]); printf(“%d\n”,myArray[1]); printf(“%d\n”,myArray[2]); printf(“%d\n”,myArray[3]); printf(“%d\n”,myArray[4]); printf(“%d”,myArray[5]); } Output: 10 20 2 12 17 3   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Array in C Read More »

Constants in C

In this tutorial, we will learn about Constants in C. Constants are fixed values. It means after defining a variable we can not change value. These fixed values are also called literals. Defining Constants in C: There are two simple ways in C to define constant − Using #define preprocessor. Using const keyword. The #define Preprocessor Please check out the following syntax to define a constants using a #define preprocessor. #define identifier value Please check out the following example program. #include <stdio.h> #define pi 3.14 int main() { printf(“Pi : %f”, pi); return 0; } Output: Pi : 3.140000 The const keyword: There is another way to create constants in C is using const keyword. Please check out the following example program. #include <stdio.h> int main() { const float pi = 3.14; printf(“Pi : %f”, pi); return 0; } Output: Pi : 3.140000   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Constants in C Read More »

Functions in C

In this tutorial, we will learn about Functions in C. While writing programs there are many situations when we need to perform the same kind functionality many times. So it means we have to write the same code multiple times in the program. To overcome this kind of problem we have concept called functions in C. Functions help us to divide a big program into the number of small blocks and we can use these small blocks any number of times in program by just writing their names. Before going further must check out some advantages to use functions in C. Advantages to using Functions in C: We can write a piece of code in a function and we can call it in the program several times so it helps us to avoid rewriting code again and again. We can call a function for any number of times. Functions make it so easy to manage program Because it easy to manage a small piece of code than a large piece of code. Types of Functions: Here we will learn what types of functions are available in C. There are a total of two types of functions available in C which are as follow: Library Function User-defined Function Library Functions in C: Library Functions are those which are already defines in header files of C such as scanf(), printf(), gets(), puts()etc. User-defined Functions in C: User-defined functions are those which programmers create in the program to perform some certain task. In this tutorial, we will learn about user-defined functions in detail. Some important aspects of Functions in C: There are two main aspects which you should keep in your mind while working with functions in C. Declaring a Function: Defining a Function: Calling a Function: Declaring a Function: The function declaration is used to tell the compiler about the return type of function, name of the function and the parameters of the function. Syntax to declaring a function in C: return_type function_name( parameter list ); Defining a Function: Defining a function mean when we write the working of function. We write the code in function destination which will work when we will call the function. Syntax to define a function in C: return_type function_name( parameter list ) { body of the function } Return Type: A function can return a value. The return_type use to mention what kind of (data type) value a particular function will return. We can also write void keyword as return type which means function would not return something. Function Name: This is the name of the function. After defining a function we use this name to call function. Parameters: We can write formal parameter while defining a function. In case formal arguments are mentioned in function definition we have to pass actual arguments while calling that particular function. Function Body: Function body is where we mention actual working of function or in other words where we write code to perform some task. Calling a Function: After defining a function our next step is to call that function. The simple way to call a method in C just write the name of the function where you want to call it. To understand it please check out the following example program. #include <stdio.h> int main() { sayHello(); // calling to sayHello() function; return 0; } //defining sayHello() function here void sayHello(){ printf(“Hello, Welcome to Owlbuddy”); } In case you have a function with parameters there are two options to call it. which are as follow. First, one is Call by value Second, one is Call by reference Call by Value: In call by value technique passing arguments to function copy the value of actual parameters into its formal parameter. It means any change to value inside will not make any impact on arguments. Please check out the following program to understand it properly. #include <stdio.h> int main () { int num1 = 10; int num2 = 20; printf(“Before swap num1: %d\n”, num1 ); printf(“Before swap num2: %d\n”, num2 ); //Calling to swap function swap(num1, num2); printf(“After swap num1: %d\n”, num1 ); printf(“After swap num2: %d\n”, num2 ); return 0; } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; return; } Output: Before swap num1: 10 Before swap num2: 20 After swap num1: 10 After swap num2: 20 Call by Reference: In call by value technique passing arguments to function copy the actual reference of parameters into its formal parameter. It means any change to value inside will make an impact on arguments. Please check out the following program to understand it properly. #include <stdio.h> //declaring function swap void swap(int *x, int *y); int main () { int num1 = 10; int num2 = 20; printf(“Before swap num1: %d\n”, num1 ); printf(“Before swap num2: %d\n”, num2 ); /* calling a function to swap the values */ swap(&num1, &num2); printf(“After swap num1: %d\n”, num1 ); printf(“After swap num2: %d\n”, num2 ); return 0; } void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return; } Output: Before swap num1: 10 Before swap num2: 20 After swap num1: 20 After swap num2: 10   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Functions in C Read More »

Looping Statements in C

In this tutorial, we will learn about Looping Statements in C. Looping statements helps us to run a piece of code for multiple time. There are many situations while coding when we need to run a piece of code multiple times. There are a total of three looping statements available in C. for loop while loop do-while loop for loop: For loop is the most common looping statement. Which helps us to run a piece of code for multiple time. Before going further please check out the syntax to define for loop in C. Syntax to define for loop in C: for (initializationStatement; testExpression; updateStatement) { // statements inside the body of for loop } As in above Syntax of for loop you can for loop take a total of three expressions which are as follow: Initialization Expression: In this expression, we initialize the counter of the loop from some value. int i=0; Test Expression: In this expression, we add a condition. For example, we want to write 0 to 10 on the screen as in the last expression we have initialise i as 0. It means we will write condition like this 1<=10. It means the loop will run until the value of i<10 to i==10. Update Expression: In this expression, we update the value of the variable which we defined in first expression(Initialization Expression). Most of the time we increment or decrement the values. For Example i++. Example Program: #include <stdio.h> int main() { int num=2; //change value here to write different table for (int i=1; i<=10; i++) { printf(“%d * %d = %d\n”,num,i,(num*i)); } } In the above example of for loop. I have written an example program using for loop to show the table of two on-screen. While Loop: While loop takes a single expression and it allows to execute code multiple time until the condition goes wrong. To understand it more clearly. Please check out the following syntax to define while loop in C. while(condition) { //body of loop } Example Program: #include <stdio.h> int main() { int i=1; while(i<=10) { printf(“Value of i= %d\n”,i); i++; } } do-while loop: The do-while loop is almost similar to while loop except for one difference. do-while loop will run at least once even in the wrong condition Let’s check the syntax of the do-while loop. do{ //body of loop } while(condition); Example Program: #include <stdio.h> int main() { int i=1; do{ printf(“Value of i= %d\n”,i); i++; }while(i<=10); }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Looping Statements in C Read More »

Conditional Statements in C

In this tutorial we will learn about Conditional Statements in C. There are many situations in the program when we required to run a specific piece of code only if a certain condition is true. In this kind of situation, Conditional statements in c language can be so useful. There are a total of four conditional statements available in C language. Which are as follow: Types of Conditional Statements in C: if statement if-else statement else-if statement switch statement if statement: In case you want to run a piece of code only if a certain condition is true in your code. In this kind of situation, you can use if statement. Before going to the example program. Here you can check syntax to define if statement in a C program. Syntax to define if statement in C: if(//condition to check){ //body of the if statement } Example Program: #include<stdio.h> int main() { int first_num=10; int second_num=5; printf(“Check if %d is greater than %d\n”,first_num,second_num); if(first_num>second_num) //if statement { printf(“Yes %d is greater than %d”,first_num,second_num); } return 0; } Output: Check if 10 is greater than 5 Yes 10 is greater than 5 if-else statement: Above we learned about if statement. As you can see in if statement body of if will only run if condition in if-statement is true. But we do not have any control in case there is a false condition inside if. In case you want to control working in both conditions (true and false condition). Then you can use the if-else statement. Because in the if-else statement we can write code in the inside body of if in case there is a condition inside if the statement is true and code in the inside body of else in case there is a condition inside if the statement is true. Please check out the following syntax to define if-else statement in c: Syntax to define if-else statement in C: if(//condition to check){ //body of if statements }else{ //body of else } Example Program: #include<stdio.h> int main() { int first_num=5; int second_num=15; printf(“Check if %d is greater than %d or Not\n”,first_num,second_num); if(first_num>second_num) //if statement { printf(“Yes %d is greater than %d”,first_num,second_num); }else{ printf(“No %d is smaller than %d”,first_num,second_num); } return 0; } Output: Check if 5 is greater than 15 or Not No 5 is smaller than 15 else-if statement: The else-if statement is also known as an else-if ladder. This else-if statement is used in case there is are multiple statements. In else-if ladder, conditions check from top to bottom and where it finds true condition it runs the following body code. Please check the following syntax to define else-if in C language. Syntax of the else-if statement in c: if(//condtion inside if){ }else if(//condtion inside else if){ }else if(//condtion inside else if){ }else{ } Example Program: #include<stdio.h> int main() { int marks=75; if(marks>80){ printf(“Grade A”); } else if(marks>60){ printf(“Grade B”); } else if(marks>40){ printf(“Grade B”); } else{ printf(“Fail”); } return 0; } Output: Grade B switch statement: Switch statement is used in case you want to user choose from multiple choices. In switch statement we add choice instead of a condition and we create case to show write code for every particular choice. Please check the following syntax to add switch statement in C. Syntax to add switch statement in c: switch (//choice) { case 1: // case 1 will run if choice = 1; break; case 2: // case 2 will run if choice = 2; break; default: // default case will run if choice doesn’t match to any case } Example Program: #include <stdio.h> int main() { int choice=2; //you can change this value to see different result switch (choice) { case 1: // case 1 will run if choice = 1; printf(“You have choosed 1”); break; case 2: // case 2 will run if choice = 2; printf(“You have choosed 2”); break; default: // default case will run if choice doesn’t match to any case printf(“Out of Range”); } } Output: You have choosed 2   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Conditional Statements in C Read More »

Storage classes in C

In this tutorial we will learn about Storage classes in C. The storage class defines the visibility and the life-time of a variable or a function during the execution of a program. There are total four storage classes available in C language which are as follow. Available Storage classes in C: auto register static extern To understand about these storage classes in C please check out the following table. Name Storage Area Initial Value Scope Life-time auto stack garbage within block end of block register CPU register garbage within block end of block static Data Segment zero within block end of program extern Data Segment zero global multiple files end of program   auto: The auto is default storage class for all the local variable(variables inside a function or block). All the auto variables can be only accessed within the block. In case you want to access auto variables from outside of scope you can use pointers(we will learn about pointers in upcoming tutorials). Here is an example program to understand auto variables more wisely. Example Program: #include <stdio.h> void show() { auto int a = 50; int b=10; // printing the auto variable a and b printf(“Here variable a and b both are auto %d %d”, a ,b); } int main() { // calling show method show(); return 0; } register: The register variables work same as auto variables the only difference is compiler store register variables in the register of the microprocessor if there is any free register. The register variables work faster than auto variables because it stores in the register. It is important to note the scope of the register variable is within the block. Please check out the following example program to understand it more wisely. Example Program: #include <stdio.h> void show() { register int a = 50; // printing the register variable a printf(“Here variable a =%d”, a); } int main() { // calling show method show(); return 0; } static: The static variables are different than all other kind of variables because static variables can maintain their values between function calls. The static variables only initialize once and then exists until the end of the program. Please check out the following program to understand static variables. Example Program: #include <stdio.h> void show() { static int i = 10; /*static varible */ int j=10; /*auto variable*/ i++; j++; printf(“i is %d and j is %d \n”, i, j); } int main() { for(int i=0;i<5;i++){ //calling show method show(); } } extern: The extern storage class is use to create global variables which visible to all the program files. It is so helpful to create global variables when two or more program files use same variable. To understand extern variable please checkout the following example program. Example Program: #include <stdio.h> int num; void show() { extern int num; printf(“Value of num %d\n”,num); // modifying value of num variable num = 2; // printing num variable printf(“New value of num %d\n”, num); } int main() { show(); }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Storage classes in C Read More »

Operators in C

In this tutorial, we will learn about Operators in C programming language. Operators are special symbols that we use to perform special operations such as the addition of two numbers(variables). C provides us with a wide range of operators which helps us to perform special operations. In C, all the operators are divided into different categories Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators Arithmetic Operators: Arithmetic operators in C are used to perform simple mathematical operations like Addition, subtraction, multiplication, division, etc. Operator Description Example + This operator is used to add to operands. 10+10=20 − This operator is used to subtract the right-hand side operand from the left-hand side operand. 20-5=15 * This operator is used to multiply both operands. 20*2=40 / This operator is used to divide the left-hand side operand with the right-hand side operand. 15/3=5 % This operator is used to get remainder after performing division. 15/2=1 Example Program: #include <stdio.h> int main() { int a=21; int b=5; printf(“Addition = %d\n”,a+b); printf(“Subtraction = %d\n”,a-b); printf(“Multiplication = %d\n”,a*b); printf(“Division = %d\n”,a/b); printf(“Modulous = %d\n”,a%b); return 0; } Output: Addition = 26 Subtraction = 16 Multiplication = 105 Division = 4 Modulous = 1 Relational Operators: Relational operators are used to checking the relation between two operands such as greater than, less than, equals to, etc. If the relation between two operands is true then the result of the relational expression will be 1 if the relation is false then the result of the relational expression will 0. Operator Description Example > This operator will return true if the left side operand is greater than the right side operand. 10>5=1 >= This operator will return true if the left side operand is greater than or equal to the right side operand. 10>=15=0 < This operator will return true if the left side operand is less than the right side operand. 5<2=0 <= This operator will return true if the left side operand is less than or equal to the right side operand. 5<=10=1 == This operator will return true if operators at both sides are equal. 20==10=0 != This operator will return true if operators at both sides are not equal. 20!=10=1 Example Program: #include <stdio.h> int main() { printf(“Greater than = %d\n”,10>5); printf(“Greater than equals to= %d\n”,10>=15); printf(“Less than = %d\n”,5<2); printf(“Less than equals to = %d\n”,5<=10); printf(“Equals to = %d\n”,20==10); printf(“Not Equal to = %d\n”,20!=10); return 0; } Output: Greater than = 1 Greater than equals to= 0 Less than = 0 Less than equals to = 1 Equals to = 0 Not Equal to = 1 Logical Operators: Generally, logical operators are used with relation expressions but we can also use logical operators with constants. If the result of the logical operator is true then 1 will return otherwise 0 will return. Operator Description Example && This operator is called Logical AND, It returns true if operands on both sides are true. 1&&1=1 || This operator is called Logical OR, It returns true if any of the two operands is true. 1||0=1 ! This operator is called Logical NOT, It returns true if an operand is false and false if an operand is true. !1=0 Example Program: #include <stdio.h> int main() { printf(“Result of 0&&0 = %d\n”,0&&0); printf(“Result of 1&&0 = %d\n”,1&&0); printf(“Result of 0&&1 = %d\n”,0&&1); printf(“Result of 1&&1 = %d\n”,1&&1); printf(“Result of 0||0 = %d\n”,0||0); printf(“Result of 1||0 = %d\n”,1||0); printf(“Result of 0||1 = %d\n”,0||1); printf(“Result of 1||1 = %d\n”,1||1); printf(“Result of !1 = %d\n”,!1); printf(“Result of !0 = %d\n”,!0); return 0; } Output: Result of 0&&0 = 0 Result of 1&&0 = 0 Result of 0&&1 = 0 Result of 1&&1 = 1 Result of 0||0 = 0 Result of 1||0 = 1 Result of 0||1 = 1 Result of 1||1 = 1 Result of !1 = 0 Result of !0 = 1 Bitwise Operators: The Bitwise operators are used to perform operations on the bit level of operands. There is a total of six bitwise operators available in the C language. Operator Description Example & This operator is called Bitwise AND. The output of this operator is 1 if the corresponding bits of two operands is 1 10&5=0 | This operator is called Bitwise OR. The output of this operator is 1 if at least one corresponding bit of two operands is 1 10|5=15 ^ This operator is called Bitwise XOR. The result of this operator is 1 if the corresponding bits of two operands are opposite 10^5=15 ~ This operator is called Bitwise Complement and it is a unary operator. It changes bits from 1 to 0 and from 0 to 1 ~10=11 << This operator is called Bitwise Left Shift. This operator shifts all bits towards the right by a certain number of specified bits 10<<2=40 >> This operator is called Bitwise Right Shift. This operator shifts all bits towards left by a certain number of specified bits 10>>2=2 Example Program: #include <stdio.h> int main() { printf(“Result of 10&5 = %d\n”,10&5); printf(“Result of 10|5 = %d\n”,10|5); printf(“Result of 10^5 = %d\n”,10^5); printf(“Result of ~10 = %d\n”,~10); printf(“Result of 10<<2 = %d\n”,10<<2); printf(“Result of 10>>2 = %d\n”,10>>2); return 0; } Output: Result of 10&5 = 0 Result of 10|5 = 15 Result of 10^5 = 15 Result of ~10 = -11 Result of 10<<2 = 40 Result of 10>>2 = 2 Assignment Operators: Assignment operators are used to assigning some value to a variable. The most common use assignment operator is =. Operator Description Example = This simple assignment operator is used to assign a right-hand side value to a variable on the left-hand side. int a=0; a=10; Here variable a have value of 10. += This simple add assignment operator is used to assign value to a variable at the left-hand side after adding left and right-hand side operands. int a=10; a+=10; Here variable a have value of 20. -= This subtracts assignment operator is used to assigning value to a variable at the left-hand side after subtracting the right-hand side operand from the

Operators in C Read More »

Variable and DataTypes in C

In this tutorial, we will learn about Variable and DataTypes in C programming language. The main motive of variables is to store data in memory while the execution of a C program. The value of variables can be changed throughout the execution of the program and we change reuse variable any number of times throughout the program. Variable Declaration in C: Here is the syntax that, how we can declare and initialize a variable in c program. data_type variable_name = value; Example Program: #include <stdio.h> int main () { /* declaring variable */ int num1, num2; num1 = 10; num2 = 20; int ans = num1 + num2; printf(“Ans : %d \n”, ans); return 0; } Output: Ans : 30 Data Types in C program: Data Type determines the type and size of data associated with variables. There are several data types available in C language. It is important to note the size(storage capacity) of data types is different in different C compilers. Type Size (bytes) Format Specifier Description int at least 2, usually 4 %d Use to store negative and positive numbers. eg: 5,10,15 char 1 %c Use to store character. eg: ‘y’, ‘n’ float 4 %f Use to store decimal numbers. eg: 10.5 double 8 %lf Use to store decimal numbers. eg: 10.5 short int 2 %hd Use to store small negative and positive numeric values. unsigned int at least 2, usually 4 %u Use to store small only positive numeric values. long int at least 4, usually 8 %li Use to store both negative or positive large numeric values. long long int 8 %lli Use to store both negative or positive very large numeric values. unsigned long int 4 %lu Use to store both only positive large numeric values. unsigned long long int 8 %llu Use to store both only positive very large numeric values. long double 10,12,16 %Lf Use to store very large negative or positive decimal values. Please keep continuing with our next tutorial in the next tutorial we will learn about Operators in C. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Variable and DataTypes in C Read More »

Scroll to Top
×