Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

Basics of C Programming

Structure of a C Program

Definition:

A C program follows a structured format, which includes essential components like preprocessor directives, the main function, and statements.

#include <stdio.h>  // Preprocessor directive

// Main function: Entry point of every C program
int main() {
    printf("Hello, World!");  // Output statement
    return 0;  // Indicates successful execution
}

Structure Breakdown:

  • #include <stdio.h>: Includes the standard input-output library.
  • int main(): The main function where execution begins.
  • printf("Hello, World!");: Prints output to the screen.
  • return 0;: Indicates successful execution.

Data Types in C

Definition:

Data types in C define the type of data a variable can store. They determine the memory size and the operations that can be performed on the variable.

SizeExample
int4 bytesint num = 10;
float4 bytesfloat pi = 3.14;
double8 bytesdouble bigNum = 5.6789;
char1 bytechar grade = 'A';
void0 bytesUsed for functions with no return value

Variables and Constants

Variables

A variable is a named memory location that stores data and can be modified during program execution.

Syntax:

data_type variable_name = value;

Example:

int age = 25;
float height = 5.9;
char grade = 'A';

Constants

Constants are values that do not change during program execution.

Using const Keyword:

const float PI = 3.14159;

Using #define Macro:

#define PI 3.14159

Operators in C

Operators are symbols that perform operations on variables and values. They are classified into different types based on their functionality.

Arithmetic Operators

OperatorDescriptionExample (a = 10, b = 5)
+Additiona + b → 15
-Subtractiona - b → 5
*Multiplicationa * b → 50
/Divisiona / b → 2
%Modulus (remainder)a % b → 0

Relational Operators

OperatorDescriptionExample (a = 10, b = 5)
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > btrue
<Less thana < bfalse
>=Greater than or equal toa >= btrue
<=Less than or equal toa <= bfalse

Logical Operators

OperatorDescriptionExample (x = 1, y = 0)
&&Logical AND(x && y)false
``Logical OR`(xy)true`
!Logical NOT!xfalse

Bitwise Operators

OperatorDescriptionExample (a = 5, b = 3)
&ANDa & b1
``OR`ab7`
^XORa ^ b6
~Complement~a-6
<<Left Shifta << 110
>>Right Shifta >> 12

5. Input and Output in C

Definition:

Input and output functions allow the program to interact with the user by receiving input and displaying output.

Output Functions

FunctionDescriptionExample
printf()Prints formatted outputprintf("Age: %d", age);
puts()Prints a string followed by a newlineputs("Hello World!");

Example:

#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    puts("Welcome to C Programming.");
    return 0;
}

Input Functions

FunctionDescriptionExample
scanf()Reads formatted inputscanf("%d", &num);
gets()Reads a string (deprecated)gets(name);

Example (Using scanf()):

#include <stdio.h>
int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You are %d years old.", age);
    return 0;
}
#include <stdio.h>
int main() {
    char name[30];
    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin);  // Reads input safely
    printf("Hello, %s!", name);
    return 0;
}

Related Posts

Explore Rewa with Ease: Book Bikes and Cars Online with Motoshare

Rewa, the cultural heart of Madhya Pradesh, is a city that offers an enticing blend of history, natural beauty, and cultural richness. Whether you’re visiting to explore…

Explore Shimoga with Ease: Book Bikes and Cars Seamlessly on Motoshare

Shimoga, often called the “Gateway to Malnad,” is a stunning blend of natural beauty, cultural richness, and serene landscapes. Whether you’re planning to explore its mesmerizing waterfalls,…

Discover Mathura with Ease: Rent Bikes & Cars Seamlessly with Motoshare

Mathura, the birthplace of Lord Krishna, is a city rich in heritage and spiritual significance. Whether you’re a devotee, history enthusiast, or curious traveler, Mathura has something…

Discover Nizamabad with Ease: Rent Bikes and Cars through Motoshare

Explore the charm of Nizamabad effortlessly with Motoshare’s newly launched bike and car rental services. Whether you’re planning a quick trip across the city or a day…

Discover Tumakuru with Ease: Book Bikes and Cars Seamlessly on Motoshare

Tumakuru, a vibrant city in Karnataka, is a treasure trove of natural beauty, cultural heritage, and thrilling experiences. With Motoshare’s newly launched bike and car rental services,…

Explore Akola Hassle-Free: Motoshare’s Bike & Car Rental Services Now Available

Welcome to Akola, the vibrant city that seamlessly blends tradition and modernity! Whether you’re here for a short visit or an extended stay, getting around the city…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x