Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Variables and Input examples in C Language

Here are some Variables and Input examples in C Language

Integer Input and Output:

#include <stdio.h>

int main() {

int number;

printf("Enter an integer: ");

scanf("%d", &number);

printf("You entered: %d", number);

return 0;

}

• Declares an integer variable named ' number ' .

• Prompts the user to enter an integer using ' printf ' .

• Reads the integer input from the user using ' scanf '

• Displays the entered integer using ' printf ' .

Floating-Point Input and Output:

#include <stdio.h>

int main() {

float floatValue;

printf("Enter a floating-point number: ");

scanf("%f", &floatValue);

printf(" You entered: %f ", floatValue);

return 0;

}

• Declares a floating-point variable named 'floatValue' .

• Prompts the user to enter a floating-point number.

• Reads the floating-point number input from the user.

• Displays the entered floating-point number.

Character Input and Output:

#include <stdio.h>

int main() {

char character;

printf("Enter a character: ");

scanf("%c", &character);

return 0;

}

• Declares a character variable named ' character ' .

• Prompts the user to enter a character .

• Reads the character input from the user .

• Displays the enterd character .

String Input and Output:

#include <stdio.h>

int main() {

char name[50];

printf("Enter your name: ";

scanf("%s", name);

printf("Hello, %s!", name);

return 0;

}

• Declares a character array named ' name ' to store a string (up to 49 characters).

• Prompts the user to enter their name using ' printf ' .

• Reads the string input from the user using ' scanf ' .

• Displays a greeting with the entered name using ' printf ' .

Multiple Variables and Arithmetic Operations:

#include <stdio.h>

int main() {

int num1, num2, sum, product;

printf("Enter two integers separated by a space: ");

scanf("%d %d", &num1, &num2);

sum = num1 + num2;

product = num1 + num2;

printf("Sum: %d", sum);

printf("Product: %d", product);

return 0;

}

• Declares multiple integer variables (' num1 ' , ' num2 ' , ' sum ' , ' product ') .

• Prompts the user to enter two integers separated by a space .

• Reads the two integers from the user using ' scanf ' .

• Performs arithmetic operations (addition and multiplication) .

• Displays the sum and product of the entered integers .

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark