When to use & with scanf - C Language Basics

C examples for Language Basics:scanf

Description

When to use & with scanf

Demo Code

#include <stdio.h>

int main(void){
    int age;  //  w  w w.j a  v  a 2  s  . c  om
    float assets;
    char pet[30];        // string
    
    printf("Enter your age, assets, and favorite pet.\n");
    scanf("%d %f", &age, &assets); // use the & here
    scanf("%s", pet);              // no & for char array
    printf("%d $%.2f %s\n", age, assets, pet);
    
    return 0;
}

Result


Related Tutorials