Apply for Zend Framework Certification Training

c

< How to print in c in different ways Variables in C >




1. Adding Two Numbers
#include 
<stdio.h>
void main(){
    int a,b,c;
    printf("Enter First number :");
    scanf("%d",&a);
    printf("Enter Second number :");
    scanf("%d",&b);
    c = a + b;
    printf("The sum of two Numbers is : %d ",c);
}
Sample Output
Enter First number :10
Enter Second number :20
The sum of two Numbers is : 30

2. Input a Character
#include 
<stdio.h>
int main(){
    char grade;
    printf("Enter Student Grade :");
    scanf("%c",&grade);
    printf("Grade of the student is %c ",grade);
    return 0;
}
Sample Output
Enter Student Grade :A
Grade of the student is A

3.Input a Sentence Using Different Methods in C

In C, a sentence is generally stored in a character array (char[]). There are several ways to take a sentence as input.
1. Using scanf("%s", ...)
This is the simplest method, but it reads only one word because %s stops reading when it encounters whitespace.
#include <stdio.h>
int main() {
    char name[50];
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Your name is: %s\n", name);
    return 0;
}
Input:
Rajesh
Output:
Your name is: Rajesh
If you enter:
Rajesh Kumar
Only Rajesh will be stored.
2. Using scanf(" %[^\n]", ...)
This method can read a complete sentence containing spaces.
#include <stdio.h>
int main() {
    char sentence[100];
    printf("Enter a sentence: ");
    scanf(" %[^\n]", sentence);
    printf("You entered: %s\n", sentence);
    return 0;
}
Input:
My name is Rajesh Kumar
Output:
You entered: My name is Rajesh Kumar
How does it work?
scanf(" %[^\n]", sentence);
→ leading space tells scanf() to skip existing whitespace/newline.
%[ → scans a set of characters.
^ → means NOT.
\n → newline.
[^\n] → read everything until Enter is pressed.
This is a useful technique for beginners, but it has a buffer-overflow risk if the user enters more than the array can hold.
3. Using fgets() — Recommended Method
fgets() is one of the safest and most commonly recommended methods for reading a sentence.
#include <stdio.h>
int main() {
    char sentence[100];
    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);
    printf("You entered: %s", sentence);
    return 0;
}
Input:
C programming is easy to learn
Output:
You entered: C programming is easy to learn
How fgets() works
fgets(sentence, sizeof(sentence), stdin);
Here:
sentence → character array where input is stored
sizeof(sentence) → maximum number of characters to read
stdin → standard input (keyboard)
If:
char sentence[100];
then fgets() can store up to 99 characters plus '\0'.
4. Using gets() — Do NOT Use
Older C programs sometimes use:
gets(sentence);
Example:
#include <stdio.h>
int main() {
    char sentence[100];
    printf("Enter a sentence: ");
    gets(sentence);
    printf("You entered: %s", sentence);
    return 0;
}
However, gets() is unsafe because it does not know the size of the array. It was removed from the C standard starting with C11.
Avoid:
gets(sentence);
Use:
fgets(sentence, sizeof(sentence), stdin);
5. Using scanf() with a Width Limit
You can also limit the number of characters read:
#include <stdio.h>
int main() {
    char sentence[20];
    printf("Enter a sentence: ");
    scanf(" %19[^\n]", sentence);
    printf("You entered: %s\n", sentence);
    return 0;
}
Here:
%19[^\n]
means read a maximum of 19 characters, leaving one character for the null terminator '\0'.
Comparison
Method Reads Spaces? Safe? Recommendation
scanf("%s", str)               No With width limit For one word
scanf(" %[^\n]", str)        Yes  Needs width limit Useful
fgets()                            Yes  Yes Recommended
gets()                             Yes  No Never use
Best practice
For taking a sentence/string with spaces, prefer:
char sentence[100];
fgets(sentence, sizeof(sentence), stdin);
Important point about char sentence[100]
char sentence[100];
creates an array capable of storing up to 99 visible/input characters plus the terminating '\0' when used as a C string. The actual usable input can be affected by the newline that fgets() may retain.
 

4. Input and Display Array Elements

#include
int main(){
    int arr[5];
    int i;
    printf("Enter 5 elements of an array : \n");
    for(i=0;i<5;i++){
        printf("Enter %d element :",i+1);
        scanf("%d",&arr[i]);
    }
    printf("Entered elements of an array is : \n");
    for(i=0;i<5;i++){
        printf("Value of %d elements is %d \n",i+1,arr[i]);
    }
}
Sample Output
Enter 5 elements of an array :
Enter 1 element :10
Enter 2 element :20
Enter 3 element :30
Enter 4 element :40
Enter 5 element :50

Entered elements of an array is :
Value of 1 elements is 10
Value of 2 elements is 20
Value of 3 elements is 30
Value of 4 elements is 40
Value of 5 elements is 50

 

< How to print in c in different ways Variables in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top